First_agent_template / tools /nmap_tool.py
bh4utik's picture
Update tools/nmap_tool.py
3ae5c22 verified
Raw History Blame Contribute Delete
1.17 kB
import subprocess
import shlex
from smolagents import tool
@tool
def scan_target(target: str, scan_type: str = "basic") -> str:
"""
Runs a controlled Nmap scan against a target.
Args:
target: Target domain or IP address to scan.
scan_type: Type of scan to perform. Supported values:
- basic: Fast port scan
- service: Service/version detection
Returns:
Raw Nmap scan output as text.
"""
allowed_scan_types = {
"basic": "-F",
"service": "-sV",
"full": "-p-"
}
if scan_type not in allowed_scan_types:
return "Error: Unsupported scan type."
# Very basic validation to reduce command injection risk
if not all(c.isalnum() or c in ".:-" for c in target):
return "Error: Invalid target format."
nmap_flag = allowed_scan_types[scan_type]
command = f"nmap {nmap_flag} {target}"
try:
result = subprocess.run(
shlex.split(command),
capture_output=True,
text=True,
timeout=60
)
return result.stdout
except Exception as e:
return f"Scan failed: {str(e)}"