OS Command Injection

ID

php.command_injection

Severity

critical

Resource

Injection

Language

Php

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 PHP applications, command injection arises when unvalidated user inputs are incorporated into shell commands. Attackers exploit this vulnerability by injecting malicious input to manipulate the command being executed, potentially leading to unauthorized system access and control.

Consider the following example:

<?php
$userinput = $_GET['cmd']; // User input from GET parameter
$output = shell_exec("ls $userinput");
echo $output;
?>

In this case, the shell_exec function executes a shell command using user-provided input directly. If an attacker crafts a request like http://example.com?cmd=;rm -rf /, it could lead to the deletion of files on the server, demonstrating the severity of command injection vulnerabilities.

Remediation

To safeguard against command injection, it is essential to adopt a series of preventive measures:

  1. 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.

  2. 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.

  3. 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.

A sanitized version of the previous code would be like this:

<?php
$userinput = $_GET['cmd'];
$safeInput = escapeshellarg($userinput); // Properly escape user input
$output = shell_exec("ls $safeInput");
echo $output;
?>

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