-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathfunctions.py
427 lines (337 loc) · 15.6 KB
/
functions.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import sys,socket,select,time,errno,base64,random,globalvars
#################################################################################################################################
# Write "pattern" to "file"
def fileWrite(file,pattern):
try:
fileHandle = open(file, 'w')
fileHandle.write(pattern)
fileHandle.close()
except:
print "\n[-] Invalid directory or directory doesn't exist"
exitProgram(4)
# ----------------------------------------------------------------------------------------------------------------------------- #
# Prints "message" and do keyboard input unless Ctrl-C
def fileInput(message):
print message + "\n"
user_input = ''
try:
while(1):
user_input = user_input + raw_input()
user_input = user_input + "\n"
except KeyboardInterrupt:
return user_input
###################################################################################################################################
# Creates a payload with ("user" + PAYLOAD) and sends the data to a TCP socket, iterating with the given minim, maxm & salt params
def fuzzUser(user):
printCommand(user)
for length in range(globalvars.minim, globalvars.maxm+1, globalvars.salt):
payloadCount(length)
pattern = createPattern(length)
pattern = addCommandPattern(user,0,pattern)
sock = createSocketTCP(pattern,length)
sendDataTCP(sock,pattern,length,1)
# ----------------------------------------------------------------------------------------------------------------------------- #
# First, sends ("user" + "username") as an authentication pattern. Then creates a payload with ("passwd" + PAYLOAD) and sends
# the data to the same TCP socket, iterating with the given minim, maxm & salt params
def fuzzPass(username,user,passwd):
printCommand(passwd)
for length in range(globalvars.minim, globalvars.maxm+1, globalvars.salt):
payloadCount(length)
pattern = createPattern(length)
pattern = addCommandPattern(passwd,0,pattern)
sock = createSocketTCP(pattern,length)
sendCredential(sock,user,username)
sendDataTCP(sock,pattern,length,1)
#################################################################################################################################
# Simply it create and fuzz a TCP socket, iterating with the given minim, maxm & salt params
def fuzzTCP():
printCommand("TCP Socket")
for length in range(globalvars.minim, globalvars.maxm+1, globalvars.salt):
payloadCount(length)
pattern = createPattern(length)
sock = createSocketTCP(pattern,length)
sendDataTCP(sock,pattern,length,1)
# ----------------------------------------------------------------------------------------------------------------------------- #
# Simply it create and fuzz a UDP socket, iterating with the given minim, maxm & salt params
def fuzzUDP():
printCommand("UDP Socket")
for length in range(globalvars.minim, globalvars.maxm+1, globalvars.salt):
payloadCount(length)
pattern = createPattern(length)
sock = createSocketUDP(pattern,length)
sendDataUDP(sock,pattern,pattern,length,1)
#################################################################################################################################
# Some different kinda of fusion-pattern's functions, pretty intuitive
def addCommandPattern(command,endcommand,pattern):
return (str(command) + " " + str(pattern))
def addCommandNoSpace(command,endcommand,pattern):
return (str(command) + str(pattern))
def addCommandPatternEmail(command,endcommand,pattern):
return (str(command) + " " + "backfuzz@" + str(pattern) + ".com")
def addDoubleCommand(command,endcommand,pattern):
return (str(command) + " " + str(pattern) + " " + str(endcommand))
def addDoubleCommandNoSpace(command,endcommand,pattern):
return (str(command) + str(pattern) + " " + str(endcommand))
# ----------------------------------------------------------------------------------------------------------------------------- #
# It uses a given alive TCP socket "sock", a given list of "commands" (like ['STAT','LIST', etc]), an optional ending "endcommand"
# (to use like COMMAND + PATTERN + ENDCOMMAND), and a "type" ("SingleCommand", "Email", "DoubleCommand" etc.).
# The main idea of this function is to expand the command's posible combination's, and use a proper one while fuzzing a particular
# combination. It iterate's with the given minim, maxm & salt params
def fuzzCommands(sock,commands,endcommand,type):
for i in range(0,len(commands)):
printCommand(commands[i])
for length in range(globalvars.minim, globalvars.maxm+1, globalvars.salt):
payloadCount(length)
pattern = createPattern(length)
Switch = {
"SingleCommand":addCommandPattern,
"SingleCommandNoSpace":addCommandNoSpace,
"Email":addCommandPatternEmail,
"DoubleCommand":addDoubleCommand,
"DoubleCommandNoSpace":addDoubleCommandNoSpace
}
pattern = Switch[type](commands[i],endcommand,pattern)
if i == (len(commands) - 1) and (length+globalvars.salt) > globalvars.maxm:
sendDataTCP(sock,pattern,length,1)
else:
sendDataTCP(sock,pattern,length,0)
#################################################################################################################################
# Create's a new TCP socket and returns it. If it's not possible to create the socket, something has happened, do some checks and
# return the propper payload with the showPayload() function, given "pattern" and "length" for that
def createSocketTCP(pattern,length):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(globalvars.timeout)
sock.connect((globalvars.host, globalvars.port))
return sock
except KeyboardInterrupt:
exitProgram(6)
except socket.error, err:
error = err[0]
if error == errno.ECONNREFUSED:
print "[!] We got a connection refused, the service almost certainly crashed"
showPayload(pattern,length)
except:
print "[!] Another socket error, the service almost certainly crashed"
showPayload(pattern,length)
# ----------------------------------------------------------------------------------------------------------------------------- #
# It uses a given alive TCP socket "sock", and send's the pattern "pattern" to the same socket. If it's not possible to send
# the data through the socket, something has happened, do some checks and return the propper payload with the showPayload()
# function, given "pattern" and "length" for that. Optionally you can specify to close the socket after sending the data or not.
def sendDataTCP(sock,pattern,length,close):
try:
time.sleep(globalvars.timeout)
sock.settimeout(globalvars.timeout)
pattern = pattern + "\r\n"
sock.send(pattern)
sock.recv(4096)
if close == 1:
sock.close()
else:
pass
except KeyboardInterrupt:
exitProgram(6)
except socket.error, err:
error = err[0]
if error == errno.EPIPE:
print "\n[!] We got a broken pipe, that is a *possible* crash. Checking if it really crashed ..."
check_conn = createSocketTCP(pattern,length)
print "[!] The service has not really crashed, continuing fuzzing ...\n"
if error == errno.ECONNREFUSED:
print "\n[!] We got a connection refused, the service almost certainly crashed"
showPayload(pattern,length)
except:
print "[!] Another socket error, the service almost certainly crashed"
showPayload(pattern,length)
#################################################################################################################################
# Create's a new UDP socket and returns it. If it's not possible to create the socket, something has happened, do some checks and
# return the propper payload with the showPayload() function, given "pattern" and "length" for that (yes, I know that UDP is
# definitely not real "connection" oriented, but only for convention)
def createSocketUDP(pattern,length):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(globalvars.timeout)
sock.connect((globalvars.host, globalvars.port))
return sock
except KeyboardInterrupt:
exitProgram(6)
except socket.error, err:
error = err[0]
if error == errno.ECONNREFUSED:
print "\n[!] We got a connection refused, the service almost certainly crashed"
showPayload(pattern,length)
except:
print "[!] Another socket error, the service almost certainly crashed"
showPayload(pattern,length)
# ----------------------------------------------------------------------------------------------------------------------------- #
# It uses a given UDP socket "sock", and send's the pattern "pattern" to the same socket. If it's not possible to send
# the data through the socket, something has happened, do some checks and return the propper payload with the showPayload()
# function, given "pattern" and "length" for that. Optionally you can specify to close the socket after sending the data or not.
def sendDataUDP(sock,pattern,spattern,length,close):
try:
time.sleep(globalvars.timeout)
sock.settimeout(globalvars.timeout)
sock.send(pattern)
sock.recv(4096)
if close == 1:
sock.close()
else:
pass
except KeyboardInterrupt:
exitProgram(6)
except socket.error, err:
error = err[0]
if error == errno.ECONNREFUSED:
print "\n[!] We got a connection refused, the service almost certainly crashed"
showPayload(spattern,length)
except:
print "[!] Another socket error, the service almost certainly crashed"
showPayload(spattern,length)
#################################################################################################################################
# Send's a "command" + "login" data to a given alive socket, for login purposes.
def sendCredential(sock,command,login):
try:
data = str(command) + " " + str(login) + "\r\n"
sock.send(data)
except:
exitProgram(5)
# ----------------------------------------------------------------------------------------------------------------------------- #
# If not "username" / "password" given, use a default one
def checkDefaultUser(username,password):
if username == '':
username = "anonymous"
if password == '':
password = "anonymous@test.com"
else:
pass
return username,password
# ----------------------------------------------------------------------------------------------------------------------------- #
# Specify a new "username" / "password" combination.
def createUser():
try:
username = raw_input("[!] Insert username (default: anonymous)> ")
password = raw_input("[!] Insert password (default: anonymous@test.com)> ")
except KeyboardInterrupt:
exitProgram(6)
return checkDefaultUser(username,password)
#################################################################################################################################
# Show payload details, with the correct "pattern" and "length"
def showPayload(pattern,length):
print "\n#####################################################################################################################################"
print "\nPayload details:\n================\n"
print "Host: " + globalvars.host
print "Port: " + str(globalvars.port)
print "Type: " + globalvars.plugin_use
print "Connection refused at: " + str(length)
if globalvars.show_pattern:
print "\nPayload:\n========\n"
print pattern
print "\n#####################################################################################################################################"
exitProgram(4)
# ----------------------------------------------------------------------------------------------------------------------------- #
def printCommand(command):
print "\n[!] " + str(command) + " fuzzing ...\n"
# ----------------------------------------------------------------------------------------------------------------------------- #
def payloadCount(pos):
print "MIN: " + str(globalvars.minim) + " MAX: " + str(globalvars.maxm) + " Giving it with: " + str(pos)
# ----------------------------------------------------------------------------------------------------------------------------- #
# Exit code's
def exitProgram(code):
if code==1:
sys.exit("\n[!] Exiting help ...")
if code==2:
sys.exit("\n[!] End of fuzzing, exiting ...")
if code==3:
sys.exit("\n[-] Check your arguments, exiting with errors ...")
if code==4:
sys.exit("\n[!] Exiting ...")
if code==5:
sys.exit("\n[-] Error sending credentials, exiting ...")
if code==6:
sys.exit("\n[!] Keyboard Interrupt, exiting ...")
#################################################################################################################################
# Colors for terminal
class colors:
BLUE = '\033[94m'
GREEN = '\033[92m'
RED = '\033[91m'
ENDC = '\033[0m'
# ----------------------------------------------------------------------------------------------------------------------------- #
# Convert a str variable "convert" to int
def strToInt(convert,typeParam):
try:
value = int(convert)
return value
except:
print "Number given in " + typeParam + " is invalid"
exitProgram(3)
# ----------------------------------------------------------------------------------------------------------------------------- #
# Convert a str variable "convert" to float
def strToFloat(convert,typeParam):
try:
value = float(convert)
return value
except:
print "Number given in " + typeParam + " is invalid"
exitProgram(3)
# ----------------------------------------------------------------------------------------------------------------------------- #
def checkMinMax(min,max):
if min >= max:
print "\n[-] MIN >= MAX"
exitProgram(3)
# ----------------------------------------------------------------------------------------------------------------------------- #
# Some check's for invalid pattern's-flavour's
def checkFlavour(flavour):
flavour_list = ["Cyclic", "CyclicExtended", "Single", "FormatString"]
if flavour not in flavour_list:
print "\n[-] Pattern-Flavour " + str(flavour) + " doesn't exist, check help"
exitProgram(3)
#################################################################################################################################
# Create's a single pattern, with the given "size"
def createPatternSingle(size):
return "A" * size
# ----------------------------------------------------------------------------------------------------------------------------- #
# Create's a format-string-like pattern, with the given "size"
def createPatternFormat(size):
pattern = ''
for cont in range(1,size+1):
pattern += "%" + random.choice('snx')
return pattern
# ----------------------------------------------------------------------------------------------------------------------------- #
# Taken from mona.py / http://redmine.corelan.be/projects/mona , Copyright (c) 2011, Corelan GCV
# Create's a cyclic pattern, with the given "size"
def createPatternCyclic(size):
char1="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
char2="abcdefghijklmnopqrstuvwxyz"
char3="0123456789"
if globalvars.pattern_flavour == "CyclicExtended":
char3 += ",.;+=-_!&()#@'({})[]%" # ascii, 'filename' friendly
charcnt=0
pattern=""
max=int(size)
while charcnt < max:
for ch1 in char1:
for ch2 in char2:
for ch3 in char3:
if charcnt<max:
pattern=pattern+ch1
charcnt=charcnt+1
if charcnt<max:
pattern=pattern+ch2
charcnt=charcnt+1
if charcnt<max:
pattern=pattern+ch3
charcnt=charcnt+1
return pattern
# ----------------------------------------------------------------------------------------------------------------------------- #
# Switch between the different pattern's
def createPattern(size):
Switch = {
"Cyclic":createPatternCyclic,
"CyclicExtended":createPatternCyclic,
"Single":createPatternSingle,
"FormatString":createPatternFormat,
}
pattern = Switch[globalvars.pattern_flavour](size)
return pattern
#################################################################################################################################