Web & Application Security
OS Command Injection:
How It Happens & How to Prevent It
Why applications that pass user input into operating-system commands create high-impact risk, and the design choices that prevent it.
August 2026·6 min read

OS command injection occurs when an application invokes the operating system with a command string that still contains untrusted input. If that input can change the command, the application has given a remote user a path to the host — not just to application data.
The pattern appears in diagnostic tools, file converters, network utilities, and legacy integrations that shell out to the operating system instead of using a native library. It is less common than SQL injection in some portfolios, and more severe when it is present.
What command injection is
Many programming languages offer functions that run a system command and return the result. Those functions are appropriate when the command is fixed and arguments are passed as a separate list the operating system will not reinterpret. They become dangerous when the application builds a single string and asks a shell to parse it.
Shells treat certain characters as separators and operators. If user input is included in that string, the intended utility can be followed by additional commands the developer never wrote.
Root causes
- Concatenating user-supplied hostnames, filenames, or options into a shell command
- Calling a shell when a language API or library could perform the same task
- Trusting that client-side validation will keep unexpected characters out
- Running the application process with more operating-system privilege than the feature requires
Application architecture risks
Command injection is often an architecture problem, not only a coding mistake. Features that ping a host, convert a file, or trigger a system backup look harmless until they are wired directly to request parameters.
The risk increases when the web application, the command, and sensitive files share the same host and the same identity. A single unsafe call then inherits the privileges of the application service. On both Linux and Windows, that can mean access far beyond the original feature.
Potential impact
Successful command injection can disclose system information, read or write files the service can access, and in serious cases lead to further compromise of the host. The blast radius depends on how the process is isolated, which accounts it uses, and whether outbound network access is restricted.
That is why organisations should treat command injection as a high-severity class even when the original feature appears minor.
How penetration testers validate the vulnerability
Testers identify features that might invoke the operating system: network diagnostics, document processing, backup or administrative utilities, and integrations with local tools. They then determine whether input is passed as data or interpolated into a command line.
Validation focuses on evidence that the application executed something beyond the intended utility — for example, a change in timing, output, or error behaviour that can only be explained by command interpretation. The aim is to confirm the flaw and its impact, not to turn the finding into a public recipe for host takeover.
Secure command execution and application design
Prefer APIs over shells
If a language or library can perform the task, use it. Native DNS, file, or image APIs do not require a shell. When an external binary is unavoidable, invoke it with an argument array rather than a concatenated command string, so the operating system does not parse user input as shell syntax.
Input validation and allowlisting
Accept only values that match a strict allowlist: known hostnames, enumerated options, or tightly constrained identifiers. Reject everything else. Validation should happen on the server, independent of any client-side checks.
Least privilege
Run the application with the minimum operating-system rights the feature needs. Isolate command-running components. Restrict filesystem and network access so that a residual bug cannot become a full host compromise.
Command injection is prevented by design: do not let untrusted input near a shell, and do not give that shell more power than the business function requires.
Continue reading
Related Insights
Web & Application SecuritySQL Injection Explained: Risks, Detection & Prevention
Understanding how SQL injection vulnerabilities arise, how security teams validate them, and how organisations can reduce their exposure.
August 2026·8 min read
Read Article
Web & Application SecurityCross-Site Scripting (XSS): Understanding the Risk & How to Prevent It
How XSS appears in web applications, why it matters to users and organisations, and the controls that prevent untrusted content from running in the browser.
August 2026·7 min read
Read Article
Web & Application SecurityLocal File Inclusion: Risks, Detection & Prevention
How local file inclusion appears when applications use user-controlled paths, what testers look for, and how secure file handling prevents it.
August 2026·5 min read
Read ArticleNeed More Than an Insight?
Our security practitioners help organisations identify vulnerabilities, strengthen defences, investigate incidents, and build secure technology.