-
Notifications
You must be signed in to change notification settings - Fork 19
/
apache_redirector_setup.py
725 lines (640 loc) · 30.9 KB
/
apache_redirector_setup.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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
#!/bin/python
import sys,os,argparse,subprocess,re #native
class MyParser(argparse.ArgumentParser):
def error(self, message):
self.print_help()
sys.exit(2)
def print_help(self, file=None):
help1 = '''Apache Redirector Setup Script -
Configures a host to perform mod_rewrite redirection based on provided parameters
Parameters:
--h Display's this menu
--silent Prevent all standard output
--block_url URL or file to redirect/proxy blocked requests to
--block_mode proxy or redirect blocked requests (it's recommended you only proxy to resources you control)\
--allow_url URL or file to redirect/proxy allowed requests to
--allow_mode proxy or redirect allowed requests (it's recommended you only proxy to resources you control)
--ip_blacklist block provided IPs or CIDR ranges. Semicolon separate (ie 1.1.1.1;2.2.2.2/8)
--ir block common IR user agents (wget;curl;HTTrack;crawl;google;bot;b\-o\-t;spider;baidu;python)
--block_ua block provided user agents
--mobile_url URL to redirect mobile users to
--mobile_mode proxy or redirect mobile users to URL
--valid_uris Semicolon-seperated list of valid URIs, all other requests are blocked
--malleable Cobalt Strike Malleable C2 profile to allow. Blocks all other requests.
--staging_uri Uri of the stager being used in cobalt strike
--backup Backs up current apache2 config and the html folder with the .bak extension.
Order of processing
1: Malleable C2
or
1: IR Blacklisting
2: IP Blacklisting
3: UA Blacklisting
4: URI Blacklisting
5: Mobile Proxy/Redirect
6: Allow Clause
Usage Examples:
Setting up a Malleable C2 Redirector:
python apache_redirector_setup.py --malleable="<Path to C2 Profile>" --block_url="https://google.com" --block_mode="redirect" --allow_url="Teamserver Address" --allow_mode="proxy"
Setting up a Malleable C2 Redirector w/ Staging:
python apache_redirector_setup.py --malleable="<Path to C2 Profile" --staging_uri="/updates/" --block_url="https://google.com" --block_mode="redirect" --allow_url="<Teamserver Address>" --allow_mode="proxy"
Setting up Mobile user redirection:
python apache_redirector_setup.py --mobile_url="<Mobile Payload>" --mobile_mode=proxy --allow_url="<Teamserver Address>" --allow_mode="proxy"
Setting up IR Blacklisting:
python apache_redirector_setup.py --ir --block_mode=redirect --block_url="https://google.com" --allow_url="<Teamserver Address>" --allow_mode="proxy"
Setting up IP Blacklisting:
python apache_redirector_setup.py --ip_blacklist="<IP ADDRESS>" --block_mode="redirect" --block_url="https://google.com" --allow_url="<Teamserver Address>" --allow_mode="proxy"
Setting up Mobile Redirection, IR Blacklisting, and IP Blacklisting:
python apache_redirector_setup.py --ip_blacklist="<IP ADDRESS>" --ip_blacklist="<IP ADDRESS>" --mobile_url="<Mobile Payload>" --mobile_mode=proxy --allow_url="<Teamserver Address>" --allow_mode="proxy"
'''
print help1
exit(-1)
redirection_options = {}
green = '\x1b[6;30;42m'
red = '\x1b[2;30;41m'
yellow = '\x1b[0;30;43m'
colorEnd = '\x1b[0m'
#default agent strings
mobile_string = 'android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos'
ir_string = 'wget;curl;HTTrack;crawl;google;bot;b\-o\-t;spider;baidu;python'
def htaccessCheck(silent,server_root):
if os.path.isfile((server_root + ".htaccess")):
if silent:
print red + "An .htaccess file was found in the "+ server_root +" webroot! This file will be overwritten with a new ruleset with this tool if you continue!" + colorEnd
prompt = raw_input(yellow + "Would you like to backup this file before continuing?(Y/N):" + colorEnd + " ")
if (prompt.lower() == ("y")) or (prompt.lower() == ("yes")):
backupConfig(silent,server_root)
elif (prompt.lower() == ("n")) or (prompt.lower() == ("no")):
print yellow + ".htaccess file removed without saving a backup" + colorEnd
else:
print red + "You didn't enter Y or N, so I don't know what you want! I am going to exit now feel free to try again" + colorEnd
sys.exit()
os.remove(server_root + ".htaccess")
def backupFile(filename):
f = open(filename, 'r')
lines = []
for line in f:
lines.append(line)
f.close()
out = filename + ".bak"
o = open(out, 'w')
for line in lines:
o.write(line)
o.close()
def checkSetup(silent,server_root):
if server_root=="/var/www/html/":
server_root="/var/www/"
try:
if silent:
print green + "Checking if Apache Server is Configured Correctly" + colorEnd
setup = False
config = open("/etc/apache2/apache2.conf", "r")
apache2config = config.readlines()
config.close()
configLineNumber = 0
configEditLineNumber = 0
directory = "<Directory %s>" % server_root
for lines in apache2config:
if directory in lines:
configEditLineNumber = configLineNumber
configLineNumber += 1
if "AllowOverride All" in apache2config[configEditLineNumber + 2]:
setup = True
if silent:
print green + "Mod_rewrite is enabled for the "+ server_root +" webroot" + colorEnd
print green + "Apache Server Configured Correctly!" + colorEnd +"\n"
return setup
else:
if silent:
print yellow + "Mod_rewrite is not enabled for the "+ server_root +" webroot" + colorEnd
print yellow + "Configuring the "+ server_root +" for Apache Mod-Rewrite!"
setup = False
return setup
except IOError:
if silent:
print yellow + "Apache2 is not installed, Installing Apache2 and configuring Mod-Rewrite Now" + colorEnd
setup = False
return setup
def backupConfig(silent,server_root):
if os.path.isfile("/etc/apache2/apache2.conf"):
backupFile("/etc/apache2/apache2.conf")
if silent:
print green + "Apache2 Configuration backed up" + colorEnd
if os.path.isfile((server_root + ".htaccess")):
backupFile((server_root + ".htaccess"))
if silent:
print green + ".htaccess file located at " + (server_root + ".htaccess") + " has been saved at " + (server_root + "html/.htaccess.bak") + colorEnd
def firstTimeSetup(silent,server_root):
if server_root == "/var/www/html/":
server_root="/var/www/"
if silent:
subprocess.call(["apt-get","install","apache2","-y","-qq"])
subprocess.call(["a2enmod", "rewrite", "proxy", "proxy_http"])
else:
FNULL = open(os.devnull, 'w')
subprocess.call(["apt-get","install","apache2","-y","-qq"],stdout=FNULL, stderr=subprocess.STDOUT)
subprocess.call(["a2enmod", "rewrite", "proxy", "proxy_http"],stdout=FNULL, stderr=subprocess.STDOUT)
apache2config = open("/etc/apache2/apache2.conf", "r")
configLineNumber = 0
configEditLineNumber = 0
oldconfig = apache2config.readlines()
directory = "<Directory %s>" % server_root
for lines in oldconfig:
if directory in lines:
configEditLineNumber = configLineNumber
configLineNumber += 1
oldconfig[configEditLineNumber + 2] = "\tAllowOverride All\n"
apache2config.close()
apache2config = open("/etc/apache2/apache2.conf","w")
for line in oldconfig:
apache2config.write(line)
apache2config.close()
if silent:
print yellow + "Restarting Apache to Enable New Configuration" + colorEnd
subprocess.call(["service","apache2","restart"])
if silent:
subprocess.call(["service","apache2","status"])
if silent:
print green + "Configuration Complete!" + colorEnd
def mobile_rule(mobile_URL,mobile_mode,server_root):
if os.path.isfile((server_root + ".htaccess")):
old = open((server_root + ".htaccess"),"r")
oldRules = []
for line in old:
oldRules.append(line)
old.close()
rule = 'RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos" [NC]\n'
if mobile_mode == "redirect":
rule = rule + 'RewriteRule ^.*$ %s [L,R=302]\n' % mobile_URL
elif mobile_mode == "proxy":
rule = rule + 'RewriteRule ^.*$ %s [P]\n' % mobile_URL
ruleFile = open((server_root + ".htaccess"), "w")
for oldRule in oldRules:
ruleFile.write(oldRule)
ruleFile.write(rule)
ruleFile.close()
else:
rule = "RewriteEngine On\n"
rule = rule + 'RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos" [NC]\n'
rule = rule + 'RewriteRule ^.*$ %s [P]\n' % mobile_URL
ruleFile = open((server_root + ".htaccess"), "w")
ruleFile.write(rule)
ruleFile.close()
def irSetup(block_url,block_mode,server_root):
if os.path.isfile((server_root + ".htaccess")):
old = open((server_root + ".htaccess"),"r")
oldRules = []
for line in old:
if "RewriteEngine On" not in line:
oldRules.append(line)
old.close()
rule = "RewriteEngine On\n"
rule = rule + 'RewriteCond %{HTTP_USER_AGENT} "wget|curl|HTTrack|crawl|google|bot|b\-o\-t|spider|baidu" [NC,OR]\n'
rule = rule + 'RewriteCond %{HTTP_USER_AGENT} =""\n'
if block_mode == "redirect":
rule = rule + 'RewriteRule ^.*$ ' + block_url + '/? [L,R=302]\n'
elif block_mode == "proxy":
rule = rule + 'RewriteRule ^.*$ ' + block_url + '/? [P]\n'
ruleFile = open((server_root + ".htaccess"), "w")
ruleFile.write(rule)
for oldRule in oldRules:
ruleFile.write(oldRule)
ruleFile.close()
else:
rule = "RewriteEngine On\n"
rule = rule + 'RewriteCond %{HTTP_USER_AGENT} "wget|curl|HTTrack|crawl|google|bot|b\-o\-t|spider|baidu" [NC,OR]\n'
rule = rule + 'RewriteCond %{HTTP_USER_AGENT} =""\n'
if block_mode == "redirect":
rule = rule + 'RewriteRule ^.*$ ' + block_url + '/? [L,R=302]\n'
elif block_mode == "proxy":
rule = rule + 'RewriteRule ^.*$ ' + block_url + '/? [P]\n'
ruleFile = open((server_root + ".htaccess"), "w")
ruleFile.write(rule)
ruleFile.close()
def ipBlacklisting(ips,block_url,block_mode,server_root):
if os.path.isfile((server_root + ".htaccess")):
old = open((server_root + ".htaccess"),"r")
oldRules = []
counter = 0
for line in old:
oldRules.append(line)
old.close()
for ip in ips:
if ip == ips[-1]:
finalIP= ''
for char in ip:
if char == ".":
finalIP += "\\."
else:
finalIP += char
rule = 'RewriteCond %{REMOTE_ADDR} ^' + finalIP + "\n"
else:
finalIP= ''
for char in ip:
if char == ".":
finalIP += "\\."
else:
finalIP += char
rule = 'RewriteCond %{REMOTE_ADDR} ^' + finalIP + ' [OR]\n'
if block_mode == "redirect":
rule = rule + 'RewriteRule ^.*$ ' + block_url + '/? [L,R=302]\n'
elif block_mode == "proxy":
rule = rule + 'RewriteRule ^.*$ ' + block_url + '/? [P]\n'
ruleFile = open((server_root + ".htaccess"), "w")
for oldRule in oldRules:
ruleFile.write(oldRule)
ruleFile.write(rule)
ruleFile.close()
else:
rule = "RewriteEngine On\n"
for ip in ips:
if ip == ips[-1]:
finalIP= ''
for char in ip:
if char == ".":
finalIP += "\."
else:
finalIP += char
rule = rule + 'RewriteCond %{REMOTE_ADDR} ^' + finalIP + "\n"
else:
finalIP= ''
for char in ip:
if char == ".":
finalIP += "\."
else:
finalIP += char
rule = rule + 'RewriteCond %{REMOTE_ADDR} ^' + finalIP + ' [OR]\n'
if block_mode == "redirect":
rule = rule + 'RewriteRule ^.*$ ' + block_url + '/? [L,R=302]\n'
elif block_mode == "proxy":
rule = rule + 'RewriteRule ^.*$ ' + block_url + '/? [P]\n'
ruleFile = open("/var/www/html/.htaccess", "w")
ruleFile.write(rule)
ruleFile.close()
def validURI(uris, block_url, block_mode,server_root):
if os.path.isfile((server_root + ".htaccess")):
old = open((server_root + ".htaccess"),"r")
oldRules = []
finalURI = ''
for line in old:
oldRules.append(line)
old.close()
if len(uris) == 1:
finalURI = uris[0]
rule = 'RewriteCond %{REQUEST_URI} !^' + finalURI + '?$ [NC]\n'
else:
for uri in uris:
if uri == uris[len(uris)-1]:
finalURI += uri
else:
finalURI += uri + "|"
rule = 'RewriteCond %{REQUEST_URI} !^/(' + finalURI + ')/?$ [NC]\n'
if block_mode == "redirect":
rule = rule + 'RewriteRule ^.*$ ' + block_url + '/? [L,R=302]\n'
elif block_mode == "proxy":
rule = rule + 'RewriteRule ^.*$ ' + block_url + '/? [P]\n'
else:
pass
ruleFile = open((server_root + ".htaccess"), "w")
for oldRule in oldRules:
ruleFile.write(oldRule)
ruleFile.write(rule)
ruleFile.close()
else:
finalURI = ''
if len(uris) == 1:
finalURI = uris[0]
else:
for uri in uris:
if uri == uris[len(uris)-1]:
finalURI += uri
else:
finalURI += uri + "|"
rule = "RewriteEngine On\n"
rule += 'RewriteCond %{REQUEST_URI} !^/(' + finalURI + ')/?$ [NC]'
if block_mode == "redirect":
rule = rule + 'RewriteRule ^.*$ ' + block_url + '/? [L,R=302]\n'
elif block_mode == "proxy":
rule = rule + 'RewriteRule ^.*$ ' + block_url + '/? [P]\n'
else:
pass
ruleFile = open((server_root + ".htaccess"), "w")
ruleFile.write(rule)
ruleFile.close()
def invalidURI(uris, block_url, block_mode,server_root):
if os.path.isfile((server_root + ".htaccess")):
old = open((server_root + ".htaccess"),"r")
oldRules = []
finalURI = ''
for line in old:
oldRules.append(line)
old.close()
if len(uris) == 1:
finalURI = uris[0]
rule = 'RewriteCond %{REQUEST_URI} ^' + finalURI + '?$ [NC]\n'
else:
for uri in uris:
if uri == uris[len(uris)-1]:
finalURI += uri
else:
finalURI += uri + "|"
rule = 'RewriteCond %{REQUEST_URI} ^/(' + finalURI + ')/?$ [NC]\n'
if block_mode == "redirect":
rule = rule + 'RewriteRule ^.*$ ' + block_url + '/? [L,R=302]\n'
elif block_mode == "proxy":
rule = rule + 'RewriteRule ^.*$ ' + block_url + '/? [P]\n'
else:
pass
ruleFile = open((server_root + ".htaccess"), "w")
for oldRule in oldRules:
ruleFile.write(oldRule)
ruleFile.write(rule)
ruleFile.close()
else:
finalURI = ''
if len(uris) == 1:
finalURI = uris[0]
rule = 'RewriteEngine On\n'
rule += 'RewriteCond %{REQUEST_URI} ^' + finalURI + '?$ [NC]\n'
else:
for uri in uris:
if uri == uris[len(uris)-1]:
finalURI += uri
else:
finalURI += uri + "|"
rule = "RewriteEngine On\n"
rule += 'RewriteCond %{REQUEST_URI} ^/(' + finalURI + ')/?$ [NC]'
if block_mode == "redirect":
rule = rule + 'RewriteRule ^.*$ ' + block_url + '/? [L,R=302]\n'
elif block_mode == "proxy":
rule = rule + 'RewriteRule ^.*$ ' + block_url + '/? [P]\n'
else:
pass
ruleFile = open((server_root + ".htaccess"), "w")
ruleFile.write(rule)
ruleFile.close()
def blockUA(ua, block_url,block_mode,server_root):
if os.path.isfile((server_root + ".htaccess")):
old = open((server_root + ".htaccess"),"r")
oldRules = []
for line in old:
oldRules.append(line)
old.close()
finalUA = re.escape(ua)
rule = 'RewriteCond %{HTTP_USER_AGENT} ^' + finalUA + ' [NC]\n'
if block_mode == "redirect":
rule += 'RewriteRule ^.*$ ' + block_url + '/? [L,R=302]\n'
elif block_mode == "proxy":
rule += 'RewriteRule ^.*$ ' + block_url + '/? [P]\n'
else:
pass
ruleFile = open((server_root + ".htaccess"), "w")
for oldRule in oldRules:
ruleFile.write(oldRule)
ruleFile.write(rule)
ruleFile.close()
else:
finalUA = re.escape(ua)
rule = "RewriteEngine On\n"
rule += 'RewriteCond %{HTTP_USER_AGENT} ^' + finalUA + ' [NC]\n'
if block_mode == "redirect":
rule += 'RewriteRule ^.*$ ' + block_url + '/? [L,R=302]\n'
elif block_mode == "proxy":
rule += 'RewriteRule ^.*$ ' + block_url + '/? [P]\n'
else:
pass
ruleFile = open((server_root + ".htaccess"), "w")
ruleFile.write(rule)
ruleFile.close()
def allowUA(ua, block_url,block_mode,server_root):
if os.path.isfile((server_root + ".htaccess")):
old = open((server_root + ".htaccess"),"r")
oldRules = []
for line in old:
oldRules.append(line)
old.close()
if ua == " ":
rule = 'RewriteCond %{HTTP_USER_AGENT} ^$ [NC]\n'
else:
finalUA = re.escape(ua)
rule = 'RewriteCond %{HTTP_USER_AGENT} !^' + finalUA + ' [NC]\n'
if block_mode == "redirect":
rule += 'RewriteRule ^.*$ ' + block_url + '/? [L,R=302]\n'
elif block_mode == "proxy":
rule += 'RewriteRule ^.*$ ' + block_url + '/? [P]\n'
else:
pass
ruleFile = open((server_root + ".htaccess"), "w")
for oldRule in oldRules:
ruleFile.write(oldRule)
ruleFile.write(rule)
ruleFile.close()
else:
finalUA = re.escape(ua)
rule = "RewriteEngine On\n"
rule += 'RewriteCond %{HTTP_USER_AGENT} !^' + finalUA + ' [NC]\n'
if block_mode == "redirect":
rule += 'RewriteRule ^.*$ ' + block_url + '/? [L,R=302]\n'
elif block_mode == "proxy":
rule += 'RewriteRule ^.*$ ' + block_url + '/? [P]\n'
else:
pass
ruleFile = open((server_root + ".htaccess"), "w")
ruleFile.write(rule)
ruleFile.close()
def malleableC2(profile, block_url, block_mode,server_root):
c2profile = open(profile,'r')
uris = []
for line in c2profile:
if "set uri" in line:
uri = line[line.index('"')+1:line.index(';')-2]
uris.append(uri)
if "set useragent" in line:
userAgent = line[line.index('"') + 1:line.index('";')]
c2profile.close()
allowUA(userAgent,block_url,block_mode,server_root)
validURI(uris,block_url,block_mode,server_root)
def Staging(profile, block_url, block_mode, allow_url, allow_mode, server_root, stagingURI="/updates/"):
c2profile = open(profile,'r')
uris = []
for line in c2profile:
if "set uri" in line:
uri = line[line.index('"')+1:line.index(';')-2]
uris.append(uri)
if "set useragent" in line:
userAgent = line[line.index('"') + 1:line.index('";')]
c2profile.close()
stagingURIS = [stagingURI]
stagingURIS2 = ["/.../"]
invalidURI(stagingURIS," "," ",server_root)
allowUA(" "," "," ",server_root)
allowClause(allow_url,allow_mode,server_root)
invalidURI(stagingURIS2," "," ",server_root)
blockUA(userAgent," "," ",server_root)
allowClause(allow_url,allow_mode,server_root)
validURI(uris," "," ",server_root)
allowUA(userAgent,block_url,block_mode,server_root)
def allowClause(allow_url,allow_mode,server_root):
if os.path.isfile((server_root + ".htaccess")):
old = open((server_root + ".htaccess"),"r")
oldRules = []
for line in old:
oldRules.append(line)
old.close()
if allow_mode == "redirect":
rule = 'RewriteRule ^.*$ ' + allow_url + '%{REQUEST_URI} [L,R=302]\n'
elif allow_mode == "proxy":
rule = 'RewriteRule ^.*$ ' + allow_url + '%{REQUEST_URI} [P]\n'
ruleFile = open((server_root + ".htaccess"), "w")
for oldRule in oldRules:
ruleFile.write(oldRule)
ruleFile.write(rule)
ruleFile.close()
else:
print "No rules to allow"
def readRules(silent,server_root):
if silent:
print (green + ("Here is a print out of the rules written to " + (server_root + ".htaccess") + colorEnd))
rules = open((server_root + ".htaccess"),"r")
for rule in rules:
print rule.strip("\n")
print "\n\n"
def processing(redirection_options):
htaccessCheck(redirection_options['silent'],redirection_options['server_root'])
setup = checkSetup(redirection_options['silent'],redirection_options['server_root'])
if setup == False:
firstTimeSetup(redirection_options['silent'],redirection_options['server_root'])
if redirection_options['malleable'] != None:
if (redirection_options['block_url'] != None) and (redirection_options['block_mode'] != None) and (redirection_options['staging_uri'] == None):
malleableC2(redirection_options['malleable'],redirection_options['block_url'],redirection_options['block_mode'],redirection_options['server_root'])
elif redirection_options['staging_uri'] != None:
if (redirection_options['block_url'] != None) and (redirection_options['block_mode'] != None) and (redirection_options['allow_url'] != None) and (redirection_options['allow_mode'] != None):
Staging(redirection_options['malleable'],redirection_options['block_url'],redirection_options['block_mode'], redirection_options['allow_url'],redirection_options['allow_mode'],redirection_options['server_root'], redirection_options['staging_uri'])
else:
print red + "In order to setup malleable C2 with staging support use the following flags, --malleable --staging_uri --block_url --block_mode --allow_url, --allow_mode" + colorEnd
else:
print red + "In order to use malleable C2, --malleable --block_url and --block_mode must be used" + colorEnd
if redirection_options['ir'] != None:
if (redirection_options['block_url'] != None) and (redirection_options['block_mode'] != None):
irSetup(redirection_options['block_url'],redirection_options['block_mode'],redirection_options['server_root'])
else:
print red + "In order to set IR Blocking, --block_url and --block_mode must be used" + colorEnd
if redirection_options['ip_blacklist'] != None:
if (redirection_options['block_url'] != None) and (redirection_options['block_mode'] != None):
ipBlacklisting(redirection_options['ip_blacklist'],redirection_options['block_url'],redirection_options['block_mode'],redirection_options['server_root'])
else:
print red + "In order to set IP Blocking, --block_url, --block_mode, and --ip_blacklist must be used" + colorEnd
if redirection_options['block_ua']!= None:
if (redirection_options['block_mode'] != None) & (redirection_options['block_url'] != None):
blockUA(redirection_options['block_ua'],redirection_options['block_url'],redirection_options['block_mode'],redirection_options['server_root'])
else:
print red + "In order to block a specific useragent use, --block_ua, --block_mode and --block_url must be used" + colorEnd
if redirection_options['valid_uris']!= None:
if (redirection_options['block_url'] != None) & (redirection_options['block_mode'] != None):
validURI(redirection_options['valid_uris'],redirection_options['block_url'],redirection_options['block_mode'],redirection_options['server_root'])
else:
print red + "In order to set URI whitelisting use, --valid_uris, --block_url and --block_mode must be used" + colorEnd
if redirection_options['mobile_url']!= None:
if (redirection_options['mobile_mode'] != None):
mobile_rule(redirection_options['mobile_url'],redirection_options['mobile_mode'],redirection_options['server_root'])
else:
print red + "In order to set mobile redirection use, --mobile_url, and --mobile_mode must be used" + colorEnd
if redirection_options['allow_url']!= None:
if redirection_options['allow_mode'] !=None:
allowClause(redirection_options['allow_url'],redirection_options['allow_mode'],redirection_options['server_root'])
else:
print red + "In order to set an allow rule use, --allow_url, and --allow_mode" + colorEnd
if os.path.isfile((redirection_options['server_root'] + ".htaccess")):
readRules(redirection_options['silent'],redirection_options['server_root'])
if __name__ == '__main__':
if not os.geteuid() == 0:
sys.exit('Script must be run as root')
parser = MyParser(description='''apache_redirector_setup.py''')
parser.add_argument('--silent', help='Prevent all standard output',action='store_false')
parser.add_argument('--teamserver', help='The IP Address of your teamserver')
parser.add_argument('--block_url', help='URL or file to redirect/proxy blocked requests to')
parser.add_argument('--block_mode', help='PROXY or REDIRECT blocked requests (it is recommended you only proxy to resources you control)')
parser.add_argument('--allow_url', help='URL or file to redirect/proxy allowed requests to')
parser.add_argument('--allow_mode', help='PROXY or REDIRECT allowed requests (it is recommended you only proxy to resources you control)')
parser.add_argument('--ip_blacklist', help='block provided IPs or regex ranges. Semicolon separate and escape periods (ie 1.1.1.1;2\.1\..*)')
parser.add_argument('--ir', help='block common IR user agents (wget;curl;HTTrack;crawl;google;bot;b\-o\-t;spider;baidu)',action='store_true')
parser.add_argument('--block_ua', help='block provided user agents. supports regex. spaces and special characters [,.\ ()[]$%!^*] must be escaped')
parser.add_argument('--mobile_url', help='URL or file to redirect mobile users to')
parser.add_argument('--mobile_mode', help='proxy or redirect mobile users to URL')
parser.add_argument('--valid_uris', help='Semicolon-seperated list of valid URIs, all other requests are blocked')
parser.add_argument('--server_root', help='path to the server root (default: /var/www/')
parser.add_argument('--invalid_uri', help='URL or file to redirect 404 requests to (default: block_url value)')
parser.add_argument('--malleable', help='path to the Cobalt Strike Malleable C2 profile to allow.')
parser.add_argument('--staging_uri', help='Uri of the stager being used in cobalt strike')
parser.add_argument('--backup', help='Backs up current apache2 config and the html folder with the .bak extension.',action='store_true')
args = parser.parse_args()
if args.silent != None:
redirection_options['silent'] = args.silent
else:
redirection_options['silent'] = False
if args.block_mode != None:
if args.block_mode.lower() in ['proxy','redirect']:
redirection_options['block_mode'] = args.block_mode.lower()
else:
parser.error('--block_mode must be "proxy" or "redirect"')
if args.block_url != None:
redirection_options['block_url'] = args.block_url
else:
redirection_options['block_url'] = None
if args.allow_mode != None:
if args.allow_mode.lower() in ['proxy','redirect']:
redirection_options['allow_mode'] = args.allow_mode.lower()
else:
parser.error('--allow_mode must be "proxy" or "redirect"')
else:
redirection_options['allow_mode'] = None
if args.allow_url != None:
redirection_options['allow_url'] = args.allow_url
else:
redirection_options['allow_url'] = None
if args.ip_blacklist != None:
redirection_options['ip_blacklist'] = args.ip_blacklist.split(';')
else:
redirection_options['ip_blacklist'] = None
if args.ir == True:
redirection_options['ir'] = 'wget;curl;HTTrack;crawl;google;bot;b\-o\-t;spider;baidu'
else:
redirection_options['ir'] = None
if args.block_ua != None:
redirection_options['block_ua'] = args.block_ua
else:
redirection_options['block_ua'] = None
if args.mobile_mode != None:
redirection_options['mobile_mode'] = args.mobile_mode
else:
redirection_options['mobile_mode'] = None
if args.mobile_url != None:
redirection_options['mobile_url'] = args.mobile_url
else:
redirection_options['mobile_url'] = None
if args.teamserver != None:
redirection_options['teamserver'] = args.teamserver
else:
redirection_options['teamserver'] = None
if args.valid_uris != None:
redirection_options['valid_uris'] = args.valid_uris.split(';')
else:
redirection_options['valid_uris'] = None
if args.server_root != None:
redirection_options['server_root'] = args.server_root
else:
redirection_options['server_root'] = "/var/www/html/"
if args.invalid_uri != None:
redirection_options['invalid_uri'] = args.invalid_uri
else:
redirection_options['invalid_uri'] = None
if args.malleable != None:
redirection_options['malleable'] = args.malleable
else:
redirection_options['malleable'] = None
if args.staging_uri != None:
redirection_options['staging_uri'] = args.staging_uri
else:
redirection_options['staging_uri'] = None
if args.backup == True:
backupConfig(redirection_options['silent'],redirection_options['server_root'])
processing(redirection_options)