OS command injection (ARCH7102)
Ce contenu n’est pas encore disponible dans votre langue.
Key php.security.os-command-injection · Severity error · Category security · CWE CWE-78
Building a command string from user input and passing it to a shell lets attackers chain extra commands via shell metacharacters. Arguments must be escaped or the shell avoided.
Incorrect
Section titled “Incorrect”<?php$host = $_GET['host'];$output = shell_exec("ping -c 1 $host");echo $output;Correct
Section titled “Correct”<?php$host = $_GET['host'];$cmd = 'ping -c 1 ' . escapeshellarg($host);$output = shell_exec($cmd);echo htmlspecialchars($output);Escape arguments with escapeshellarg() or validate against an allowlist instead of passing input straight to a shell.