T09 · Insecure Skill Coding Practices
- Location
scripts/nmap_pentest_scans.py:75- Finding
Unvalidated target interpolation creates command injection risk in generated scan artifacts
- Content
View full analysis
Vulnerability Details
File Location:
scripts/nmap_pentest_scans.py:75-113andscripts/nmap_pentest_scans.py:169-172
Vulnerability Type: Command injection through unsafe command generation
Risk Level: MediumVulnerable Code
python def build_command_plan(target: str, profile: str, include_udp: bool) -> list[dict[str, str]]: target_basename = normalize_target_basename(target) if profile == "stealth": timing = "-T2" rate = "--max-retries 2 --scan-delay 5ms" elif profile == "fast": timing = "-T4" rate = "--min-rate 500 --max-retries 1 --host-timeout 10m" else: timing = "-T3" rate = "--min-rate 100 --max-retries 2" plan: list[dict[str, str]] = [ { "stage": "host-discovery", "command": ( f"nmap -sn {target} -n --reason " f"-oA results/{target_basename}-01-discovery" ), "purpose": "Detect responsive hosts and collect probe reasons.", }, { "stage": "tcp-triage", "command": ( f"nmap -sS --top-ports 1000 {target} -n {timing} {rate} --reason " f"-oA results/{target_basename}-02-tcp-top1000" ), "purpose": "Find common exposed TCP services quickly.", }, { "stage": "service-enum", "command": ( f"nmap -sV -sC -p <OPEN_TCP_PORTS> {target} -n {timing} --reason " f"-oA results/{target_basename}-03-svc-default" ), "purpose": "Fingerprint service versions and run default safe scripts.", }, { "stage": "web-tls", "command": ( f"nmap -p 80,443 --script http-title,http-headers,http-methods,ssl-cert " f"{target} -n --script-timeout 30s " ...[truncated 2782 chars]- Remediation
View remediation
Remediation Suggestions
- Parse IP addresses and CIDR blocks with Python's
ipaddress.ip_address()andipaddress.ip_network()APIs. - Apply a strict, length-limited hostname validator where DNS names are supported.
- Reject targets containing whitespace, control characters, shell metacharacters, or a leading hyphen.
- Represent commands internally as argument arrays rather than shell strings.
- If commands must be displayed in shell syntax, quote each argument with
shlex.quote(). - Revalidate the final command against an allowlist of supported Nmap options before export.
- Avoid directing users or automation to execute generated text through
shell=True,eval, or equivalent shell interpretation. - Add tests covering semicolons, pipes, redirection, command substitution, newlines, leading-dash values, and malformed CIDR notation.
- Parse IP addresses and CIDR blocks with Python's
