-
Notifications
You must be signed in to change notification settings - Fork 4
/
intruder.py
320 lines (293 loc) · 14.3 KB
/
intruder.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
#!/usr/bin/python3
"""
Description:
This uses a single set of payloads_sets. It targets each payload position in turn, and places each payload into that position in turn.
Positions that are not targeted for a given request are not affected - the position markers are removed and any enclosed text that appears between them in the template remains unchanged.
This attack type is useful for fuzzing a number of request parameters individually for common vulnerabilities.
The total number of requests generated in the attack is the product of the number of positions and the number of payloads_sets in the payload set.
"""
import argparse
import requests
import Burpee.burpee as burp
import re
import string
import urllib3
from urllib.parse import urlparse
from bs4 import BeautifulSoup
from tabulate import tabulate
from core.colors import *
from itertools import combinations, product
from time import sleep
# Defines Parser:
parser = argparse.ArgumentParser(description = 'Intruder is a powerful tool for automating customized attacks against web applications. It can be used to automate all kinds of tasks that may arise during your testing.')
parser.add_argument('request_file', help = 'Request file with marked variables (POST or GET).')
parser.add_argument('-p --payloads_sets', help = 'Set or multiple sets of payloads_sets to run.', required =
True,
nargs = '+', dest = 'payloads_sets')
parser.add_argument('-o', '--output', help = 'Name for the output file. (Default: output.txt)',
dest = 'output_path',
default = 'output.txt')
parser.add_argument('-s', '--sleep', help = 'Sets a sleep timer (in secs) between requests.', type = float)
parser.add_argument('-v', '--verbose', help = 'Verbose mode to show errors', action = 'store_true',
default = False)
# Args to be parsed:
args = parser.parse_args()
delay = args.sleep
output_path = args.output_path
payloads_sets = args.payloads_sets
request_file = args.request_file
verbose = args.verbose
headers, POST_data = burp.parse_request(request_file)
METHOD = burp.get_method_and_resource(request_file)[0] # Sets METHOD (POST \ GET)
def main_menu() -> str:
"""Main Menu that will prompt the user to choose the MARKER that will be used and attack type.
Prints the MARKER and METHOD used."""
global table, url
choice = '0'
# Welcome message and input for MARKER:
print('%s%sWelcome to Intruder!%s' % (bold, underline, end))
MARKER = input("%s %sWhat are the markers for the variables? (Default: '$')%s: " % (que, bold,
end)) or '$'
print(f"{info} %s%sMarker set to%s: '{MARKER}'" % (bold, underline, end))
if MARKER in string.punctuation: MARKER = f'\{MARKER}'
# After setting the MARKER, gets the vars from get_vars() using the MARKER as an arg:
url, data_dict = get_vars(MARKER)
# Prints Main-Menu, asks for user's choice:
while choice == '0':
print("%s%sPlease choose an attack-type to run ('q' to quit):%s" % (underline, yellow, end))
print('%s Type %s1%s for %sSniper%s.' % (run, bold, end, bold, end))
print('%s Type %s2%s for %sBattering-Ram%s.' % (run, bold, end, bold, end))
print('%s Type %s3%s for %sPitchfork%s.' % (run, bold, end, bold, end))
print('%s Type %s4%s for %sCluster-Bomb%s.' % (run, bold, end, bold, end))
choice = input('%s %sType your choice%s: ' % (que, bold, end))
# Options:
if choice == '1':
print("%s %sStarting Sniper Attack...%s" % (run, bold, end))
table = sniper(url, data_dict)
return table
elif choice == '2':
print("%s %sStarting Battering-Ram Attack...%s" % (run, bold, end))
table = battering_ram(url, data_dict)
return table
elif choice == '3':
print("%s %sStarting Pitchfork Attack...%s" % (run, bold, end))
table = pitchfork(url, data_dict)
return table
elif choice == '4':
print("%s %sStarting Clusterbomb Attack...%s" % (run, bold, end))
table = clusterbomb(url, data_dict)
return table
elif choice == 'q':
exit()
else:
print('%s %s%sInvalid choice.%s' % (bad, bold, red, end))
choice = '0'
def get_vars(MARKER: str) -> (str, dict):
"""Returns 2 variables depending on the request METHOD:
data_dict = The data dictionary that will be used with requests.
dest_url = Destination URL for the requests."""
referer = headers.get('Referer') # URL Referer
destination = burp.get_method_and_resource(request_file)[1] # Where the referer sends the request to.
local_base = f"{urlparse(referer).scheme}://{urlparse(referer).netloc}"
dest_url = local_base + destination # Finalized destination URL.
# Request Data\Params Builder. Depending on METHOD:
if METHOD == 'POST':
print(f'{info}%s%s Request Method%s: {METHOD}' % (bold, underline, end))
parameters = POST_data.strip().split('&')
data_dict = {}
for f in parameters:
data_dict[f.split('=')[0]] = f.split('=')[1].strip(f'{MARKER}')
if METHOD == 'GET':
print(f'{info} %s%sRequest Method%s: {METHOD}' % (bold, underline, end))
parameters = burp.get_method_and_resource(request_file)[1].strip().split('?')[1]
parameters = parameters.split('&')
data_dict = {}
for f in parameters:
data_dict[f.split('=')[0]] = f.split('=')[1].strip(f'{MARKER}')
return dest_url, data_dict
def sniper(dest_url: str, data_dict: dict) -> str:
"""This uses a single set of payloads_sets. It targets each payload position in turn, and places each payload into that position in turn."""
# Check amount of payloads_sets:
if len(payloads_sets) >= 2:
print(f"{bad} %s%sDetected multiple sets of payloads!%s" % (underline, bold, end))
print(f"{info} %s%sSniper method takes only 1 payload set.%s First payload "
f"provided will "
"be used now." % (bold, yellow, end))
# Builds payloads list:
with open(payloads_sets[0]) as file:
payloads = []
for line in file.readlines(): payloads.append(line.strip()) # Appends payloads_sets from payloads_sets file.
# For table usage:
request_counter, position = 0, 0
Request, Position, Payload, Status_Code, Content, Content_Length = [], [], [], [], [], []
for key in data_dict:
position += 1
original_key = data_dict[key] # Saves the original value of key to a variable.
for payload in payloads:
Request.append(request_counter); Position.append(position); Payload.append(payload)
data_dict[key] = payload
# Starts sending requests with payloads:
try:
request_counter += 1
if METHOD == 'POST': response = requests.post(dest_url, headers = headers, data = data_dict)
if METHOD == 'GET': response = requests.get(dest_url, headers = headers, params = data_dict)
content = BeautifulSoup(response.content, "lxml").text
Status_Code.append(response.status_code); Content.append(content); Content_Length.append(len(content))
except Exception as error:
print(f"\n{bad}%s%s Connection Refused%s (--verbose to check error)" % (bold, red, end))
if verbose: print(f'%sError Message%s:\n{error}' % (underline, end))
print(f'{tab}%sPayload%s: {data_dict[key]}' % (underline, end))
print(f'{tab}%sPosition%s: {position}' % (underline, end))
Status_Code.append('None'); Content.append('[X] Error'); Content_Length.append('None')
pass
data_dict[key] = original_key
if delay: sleep(delay)
Table = tabulate({
'Request' : Request,
'Position' : Position,
'Payload' : Payload,
'Status Code' : Status_Code,
'Content' : Content,
'Content Length': Content_Length,
}, headers = 'keys', tablefmt = 'psql', colalign = ('center', 'center'), disable_numparse=True)
print(f"\n%s %s%sFinished Sniper attack on%s: {url}\n" % (good, underline, bold, end))
print(Table)
return Table
def battering_ram(dest_url: str, data_dict: dict) -> str:
"""Allows only 1 payload, runs on ALL the marked positions in the same time. Prints results to stdout."""
# Check amount of payloads_sets:
if len(payloads_sets) >= 2:
print(f"{bad} %s%sDetected multiple sets of payloads!%s" % (underline, bold, end))
print(f"{info} %s%sBattering-Ram method takes only 1 payload set.%s First payload "
f"provided will "
"be used now." % (bold, yellow, end))
# Builds payloads list:
with open(payloads_sets[0]) as file:
payloads = []
for line in file.readlines(): payloads.append(line.strip()) # Appends payloads_sets from payloads_sets file.
# For table usage:
request_counter = 0
Request, Payload, Status_Code, Content, Content_Length = [], [], [], [], []
for payload in payloads:
for key in data_dict:
data_dict[key] = payload
# Starts sending requests with payloads:
try:
request_counter += 1
Request.append(request_counter); Payload.append(payload)
if METHOD == 'POST': response = requests.post(dest_url, headers = headers, data = data_dict)
if METHOD == 'GET': response = requests.get(dest_url, headers = headers, params = data_dict)
content = BeautifulSoup(response.content, "lxml").text
Status_Code.append(response.status_code); Content.append(content); Content_Length.append(len(content))
except Exception as error:
print(f"\n{bad}%s%s Connection Refused%s (--verbose to check error)" % (bold, red, end))
if verbose: print(f'%sError Message%s:\n{error}' % (underline, end))
print(f'{tab}%sPayload%s: {data_dict[key]}' % (underline, end))
Status_Code.append('None'); Content.append('[X] Error'); Content_Length.append('None')
pass
if delay: sleep(delay)
Table = tabulate({
'Request' : Request,
'Payload' : Payload,
'Status Code' : Status_Code,
'Content' : Content,
'Content Length': Content_Length,
}, headers = 'keys', tablefmt = 'psql', colalign = ('center', 'center'), disable_numparse = True)
print(f"\n%s %s%sFinished Battering-Ram attack on%s: {url}\n" % (good, underline, bold, end))
print(Table)
return Table
def pitchfork(dest_url: str, data_dict: dict) -> str:
"""Uses multiple payload sets. There is a different payload set for each defined position (up to a
maximum of 20). The attack iterates through all payload sets simultaneously, and places one payload
into each defined position. Prints results to stdout."""
# Creates a list of lists of payloads [[p1, p2, p3], [p21, p22, p23]...]
payloads_list_of_lists = []
for Set in payloads_sets:
with open(Set) as Set:
payloads = []
for line in Set.readlines(): payloads.append(line.strip())
payloads_list_of_lists.append(payloads)
# For table usage:
request_counter = 0
Request, Payloads, Status_Code, Content, Content_Length = [], [], [], [], []
# Creates data dict with current payloads:
for cur_values in zip(*payloads_list_of_lists):
named_values = zip(data_dict.keys(), cur_values)
payloads_for_request = dict(named_values)
print(dest_url)
# Starts sending requests for each data dict created:
try:
request_counter += 1
Request.append(request_counter); Payloads.append(payloads_for_request)
if METHOD == 'POST': response = requests.post(dest_url, headers = headers, data = data_dict)
if METHOD == 'GET': response = requests.get(dest_url, headers = headers, params = data_dict)
content = BeautifulSoup(response.content, "lxml").text
Status_Code.append(response.status_code); Content.append(content); Content_Length.append(len(content))
except Exception as error:
print(f"\n{bad}%s%s Connection Refused%s (--verbose to check error)" % (bold, red, end))
if verbose: print(f'%sError Message%s:\n{error}' % (underline, end))
print(f'{tab}%sPayloads%s: {payloads_for_request}' % (underline, end))
Status_Code.append('None'); Content.append('[X] Error'); Content_Length.append('None')
pass
if delay: sleep(delay) # If sleep argument provided.
Table = tabulate({
'Request' : Request,
'Payloads' : Payloads,
'Status Code' : Status_Code,
'Content' : Content,
'Content Length': Content_Length,
}, headers = 'keys', tablefmt = 'psql', colalign = ('center', 'center'), disable_numparse = True)
print(f"\n%s %s%sFinished Pitchfork attack on%s: {url}" % (good, underline, bold, end))
print(Table)
return Table
def clusterbomb(dest_url: str, data_dict: dict) -> str:
"""Allows up to 20 payloads, 1 payload for each position marked. Tries all possible combinations of
payloads per position."""
# Creates a list of lists of payloads [[p1, p2, p3], [p21, p22, p23]...]
payloads_list_of_lists = []
for Set in payloads_sets:
with open(Set) as Set:
payloads = []
for line in Set.readlines(): payloads.append(line.strip())
payloads_list_of_lists.append(payloads)
# For table usage:
request_counter = 0
Request, Payloads, Status_Code, Content, Content_Length = [], [], [], [], []
# Creates data dict with current payloads:
combs_list = list(product(*payloads_list_of_lists))
for i in combs_list:
named_values = zip(data_dict.keys(), i)
payloads_for_request = dict(named_values)
# Start sending requests for each data dict created:
try:
request_counter += 1
Request.append(request_counter); Payloads.append(payloads_for_request)
if METHOD == 'POST': response = requests.post(dest_url, headers = headers, data = data_dict)
if METHOD == 'GET': response = requests.get(dest_url, headers = headers, params = data_dict)
content = BeautifulSoup(response.content, "lxml").text
Status_Code.append(response.status_code); Content.append(content); Content_Length.append(len(content))
except Exception as error:
print(f"\n{bad}%s%s Connection Refused%s (--verbose to check error)" % (bold, red, end))
if verbose: print(f'%sError Message%s:\n{error}' % (underline, end))
print(f'{tab}%sPayloads%s: {payloads_for_request}' % (underline, end))
Status_Code.append('None'); Content.append('[X] Error'); Content_Length.append('None')
pass
if delay: sleep(delay)
Table = tabulate({
'Request' : Request,
'Payloads' : Payloads,
'Status Code' : Status_Code,
'Content' : Content,
'Content Length': Content_Length,
}, headers = 'keys', tablefmt = 'psql', colalign = ('center', 'center'), disable_numparse = True)
print(f"%s %s%sFinished Cluster-Bomb attack on%s: {url}" % (good, underline, bold, end))
print(Table)
return Table
def output(table: str):
"""Saves table to output path."""
print(f"%s %s%sTable saved to file%s: {output_path}" % (good, underline, bold, end))
with open(output_path, 'w') as output_p:
output_p.write(table)
if __name__ == '__main__':
table = main_menu() # Gets the finalized table.
output(table) # Sends table to output func.