The Novee Gym is open for business at Black Hat Europe

Book a meeting

The Novee Gym is open for business at Black Hat Europe

Book a meeting

Glossary › Command Injection

Command Injection

Explore Article +

Key Takeaways

  • Command injection lets an attacker run operating system commands through an application that passes user input to a shell.
  • Injected commands run with the application’s privileges, so every OS command injection flaw is effectively an RCE vulnerability.
  • Blind command injection returns no output, so testers confirm it through time delays or out-of-band callbacks.
  • The strongest fix is to stop calling the shell; where that is impossible, pass arguments as arrays and allowlist values.

What Is Command Injection?

Command injection is a vulnerability in which an application builds an operating system command from untrusted input, letting an attacker append commands of their own that the server then executes. MITRE tracks it as CWE-78, Improper Neutralization of Special Elements used in an OS Command.

It is one of the most direct routes to server compromise. OWASP groups it under Injection, ranked A05 in the OWASP Top 10:2025.

How Command Injection Works

The flaw appears when code hands a concatenated string to a shell. A network diagnostics page might run ping -c 4 followed by whatever address the user typed. If the input is 8.8.8.8; cat /etc/passwd, the shell sees two commands and runs both.

Shell metacharacters make this possible. Separators such as ;, &&, || and | chain commands, while backticks and $() substitute one command’s output into another. On Windows, & and | do the same job in cmd.exe.

The input can come from anywhere the app trusts: form fields, URL parameters, HTTP headers, uploaded file names or CI/CD metadata. Novee’s Cordyceps research found exactly this pattern in GitHub Actions workflows, where branch names and pull request titles flowed straight into shell steps.

What Command Injection Enables When Exploited

Injected commands run as the user behind the application process, which turns a command injection vulnerability into full remote code execution. Typical follow-on actions include:

  • Data theft. Reading configuration files, source code and environment variables that hold API keys and database credentials.
  • Persistence. Installing a reverse shell, web shell or scheduled task to keep access after the bug is patched.
  • Lateral movement. Using the server as a foothold to reach internal systems that are not exposed to the internet.
  • Privilege escalation. Chaining a local misconfiguration or kernel flaw to gain root.
  • Disruption. Deleting data, deploying ransomware or hijacking compute for cryptomining.

How Pentesters Identify Command Injection Vulnerabilities

Testers start by mapping every input that might reach a system call, especially features that wrap OS utilities: file conversion, image processing, DNS or ping lookups, backups and PDF generation.

They then inject separators and substitution syntax and watch the response. When output is reflected, ; id or | whoami confirms execution immediately. Blind command injection needs indirect proof:

  • Time delays. A payload such as ; sleep 10 that stalls the response by exactly ten seconds.
  • Out-of-band callbacks. A nslookup or curl to a tester-controlled domain that logs the incoming request.
  • Output redirection. Writing command output to a file in the web root, then requesting it.

Burp Suite’s scanner and Collaborator, OWASP ZAP and the open-source tool Commix automate much of this. Manual review still matters for filters that block obvious payloads but miss encodings, newline characters or alternate separators.

How to Prevent Command Injection

  • Avoid the shell. Use language libraries instead of OS commands, for example a native DNS or image library rather than calling nslookup or ImageMagick through a shell.
  • Separate arguments. If a process must be started, pass the program and its arguments as an array, such as Python subprocess.run([...]) with shell=False, so input is never parsed as shell syntax.
  • Allowlist input. Accept only expected formats, such as a strict IP address pattern, and reject everything else.
  • Run with least privilege. Use a dedicated low-privilege service account and container isolation to limit the blast radius.

Escaping metacharacters is a last resort, since it is easy to get wrong across shells. As covered in modern application penetration testing, these flaws need to be tested continuously as code changes. Novee’s AI pentesting agents chain inputs the way an attacker would, three independent agents validate each finding, and Agentic Remediation retests to confirm fixes hold.

FAQ

Command injection runs operating system commands through a shell the application calls, such as bash or cmd.exe. Code injection inserts code in the application’s own language, such as PHP or Python, which the interpreter then runs through functions like eval(). Both can lead to full compromise, but they exploit different execution layers.

Any function that passes a string to a shell. Common examples are PHP’s system(), exec(), shell_exec() and passthru(), Python’s os.system() and subprocess with shell=True, Node.js child_process.exec(), and Java’s Runtime.exec() when it launches sh -c. Risk appears when user input is concatenated into the command string.

Blind command injection occurs when the server executes the injected command but returns none of its output in the response. Attackers and testers confirm it indirectly, through response delays from commands like sleep, DNS or HTTP callbacks to a server they control, or by writing output to a file they can later retrieve.

Command injection is a flaw in application code, not the operating system, so Linux, Windows and macOS servers are all exposed. Linux shells offer more chaining and substitution syntax, which gives attackers more payload options. Embedded Linux devices such as routers and cameras are frequent targets because their web interfaces often wrap shell utilities.

Both occur when untrusted input is mixed into an interpreted command, but the interpreter differs. SQL injection targets the database query engine and typically exposes or alters data. Command injection targets the operating system shell, giving direct control of the server. Parameterized queries fix SQL injection; argument arrays and avoiding shells fix command injection.