"""Port Scanning Module"""
from typing import Dict
from src.utils.helpers import run_command, get_timestamp
async def scan_ports(
target: str,
ports: str = "top-1000",
scan_type: str = "quick",
service_detection: bool = True
) -> Dict:
"""
Scan ports using nmap/naabu
Args:
target: Target IP/domain
ports: Port specification
scan_type: Scan type
service_detection: Enable service detection
Returns:
Dictionary with open ports
"""
results = {
"target": target,
"timestamp": get_timestamp(),
"scan_type": scan_type,
"open_ports": []
}
# Use naabu for fast scanning
cmd = f"naabu -host {target} -silent"
if ports == "all":
cmd += " -p -"
elif ports == "top-1000":
cmd += " -top-ports 1000"
else:
cmd += f" -p {ports}"
result = await run_command(cmd, timeout=600)
if result['success']:
ports_found = [line.strip() for line in result['stdout'].split('\n') if line.strip()]
results['open_ports'] = ports_found
results['total_count'] = len(ports_found)
return results