清理在 PHP 中构建操作系统命令的不受信任的输入


cleaning untrusted inputs that build os commands in PHP?

如何在 PHP 中删除从 URL 构建操作系统命令的不受信任的输入?

当我运行自动测试 zaproxy 时,我收到 P1 的警报,即您的输入正在构建 os 命令。所以我想知道如何清理这些命令。

使用 escapeshellarg()escapeshellcmd() 转义数据以用作 shell 命令或参数。

// escapes a single argument
// sample input: "/foo/bar/"
$argument = escapeshellarg($userInput1); 
exec("ls $argument");
// escapes all special characters like [];{} for usage in command line
// sample input: "ls -l; rm -rf /"
$command = escapeshellcmd($userInput2);
exec($command);

应同时使用这两个命令,以防止用户在服务器上执行任意逗号。

文档:

http://php.net/manual/en/function.escapeshellarg.phphttp://php.net/manual/en/function.escapeshellcmd.php