To get a trial key
fill out the form below
Team License (a basic version)
Enterprise License (an extended version)
* By clicking this button you agree to our Privacy Policy statement

Request our prices
New License
License Renewal
--Select currency--
* By clicking this button you agree to our Privacy Policy statement

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

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

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

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}");
    // 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
The way static analyzers fight against false positives, and why they do it

Date: Mar 20 2017

Author: Andrey Karpov

In my previous article I wrote that I don't like the approach of evaluating the efficiency of static analyzers with the help of synthetic tests. In that article, I give the example of a code fragment…
The Ultimate Question of Programming, Refactoring, and Everything

Date: Apr 14 2016

Author: Andrey Karpov

Yes, you've guessed correctly - the answer is "42". In this article you will find 42 recommendations about coding in C++ that can help a programmer avoid a lot of errors, save time and effort. The au…
Static analysis as part of the development process in Unreal Engine

Date: Jun 27 2017

Author: Andrey Karpov

Unreal Engine continues to develop as new code is added and previously written code is changed. What is the inevitable consequence of ongoing development in a project? The emergence of new bugs in th…
The Last Line Effect

Date: May 31 2014

Author: Andrey Karpov

I have studied many errors caused by the use of the Copy-Paste method, and can assure you that programmers most often tend to make mistakes in the last fragment of a homogeneous code block. I have ne…
Characteristics of PVS-Studio Analyzer by the Example of EFL Core Libraries, 10-15% of False Positives

Date: Jul 31 2017

Author: Andrey Karpov

After I wrote quite a big article about the analysis of the Tizen OS code, I received a large number of questions concerning the percentage of false positives and the density of errors (how many erro…
Free PVS-Studio for those who develops open source projects

Date: Dec 22 2018

Author: Andrey Karpov

On the New 2019 year's eve, a PVS-Studio team decided to make a nice gift for all contributors of open-source projects hosted on GitHub, GitLab or Bitbucket. They are given free usage of PVS-Studio s…
PVS-Studio for Java

Date: Jan 17 2019

Author: Andrey Karpov

In the seventh version of the PVS-Studio static analyzer, we added support of the Java language. It's time for a brief story of how we've started making support of the Java language, how far we've co…
The Evil within the Comparison Functions

Date: May 19 2017

Author: Andrey Karpov

Perhaps, readers remember my article titled "Last line effect". It describes a pattern I've once noticed: in most cases programmers make an error in the last line of similar text blocks. Now I want t…
How PVS-Studio Proved to Be More Attentive Than Three and a Half Programmers

Date: Oct 22 2018

Author: Andrey Karpov

Just like other static analyzers, PVS-Studio often produces false positives. What you are about to read is a short story where I'll tell you how PVS-Studio proved, just one more time, to be more atte…
PVS-Studio ROI

Date: Jan 30 2019

Author: Andrey Karpov

Occasionally, we're asked a question, what monetary value the company will receive from using PVS-Studio. We decided to draw up a response in the form of an article and provide tables, which will sho…

Comments (0)

Next comments
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →