OS Command Injection: Examples & Prevention | Vooki Pro

OS command injection: how it works and how to prevent it

Command injection is an attack in which the attacker runs arbitrary operating-system commands on the server through a vulnerable application.

It is possible when an application passes unsafe user-supplied data, from forms, cookies or HTTP headers, to a system shell. The exact syntax depends on the server's operating system, such as Linux or Windows.

See it in action

This demonstration uses the Damn Vulnerable Web Application (DVWA), a deliberately vulnerable application for practice, hosted on Windows. You can get DVWA from github.com/digininja/DVWA. Only test applications you own or are authorised to test.

Examples of input used in the demonstration, where a second command is appended to an expected IP address:

  • 127.0.0.1 && dir
  • 127.0.0.1 | dir
  • 127.0.0.1 & calc

Prevention

  • Avoid calling operating-system commands from application code. Use the language's built-in libraries instead, for example a network library rather than the ping command.
  • If a command is unavoidable, pass the arguments as a list without invoking a shell, so that characters such as &, | and ; have no special meaning.
  • Validate input against a strict allow-list, such as a valid IP address format. Blocking a few characters like && and | is not enough, because there are many ways around a block-list.
  • Run the application with the least operating-system privileges it needs.

How Vooki Pro helps

  • Tests inputs for OS command injection.
  • Also covers code injection, server-side template injection and CVE-class issues such as Shellshock.
  • Gives step-by-step reproduction and remediation for each finding.

Frequently asked questions

How do I prevent command injection?

Avoid calling the shell with user input. Use library functions or pass arguments as a list without a shell, and allow-list any values you must accept.

Is escaping enough?

Escaping is error-prone. Removing the shell from the code path is far safer.

Try Vooki Pro free for 7 days

Find command injection before it becomes a server takeover. The trial unlocks every feature for 7 days, and scanning runs on your own machine.

Get Vooki Pro →  ·  How to perform security testing (DAST)

Related: Server-side template injection (SSTI) · Insecure deserialization

    • Related Articles

    • Blind SQL injection: how it works and how to stop it

      SQL injection happens when user input changes the meaning of a database query. In blind SQL injection the application never shows the database error or the query result, yet an attacker can still extract data one piece at a time by watching how the ...
    • Error-based SQL injection

      SQL injection lets an attacker change a database query by supplying crafted input. There are three common techniques: union-based, error-based and blind SQL injection. This article covers the error-based technique. How it works A common first test ...
    • Common web application attacks and quick steps to prevent them

      The attacks that make the news are usually the largest or most sophisticated ones, but web applications of every size are attacked every day. The good news is that a small number of well-known attack types account for most incidents, and each has ...
    • XML External Entity (XXE) injection

      XML External Entity (XXE) injection is an attack against applications that parse XML input. If the XML parser is configured to process external entities, an attacker can make it read local files, send requests to internal systems, or exhaust server ...
    • NoSQL injection: risks for MongoDB and other document databases

      NoSQL databases such as MongoDB do not use SQL, but they are still vulnerable to injection. NoSQL injection happens when user input is placed into a database query object or operator, changing what the query does. Many teams assume that moving away ...