Pcornel
Browse topics
Options
Recent
Favorites
Security · Technical reference

Command Injection

Direct answer

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.

When to use this

Use this when user input could reach a system shell.

First 30 seconds

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.

Operational verification

input → command builder → (no shell) → process

Context & applicabilityCommon mistakes · platform/version boundaries
Common mistakes
Common mistakes
  • 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.
Platform / version applicability
Security architecture and operations
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformSecurity architecture and operations
Scope / prerequisiteUse this when user input could reach a system shell.
What next?2 suggested next steps
Evidence1
On this pageJump to a specific section
Evidence references

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.

observation · 1
Key points
  • Avoid calling a shell with user input.
  • Pass arguments as lists to process APIs.
  • Validate input strictly.
Key checks
  • 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([...]), not shell=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)
Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
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
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docSecurity architecture and operations current

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%
Confirmation

Confirm action

Continue?