-
Notifications
You must be signed in to change notification settings - Fork 0
/
forbidden_bypass.py
599 lines (487 loc) · 25.1 KB
/
forbidden_bypass.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import requests
import argparse
import json
import ssl
import time
import http.client
from urllib.parse import urlparse, urlunparse, urljoin, quote
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# ANSI Colors
RESET = "\033[0m"
BLACK = "\033[30m"
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
MAGENTA = "\033[35m"
CYAN = "\033[36m"
WHITE = "\033[37m"
rate_limit_value = 10
def rate_limit(calls_per_second):
min_interval = 1.0 / calls_per_second
def decorator(func):
last_time = [0.0]
def wrapper(*args, **kwargs):
nonlocal last_time
elapsed_time = time.time() - last_time[0]
if elapsed_time < min_interval:
time.sleep(min_interval - elapsed_time)
result = func(*args, **kwargs)
last_time[0] = time.time()
return result
wrapper.__name__ = func.__name__
wrapper.__doc__ = func.__doc__
return wrapper
return decorator
def print_banner():
print("""
FORIDDEN_BYPASS @ S2kynet
""")
def is_json(data):
try:
json.loads(data)
return True
except (ValueError, TypeError):
return False
@rate_limit(rate_limit_value)
def perform_headers_bypass(url, args, headers_bypass, custom_headers=None, custom_data=None):
print(f"{YELLOW}[INFO] Trying to bypass with headers...{RESET}")
if args.method:
user_method = args.method.upper()
else:
user_method = "GET"
data = custom_data if custom_data is not None else {}
for header_line in headers_bypass:
header_parts = header_line.strip().split(": ")
if len(header_parts) == 2:
header_key, header_value = header_parts
headers = {}
if custom_headers is not None:
headers.update(custom_headers)
headers[header_key] = header_value
if "X-Original-URL" in headers or "X-Rewrite-URL" in headers:
parsed_url = requests.utils.urlparse(url)
url = requests.utils.urlunparse(parsed_url._replace(path="/"))
r = requests.request(user_method, url, headers=headers, data=data, verify=False, allow_redirects=False)
print(f"{YELLOW}[INFO] Changing path to '/' and setting header to the value of the original path...{RESET}")
else:
r = requests.request(user_method, url, headers=headers, data=data, verify=False, allow_redirects=False)
if user_method == "POST":
if data:
if is_json(data):
headers['Content-Type'] = 'application/json'
r = requests.post(url, verify=False, json=data, headers=headers, allow_redirects=False)
else:
headers['Content-Type'] = 'application/x-www-form-urlencoded'
r = requests.post(url, verify=False, data=data, headers=headers, allow_redirects=False)
else:
r = requests.post(url, verify=False, headers=headers, allow_redirects=False)
elif user_method == "PUT":
if data:
if is_json(data):
headers['Content-Type'] = 'application/json'
r = requests.put(url, verify=False, json=data, headers=headers, allow_redirects=False)
else:
headers['Content-Type'] = 'application/x-www-form-urlencoded'
r = requests.put(url, verify=False, data=data, headers=headers, allow_redirects=False)
else:
r = requests.put(url, verify=False, headers=headers, allow_redirects=False)
elif user_method == "PATCH":
if data:
if is_json(data):
headers['Content-Type'] = 'application/json'
r = requests.put(url, verify=False, json=data, headers=headers, allow_redirects=False)
else:
headers['Content-Type'] = 'application/x-www-form-urlencoded'
r = requests.put(url, verify=False, data=data, headers=headers, allow_redirects=False)
else:
r = requests.put(url, verify=False, headers=headers, allow_redirects=False)
elif user_method == "DELETE":
r = requests.delete(url, verify=False, data=data, headers=headers, allow_redirects=False)
else:
r = requests.get(url, verify=False, data=data, headers=headers, allow_redirects=False)
if args.proxy:
r = requests.request(user_method, url, proxies={"http": args.proxy, "https": args.proxy}, headers=headers, data=data, verify=False, allow_redirects=False)
status_code = r.status_code
if status_code == 200:
status_color = GREEN
elif status_code in (401,403):
status_color = RED
else:
status_color = RESET
print(f"{status_color}{user_method} {url} with header {header_key}: {status_code}{RESET}")
@rate_limit(rate_limit_value)
def perform_method_bypass(url, args, headers_bypass, method_bypass, custom_headers=None, custom_data=None):
print(f"\n{YELLOW}[INFO] Trying to bypass with HTTP methods...{RESET}")
for method in method_bypass:
if args.method:
user_method = args.method.upper()
else:
user_method = "GET"
if user_method == method:
continue
headers = {}
if custom_headers is not None:
headers.update(custom_headers)
data = custom_data if custom_data is not None else {}
if method == "POST":
if data:
if is_json(data):
headers['Content-Type'] = 'application/json'
r = requests.post(url, verify=False, json=data, headers=headers, allow_redirects=False)
else:
headers['Content-Type'] = 'application/x-www-form-urlencoded'
r = requests.post(url, verify=False, data=data, headers=headers, allow_redirects=False)
else:
r = requests.post(url, verify=False, headers=headers, allow_redirects=False)
elif method == "PUT":
if data:
if is_json(data):
headers['Content-Type'] = 'application/json'
r = requests.put(url, verify=False, json=data, headers=headers, allow_redirects=False)
else:
headers['Content-Type'] = 'application/x-www-form-urlencoded'
r = requests.put(url, verify=False, data=data, headers=headers, allow_redirects=False)
else:
r = requests.put(url, verify=False, headers=headers, allow_redirects=False)
elif method == "PATCH":
if data:
if is_json(data):
headers['Content-Type'] = 'application/json'
r = requests.put(url, verify=False, json=data, headers=headers, allow_redirects=False)
else:
headers['Content-Type'] = 'application/x-www-form-urlencoded'
r = requests.put(url, verify=False, data=data, headers=headers, allow_redirects=False)
else:
r = requests.put(url, verify=False, headers=headers, allow_redirects=False)
elif method == "DELETE":
r = requests.delete(url, verify=False, data=data, headers=headers, allow_redirects=False)
else:
r = requests.get(url, verify=False, data=data, headers=headers, allow_redirects=False)
if args.proxy:
r = requests.request(method, url, proxies={"http": args.proxy, "https": args.proxy}, headers=headers, data=data, verify=False, allow_redirects=False)
status_code = r.status_code
if status_code == 200:
status_color = GREEN
elif status_code in (401,403):
status_color = RED
else:
status_color = RESET
print(f"{status_color}{method} {url}: {status_code}{RESET}")
def generate_path_variants(path):
paths = [
path,
path.upper(),
path + "/",
path + "/.",
"//" + path + "//",
"." + path + "/..",
"/;" + path,
"/.;" + path,
"//;/" + path,
path.split('/')[-1] + ".json"
]
return paths
@rate_limit(rate_limit_value)
def perform_path_bypass(url, path, args, user_method, custom_headers=None, custom_data=None):
print(f"\n{YELLOW}[INFO] Trying to bypass with path fuzzing...{RESET}")
base_url = url
data = custom_data if custom_data is not None else {}
for path_variant in generate_path_variants(path):
path_variant = path_variant.lstrip('/')
parsed_url = urlparse(urljoin(base_url, path_variant))
parsed_url._replace(path=parsed_url.path.lstrip('/'))
request_url = urlunparse(parsed_url._replace(path=path_variant))
headers = {}
if custom_headers is not None:
headers.update(custom_headers)
if user_method == "POST":
if data:
if is_json(data):
headers['Content-Type'] = 'application/json'
r = requests.post(url, verify=False, json=data, headers=headers, allow_redirects=False)
else:
headers['Content-Type'] = 'application/x-www-form-urlencoded'
r = requests.post(url, verify=False, data=data, headers=headers, allow_redirects=False)
else:
r = requests.post(url, verify=False, headers=headers, allow_redirects=False)
elif user_method == "PUT":
if data:
if is_json(data):
headers['Content-Type'] = 'application/json'
r = requests.put(url, verify=False, json=data, headers=headers, allow_redirects=False)
else:
headers['Content-Type'] = 'application/x-www-form-urlencoded'
r = requests.put(url, verify=False, data=data, headers=headers, allow_redirects=False)
else:
r = requests.put(url, verify=False, headers=headers, allow_redirects=False)
elif user_method == "PATCH":
if data:
if is_json(data):
headers['Content-Type'] = 'application/json'
r = requests.put(url, verify=False, json=data, headers=headers, allow_redirects=False)
else:
headers['Content-Type'] = 'application/x-www-form-urlencoded'
r = requests.put(url, verify=False, data=data, headers=headers, allow_redirects=False)
elif user_method == "DELETE":
r = requests.delete(request_url, verify=False, headers=headers, data=data, allow_redirects=False)
else:
r = requests.get(request_url, verify=False, headers=headers, data=data, allow_redirects=False)
if args.proxy:
r = requests.request(user_method, request_url, headers=headers, data=data, proxies={"http": args.proxy, "https": args.proxy}, verify=False, allow_redirects=False)
status_code = r.status_code
if status_code == 200:
status_color = GREEN
elif status_code in (401,403):
status_color = RED
else:
status_color = RESET
print(f"{status_color}{user_method} {request_url}: {r.status_code}{RESET}")
@rate_limit(rate_limit_value)
def perform_unicode_bypass(url, path, user_method, args, custom_headers=None, custom_data=None):
print(f"\n{YELLOW}[INFO] Trying to bypass path with unicode fuzzing...{RESET}")
headers = {}
if custom_headers is not None:
headers.update(custom_headers)
base_url = url.rstrip('/')
data = custom_data if custom_data is not None else {}
with open('./wordlists/Unicode.txt', 'r') as file:
fuzz_strings = file.read().splitlines()
for fuzz_string in fuzz_strings:
variants = [
f"/{quote(fuzz_string)}{quote(path)}",
f"/{quote(path)}/{quote(fuzz_string)}",
f"/{quote(path)}{quote(fuzz_string)}"
]
for path_variant in variants:
path_variant = path_variant.lstrip('/')
request_url = urljoin(base_url, path_variant)
if user_method == "POST":
if data:
if is_json(data):
headers['Content-Type'] = 'application/json'
r = requests.post(url, verify=False, json=data, headers=headers, allow_redirects=False)
else:
headers['Content-Type'] = 'application/x-www-form-urlencoded'
r = requests.post(url, verify=False, data=data, headers=headers, allow_redirects=False)
else:
r = requests.post(url, verify=False, headers=headers, allow_redirects=False)
elif user_method == "PUT":
if data:
if is_json(data):
headers['Content-Type'] = 'application/json'
r = requests.put(url, verify=False, json=data, headers=headers, allow_redirects=False)
else:
headers['Content-Type'] = 'application/x-www-form-urlencoded'
r = requests.put(url, verify=False, data=data, headers=headers, allow_redirects=False)
else:
r = requests.put(url, verify=False, headers=headers, allow_redirects=False)
elif user_method == "PATCH":
if data:
if is_json(data):
headers['Content-Type'] = 'application/json'
r = requests.put(url, verify=False, json=data, headers=headers, allow_redirects=False)
else:
headers['Content-Type'] = 'application/x-www-form-urlencoded'
r = requests.put(url, verify=False, data=data, headers=headers, allow_redirects=False)
elif user_method == "DELETE":
r = requests.delete(request_url, verify=False, headers=headers, data=data, allow_redirects=False)
else:
r = requests.get(request_url, verify=False, headers=headers, data=data, allow_redirects=False)
if args.proxy:
r = requests.request(user_method, request_url, headers=headers, data=data, proxies={"http": args.proxy, "https": args.proxy}, verify=False, allow_redirects=False)
status_code = r.status_code
if status_code == 200:
status_color = GREEN
elif status_code in (401,403):
status_color = RED
else:
status_color = RESET
print(f"{status_color}{user_method} {request_url}: {status_code}{RESET}")
@rate_limit(rate_limit_value)
def perform_user_agent_bypass(url, args, user_method, custom_headers=None, custom_data=None):
print(f"{YELLOW}\n[INFO] Trying to bypass with User-Agent fuzzing...{RESET}")
headers = {}
if custom_headers is not None:
headers.update(custom_headers)
data = custom_data if custom_data is not None else {}
with open('./wordlists/UserAgents.fuzz.txt', 'r') as file:
user_agents = file.read().splitlines()
for user_agent in user_agents:
headers["User-Agent"] = user_agent
r = requests.get(url, headers=headers, verify=False, allow_redirects=False)
if user_method == "POST":
if data:
if is_json(data):
headers['Content-Type'] = 'application/json'
r = requests.post(url, verify=False, json=data, headers=headers, allow_redirects=False)
else:
headers['Content-Type'] = 'application/x-www-form-urlencoded'
r = requests.post(url, verify=False, data=data, headers=headers, allow_redirects=False)
else:
r = requests.post(url, verify=False, headers=headers, allow_redirects=False)
elif user_method == "PUT":
if data:
if is_json(data):
headers['Content-Type'] = 'application/json'
r = requests.put(url, verify=False, json=data, headers=headers, allow_redirects=False)
else:
headers['Content-Type'] = 'application/x-www-form-urlencoded'
r = requests.put(url, verify=False, data=data, headers=headers, allow_redirects=False)
else:
r = requests.put(url, verify=False, headers=headers, allow_redirects=False)
elif user_method == "PATCH":
if data:
if is_json(data):
headers['Content-Type'] = 'application/json'
r = requests.put(url, verify=False, json=data, headers=headers, allow_redirects=False)
else:
headers['Content-Type'] = 'application/x-www-form-urlencoded'
r = requests.put(url, verify=False, data=data, headers=headers, allow_redirects=False)
elif user_method == "DELETE":
r = requests.delete(url, verify=False, headers=headers, data=data, allow_redirects=False)
else:
r = requests.get(url, verify=False, headers=headers, data=data, allow_redirects=False)
if args.proxy:
r = requests.request(user_method, url, headers=headers, data=data, proxies={"http": args.proxy, "https": args.proxy}, verify=False, allow_redirects=False)
status_code = r.status_code
if status_code == 200:
status_color = GREEN
elif status_code in (401,403):
status_color = RED
else:
status_color = RESET
print(f"{status_color}{user_method} User-Agent: {user_agent} - Status Code: {r.status_code}{RESET}")
@rate_limit(rate_limit_value)
def perform_protocol_bypass(url, user_method, args, custom_headers=None, custom_data=None):
print(f"\n{YELLOW}[INFO] Trying to bypass with HTTP protocols...{RESET}")
base_url = url
data = custom_data if custom_data is not None else {}
http_versions = ["HTTP/1.0", "HTTP/1.1", "HTTP/2"]
for version in http_versions:
if args.method:
user_method = args.method.upper()
if not url.startswith("http"):
url = "http://" + url
parsed_url = urlparse(url)
host = parsed_url.netloc
path = parsed_url.path
headers = {"Host": host}
if custom_headers:
headers.update(custom_headers)
try:
if args.proxy:
proxy_url = urlparse(args.proxy)
conn = http.client.HTTPSConnection(proxy_url.netloc, context=ssl._create_unverified_context())
conn._http_vsn_str = version
conn._http_vsn = int(version[5])
request_line = f"{user_method} {url}"
conn.set_tunnel(host, headers=headers)
else:
conn = http.client.HTTPConnection(host)
conn._http_vsn_str = version
conn._http_vsn = int(version[5])
request_line = f"{user_method} {path}"
if user_method in ["POST", "PATCH", "PUT"] and data:
if isinstance(data, dict):
headers['Content-Type'] = 'application/json'
data_bytes = json.dumps(data).encode('utf-8')
else:
try:
json.loads(data)
headers['Content-Type'] = 'application/json'
data_bytes = data.encode('utf-8')
except ValueError:
headers['Content-Type'] = 'application/x-www-form-urlencoded'
data_bytes = data.encode('utf-8')
conn.request(user_method, path, body=data_bytes, headers=headers)
else:
conn.request(user_method, path, headers=headers)
response = conn.getresponse()
conn.close()
if response.status == 200:
status_color = GREEN
elif response.status in (401, 403):
status_color = RED
else:
status_color = RESET
print(f"{status_color}{version} {user_method} {url}: {response.status}{RESET}")
except http.client.BadStatusLine as e:
print(f"{RED}[ERROR] Bad Status Line: {e}{RESET}")
continue
def validate_url(url):
if not url.startswith("http://") and not url.startswith("https://"):
url = "http://" + url
try:
parsed_url = urlparse(url)
return parsed_url
except ValueError:
raise ValueError("Invalid URL format. URL must be of the form 'http://example.com' or 'https://example.com'.")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--url", help="Full path to be used", required=True, nargs=1)
parser.add_argument("-m", "--method", help="Method to be used. Default is GET")
parser.add_argument("-H", "--header", action="append", help="Add a custom header")
parser.add_argument("-d", "--data", help="Add data to requset body. JSON is supported with escaping")
parser.add_argument("-p", "--proxy", help="Use Proxy")
parser.add_argument("--rate-limit", type=int, default=10, help="Rate limit (calls per second)")
parser.add_argument("--include-unicode", action="store_true", help="Include Unicode fuzzing (stressful)")
parser.add_argument("--include-user-agent", action="store_true", help="Include User-Agent fuzzing (stressful)")
args = parser.parse_args()
global rate_limit_value
rate_limit_value = args.rate_limit
custom_headers = None
custom_data = None
if args.header is not None:
custom_headers = {}
for header in args.header:
key_value = header.split(':')
if len(key_value) == 2:
key, value = key_value
custom_headers[key.strip()] = value.strip()
else:
print(f"\n{YELLOW}[WARNING] Invalid header format: {header}. Skipping...{RESET}\n")
if args.data is not None:
custom_data = args.data
try:
with open('./wordlists/headers_bypass.txt') as f:
headers_bypass = f.readlines()
parsed_url = urlparse(args.url[0])
path = parsed_url.path
headers_bypass.append(f"X-Original-URL: {path}")
headers_bypass.append(f"X-Rewrite-URL: {path}")
method_bypass = ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "CONNECT", "TRACE", "OPTIONS", "INVENTED", "HACK"]
if args.url:
url = args.url[0]
if not url.startswith('http://') and not url.startswith('https://'):
url = 'http://' + url # Add "http://" as the default scheme if missing
validate_result = validate_url(url)
user_method = args.method if args.method else "GET"
path = urlparse(url).path
perform_headers_bypass(url, args, headers_bypass, custom_headers, custom_data)
perform_method_bypass(url, args, headers_bypass, method_bypass, custom_headers, custom_data)
perform_path_bypass(url, path, args, user_method, custom_headers, custom_data)
perform_protocol_bypass(url, user_method, args, custom_headers, custom_data)
if args.include_unicode:
perform_unicode_bypass(url, path, user_method, args, custom_headers, custom_data)
if args.include_user_agent:
perform_user_agent_bypass(url, args, user_method, custom_headers, custom_data)
print(f"{GREEN}\n[+] Done. you may review the results{RESET}")
except KeyboardInterrupt:
print(f"\n{YELLOW}[WARNING] Stopping...{RESET}")
except ConnectionRefusedError:
print(f"\n{RED}[ERROR] Connection refused{RESET}")
except ConnectionError:
print(f"\n{RED}[ERROR] Connection Error detected{RESET}")
except requests.exceptions.SSLError as ssl_error:
print(f"\n{RED}[ERROR] SSL Error: \n{ssl_error}{RESET}")
except ValueError:
print(f"\n{YELLOW}[WARNING] Please include a scheme (http:// or https://) inside the provided URL{RESET}")
except requests.exceptions.ConnectionError as e:
print(f"\n{RED}[ERROR] Connection Error: \n{e}{RESET}")
except requests.exceptions.RequestException as e:
print(f"\n{RED}[ERROR] Request Error: \n{e}{RESET}")
if __name__ == '__main__':
print_banner()
main()