-
Notifications
You must be signed in to change notification settings - Fork 0
/
vulnerability_scan.py
72 lines (56 loc) · 2.41 KB
/
vulnerability_scan.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
import os
import subprocess
import time
def vulnerability_scan(file_name):
"""
Description: This function scans the hosts in the file_name for vulnerabilities using nmap.
It creates a new folder for each host in the scanned_hosts directory and saves the scan results in a text file.
:param file_name: The name of the file containing the hosts to be scanned.
:return: None
"""
current_dir = os.getcwd()
subprocess.run(["sudo", "nmap", "--script-updatedb"])
os.popen("cd " + os.getcwd())
response = []
file_name2 = "scan_history/" + file_name
file_list = []
with open(file_name2, "r") as file:
for line in file:
file_list.append(line)
file.close()
for line in file_list:
line = line.split(" | ")[0]
ip = line.strip()
print("Scanning " + ip + " for vulnerabilities")
for line in os.popen("sudo nmap -sS -A -O --script vuln " + ip):
time.sleep(1) # Waiting one seccond to catch the entire output (not sure if it works like that tbh)
response.append(line)
if "Host seems down" in response:
print(ip + " is down")
else:
os.system(
"mkdir -p "
+ os.getcwd()
+ "/scanned_hosts/"
+ file_name.split("_hosts.txt")[0]
+ "/"
)
new_folder = os.getcwd() + "/scanned_hosts/" + file_name.split("_hosts.txt")[0] + "/"
newest_folder = file_name.split("_hosts.txt")[0]
scaned_host_dir_list = f'{current_dir}/scanned_hosts/'
if newest_folder not in os.listdir(scaned_host_dir_list):
os.mkdir(new_folder)
folder_content = os.listdir("scanned_hosts/" + file_name.split("_hosts.txt")[0] + "/")
count = 0
for i in folder_content:
if i == ip + ".txt":
count += 1
new_Path = "scanned_hosts/" + file_name.split("_hosts.txt")[0] + "/" + ip + ".txt"
subprocess.Popen(["touch", new_Path])
with open(new_Path, "w") as file:
for i in response:
file.writelines(i)
del response[:]
# This call to the function vulnerability_scan() is here for debugging purposes. Uncomment it if needed.
# path = os.getcwd()
# vulnerability_scan(path)