我正在为我们公司使用的一个可怕的软件编写一个Web界面。该软件没有真正的用户界面,需要我们为我们的客户提供putty访问我们的系统甚至提取数据。我的Web界面必须运行exec();
函数,并且必须传递用户输入的一些变量。
$command = "report-call '$type' '$study' '$server' '$tag' '$specopt1' '$specopt2' '$specopt3' '$specopt4'";
$last_line = exec($command, $output, $returnvalue);
现在我假设我可能只是删除$command
变量中的任何分号并且是安全的,但我不确定,这就是为什么我在下个月上线之前在这里摆出这个。< / p>
消毒$command
的最佳方法是什么?我需要在变量[ ] < > ! # $
中添加一些特殊字符。
答案 0 :(得分:21)
使用PHP具有的功能:
$cmd =
"/usr/bin/do-something " .
escapeshellarg($arg1) .
' ' .
escapeshellarg($arg2);
您也可以使用escapeshellcmd()
有什么区别?
escapeshellarg()
仅在字符串周围添加',然后在任何其他'字符之前添加\。
http://www.php.net/escapeshellarg
escapeshellcmd()
转义所有对shell敏感的字符($,\等等),但不添加引号。
http://www.php.net/manual/en/function.escapeshellcmd.php
如果您使用escapeshellarg()
作为QUOTED参数的一部分,则会出现问题。然后它变得无用(实际上在混音中添加引号)。
一般来说,我们更倾向于使用escapeshellcmd()
添加我们自己的引号。
$cmd =
"/usr/bin/do-something '" .
escapeshellcmd($arg1) .
"' '" .
escapeshellcmd($arg2) .
"'";
安全!