Writeup · 31 Nights of Root

Thornwall

reversingmediumsolvedFlag: r00t{REDACTED}

Overview

Three binaries were bundled: thornwall, check, and validator. They are not byte-identical. check is the real grader; thornwall (the binary sharing the challenge’s title) is an elaborate decoy; validator rejects the flag. A filename matching the challenge’s title is not evidence that it is the file that grades the flag — test every bundled binary directly.

Prerequisites / Setup

  • objdump, readelf, strings, Python 3
  • check, thornwall, validator in the working directory

Recon

Step 1 — Inspect the binaries

Goal: Confirm the three files are distinct stripped x86-64 PIE ELFs and identify which one actually checks the flag.

file check thornwall validator
md5sum check thornwall validator
cmp -l check thornwall
Binary MD5 Mechanism Accepts the given flag?
check abe9abc3bd06f1c1c977e1325b8c1d87 strcmp against a 40-byte .rodata secret XOR’d with 0x5c Yes — prints CORRECT, exit 0
thornwall 557b8fabc44819c1304c1d5c78e2c1ae Custom 20-byte keystream cipher checked with memcmp No — memcmp fails, exit 1
validator 1e4c689a7b974855ba6a1cacb7f7b106 Same general shape as thornwall, different embedded constants No — prints INVALID -- formula mismatch, exit 1
strings -a check | grep -E 'CORRECT|INCORRECT|FLAG'

The INCORRECT string is a strong hint that check does a simple compare-and-print rather than a silent memcmp-only check like thornwall.

Exploitation

Step 2 — Reverse the real checker (check)

Goal: Confirm check is the real grader and recover the flag from its .rodata.

check is small and has no anti-debug logic — it is a straightforward decode-then-compare. The relevant disassembly (objdump -d check -M intel):

1080: push rbp
1081: pxor xmm0,xmm0
1085: mov eax,0x2e              ; 46-byte loop counter
108a: lea rdx,[rip+0xf8f]       ; rdx -> .rodata+0x2020 (the encoded secret)
...
10e0: xor eax,0x5c              ; decode byte: enc[i] ^ 0x5c
10e3: add rcx,0x1
10e7: add rdx,0x1
10eb: mov BYTE PTR [rcx-0x1],al ; write decoded byte to a stack buffer
10ee: movzx eax,BYTE PTR [rdx]
10f1: test al,al
10f3: jne 10e0                  ; loop until the 0x00 terminator byte is hit
...
1150: call fgets                ; read a line from stdin
115d: call strlen
1167: call strcmp                ; compare decoded secret vs the stdin line (trailing newline stripped first)
1176: je 11ae                    ; equal -> puts("CORRECT"); return 0
1178: call puts                  ; not equal -> puts("INCORRECT"); return 1

The encoded secret lives at .rodata+0x2020 (46 bytes allocated, but the decode loop stops at the first 0x00 byte, which lands after 40 encoded bytes):

2020  2e 6c 6c 28 27 28 34 6c 2e 32 2b 68 30 30 03 64
2030  3a 3e 68 3e 6d 68 6f 68 6c 3f 6a 68 6e 65 3d 3d
2040  64 6d 3f 39 64 6f 6d 21 00

XOR every byte with 0x5c up to (not including) the 0x00 terminator:

raw = bytes.fromhex("2e6c6c282728346c2e322b68303003643a3e683e6d686f686c3f6a686e653d3d646d3f39646f6d2100")
secret = bytes(b ^ 0x5c for b in raw if b != 0)
# secret == b'r00t{REDACTED}'

Verify by running the binary:

printf 'r00t{REDACTED}' | ./check
# CORRECT
echo $?
# 0

Flag

r00t{REDACTED}

Appendix: thornwall deep dive (decoy binary, not the real checker)

The sections below describe thornwall, the binary sharing the challenge’s title, which is an elaborate decoy. Its analysis is real and independently verified, but it is irrelevant to recovering the flag — check is the actual grader. It is kept here because the misleading reference files targeted this binary, and it is a good example of why to verify against the binary rather than bundled notes.

Thornwall structure

thornwall is a stripped x86-64 PIE ELF. The program:

  1. readlink("/proc/self/exe", ...) to find its own path, then fopens and freads its own file bytes into a scratch buffer (never used before being overwritten — a decoy self-integrity check).
  2. fread(stdin, 1, 20, ...) — reads exactly 20 bytes; if fewer are read, exits with code 1.
  3. Opens /proc/self/status, scans for the TracerPid: line, and strtols the value.
    • If TracerPid != 0 (process is traced/debugged): falls into a decoy comparison against unrelated constant data, always returns 1, and never reaches the real algorithm. This is the entire anti-debug mechanism — a silent short-circuit, not a trap requiring patches.
    • If TracerPid == 0 (normal, undebugged execution — the default for ./thornwall): jumps straight to the real check logic.
  4. Keystream generation: for each of the 20 output bytes k (0-indexed), starting fresh from seed = 0xDB1F450C each time, apply k+1 rounds of rotate-left by 7 (even round index) or 3 (odd round index), then add 0x9E3779B9 (mod 2³²). Take the low byte of the final accumulator as keystream[k].
  5. Final buffer construction:
    • bytes 0–15: keystream[i] XOR rodata[0x2070 + i] (a 16-byte XOR mask, applied via a single pxor)
    • bytes 16–19: ((rodata[0x2050 + (i-16)] XOR i) ROR 3) XOR keystream[i] XOR 0x77
  6. memcmp(user_input, computed_buffer, 20) — equal means accepted (exit 0), anything else rejects (exit 1).

The decoy branch only runs when a debugger is attached; a normal untraced invocation takes the top path straight into the real algorithm.

Why the supplied “reference” files were wrong

The upload included thorn_reference.py, memory_map.txt, and bypass_guide.txt, presented as recovered analysis. They were wrong in several load-bearing ways, confirmed by direct disassembly:

Claim in the notes Reality in the binary
Seed is seed_enc[4] at offset 0x402010, XOR’d with 0x55 Seed is a hardcoded immediate 0xDB1F450C baked directly into mov eax, 0xdb1f450c at 0x1240. No rodata read, no XOR for the seed.
Rotation constant is 7, fixed, for every step The real inner loop alternates rotate-by-7 and rotate-by-3 depending on whether the (0-indexed) inner step counter is even or odd.
Check values live at 0x402080 (single XOR with 0x77) The real transform uses two different constructions: bytes 0–15 are keystream XOR a 16-byte block at rodata 0x2070; bytes 16–19 use rodata byte XOR index, rotate-right-3, XOR keystream byte, XOR 0x77. The 0x77 XOR only applies to the last 4 bytes, not all 20.
A TracerPid check and a separate CRC32 integrity guard over [0x1000:0x2000], both needing byte patches The binary reads /proc/self/status and compares against TracerPid:, but there is no CRC guard anywhere in the disassembly. No patching was needed — running the binary normally (no ptrace) takes the “not traced” branch since TracerPid is 0 by default.

The addition constant 0x9E3779B9 from thorn_reference.py happened to be correct, just disguised: the real instruction is sub eax, 0x61C88647, and 0x61C88647 is the exact two’s-complement negation of 0x9E3779B9, so sub eax, 0x61C88647 is mathematically identical to add eax, 0x9E3779B9 — the one place the notes lined up with reality.

Lessons / Takeaways

  • Treat bundled “analysis” or “reference implementation” files as a hypothesis, not ground truth. Verify every claimed offset, constant, and control-flow branch against the real disassembly.
  • When a claimed “anti-debug + integrity guard” requires multiple binary patches, check whether the normal (non-debugged) execution path already takes the branch you want before patching anything — here it did, and no patching was ever necessary.
  • Cross-check a derived solution against the real oracle (the binary’s own exit code / printed output) rather than stopping once arithmetic “looks done.”
  • A filename matching the challenge’s title is not evidence that it is the file that grades the flag. Three binaries were bundled here; the one actually named check was the real validator, while thornwall — sharing the challenge’s name — was an elaborate decoy. Test every bundled binary against a candidate flag directly before committing to a deep static analysis of just one of them.
  • Prefer the simplest working hypothesis first. strings on check immediately showed INCORRECT/CORRECT output strings — a strong, cheap signal that a binary does a plain compare-and-print rather than a silent memcmp. A five-second strings pass before diving into objdump would have pointed at the real checker much sooner.
  • When static and dynamic analysis disagree with a bundled claim, trust the layer closest to ground truth — running the real binary — over any derived or secondhand description of it.

Techniques

References