-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.py
More file actions
80 lines (64 loc) · 2.36 KB
/
Copy pathscanner.py
File metadata and controls
80 lines (64 loc) · 2.36 KB
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
import nmap
import json
from datetime import datetime
def scan_target(target: str) -> dict:
"""
Scan a single IP address using Nmap -sV (service/version detection).
Returns a structured dict of open ports with service and version info.
"""
scanner = nmap.PortScanner()
print(f"[*] Starting scan on {target} ...")
try:
scanner.scan(hosts=target, arguments="-sV --open -T4")
except nmap.PortScannerError as e:
print(f"[!] Nmap error: {e}")
return {}
except Exception as e:
print(f"[!] Unexpected error during scan: {e}")
return {}
results = {
"target": target,
"scan_time": datetime.now().isoformat(),
"services": []
}
if target not in scanner.all_hosts():
print(f"[!] Host {target} is down or not reachable.")
return results
host = scanner[target]
for proto in host.all_protocols():
ports = host[proto].keys()
for port in ports:
service_info = host[proto][port]
# Only process open ports
if service_info.get("state") != "open":
continue
service = {
"port": port,
"protocol": proto,
"state": service_info.get("state", ""),
"name": service_info.get("name", ""),
"product": service_info.get("product", ""),
"version": service_info.get("version", ""),
"extra_info": service_info.get("extrainfo", ""),
"cpe": service_info.get("cpe", "")
}
results["services"].append(service)
print(f"[+] Port {port}/{proto} — {service['name']} {service['product']} {service['version']}")
print(f"[*] Scan complete. Found {len(results['services'])} open service(s).")
return results
def build_version_string(service: dict) -> str:
"""
Build a clean version string from service info for CVE lookup.
Example: 'Apache httpd 2.4.49'
"""
parts = [
service.get("product", ""),
service.get("version", "")
]
return " ".join(p for p in parts if p).strip()
if __name__ == "__main__":
# Quick test — replace with any IP on your network
target_ip = input("Enter target IP: ").strip()
result = scan_target(target_ip)
print("\n--- Scan Results ---")
print(json.dumps(result, indent=2))