Re-alloc Revenge — pwnable.tw (350 pts)
← Back to pwnable.tw Challenge Index
nc chall.pwnable.tw 10310Flag:
FLAG{r3alloc_the_heap_r3alloc_the_file_Str34m_r3alloc_my_lif3}
Challenge Overview
A heap-menu binary (x86-64, Full RELRO, PIE, NX, Canary, FORTIFY) linked against glibc 2.29 (Ubuntu 2.29-0ubuntu2). The program provides only two heap slots (heap[0], heap[1]), allocations capped at 0x78 bytes, and no view/print functionality. All allocation uses realloc() under the hood — even the "free" operation is realloc(ptr, 0).
checksec:
Arch: amd64-64-little
RELRO: Full RELRO # GOT not writable
Stack: Canary found
NX: NX enabled
PIE: PIE enabled
FORTIFY: EnabledProgram Logic
| Menu | Action | Behavior |
|---|---|---|
| 1 Alloc | allocate(idx, size, data) |
Requires heap[idx] == NULL; size <= 0x78; malloc(size) + read_input |
| 2 Realloc | reallocate(idx, size, data) |
Requires heap[idx] != NULL; size <= 0x78; realloc(ptr, size) |
| 3 Free | rfree(idx) |
realloc(heap[idx], 0) then heap[idx] = NULL |
| 4 Exit |
Vulnerabilities
V1 — realloc(ptr, 0) UAF / Dangling Pointer (Primary)
Root Cause: The reallocate function (menu option 2) calls realloc(heap[idx], size). When size == 0, glibc's realloc behaves as free(ptr) and returns NULL. The program checks the return value — since it's NULL, it treats this as a "failure" and does not clear heap[idx]:
// reallocate (simplified)
void *p = realloc(heap[idx], size);
if (p == NULL) {
// "failure" path — heap[idx] NOT cleared
return;
}
heap[idx] = p; // only reached if realloc succeededIn contrast, the clean rfree (menu option 3) also calls realloc(ptr, 0) but unconditionally sets heap[idx] = 0.
Impact: After realloc(idx, 0), heap[idx] holds a dangling pointer to a freed chunk. This enables:
- UAF write:
realloc(idx, original_size, data)writes into the freed chunk (editing itsfdpointer in tcache). - Double-free:
realloc(idx, 0)again frees the same chunk a second time (after clearing the tcachekeyfield to bypass glibc 2.29's double-free check).
V2 — Off-by-One NULL in allocate (Minor, Enabler)
The allocate function's read_input appends a NUL byte at buf[nread]. When exactly size bytes are sent (no newline), this writes a \x00 one byte past the chunk's usable area — a poison null byte that can corrupt the size field of the next chunk.
Key Constraints
- Only 2 slots (
heap[0],heap[1]) — severely limits heap manipulation. Requires creative use ofreallocto resize/split/merge chunks. - Max size 0x78 — all allocations fit in tcache bins (0x20–0x80). Getting libc pointers requires forcing chunks into unsorted/smallbins.
- No output primitive — no "view" or "print" function. Leak must come from FSOP (overwriting
_IO_2_1_stdout_). - Full RELRO — GOT is read-only. Must target hooks (
__free_hook,__malloc_hook,__realloc_hook). - glibc 2.29 — tcache has a
keyfield for double-free detection (bypass by overwriting it via UAF).
Key Technique: Tcache Poisoning with 2 Slots
The fundamental write-what-where primitive with only 2 slots:
alloc(0, 0x78) # allocate chunk P (tcache bin 0x80)
realloc(0, 0) # free P → tcache[0x80] = [P], heap[0] = P (dangling)
realloc(0, 0x78, p64(target_addr)) # UAF write: P.fd = target_addr
alloc(1, 0x78) # pop P from tcache; entries[0x80] = target_addr
realloc(1, 0x28) # shrink P to 0x30, remainder goes to different bin
free(1) # free P(0x30) → tcache[0x30]; heap[1] = NULL
alloc(1, 0x78, payload) # pops target_addr from tcache[0x80] → write payload thereThe trick: after popping P, shrink it to a different size before freeing, so tcache[0x80]'s head stays pointing at target_addr.
Exploit Paths
All 51 solutions share the same core strategy (V1 UAF → tcache poison → stdout leak → hook overwrite), differing mainly in how they obtain libc pointers.
Path A — Fake Large Chunk → Unsorted Bin → Partial Overwrite stdout (Most Common)
Used by: Solutions 821, 2972, 3025, 3148, 6923, 7905, 8153, 9074, 9251, 9448, 15522, 17006, 20719, 26250, 31599, 34817, exp.py
Steps:
Craft a fake large chunk (size
0x421–0x811) by writing a fake size field into an adjacent chunk viareallocresizing. The fake chunk must be large enough to bypass tcache (> 0x408 bytes or fill tcache count to 7).Free the fake chunk so it enters the unsorted bin. This writes
main_arenapointers (libc addresses) into the chunk'sfd/bkfields.Partial overwrite the
fdpointer (low 2 bytes) to redirect it to_IO_2_1_stdout_(orstdout - 8). Since libc is page-aligned, the 4th nibble (bits 12–15) is random → 1/16 bruteforce.Allocate at stdout and overwrite its
_flagsto0xfbad1800and clear_IO_read_ptr,_IO_read_end,_IO_read_baseto zero. The nextputs()call (menu output) then leaks libc memory.Compute libc base from the leak:
libc_base = leaked_ptr - 0x1e7570.Tcache poison a bin to point at
__free_hook - 8(or__realloc_hook). Write"/bin/sh\0" + p64(system)there.Trigger:
free(idx)→realloc(heap[idx], 0)→__realloc_hook(heap[idx], 0)=system("/bin/sh").
Variant — Fake Chunk via Fill tcache + malloc_consolidate: Some solutions fill the tcache for a given bin size to 7 entries (using repeated realloc(0)/realloc(size) UAF), then trigger malloc_consolidate by sending a long input ("1" * 0x400) to the menu. This merges fastbin chunks into smallbin, providing libc pointers without needing a fake large chunk.
Path B — Tcache perthread_struct Poisoning (1/16 or 1/256)
Used by: Solutions 821, 3480, 6247, 15522 (1/256 variant), 34817
Steps:
Double-free via UAF to get a tcache chunk whose
fdcan be overwritten.Partial overwrite
fdwith\x10\x60(or\x10\x90,\x10\xa0depending on heap layout) to point at thetcache_perthread_struct(heap offset ~0x10). This is a 1/16 bruteforce on the heap ASLR nibble.Allocate the tcache struct as a regular chunk → gain full control over all tcache bin counts and entry pointers.
Set all counts to 0xff (or 0x07) → freeing any chunk now bypasses tcache and goes to unsorted bin (since tcache appears "full").
Free the tcache struct itself → goes to unsorted bin → libc pointers land in the struct.
Partial overwrite the unsorted bin
fdto point at_IO_2_1_stdout_→ FSOP leak (another 1/16 bruteforce, making total 1/256 for this path).Same finish as Path A: hook overwrite → shell.
Path C — Fill Tcache via Repeated UAF + Smallbin Consolidation
Used by: Solutions 9251, 26250, 34817
A cleaner variant that avoids fake chunk sizes entirely:
Fill tcache for a specific bin size to 7 entries using repeated
realloc(0)+realloc(size)UAF cycles (each cycle frees the same chunk, but thekeyis cleared between frees).Trigger
malloc_consolidateby sending an oversized menu input (sla("choice: ", "1"*0x400)). Fastbin chunks merge into smallbin → libc pointers appear.UAF edit the smallbin
fd→ partial overwrite to stdout → FSOP leak.Same finish: hook overwrite → shell.
Exploit Primitive Summary
| Primitive | Source | Reliability |
|---|---|---|
| UAF / Dangling pointer | V1: realloc(ptr, 0) returns NULL, slot not cleared |
Deterministic |
| Tcache fd overwrite | UAF + realloc(idx, size, payload) |
Deterministic |
| Double-free | UAF + clear key field + realloc(idx, 0) again |
Deterministic |
| Unsorted/smallbin libc pointers | Fake large chunk or fill tcache + consolidate | Deterministic (heap groom) |
| Stdout FSOP leak | Partial overwrite fd → _IO_2_1_stdout_ |
1/16 bruteforce |
| Tcache struct control | Partial overwrite fd → tcache_perthread_struct |
1/16 bruteforce |
| Arbitrary write | Tcache poisoning with 2-slot shrink trick | Deterministic after leak |
| Code execution | __free_hook = system or __realloc_hook = system/one_gadget |
Deterministic after arb write |
Offsets (glibc 2.29, Ubuntu 2.29-0ubuntu2)
_IO_2_1_stdout_ = 0x1e5760
_IO_file_jumps = 0x1e7570 # common leak delta
system = 0x52fd0
__free_hook = 0x1e75a8
__realloc_hook = 0x1e4c28
__malloc_hook = 0x1e4c30
main_arena_top = 0x1e4ca0
one_gadgets = [0xe21ce, 0xe21d1, 0xe21d4, 0xe237f, 0xe2383, 0x106ef8]Hook Targets Comparison
| Target | How Triggered | Argument |
|---|---|---|
__free_hook |
rfree(idx) → realloc(heap[idx], 0) internally calls free() |
heap[idx] (set to "/bin/sh") |
__realloc_hook |
Any realloc() call |
(heap[idx], size) — first arg is the pointer |
__malloc_hook |
alloc(idx, size) triggers malloc() |
size argument (less useful) |
Most solutions target __free_hook - 8 and write "/bin/sh\0" + p64(system), then free(idx) calls system("/bin/sh"). Some target __realloc_hook with a one_gadget instead.
Solution Write-ups
| File | Path | Bruteforce | Key Technique |
|---|---|---|---|
exp.py |
A | 1/16 | Fake 0x431 chunk → unsorted bin → stdout partial overwrite |
821.md |
B | 1/16 | Tcache struct poison → unsorted bin → stdout |
1763.md |
A | 1/16 | Minimal tcache poison → stdout |
2972.md |
A | 1/16 | Fake 0x4d1 chunk → partial overwrite stdout |
3025.md |
A | 1/16 | Fake chunk + partial write __free_hook |
3148.md |
A | 1/16 | Standard fake chunk + stdout FSOP |
3480.md |
B | 1/16 | Tcache struct → unsorted bin → stdout; also 1/256 variant |
6247.md |
A | 1/16 | Chunk spray + fake 0x811 → partial overwrite |
6249.md |
A | 1/16 | Tcache struct control + fake unsorted chunk |
6923.md |
A | 1/16 | Tcache struct + __free_hook overwrite |
7905.md |
A | 1/16 | Detailed writeup with Chinese analysis; fake 0x431, stdout FSOP |
8153.md |
A | 1/16 | Tricks writeup; 1/16 and 1/256 variants documented |
9074.md |
A | 1/16 | Fake 0x91/0x81 chunk gymnastics → unsorted bin |
9251.md |
A | 1/16 | tcache perthread struct + stdout overwrite |
9448.md |
B | 1/256 | Double bruteforce: heap + libc nibbles |
15522.md |
B | 1/256 | Tcache struct + unsorted bin + partial write |
20719.md |
A | 1/16 | Fake 0xa1 chunk + fill tcache via UAF |
26250.md |
C | 1/16 | Solve detailed with 0xe7 stdout offset |
31599.md |
A | 1/16 | Clean solve, same struct as 3025 |
34817.md |
C | 1/16 | Fill tcache + malloc_consolidate via long menu input |
Files
| File | Description |
|---|---|
exp.py |
Working exploit (sockets-only, no pwntools, auto-retry bruteforce) |
desc.txt |
Challenge description |
artifacts/ |
Original binary and libc |
solution/*.md |
Community write-ups (51 solutions) |