T09 · Insecure Skill Coding Practices
- Location
scripts/quick-audit.sh:10- Finding
Nmap Option Injection Through Unvalidated Target Input
- Content
View full analysis
Vulnerability Details
File Location:
scripts/quick-audit.sh, lines 10 and 26-31
Vulnerability Type: Argument injection into a security scanning utility
Risk Level: MediumVulnerable Code
bash TARGET="${1:?Usage: quick-audit.sh <target> [output_dir]}" OUTPUT_DIR="${2:-./audit-reports}" DATE=$(date +%Y-%m-%d_%H%M%S) REPORT="$OUTPUT_DIR/audit-${TARGET//\//-}-$DATE.txt" mkdir -p "$OUTPUT_DIR" echo "========================================" | tee "$REPORT" echo "Security Audit: $TARGET" | tee -a "$REPORT" echo "Date: $(date)" | tee -a "$REPORT" echo "========================================" | tee -a "$REPORT" echo "" | tee -a "$REPORT" # 1. Port scan echo "--- Port Scan (top 1000) ---" | tee -a "$REPORT" nmap -sV --top-ports 1000 "$TARGET" 2>&1 | tee -a "$REPORT" echo "" | tee -a "$REPORT" # 2. SSL/TLS check (if HTTPS) echo "--- SSL/TLS Check ---" | tee -a "$REPORT" nmap --script ssl-cert,ssl-enum-ciphers -p 443 "$TARGET" 2>&1 | tee -a "$REPORT" || trueTechnical Analysis
The script accepts its scan target directly from the first positional argument and forwards it to Nmap without validating that it is an IPv4 address, IPv6 address, CIDR range, or hostname.
Shell quoting prevents whitespace splitting, wildcard expansion, and direct shell metacharacter execution. However, quoting does not prevent the receiving program from interpreting a value beginning with
-as a command-line option. Consequently, a target controlled by an untrusted caller can be parsed by Nmap as an option rather than as a host.This is application-level argument injection rather than shell command injection. The available behavior depends on the installed Nmap version and which option forms it accepts as one argument. A crafted option can alter target selection, script selection, data sources, output behavior, or other scan settings. Even where an injected option causes the scan to fail because no po ...[truncated 1664 chars]
- Remediation
View remediation
Remediation Suggestions
- Validate the target before constructing report names or invoking any scanner. Accept only explicitly supported formats:
- IPv4 addresses
- IPv6 addresses
- CIDR ranges
- DNS hostnames
- Reject empty values, values beginning with
-, control characters, whitespace, URL components, and values containing scanner-option syntax. - Use a structured parser rather than relying exclusively on a permissive regular expression. For example, use Python's
ipaddressmodule for IP addresses and networks, with a separate strict hostname validator. - If the installed Nmap version supports an end-of-options delimiter for the relevant invocation, place it immediately before the target. This should be defense in depth and not a replacement for validation.
- Run scans under a dedicated, minimally privileged account with restricted filesystem access and controlled network egress.
- If the script is called by a web service or job processor, enforce server-side target allowlists and authorization checks. Do not rely on client-side validation.
- Add regression tests confirming that inputs such as
--script=...,--datadir=..., values beginning with-, values containing whitespace, and malformed CIDR ranges are rejected before Nmap is started.
A minimal defensive check should reject option-like input before use:
bash TARGET="${1:?Usage: quick-audit.sh <target> [output_dir]}" if [[ "$TARGET" == -* || "$TARGET" =~ [[:space:][:cntrl:]] ]]; then echo "Error: invalid scan target" >&2 exit 2 fiThis check should be supplemented with strict IP, CIDR, or hostname validation.
- Validate the target before constructing report names or invoking any scanner. Accept only explicitly supported formats:
