Use this when user input could reach a system shell.
Command Injection
Command injection happens when user input reaches an operating system shell, letting attackers run commands. Avoid shell calls, use APIs with argument lists and strictly validate input.
1. Command injection runs attacker commands via shell calls.
2. Avoid shells; use APIs or argument arrays.
3. Strict allowlists.
4. Least-privilege service accounts.
5. Sandboxing.
input → command builder → (no shell) → process
Context & applicabilityCommon mistakes · platform/version boundaries
- Escaping input instead of avoiding the shell (
shell=True,exec(),system()). - Allow-listing characters but forgetting newlines,
$()or backticks. - Passing user input as an argument that starts with
-(option injection) — use--before arguments. - Running the application as root, so injection gives full control.
Operational reference
Fast context before you go deeper.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Avoid calling a shell with user input.
- Pass arguments as lists to process APIs.
- Validate input strictly.
- Code review: shell=True/exec/system calls — dangerous sinks
- Use argument arrays (subprocess.run([...])) — no shell
- Input allowlists — validation
ReferenceWhy it matters1 section · depth on demand
Example and fix
Vulnerable: os.system("ping -c 1 " + host) — input 8.8.8.8; cat /etc/passwd runs a second command.
Fixed: call the program without a shell and pass arguments as a list (subprocess.run(["ping", "-c", "1", host])), and validate host against an allowlist pattern. Run the application with minimal OS privileges.
ReferenceImportant terms & boundaries1 section · depth on demand
In depth
Command injection occurs when an application passes user input to an operating system shell, letting an attacker run additional commands on the server. It is common in admin interfaces, network appliances and scripts that wrap command-line tools.
Prevention:
- Avoid calling the shell: use libraries or APIs instead of shell commands.
- If a command must run, pass arguments as a list without a shell (
subprocess.run([...]), notshell=True). - Validate input against a strict allow-list (e.g. hostnames, IP addresses with a parser).
- Run the process with least privilege and in a restricted environment.
import ipaddress, subprocess
ip = str(ipaddress.ip_address(user_input)) # raises on invalid input
subprocess.run(["ping", "-c", "3", ip], check=False, timeout=10)
ReferenceConfiguration & state1 section · depth on demand
Safe patterns
# Python
subprocess.run(["ping", "-c", "1", host], check=True) # no shell
# PHP
$out = shell_exec("ping -c 1 " . escapeshellarg($host)); # if a shell is unavoidable
Documented example, not captured output. Validate input against a strict pattern (e.g. hostname or IP) before calling any command.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Web Security → Command Injection. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://owasp.org/www-project-application-security-verification-standard/ · confidence 94%