OS Command Injection
ID |
python.command_injection |
Severity |
critical |
Resource |
Injection |
Language |
Python |
Tags |
CWE:77, CWE:78, NIST.SP.800-53, OWASP:2021:A3, PCI-DSS:6.5.1 |
Description
Improper neutralization of special elements used in a command ('Command Injection').
Command injection vulnerabilities occur when an application passes untrusted input to a system shell command without proper validation or sanitization.
Such vulnerabilities allow attackers to execute arbitrary shell commands with the privileges of the user running the application, which may result in complete system compromise.
Attackers exploiting the vulnerability can then install a reverse shell, download and install malware or ransomware, cryptocurrency miners, run database clients for data exfiltration, etc.
Understanding and mitigating this risk is crucial, as it can facilitate data breaches, unauthorized data manipulation, or any type of attack that could be crafted via system commands.
Rationale
In Python, command injection vulnerabilities typically stem from using functions like os.system()
, subprocess.call()
, or subprocess.Popen()
with unsanitized user input. This can allow an attacker to execute harmful system commands.
Consider the following vulnerable Python code:
import os
def execute_command(user_input):
os.system("echo " + user_input)
execute_command("Hello, World!")
In this example, if an attacker provides malicious input, it can lead to executing arbitrary commands, such as:
Remediation
To safeguard against command injection, it is essential to adopt a series of preventive measures:
-
Avoid Direct Command Execution with Untrusted Input: Avoid using functions for executing shell commands with untrusted inputs. Where command execution is necessary, parameterize inputs as separate command-line arguments, and do not concatenate untrusted inputs into shell commands.
-
Input Validation and Whitelisting: Perform rigorous input validation to ensure that the input conforms to expected and safe formats. Whitelisting valid input patterns is preferable over blacklisting potentially harmful inputs.
-
Escape Shell Metacharacters: If the input must be included in a command, ensure any shell metacharacters are properly blacklisted, escaped or sanitized using a dedicated library.
Characters in
{ } ( ) < > & * ‘ | = ? ; [ ] ^ $ – # ~ ! . ” % / \ : + , \`
are shell metacharacters for most OSes and shells.This is difficult to do well, and attackers have many ways of bypassing them so it is not recommended. You have been warned !
By implementing these practices, you can significantly minimize the potential for command injection vulnerabilities, enhancing the application’s resistance to this type of attack.
Configuration
The detector has the following configurable parameters:
-
sources
, that indicates the source kinds to check. -
neutralizations
, that indicates the neutralization kinds to check.
Unless you need to change the default behavior, you typically do not need to configure this detector.
References
-
CWE-77: Improper Neutralization of Special Elements used in a Command ('Command Injection').
-
CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection').
-
OWASP Top 10 2021 - A03 : Injection.
-
OWASP Cheat Sheet Series: OS Command Injection Defense.
-
PortSwigger Web Security Academy: OS command injection.
-
CISA Secure by Design Alert: Eliminating OS Command Injection Vulnerabilities.
-
CAPEC-88 : OS Command Injection.