16

我正在为我们公司使用的一个可怕的软件编写一个 Web 界面。该软件没有真正的用户界面,并且要求我们授予 putty 访问我们系统的权限,以便我们的客户甚至提取数据。我的 Web 界面必须运行一个exec();函数,并且它必须传递一些用户输入的变量。

$command = "report-call '$type' '$study' '$server' '$tag' '$specopt1' '$specopt2' '$specopt3' '$specopt4'";
$last_line = exec($command, $output, $returnvalue); 

现在我假设我可以从$command变量中删除任何分号并且是安全的,但我不确定,这就是为什么我在下个月上线之前在这里摆这个姿势。

最好的消毒方法是$command什么?我确实需要在变量中包含一些特殊字符 [ ] < > ! # $

4

1 回答 1

22

为此目的使用 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()其用作 PART OF A QUOTED 参数。然后它变得无用(实际上是在混合中添加引号)。

一般来说,我们更喜欢使用escapeshellcmd()我们自己添加的引号。

$cmd = 
    "/usr/bin/do-something '" . 
    escapeshellcmd($arg1) . 
    "' '" . 
    escapeshellcmd($arg2) . 
    "'";

注意安全!

于 2009-06-11T18:57:46.343 回答