File size: 1,174 Bytes
633f392
 
 
 
 
 
 
 
 
3ae5c22
 
 
 
 
 
 
 
633f392
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)}"