Command injection via exec functions
ID |
c.command_injection.command_injection_exec |
Severity |
critical |
Resource |
Command Injection |
Language |
C / C++ |
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
User-controlled input flows into command execution function at argument '$SINK', potentially allowing command injection. Validate and sanitize input, use allowlists for permitted commands, or do not pass untrusted input to the executable program name.
The following code illustrates a vulnerable pattern detected by this rule:
void vulnerable_execl(int argc, char *argv[]) {
// VULNERABLE: Command injection via exec functions
// User input from argv flows directly to execl
execl(argv[1], argv[1], NULL);
}
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.
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.