nc 0.cloud.chals.io 26166
§0 Paste-ready handoff
- Status: SOLVED. Flag recovered and reproduced twice (see
solve.log,hammer.log). - Category: pwn/misc - TOCTOU race condition on shared server state.
- Target:
0.cloud.chals.io 26166, line-based text protocol. - Flag:
isfcr{t0ct0u_w1nd0w_0nly_0p3ns_c0ncurr3ntly} - One-line repro:
python3 solve.py(64 threads, 120 s budget; hits in well under a minute - reproduced 3/3 consecutive fresh runs). - Root cause: the vault's
(prizes, tickets)counters are global (shared by every connection) andclaimdoes an un-locked check-then-act (if prizes >= 15:… then update). Concurrentclaims all read the sameprizes >= 15snapshot and every one of them takes the success branch. - Do not re-derive (negatives):
- Command matching is a prefix test (
redeem:1,status(),redeem\t1all dispatch;REDEEM/Redeem/leading-space do not). There is no argument parsing -redeem 15,claim:15send no count. No hidden commands exist (119-word sweep: only prefix-matches, everything else →?). - No injection surface.
%s%n,',",{}, NUL bytes, 5000-byte lines, ANSI escapes,\rall return a plain?- no traceback, no eval. - State is NOT per-connection. Fresh connections observe whatever the
global state currently is (e.g.
prizes=4 tickets=-3), so single-threaded probing appears "stuck at1/0" and looks like a dead end. It is not. prizes + tickets == 1is invariant -redeemtransfers a ticket into a prize,claimtransfers 15 prizes into a ticket. Reachingprizes >= 15requirestickets == -14, which is only reachable becauseredeemalso mutates under the same un-locked window. Single-threaded you can never escapeprizes = 1, so serial play is provably useless - do not grind it.- Idle state drifts every ~40 - 80 s even with no commands from you; that is the shared instance being touched by other traffic/timers. It is a red herring, not the exploit.
- Command matching is a prefix test (
- Ranked leads (all resolved): concurrency (win) > protocol fuzzing (empty) > hidden-command brute (empty).
§1 Challenge text
tearing - 50 pts - NotKnox - 3 solves One ticket in the drawer. Leave with two.
nc 0.cloud.chals.io 26166
§2 Service protocol
vault. commands: redeem | status | claim | quit
> status
prizes=1 tickets=0 need=15
> redeem
no tickets left
> claim
not enough prizes
> quit
| command | guard | success | failure |
|---|---|---|---|
status |
- | prizes=P tickets=T need=15 |
- |
redeem |
tickets > 0 |
redeemed. prizes=P tickets=T |
no tickets left |
claim |
prizes >= 15 |
prints the flag | not enough prizes |
quit |
- | closes the connection | - |
Dispatch is line.startswith(cmd); unknown input → ?.
§3 Root cause - TOCTOU on the global drawer
The service shares one mutable (prizes, tickets) pair across all clients and
checks the guard separately from the update, with no locking:
1# reconstructed
2def claim():
3 if prizes >= 15: # <-- check
4 ... # <-- (no lock / no re-read)
5 tickets += 1 # <-- act, on a stale snapshot
6 print(FLAG)
Because the check and the act are not atomic, a burst of concurrent claims
all observe prizes >= 15, all enter the branch, and all receive the flag -
"one ticket in the drawer, leave with two". Under load the shared counters were
repeatedly observed at prizes=17 tickets=-16, i.e. the tickets > 0 guard on
redeem tears open the same way and lets prizes climb past 15.
The invariant prizes + tickets == 1 holds at rest; the race breaks the read
consistency, not the arithmetic.
§4 Exploit
1python3 solve.py # 64 threads, 120 s budget (hits in <60 s)
2python3 solve.py <host> <port> [seconds] # custom target / budget
Worker loop (see solve.py) - randomised command bursts, one recv per burst:
1s.sendall(random.choice(BURSTS)) # b"redeem\n"*8, b"claim\n"*4,
2 # b"redeem\nclaim\n"*8, ...
3buf = recv_until_prompt(s)
4if b"isfcr{" in buf: win()
redeembursts drive the sharedprizescounter up through the torn guard.claimbursts land inside the window the instantprizescrosses 15.- First
isfcr{...}seen wins; all threads stop.
Observed in one 40 s / 24-thread run (hammer.log):
2452 b'no tickets left'
1393 b'isfcr{t0ct0u_w1nd0w_0nly_0p3ns_c0ncurr3ntly}'
683 b'prizes=17 tickets=-16 need=15'
259 b'not enough prizes'
140 b'prizes=4 tickets=-3 need=15'
4 b'prizes=0 tickets=1 need=15'
The flag was returned 1393 times - every racer that entered the window got
it, exactly as the TOCTOU model predicts. solve.py independently produced the
same flag (solve.log).
§5 Flag
isfcr{t0ct0u_w1nd0w_0nly_0p3ns_c0ncurr3ntly}
§6 Files
| file | role |
|---|---|
solve.py |
final solver (64 threads, randomised bursts), flag regex + early stop |
hammer.py |
first concurrency fuzzer to win; prints the response histogram |
probe.py |
protocol recon (command semantics) |
monitor.py |
idle-state drift observer |
solve.log, hammer.log |
captured runs proving the flag |
§7 Reproduce from scratch
1git clone tearing.bundle tearing && cd tearing
2python3 solve.py # needs network access to 0.cloud.chals.io:26166