OS Command Injection

OS Command injection

What is OS Command Injection

OS Command Injection (Operating System) is a type of vulnerability that arises when an application allows a user to provide input that is then executed as a command by the underlying operating system. In simpler terms, it is a type of attack where an attacker can execute arbitrary commands on a vulnerable system by injecting malicious code into an application.

It can be extremely dangerous as they allow an attacker to gain control over the targeted system and perform actions such as stealing sensitive data, modifying system settings, or even bringing down the entire system. In this blog post, we will dive deeper into OS Injection attacks, how they work, and how to prevent them.

How OS Command Injection Works

OS Command Injection attacks occur when an application does not properly validate user input before passing it on to the underlying operating system. This allows an attacker to inject malicious commands into the application, which are then executed as if they were legitimate commands. In some cases, an attacker may be able to provide additional command-line arguments or parameters to further manipulate the system.

Code Injection Vs Command Injection

Code injection and command injection are both types of injection attacks, but they differ in their targets and methods.

Code injection is an attack where an attacker injects code into an application that is then executed by the application itself. The attacker’s goal is to take control of the application and execute arbitrary code on the target system. Code injection can be performed through various means, such as SQL injection, cross-site scripting (XSS), and remote code execution (RCE) vulnerabilities.

Command injection, on the other hand, is an attack where an attacker injects malicious commands into an application that are then executed by the underlying operating system. The attacker’s goal is to execute arbitrary commands on the target system and potentially gain control of the entire system. Command injection can occur through various input vectors, such as web forms, command-line interfaces, and file uploads.

Command Injection Examples and Attacks Scenario


To understand this,we will try to solve the portswigger lab named-

OS injection, simple case:

This lab contains an OS injection vulnerability in the product stock checker

The program runs a shell command with input from the user for the product and store IDs and responds with the command’s output.

  • Click on Check stock and intercept the request using burpsuite.
  • Send the captured request to the repeater tab.
  • Modify the storeID parameter, insert the value 1|whoami
  • You will get the current user name in the response.

How to prevent OS Command Injection


Preventing OS command injection requires a combination of secure coding practices and proper input validation. Here are some best practices to prevent OS command injection:

  • Validate all user input: All user input should be validated before it is passed to an operating system command. This includes input from web forms, file uploads, and command-line arguments.
  • Use parameterized commands: Parameterized commands can be used to separate user input from the operating system command. This ensures that user input is not executed as part of the command. Instead, it is treated as data that is passed to the command.
  • Use input sanitization: Input sanitization can be used to remove any special characters or commands from user input. This ensures that only valid data is passed to the operating system command.
  • Use a whitelist approach: A whitelist approach can be used to only allow certain commands or characters to be used in user input. This ensures that only valid data is passed to the operating system command.
  • Limit user privileges: Limiting user privileges can help to reduce the impact of an OS command injection attack. By limiting user privileges, attackers will have a more difficult time executing malicious commands.

Lokesh Yadav

(Cyber Securtiy Intern

Table of Contents

Social Media