forked from mazen160/shennina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
392 lines (284 loc) · 10.8 KB
/
utils.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
#!/usr/bin/env python3
import config
from datetime import datetime
import json
import random
from termcolor import colored
import ipaddress
import os
import uuid
import requests
import subprocess
banner = """ ____ _ _
/ ___|| |__ ___ _ __ _ __ (_)_ __ __ _
\___ \| '_ \ / _ \ '_ \| '_ \| | '_ \ / _` |
___) | | | | __/ | | | | | | | | | | (_| |
|____/|_| |_|\___|_| |_|_| |_|_|_| |_|\__,_|
v0.3
"""
def current_date():
return datetime.now().strftime("%d-%m-%Y")
def current_time():
return datetime.now().strftime("%H:%M:%S")
def uuidgen():
return str(uuid.uuid4())
def generate_ip_addresses_from_ip_range(ip_range):
return [str(_) for _ in ipaddress.IPv4Network(ip_range)]
def generate_random_port():
return str(random.randint(4096, 65535))
def generate_scan_path(host):
output_path = "{SCANS_PATH}info_{host}.json".format(SCANS_PATH=config.SCANS_PATH, host=host)
return(output_path)
def generate_exploitation_result_path(host):
output_path = "{SCANS_PATH}info_exploitation_scan_{host}.json".format(SCANS_PATH=config.SCANS_PATH, host=host)
return(output_path)
def check_if_cached_scan_exists(host):
return os.path.exists(generate_scan_path(host))
def check_if_filename_exists(filename):
file_path = "data/{}.json".format(filename)
return os.path.exists(file_path)
def load_scan(host):
f = open(generate_scan_path(host), "r")
output = json.loads(f.read())
f.close()
return output
def load_exploitation_result(host):
f = open(generate_exploitation_result_path(host), "r")
output = json.loads(f.read())
f.close()
return output
def get_successful_ports(host):
result = load_exploitation_result(host)
return len(list(set([i['port'] for i in result['results']])))
def get_exploit_and_port_reward(host, exploit, port):
try:
result = load_exploitation_result(host)
for i in result['results']:
if i['exploit_name'] == exploit and i['port'] == port:
return 1
return -1
except Exception as e:
print(e)
return -1
def get_exploit_reward(input_data, action):
try:
result = load_exploitation_result(input_data['host'])
for i in result['results']:
if i['exploit_name'] == action and i['name'] == input_data['name'] and i['platform'] == input_data['platform']:
return i
return {"host": None}
except Exception as e:
print(e)
return {"host": None}
def generate_exploitation_scan(host):
output_path = "{SCANS_PATH}info_exploitation_scan_{host}.json".format(SCANS_PATH=config.SCANS_PATH, host=host)
return(output_path)
def generate_exploitation_report_path(host):
output_path = "{REPORTS_PATH}report_{host}.md".format(REPORTS_PATH=config.REPORTS_PATH, host=host)
return(output_path)
def generate_exploitation_report(host, exploitation_result, exfiltration_output):
report_path = generate_exploitation_report_path(host)
exploit_details = get_exploit_details(exploitation_result["exploit_name"])
references = "\n".join(["-".join([b for b in a]) for a in exploit_details["references"]])
references = references.replace("URL-http", "http") # URLs are received from MSF in a different format
output = """
# Shennina Exploitation Report
## Target: `{target}`
#### The target has been compromised using the following:
## Exploit: `{exploit_name}`
## Exploit Details
### Name
```
{exploit_detailed_name}
```
### Description
```
{exploit_description}
```
### References
```
{exploit_references}
```
## Shell Type: `{shell}`
## Payload: `{payload}`
""".format(target=host, exploit_name=exploitation_result["exploit_name"],
exploit_detailed_name=exploit_details["name"],
exploit_description=exploit_details["description"],
exploit_references=references,
shell=exploitation_result["type"],
payload=exploitation_result["payload"])
if len(exfiltration_output) == 0:
f = open(report_path, "w")
f.write(output)
f.close()
return report_path
output += """
---
# Data Obtained From Target via Exfiltration Server
"""
for _ in exfiltration_output.keys():
output += """### {name}\n\n```\n{data}\n```\n\n""".format(name=_, data=exfiltration_output[_])
f = open(report_path, "w")
f.write(output)
f.close()
return report_path
def generate_vulnerability_scan_report_path(host):
output_path = "{REPORTS_PATH}vulnerability_scan_report_{host}.md".format(REPORTS_PATH=config.REPORTS_PATH, host=host)
return(output_path)
def check_access_to_exfiltration_server():
ping_url = "http://{}/ping".format(config.EXFILTRATION_SERVER)
try:
if requests.get(ping_url).text == "pong\n":
return True
else:
return False
except KeyboardInterrupt:
return False
def check_if_current_user_is_root():
if os.getuid() == 0:
return True
else:
return False
def generate_vulnerability_scan_report(host, results_list, failed_list):
output = """# Shennina Vulnerability Scan Report
## Target: `{target}`
""".format(target=host)
output += "\n\n## Total Number of Successful Exploits: `{}`".format(len(results_list))
output += "\n\n## Total Number of Tested Exploits: `{}`".format(len(results_list) + len(failed_list))
output += "\n\n## Results\n\n"
if len(results_list) == 0:
output += "No automated remote exploit was identified to be working against the host.\n\n"
report_path = generate_vulnerability_scan_report_path(host)
f = open(report_path, "w")
f.write(output)
f.close()
return report_path
output += "\n\n---\n\n"
for result in results_list:
exploit_details = get_exploit_details(result["exploit_name"])
references = "\n".join(["-".join([b for b in a]) for a in exploit_details["references"]])
references = references.replace("URL-http", "http") # URLs are received from MSF in a different format
output += """\n\n## Exploit: `{exploit_name}`
#### Port: `{port}`
#### Service Name: `{name}`
#### Product: `{product}` `{version}`
#### Exploit Details
##### Name
```
{exploit_detailed_name}
```
##### Description
```
{exploit_description}
```
##### References
```
{exploit_references}
```
#### Shell Type: `{shell}`
#### Payload: `{payload}`
---
""".format(exploit_name=result["exploit_name"],
name=result["name"],
product=result["product"],
exploit_detailed_name=exploit_details["name"],
exploit_description=exploit_details["description"],
exploit_references=references,
version=result["version"],
shell=result["type"],
payload=result["payload"],
port=result["port"])
report_path = generate_vulnerability_scan_report_path(host)
f = open(report_path, "w")
f.write(output)
f.close()
return report_path
def save_exploitation_scan(host, results_list, failed_list):
output_path = generate_exploitation_scan(host)
data = {"target": host,
"results": results_list,
"failed_tests": failed_list}
f = open(output_path, "w")
f.write(json.dumps(data, indent=4))
f.close()
def save_file(filename, json_result):
output_path = "data/{}.json".format(filename)
f = open(output_path, "w")
f.write(json.dumps(json_result, indent=4))
f.close()
def load_exploitation_scan(host):
f = open(generate_exploitation_scan(host), "r")
output = json.loads(f.read())
f.close()
return output
def load_file(filename):
file_path = "data/{}.json".format(filename)
f = open(file_path, "r")
output = json.loads(f.read())
f.close()
return output
def check_if_exploit_is_in_exploits_tree(exploit):
for _ in config.EXPLOITS_TREE:
if _["exploit"] == exploit:
return True
return False
def get_value(array, ind):
if (ind - 1) < len(array):
return array[ind - 1]
return None
def get_index(array, item):
# preventing negative values
return array.index(item) + 1 if item in array else 0
def get_exploit_details(exploit_name):
if not exploit_name.startswith("exploit/"):
exploit_name = "exploit/" + exploit_name
exploit_name = exploit_name[8:]
for exploit in config.EXPLOITS_TREE:
if exploit["exploit"] == exploit_name:
return exploit
return None
def execute_linux_exploit_suggester_tool(uname_output):
LINUX_EXPLOIT_SUGGESTER_PATH = "{PROJECT_PATH}/thirdparty/linux-exploit-suggester.sh".format(PROJECT_PATH=config.PROJECT_PATH)
uname_output = uname_output.replace("'", "INVALID").replace("\n", "INVALID").replace("\\", "INVALID")
cmd = """bash '{LINUX_EXPLOIT_SUGGESTER_PATH}' -u '{uname_output}' """.format(LINUX_EXPLOIT_SUGGESTER_PATH=LINUX_EXPLOIT_SUGGESTER_PATH,
uname_output=uname_output)
output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read()
if type(output) == bytes:
output = output.decode("utf-8")
return output
def generate_chunks(seq, num):
# This method takes an array and divides it into
# an n number of arrays
avg = len(seq) / float(num)
out = []
last = 0.0
while last < len(seq):
out.append(seq[int(last):int(last + avg)])
last += avg
return out
class PPrint(object):
def error(self, text):
print("{} [{}] {}".format(colored("[!]", 'red', attrs=['bold']),
colored(current_time(), "magenta"),
text))
def success(self, text):
print("{} [{}] {}".format(colored("[$]", 'green', attrs=['bold']),
colored(current_time(), "magenta"),
text))
def info(self, text):
print("{} [{}] {}".format(colored('[*]', 'cyan', attrs=['bold']),
colored(current_time(), "magenta"),
text))
def testing_exploit(self, text):
print("{} [{}] {}".format(colored('[.]', attrs=['bold']),
colored(current_time(), "magenta"),
text))
def initial_message(self):
starting_time = "{} / {}".format(current_time(), current_date())
print("{}".format(colored(banner, 'green', attrs=["bold", "dark"])))
print("{} Starting at {}".format(colored('[%]', 'cyan', attrs=['bold']),
colored(starting_time, "magenta")))
def finishing_message(self):
finishing_time = "{} / {}".format(current_time(), current_date())
print("{} Finished at {}".format(colored('[%]', 'cyan', attrs=['bold']),
colored(finishing_time, "magenta")))