OS Command Injection
User input is placed into an operating system command that the application runs through a shell, so it can add to or change the commands that are executed.
| Severity | Critical |
| CVSS 3.1 | 9.8 CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H Open in the calculator |
| CVSS 4.0 | 9.3 CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N Open in the calculator |
| CWE | CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection') |
| OWASP Top 10 | A05:2025 Injection |
| MITRE ATT&CK | T1190 Exploit Public-Facing Application, T1059 Command and Scripting Interpreter |
| Category | Injection |
Description
The application runs operating system commands to carry out some of its functions, such as network diagnostics, file conversion or image processing, and includes values supplied by the user in the command line. When the command is passed to a shell, characters such as ;, |, &, backticks, $() and line breaks let the input end the intended command and start another, which the server then runs.
An attacker can therefore run arbitrary commands on the server with the privileges of the account that runs the application, and no user interaction is needed. This typically allows them to read and change application data and configuration, including credentials for databases and other services, to install persistent access such as a web shell, and to use the server to reach other systems on the internal network. Where the application runs as a highly privileged account, the whole host is compromised.
The weakness can be confirmed directly, where the output of an injected command appears in the response, or blind, where the response does not change but an injected delay (such as sleep or a repeated ping) slows it down, or an injected DNS lookup or HTTP request reaches a server under the tester's control. Burp Suite, OWASP ZAP and other scanners report OS command injection using these techniques, and tools such as Commix automate its exploitation, so it is commonly found and exploited by automated tools.
A related form, argument injection, occurs when the application avoids the shell but lets input that begins with a hyphen be read as an option of the program it runs, which for some programs allows files to be written or further commands to be run.
Remediation
Remove the shell from the path between user input and the operating system:
- Where possible, perform the task with a library or built-in API instead of an external program, for example a DNS or file-handling library rather than calling
nslookuporcp. - Where running an external program cannot be avoided, call it directly with an argument array and without a shell, for example
execFile()orspawn()without the shell option in Node.js,subprocess.run()with a list of arguments andshell=Falsein Python, orProcessBuilderin Java. Never build a single command string from user input. - Validate each value against a strict allowlist of expected formats, such as a hostname or IP address, and place
--before user-supplied arguments where the program supports it, so that they cannot be read as options. - Run the application with the least privilege it needs, and restrict what its account can execute and reach on the network.
As an interim measure, a web application firewall rule can block shell metacharacters on the affected parameters, but it can be bypassed and is not a fix. Confirm the fix by repeating the time-based and out-of-band tests against every affected parameter.
References
Add This Finding to Your Library
In Pental this write-up is in Settings, Library. Add to my library copies it into your own, rated in your firm's CVSS version, ready to put in your own words.
Free trialYour trial starts when your portal goes live, not when you sign up, so the time you spend setting up does not count. · No card required
Only need the reporting? Our reporting tool is free and open source, with the same library.