Skip to content

OS Command Injection via eval()

Description

Evaluating non-constant commands using the eval() function can lead to OS command injection, allowing an attacker to execute arbitrary system commands.

Examples

Insecure Code

php
eval($user_input);

Secure Code

php
$allowed_commands = ['ls', 'pwd']; if (in_array($user_input, $allowed_commands)) { exec($user_input); }

Remediation

Avoid using eval() with user-controlled input. Instead, use a whitelist of allowed commands or validate and sanitize the input.

Rule Details

FieldValue
IDCODE-0748
CategoryInjection
SeverityCRITICAL
CWECWE-78
ConfidenceHIGH
ImpactHIGH
LikelihoodMEDIUM
ExploitabilityEASY
Tagscommand injection, eval
OWASPA1:2017-Injection, A03:2021-Injection

References