-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnmap_script4.py
95 lines (81 loc) · 3.67 KB
/
nmap_script4.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
import nmap
import json
def scan_service_version(target_ip, output_file):
scanner = nmap.PortScanner()
print(f"Scanning {target_ip} for service/version detection...")
scanner.scan(target_ip, arguments='-sV -O --script vuln')
with open(output_file, 'w') as f:
for host in scanner.all_hosts():
f.write(f"\nHost: {host} ({scanner[host].hostname()})\n")
f.write(f"State: {scanner[host].state()}\n")
for protocol in scanner[host].all_protocols():
f.write(f"\nProtocol: {protocol}\n")
ports = scanner[host][protocol].keys()
for port in ports:
port_info = scanner[host][protocol][port]
f.write(f"Port: {port}\tState: {port_info['state']}\tService: {port_info['name']}\n")
if 'product' in port_info:
f.write(f"Service version: {port_info['product']} {port_info['version']}\n")
if 'osmatch' in scanner[host]:
for osmatch in scanner[host]['osmatch']:
f.write(f"OS: {osmatch['name']} ({osmatch['accuracy']}% accuracy)\n")
for protocol in scanner[host].all_protocols():
ports = scanner[host][protocol].keys()
for port in ports:
port_info = scanner[host][protocol][port]
if 'script' in port_info:
for script, output in port_info['script'].items():
f.write(f"[{script}] => {output}\n")
print(f"Results saved to {output_file}")
print_important_info(scanner, target_ip)
def print_important_info(scanner, target_ip):
host_data = {
"IP": target_ip,
"State": scanner[target_ip].state(),
"OS": {},
"Ports": []
}
if 'osmatch' in scanner[target_ip]:
os_match = scanner[target_ip]['osmatch'][0]
host_data["OS"] = {
"Name": os_match['name'],
"Version": os_match['osclass'][0]['osgen'],
"Accuracy": f"{os_match['accuracy']}%"
}
for protocol in scanner[target_ip].all_protocols():
ports = scanner[target_ip][protocol].keys()
for port in ports:
port_info = scanner[target_ip][protocol][port]
port_data = {
"Port": port,
"Protocol": protocol,
"State": port_info['state'],
"Service": port_info['name'],
"Service Version": f"{port_info.get('product', '')} {port_info.get('version', '')}".strip(),
"Vulnerabilities": []
}
if 'script' in port_info:
for script, output in port_info['script'].items():
if script == 'vulners':
vulns = parse_vulners_output(output)
port_data["Vulnerabilities"] = vulns
host_data["Ports"].append(port_data)
print(json.dumps(host_data, indent=2))
def parse_vulners_output(output):
vulnerabilities = []
for line in output.split('\n'):
if line.strip() and not line.startswith('cpe:'):
parts = line.split()
if len(parts) >= 3 and parts[1].replace('.', '').isdigit():
vuln = {
"CVE": parts[0],
"Severity": float(parts[1]),
"URL": parts[2]
}
if float(vuln["Severity"]) >= 5.0:
vulnerabilities.append(vuln)
return vulnerabilities
if __name__ == "__main__":
target_ip = input("Enter the IP address to scan for service/version detection: ")
output_file = "nmap_scan_results.txt"
scan_service_version(target_ip, output_file)