Pour obtenir une clé
d'essai remplissez le formulaire ci-dessous
Demandez des tariffs
Nouvelle licence
Renouvellement de licence
--Sélectionnez la devise--
USD
EUR
RUB
* En cliquant sur ce bouton, vous acceptez notre politique de confidentialité

Free PVS-Studio license for Microsoft MVP specialists
To get the licence for your open-source project, please fill out this form
** En cliquant sur ce bouton, vous acceptez notre politique de confidentialité.

I am interested to try it on the platforms:
** En cliquant sur ce bouton, vous acceptez notre politique de confidentialité.

Votre message a été envoyé.

Nous vous répondrons à


Si vous n'avez toujours pas reçu de réponse, vérifiez votre dossier
Spam/Junk et cliquez sur le bouton "Not Spam".
De cette façon, vous ne manquerez la réponse de notre équipe.

>
>
>
OS Command injection

OS Command injection

09 Nov 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

Comments (0)

Next comments
Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter