TL;DR
A 34 MB stripped PIE hides its license check in 40 encrypted x86 shellcode chunks that are decrypted inside the kernel by an embedded eBPF tracepoint program (bpf_probe_write_user on syscalls:sys_enter_openat with dirfd 0x1337) and dispatched via int3/SIGTRAP. Dumping the chunks in a privileged container, reconstructing the 8×4-round Feistel/PRNG license transform, scraping the expected 512-bit target out of the seccomp ERRNO oracle's own argument stream, and z3- solving the 14 hidden username byte-checksums plus crc32(user)==0xbf2b0fa2 yields the unique username xorionxarcheryxseedx, whose derived 72-byte license.dat makes the untouched binary print the flag.
01Recon
Files. One artifact: /home/kali/Desktop/CTF/rev/extended/challenge, 34,798,808 bytes, ELF 64-bit LSB pie, x86-64, stripped, SHA256 05cca43b4b9ef229e402fb6869f6613209a3d91542993af32e9984bb99d40563. Linked against libelf, libz, libcrypto (SHA256, AES_set_decrypt_key, AES_ecb_encrypt), i.e. a statically-embedded libbpf.
Run unprivileged it prints the banner and exits 1:
===============================================
Do you have a license?
===============================================
EXIT=1strace shows why: three bpf(BPF_PROG_LOAD...) calls all return -EPERM. /proc/sys/kernel/ unprivileged_bpf_disabled = 2.
Interesting rodata at 0x2111750 – 0x2111850: the banner, SUDO_UID, SUDO_USER, /proc/self/loginuid, sys_enter_openat, syscalls, rb, Congrats:, I don't think so, and immediately after them at 0x2111850 an 80-byte blob of high-entropy data (31626a4c1711dc1f…), the encrypted flag.
main (0x68c0). puts banner → signal(SIGALRM=14, 0x74d0) → alarm(5) → rdtsc → resolve username from SUDO_USER / SUDO_UID / /proc/self/loginuid + getpwuid → load & attach the BPF program → then:
6c56: mov $0x35,%esi
6c5b: mov $0x539,%edi
6c60: call 70b0 ; id-mangler, returns u16
6c65: lea 0x2129954(%rip),%rdx ; 0x21305c0 = ctx
6c6f: mov %rax,%r10 ; r10 = chunk id (0x539 first)
6c72: mov %rdx,%r12 ; r12 = ctx
6c75: int3 ; <-- dispatch
6c77: cmpq $0x0,0x2130618(%rip)
6c81: cmp $0xbf2b0fa2,%ebx ; <-- win condition
6c87: je 6d5a ; flag path
6c8d: lea 0x2111839(%rip),%rdi ; "I don't think so"The flag path at 0x6d5a runs SHA256 (0x6e40) → AES_set_decrypt_key (0x6e68) → AES_ecb_encrypt (0x6ea0) over the blob at 0x2111850, then puts.
Embedded blobs.
0x20e6b18, length0x2ab55(174,933 bytes): the first, plaintext shellcode.0x92e0memcpy s it into an RWX mmap and calls it with rdi=username, rsi=ctx.0x2129010, length0x6218: ELF 64-bit LSB relocatable, eBPF, not stripped. llvm-readelf -p.strtab gives the source nameoooo.bpf.cand symbols qh, q0_prog, q1_prog, maps buffer_map (PERCPU_ARRAY, value 8192), rb (RINGBUF). Section tp/syscalls/sys_enter_openat, 1,394 instructions, first block:
0: r9 = r1
1: call 0xe ; bpf_get_current_pid_tgid
3: r1 = *(u64 *)(r9 + 0x10) ; dfd
4: if r1 != 0x1337 goto +0x56b ; magic dirfd
...
28: call 0x70 ; bpf_probe_write_userShellcode shape. objdump of sc.bin shows a control-flow-flattened dispatcher: a 32-bit state word lives at ctx+0x140, scratch at +0x144 / +0x148, saved EFLAGS at +0x150. Every real basic block is wrapped as
mov 0x150(%r12),%r11 ; push %r11 ; popf ; restore flags
<real instructions, interleaved with SSE/lea/xchg junk>
pushf ; pop %r11 ; and $~0x100,%r11 ; mov %r11,0x150(%r12)
mov $K1,%r11d ; mov %r11d,0x144(%r12)
mov $K2,%r11d ; cmov<cc> 0x144(%r12),%r11d ; branch = state selection
xor $M,%r11d ; xor $M,%r11d
mov %r11d,0x140(%r12) ; jmp <dispatcher>The dispatcher (0x191 in sc.bin, 0x18c in every decrypted chunk) is a linear chain of 341 cmp $state, %r11d / je <block> pairs. Only one real rdtsc exists in the whole blob (0x1c121).
First shellcode = CRC32. A Unicorn emulator over sc.bin reproduced the return exactly: ret == crc32(username) ^ 0x14 for 25 random inputs, except for one input class (see Vulnerability).
Chunk chain. The int3 at 0x6c75 raises SIGTRAP into the handler at 0x97e0 (installed via sigaction at 0x6c38, setjmp at 0x9832, a 600 M-cycle rdtsc watchdog at 0x989d). Under strace in a privileged container:
openat(4919, "\334\276\226\315\3\3477\320…", O_RDONLY|O_DSYNC) = -1 EBADF4919 = 0x1337. Each call hands the kernel one 4096-byte page of ciphertext; the eBPF program rewrites it in place with bpf_probe_write_user and the syscall itself fails with EBADF. Chunk k ends with its own int3 (r10 = next chunk id) so the chain is: 0x539 → 0x5cf → 0x535 → 0x596 → 0x5f7 → 0x55d → 0x5be → 0x524 → 0x585 → …, 40 chunks total, 8 KB to 7.3 MB each, 8,385 pages.
Syscalls the chain makes, in order: memfd_secret(0) → ftruncate(fd,4096) → mmap(...,MAP_SHARED,fd,0) (a rw-s /secretmem page) → prctl(0x26,1) = PR_SET_NO_NEW_PRIVS → prctl(0x16,2) = PR_SET_SECCOMP, SECCOMP_MODE_FILTER → open("license.dat",O_RDONLY) → read(fd, ctx+0x98, 0x80) → the checks → mprotect → write / exit.
ctx layout (0x21305c0, i.e. area+0x120): +0x00 username ptr, +0x10 username hash, +0x60 shellcode CRC result, +0x88 license length, +0x98 license buffer (128 B), +0x140/144/148 flattening PRNG, +0x150 saved flags.
Seccomp filter (30 instructions, dumped from the prctl argument):
000 ld arch 004 jeq nr,0x1337 else -> 029 ALLOW
005 ld [0x18] ; args[1] = "mode"
006 jeq 1 -> 011 007 jeq 2 -> 016 008 jeq 3 -> 021 009 jeq 4 -> 025
011 ld [0x10] ; and 1 ; jeq 0 -> ret ERRNO(1) else ret ERRNO(0)
016 ld [0x10] ; and 1 ; jeq 1 -> ret ERRNO(1) else ret ERRNO(0)
...
029 ret ALLOW (0x7fff0000)syscall(0x1337, bit, mode) is a pure oracle: the filter turns (args[0]&1, args[1]) into 0 or EPERM. 512 such calls per run, 64 bytes × 8 bits.
02The bug
Three separate root causes chain into the solve.
- The bit-comparison oracle leaks the expected value in its own arguments. The final check compares the Feistel-transformed license payload against a hard-coded 512-bit constant one bit at a time, using syscall(
0x1337, actual_bit, mode) and reading the seccomp SECCOMP_RET_ERRNO return as the comparison result. But the seccomp program has four separate parity arms selected by args[1], and the shellcode picks the arm according to the expected bit. So the mode argument is the answer. Scraping 512 strace lines and reading args[1] off each yields the target directly:
def decode(calls): # calls = [(arg0, arg1, arg5), ...]
actual=[]; expected=[]
for i in range(0, len(calls), 8):
a=e=0
for j,(a0,a1,_) in enumerate(calls[i:i+8]):
a |= (a0 & 1) << j # what the license produced
e |= (1 if a1==1 else 0) << j # what it should have produced
actual.append(a); expected.append(e)
return bytes(actual), bytes(expected)Target (username-independent, identical for alice, bob, zzzz): 5865ae6b9ab3837b261c1051d1bb1ee69e5d9ae4580d42f1d867ca7d9d9229e38fb236f04e2012894f66f96f573ef8b00ec30c eb7388b9684a38ee408c9e7504
- The license transform is an invertible 8×4-round Feistel network. Differential probing showed byte-8 locality (flipping payload byte i only changes output bytes 8*(i//8).. 8*(i//8)+7), and it is not affine. Chunks 23–30 are eight near-identical copies differing only in the [r14 +
0x00/0x08/0x10/…] offsets, one per 8-byte block. Each does mov ecx,4 (rounds) and call a common subroutine whose real instructions are:
movq xmm0, r13
movq xmm1, [rip+0xd337] ; 0x1d872b41f2a3c4d5
pclmulqdq xmm0, xmm1, 0
movq r13, xmm0
xor r13, [rip+0x96ad] ; 0x9e3779b97f4a7c15
mov eax, r13d
cvtsi2ss xmm2, rax
mulss xmm2, [rip+0x1b349] ; 2^-32
mulss xmm3, [rip+0x17043] ; 1.69
addss xmm3, [rip+0xadaa] ; 0.1234567
vfmadd132ss xmm7, xmm3, xmm2
roundss / subss / mulss 2^32 / cvttss2sii.e. a carry-less-multiply PRNG passed through a float32 fractional mixer, producing 4 round keys per block. The round function is g(t,k) = (t+k) ^ ((t+k)<<7) ^ ((t+k)>>9) on 32-bit halves. The PRNG is seeded from ctx+0x10 = SHA256(username padded/truncated to 20 bytes) XOR-folded to 64 bits. Feistel ⇒ invertible, so payload = inverse(TARGET, username) gives a valid license.dat for any username.
- The CRC path self-poisons, forcing the intended username. main wins only if ebx ==
0xbf2b0fa2after the chain. The first shellcode is a control-flow-flattened CRC32, but before returning it does (blocks0x4a816ca8→0xf5d7ecb7→0xbe1daee1insc.bin):
mov r8d, eax
rdtsc ; edx = TSC high
mov ecx, edx ; @ sc.bin+0x1c137
xor eax, ecx
xor eax, 0x8(%r12)
...
xor eax, 0x14
cmp eax, 0xbf2b0fa2 ; @ sc.bin+0x200b7
cmovne <continue>, <poison> ; if EQUAL -> result ^= 0x42483236Measured directly with the emulator:
crc32(u)=0xbf2b0fb6 -> ret=0xfd633d94 (expected 0xbf2b0fa2, delta 0x42483236)
crc32(u)=0xbf2b0fa2 -> ret=0xbf2b0fb6 (no poison)So any username forged to hit the magic CRC is deliberately corrupted. The clean path is a different branch entirely, gated by:
cmp r8d, 0x14 ; length == 20?
cmp byte ptr [rdi+0x14], 0 ; and NUL-terminated at 20?
cmp al, 0x61 / cmp al, 0x7a ; every byte in [a-z] (x20)Only a username of exactly 20 lowercase letters enters that branch, which skips the poison and instead runs 14 hidden byte-checksum constraints over all 20 username bytes (rotate / add / sub / and / or / xor, e.g. ror(u[6],1); sub; add u[0x13]; sub u[0]; and ror(u[9],3); add ror(u[12],1); add ror(u[3],6); cmp al, 0xfe). Failing any of them jumps to the flattening state 0x2d0d239c (the reject sink). The username is therefore over-determined and recoverable.
03Exploitation
- Get BPF privileges. Unprivileged the binary silently exits 1. Build a container image with the right runtime libs and run privileged with tracefs/debugfs mounted:
FROM debian:trixie-slim RUN apt-get update && apt-get install -y --no-install-recommends \ libelf1t64 zlib1g libssl3t64 gdb python3 strace file procps
docker run --rm --privileged -e SUDO_USER=alice \
-v /home/kali/Desktop/CTF/rev/extended:/ch:ro -v "$PWD":/w chsolve bash -c \
'mount -t debugfs none /sys/kernel/debug; mount -t tracefs none /sys/kernel/tracing;
cd /w/work && /ch/challenge'- Dump all 40 decrypted chunks. GDB single-stepping the SIGTRAP chain does not survive (the handler setjmps and re-raises). Instead LD_PRELOAD a
syscall()interposer that writes each page after the real call returns, by then the eBPF program has already rewritten it:
long syscall(long nr, ...) {
/* varargs -> a0..a5 */
long r = real_syscall(nr,a0,a1,a2,a3,a4,a5);
if (nr == 257 && a0 == 0x1337) { /* openat, dirfd magic */
char name[128];
snprintf(name,sizeof name,"/w/pages/p%05d_%p_%ld.bin",idx++,(void*)a1,a2);
FILE *g = fopen(name,"wb");
if (g) { fwrite((void*)a1,1,a2,g); fclose(g); }
}
return r;
}Reassemble pages into chunks by address contiguity (a short final page ends a chunk). First pass: 9 chunks; the chain only unlocks further chunks as the license gets more correct, so this step is re-run after every license improvement.
- Build a standalone chunk runner so the chain can be driven thousands of times without Docker.
harness3.c: mmap a 32 MB RWX region, install a SIGTRAP handler that maps r10 (chunk id) → chunk file, memcpys it into the region, and rewrites uc_mcontext.gregs[REG_RIP] = region, REG_R8 = rip+1. Entry trampoline sets r12 = ctx, ebx = crc32(user)^0x14, ctx+0x10= SHA256(user padded to 20) XOR-folded. This reproduces the real chain exactly:
[trap] id=0x5cf -> chunk 1 (len 113482)
[trap] id=0x535 -> chunk 2 (len 156122)
...- Recover the license header byte-by-byte. Each header byte is checked by a single real cmp inside its own chunk, and a correct byte unlocks a brand-new chunk (new int3 id → [!] unknown id 0x…). Extracting the wrapped-block payloads with the pattern 4d8b9c2450010000 4153 9d … 9c415b shows exactly one comparison per chunk:
chunk08: 80 3e 42 cmp byte [rsi], 0x42 'B'
chunk09: 80 7e 01 48 cmp byte [rsi+1], 0x48 'H'
...Iterating (dump chunks → read the new cmp → append the byte) yields BHLCNS_, then a cmp byte [rsi+rbx], 0x21 with rbx=0x47 ⇒ '!' at index 71. Length probe: only BHLCNS_ + 64 bytes +! (72 bytes) reaches the transform.
- Scrape the 512-bit target from the oracle. With a 72-byte license installed, strace the harness and parse the 512 syscall_0x1337(a0, a1, …) lines; a0&1 is the produced bit, a1 selects the expected parity (see Vulnerability). This is username-independent.
- Reconstruct the Feistel/PRNG and invert it. Locate the round subroutine via call 0x… inside chunk 23 (e840bf0000 at
0x24326→0x3026b), read the constant pool at0x51200(d5c4a3f2412b871d 157c4a7fb979379e 0000802f 0000804f ec51d83f ded6fc3d), and re-implement in Python (see keycode). Verified against ground truth:
transform(b'A'*64, 'alice')
got 0c2fa824728196fb0dd382ad62b8843f…de81c2
expected 0c2fa824728196fb0dd382ad62b8843f…de81c2 MATCH TrueThen license_for(u) = b'BHLCNS_' + inverse(TARGET, u) + b'!' produces a valid license for any username.
- Confirm end-to-end with the CRC compare patched out. Patching
0x6c81cmp $0xbf2b0fa2,%ebx to always take the branch made the unmodified license logic reach the decryptor and print Congrats: <garbage>, proving only the username remained unknown. - Recover the username. Brute-force is dead: forging crc32 triggers the ^
0x42483236poison, and rockyou/seclists/binary-strings searches (≈140 M candidates, filtered on "AES plaintext looks like a flag") produced nothing. Instead, exploit the 20-lowercase branch: • Patchsc.bin+0x1c137(89 d1 mov ecx,edx) → 31 c9 xor ecx,ecx to neutralise rdtsc, giving a deterministic emulator. • Emulate with Unicorn + Capstone. On every cmov{e,ne} r11d, [r12+0x144] where the loser state is0x2d0d239c(the reject sink), flip ZF so the continue state always wins. This forces past all failing constraints and exposes the full check chain:
ret 0x1a596ae5 stream 243930 forced 14- Replay the instruction stream (all junk-free, since only instructions between popfq and pushfq are real), and lift it to an op list: al_buf i / cl_buf i (mov al, [r10+i]), rot {ror,rol} k (the mov ecx,k / shr edx,cl / mov ecx,8 / shl eax,cl / or eax,edx / and eax,
0xffidiom), binreg add|sub|xor|and|or, binimm, cmpimm, plus a DECIDE marker at each forced branch. 448 ops, 14 decides, referencing buffer
indices 0–19. The op list is identical for abcdefghijklmnopqrst, zyxwvutsrqponmlkjihg and qqqqqqqqqqqqqqqqqqqq, i.e. it is a fixed straight-line formula. • Re-evaluate symbolically in z3 with byte constraints and the CRC:
N=20
buf=[BitVec('b%d'%i,8) for i in range(N)]
s=Solver()
for b in buf: s.add(UGE(b,0x61), ULE(b,0x7a)) # [a-z]
for z in sym_eval(ops,buf): s.add(z==0) # the 14 checksums
s.add(crc32_sym(buf)==BitVecVal(0xbf2b0fa2,32)) # main's magic at 0x6c81
check: sat
SOLUTION: b'xorionxarcheryxseedx' crc=0xbf2b0fa2
check: unsat <-- unique- Derive the license and the flag.
username: xorionxarcheryxseedx crc32 = 0xbf2b0fa2
license.dat (72 raw bytes):
42484c434e535f667b1f27e68d329b95ba85c41eb42c68d36b144af250b84d72
69063c50b400db2bf1caeaa28dcfb9e54c8620d2fff7c1b62bab2a651e39373f
e292d650a11ec621
AES key = SHA256(license || username)
= 808d79f30075750b3897f3ddff0268dd240449dbd9a481e7c600c14c859b3382
flag = AES-256-ECB-decrypt(challenge[0x2111850 : +80], key)
= b"BHFlagY{...}\x07\x07\x07\x07\x07\x07\x07"- Verify on the untouched binary (SHA256 05cca43b…), with the raw 72-byte license.dat in cwd:
SUDO_USER=xorionxarcheryxseedx ./challenge
===============================================
Do you have a license?
===============================================
Congrats: BHFlagY{...}
EXIT=0Key code import struct, hashlib, math
C1=0x1d872b41f2a3c4d5 # clmul multiplier (chunk23 @0x51204)
C2=0x9e3779b97f4a7c15 # xor constant
C3=struct.unpack('<f', bytes.fromhex('0000802f'))[0] # 2^-32
C6=struct.unpack('<f', bytes.fromhex('0000804f'))[0] # 2^32
C4=struct.unpack('<f', bytes.fromhex('ec51d83f'))[0] # 1.69
C5=struct.unpack('<f', bytes.fromhex('ded6fc3d'))[0] # 0.1234567
M=(1<<32)-1
def f32(x): return struct.unpack('<f', struct.pack('<f', x))[0]
def clmul64(a,b): # pclmulqdq xmm0, xmm1, 0
r=0
for i in range(64):
if (b>>i)&1: r ^= a<<i
return r
def prng(state): # one round key
state = (clmul64(state, C1) & ((1<<64)-1)) ^ C2
u = state & M
x2 = f32(f32(float(u)) * C3) # cvtsi2ss ; mulss
x3 = f32(f32(x2 * C4) + C5) # mulss ; addss
x3 = f32(x3 - math.floor(x3)) # roundss floor ; subss
return state, int(f32(x3 * C6)) & M # mulss 2^32 ; cvttss2si
def g(t, k): # Feistel round function
t = (t + k) & M
return (t ^ ((t<<7)&M) ^ (t>>9)) & M
def userhash(u): # ctx+0x10 seed
if isinstance(u,str): u=u.encode()
w=struct.unpack('<4Q', hashlib.sha256(u[:20].ljust(20,b'\0')).digest())
return w[0]^w[1]^w[2]^w[3]
def keystream(seed, nblocks=8, rounds=4):
st=seed; ks=[]
for _ in range(nblocks):
blk=[]
for _ in range(rounds):
st,k=prng(st); blk.append(k)
ks.append(blk)
return ks
def dec_block(L,R,ks):
for k in reversed(ks):
Rp = L
L = (R ^ g(Rp,k)) & M
R = Rp
return L,R
def inverse(target, user): # 64-byte target -> 64-byte payload
ks=keystream(userhash(user)); out=b''
for i in range(8):
L,R=struct.unpack('<II', target[i*8:i*8+8])
L,R=dec_block(L,R,ks[i])
out+=struct.pack('<II',L,R)
return out
TARGET=bytes.fromhex('5865ae6b9ab3837b261c1051d1bb1ee69e5d9ae4580d42f1'
'd867ca7d9d9229e38fb236f04e2012894f66f96f573ef8b0'
'0ec30ceb7388b9684a38ee408c9e7504')
license_for = lambda u: b'BHLCNS_' + inverse(TARGET, u) + b'!' # 72 bytes04Dead ends
- GDB cannot walk the SIGTRAP chain. hbreak past the int3 and signal SIGTRAP loops both ended in Program terminated with signal SIGTRAP after 1–4 stops, the handler setjmp s at
0x9832and re- raises. Only a breakpoint at the pre- memcpy point (base+0x9986) captured a chunk, and only the first one. Replaced entirely by the LD_PRELOADsyscall()interposer, which got all 8,385 pages in one run. - Unprivileged runs look like a working program. The banner prints and it exits 1 with no error; all three bpf(BPF_PROG_LOAD) calls fail with EPERM. sagemath/sagemath as a container base segfaulted the binary (wrong glibc); a debian:trixie-slim image with libelf1t64 zlib1g libssl3t64 worked. tracefs / debugfs must also be mounted inside the container or sys_enter_openat can't be attached.
- Unicorn refused vfmadd132ss (UC_ERR_INSN_INVALID at chunk23+
0x49da5) until mu.ctl_set_cpu_model(UC_CPU_X86_SKYLAKE_SERVER) was set. - Harness exec region too small. The runner mapped 1 MB but chunks 31–34 are 7.3 MB; the chain silently died mid-way. Bumped regsz to 32 MB, NCH to 128, and the scaddr table to 512 entries.
- rdtsc made the emulator non-deterministic in exactly the branch that mattered. Patching
sc.bin+0x1c137(mov ecx,edx → xor ecx,ecx) intosc_nots.binwas required before any of the constraint work was reproducible. - Long dead end: forging the CRC. Built
crcgen.c(CRC-suffix solver + full Feistel + AES) and swept ~1.2 G username prefixes across charsets, filtering on "license payload is printable" and "AES plaintext contains { ". Also ran rockyou (14.3 M), seclists (124 M),/usr/share/dict, binary strings (318 K) and 9 key-derivation variants (lic+u, lic+u+NL, …). All produced only false positives, because the shellcode poisons any username whose CRC matches, this was only understood after locating cmp eax,0xbf2b0fa2inside the shellcode atsc.bin+0x200b7and measuring delta =0x42483236. - Block-level tracing mis-modelled the constraints. A first pass that lifted whole flattened blocks reported 10 constraints and reproduced only 9 of the observed al values (MATCH als: False) because
block boundaries interleave real instructions across state transitions. Redone at single-instruction granularity, keying "real" on the popfq … pushfq window, which found 14. • Over-broad branch forcing. The first forcing hook also clobbered the cmp al,0x61 / cmp al,0x7a range checks, so the trace showed 40 irrelevant character comparisons and 0 checksums. Restricting the force to cmov* whose losing state is 0x2d0d239c fixed it. • license.dat written as hex text. A 145-byte file containing the 144-char hex string silently fails, the program read s the file raw and needs the decoded 72 bytes. • No public writeup exists for this challenge (web search returned only unrelated FlagYard writeups), so everything had to come from the binary.