CCyberwiki

Command injection

Command injection
ClassInjection
Also known asOS command injection, shell injection
CWECWE-78: Improper Neutralization of Special Elements used in an OS Command
OWASP Top 10A05:2025 Injection
Typical severityCritical
AffectsServer code that starts another program with a command line built from request data: exec in Node.js, shell=True in Python, system in PHP and C

An application that builds an operating system command from outside input, and leaves in the characters that can change what the command does, has a vulnerability called command injection or OS command injection.1 It is one of the common forms of injection.2 An attacker who controls the input can make the server run programs of their choosing with the permissions of the application.

Description

Web applications sometimes hand work to another program on the server: a DNS lookup with nslookup, an image conversion, a file download with curl. When the code passes a single line of text to a shell, the shell splits that text into commands and arguments by its own syntax rules. A value from a request that contains shell metacharacters, such as ;, |, & or $( ), is parsed as syntax, and the shell runs whatever follows.3

The flaw takes two forms. In one, the application runs a fixed program and inserts the input as an argument. That program always runs, and a command separator in the argument starts a second program after it. In the other, the input chooses the whole command, and the attacker runs any program directly.1

A related weakness is argument injection (CWE-88). Even when separators are escaped, an input that begins with - can be read by the program as an option. An escaped value --help passed to curl is one example. Depending on the program, such an option can disclose information or even allow remote code execution.3

Mechanism

A Node.js route looks up a host name entered by the user:

// Node.js: exec passes the whole string to a shell
const { exec } = require("node:child_process");
exec("nslookup " + req.query.host, (err, out) => res.send(out));

The exec function starts a shell and gives it the string, so the shell interprets any special characters in it.4 A request with host=example.com runs one lookup. A request with host=example.com; whoami produces this command line:

nslookup example.com; whoami

The shell runs nslookup, then runs whoami and returns the name of the account the application runs as. Any other program can take the place of whoami.

Impact

An attacker can run unauthorized code or commands, read and change files and application data, stop the service, and hide their activity. Because the application itself runs the commands, the actions appear to come from the application or its owner.1 The damage grows with the permissions of the process: code that runs as root gives the attacker root.3

Notable incidents

Shellshock (CVE-2014-6271), published in the National Vulnerability Database on 24 September 2014, was a flaw in GNU Bash up to version 4.3. Bash ran text that followed a function definition stored in an environment variable, so a remote attacker who could set an environment variable could run code. Known exploitation routes included the mod_cgi and mod_cgid modules of the Apache HTTP Server, the ForceCommand feature of OpenSSH and DHCP client scripts. The first patch was incomplete and the remaining flaw received a separate identifier, CVE-2014-7169. The NVD record maps it to CWE-78 with a CVSS 3.1 base score of 9.8, and CISA added it to the Known Exploited Vulnerabilities catalog on 28 January 2022.5

Detection

Search the source for calls that run a command line through a shell: exec and execSync in Node.js, spawn or execFile with the shell option, shell=True in Python, and system() in PHP and C. A match is a vulnerability when any part of the string can come from a request, a file upload name, a header or the database.

Static application security testing tools find many cases by following data from input to these calls. They can miss commands started inside third-party libraries, and they can flag code that already validates its input. Dynamic application security testing tools and fuzzing send separators in every parameter and watch the response, a method rated moderately effective.1

Prevention

Use a library function

The primary defence is to avoid starting a command at all. A built-in function, such as mkdir() in place of system("mkdir ..."), does one job and cannot be made to run another program.3

Pass arguments as a list

When a program must run, start it without a shell and pass each argument as a separate element. The program receives the input as one argument, and characters such as ; have no special meaning:46

// Node.js: execFile runs the program directly, with no shell
const { execFile } = require("node:child_process");
execFile("nslookup", [host], (err, out) => res.send(out));
Table 1. Safe and unsafe forms by language
LanguageSafe formUnsafe form
Node.jsexecFile("nslookup", [host]), spawn("nslookup", [host])exec(), or shell: true
Pythonsubprocess.run(["nslookup", host])shell=True with joined text
PHPsystem("nslookup " . escapeshellarg($host))input joined into the string unescaped
Javanew ProcessBuilder("nslookup", host)Runtime.exec(String), deprecated since Java 18

Python's subprocess module does not call a shell unless shell=True is set; with it, quoting becomes the application's job, and shlex.quote() does that on some platforms.6 PHP's escapeshellarg() wraps the value in single quotes so that it reaches the program as one argument.3 On Windows, Python may start .bat and .cmd files through a shell whatever arguments are passed, so the list form does not protect them.6

Validate the arguments

A list of arguments still lets an attacker pass an option. Check each value against an allowlist: a fixed set of choices, or a pattern of permitted characters and a maximum length, such as ^[a-z0-9]{3,10}$. For programs that support it, put -- before user values so that later arguments are not read as options.3

Limit the account

Run the application under an account with only the rights it needs, and use separate accounts for separate tasks. A runtime policy such as AppArmor can restrict the programs a process may start.1 See Principle of least privilege.

Prompt for a coding agent

Find every place in this project that starts another program: exec, execSync, spawn or execFile with shell: true in Node.js; subprocess with shell=True, os.system or os.popen in Python; system, exec, shell_exec, passthru, popen or backticks in PHP. For each one, check whether any part of the command can come from a request, a header, a file name, a form or the database. Where a library function can do the same job, use it instead. Otherwise rewrite the call to run the program without a shell and pass each argument as a separate list element. Check each user value against a strict allowlist and reject values that start with "-". List each file and line you changed.

In SecFlash

SecFlash reports a shell command built from values in the source code as SF-SEC-706 on the Security page. When the attack test makes the live server run a command by adding characters such as ; or | to a parameter, it reports SF-SEC-607, and a server that runs a Bash version affected by Shellshock is reported as SF-SEC-653.

See also

References

  1. a b c d e "CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')". Common Weakness Enumeration. MITRE.
  2. back "A05:2025 Injection". OWASP Top 10:2025. OWASP Foundation.
  3. a b c d e f "OS Command Injection Defense Cheat Sheet". OWASP Cheat Sheet Series.
  4. a b "Child process". Node.js documentation. OpenJS Foundation.
  5. back NIST (published 24 September 2014). "CVE-2014-6271 Detail". National Vulnerability Database.
  6. a b c "subprocess: Subprocess management, Security Considerations". Python documentation. Python Software Foundation.