T09 · Insecure Skill Coding Practices
- Location
scripts/scan.py:61- Finding
Shell Command Injection Through Unvalidated Network and DNS Parameters
- Content
View full analysis
/dev/null" nmap_output = run_cmd(nmap_cmd, timeout=180) ``` ```python elif args.network in networks: net = networks[args.network] cidr = net['cidr'] dns = args.dns or net.get('dns') name = net.get('description', args.network) trusted_network = True # Pre-configured = trusted ``` ### Technical Analysis The centralized `run_cmd()` function executes command strings with `shell=True`. Consequently, shell metacharacters contained in interpolated values are interpreted by the operating-system shell rather than passed as literal arguments to `dig` or `nmap`. The following externally controllable values reach these command strings: - The `--dns` command-line argument. - The `dns` property of a configured network. - The `cidr` property of a named network loaded from a user-selected JSON file. - A configuration file selected through the `--config` argument. Ad hoc network arguments receive some val ...[truncated 3009 chars]- Remediation
View remediation
/dev/null`. Use `stderr=subprocess.DEVNULL` or capture stderr through `subprocess.run()`. 4. Validate every CIDR, including values from trusted named networks: ```python network = ipaddress.ip_network(cidr, strict=False) cidr = str(network) ``` 5. Validate DNS server values before use: ```python dns_server = str(ipaddress.ip_address(dns_server)) ``` 6. Reject unexpected option-like values and ensure user-controlled values cannot become executable names or nmap flags. 7. Treat configuration files as untrusted input regardless of their location or whether a network is labeled trusted. 8. Avoid invoking nmap through unrestricted sudo. If elevated scanning is required, use a narrowly scoped privilege mechanism and a fixed executable/argument policy. ]]>
