Our website uses cookies to enhance your browsing experience.
Accept
to the top
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
>
OS Command injection

OS Command injection

Nov 09 2021

OS Command injection is a type of an application attack. With this type of attack, hackers do not need authorization to execute OS commands on a machine.

Aside from injecting an entire OS command, an attacker may execute argument injection only. All these attacks may disrupt the system's work, disclose confidential data, etc.

If you want to protect your code from this attack, you should check the input data. The specific implementation of this check depends on the situation.

Vulnerabilities to such attacks belong to category A3:2021-Injection in the OWASP Top Ten 2021. Common Weakness Enumeration puts them on positions CWE-77, CWE-78, and CWE-88.

Example of a vulnerability

To make it clearer, we'll analyze this attack on the following example:

const string OperationsExecutorPath = "executor.exe";

private void HandleRequest(HttpRequest request)
{
  ....
  string operationIndex = request.QueryString["operationIndex"];
  Process.Start("cmd", $"/c {OperationsExecutorPath} {operationIndex}");

  ....
}

With the 'executor.exe' utility, the application performs various actions depending on the query content. The value of the 'operationIndex' parameter defines the specific operations. The application expects that we'll write a number to this parameter. This number will be passed to the command. For example, if 'operationIndex' equals 1, the application runs the following command:

cmd /c executor.exe 1

The problem with the above code is that the input value is not checked in any way. This makes the system vulnerable to command/argument injections. Instead of the expected numbers, an attacker can pass a value that leads to the execution of the malicious command chosen by them. For example, the following string can be passed as the 'operationIndex' parameter value:

0 & del /q /f /s .

Below is a command that is executed when the application calls Process.Start:

cmd /c executor.exe 0 & del /q /f /s .

The command interpreter (cmd) takes the '&' character as the command separator. First, the application calls utility 'executor.exe' that has argument '0'. Then the application calls the 'del' command. The 'del' command with these arguments deletes all the files in the current and nested directories (if the application has the sufficient file access rights). Obviously, the attacker can use any other command instead of 'del'. Thus, the correctly selected value of 'operationIndex' allows the attacker to execute arbitrary commands in the system where the application is running.

Command injection and argument injection

Command and argument injections are closely related. In many cases, the term 'command injection' is used for both types of attacks. The given example relates to both types of attack at once.

On the one hand, the attacker added command separator '&' to the passed value. Since the application hasn't checked the received string for the presence of this separator, after a normal call 'executor.exe ' an additional 'del' command will be executed. Attacks involving the use of command separators are classified as command injection. According to the Common Weakness Enumeration, such attacks belong to the CWE-78 category.

On the other hand, the application assumes that the string passed by the user is used as an argument (and both cmd and the 'executor.exe' utility at the same time). In the example above, the application does not check the string for the presence of argument separators (spaces in this case). Because of this, an attacker can pass additional arguments to cmd. These additional arguments lead to the execution of malicious commands. Attacks with using additional arguments are called argument injection. They correspond to CWE-88.

Both types of attacks can also be classified as CWE-77, which is the parent of CWE-78 and CWE-88.

Methods of protection

The main reason for the vulnerability to command/argument injections is the lack of correct validation of input data. This validation depends on the situation. The most common approach is to check the input data for command and argument separators. You also need to make sure that external data does not lead to the substitution of the original command.

As a rule, the most convenient option would be to use approaches relevant to a particular case. For example, if the input string must contain a number, then to protect against attacks, it is enough to check whether the user actually passed the number:

private void HandleRequest(HttpRequest request)
{
  ....
  string operationIndex = request.QueryString["operationIndex"];

  if (int.TryParse(operationIndex, out int index))
  {
    Process.Start("cmd", $"/c {OperationsExecutorPath} {index}");
  }
  else
  {
    // error handling
  }
  ....
}

You can use taint analysis to find such problems in code. This technology allows you to automatically track the places where insecure data may get into the methods of starting processes.

Additional links

Popular related articles


Comments (0)

Next comments next comments
close comment form