2009-06-11 13 views
14

我正在编写我们公司使用的一个可怕的通信软件的web界面。该软件没有真正的用户界面,并要求我们给我们的系统提供腻子访问,以便我们的客户甚至可以提取数据。我的网络界面必须运行一个exec();函数,它必须传递用户输入的一些变量。使用用户插入变量来清理exec命令的最佳方法

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

现在,我想我也许可以刚刚从$command varible删除任何分号和是安全的,但我不知道,这就是为什么我们去住下个月之前我在这里冒充本。

什么是最好的消毒方法$command?有几个特殊的字符,我需要在变量[ ] < > ! # $

回答

17

请使用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()作为引用的一部分的情况下。然后它变得毫无用处(实际上将引号添加到混音中)。

一般来说,我们倾向于使用escapeshellcmd()加上我们自己的引号。

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

请安全!

+3

删除我的答案,因为显然我们在同一时间打字,但你给了体面的例子。链接手册:http://us.php.net/manual/en/function.escapeshellarg.php,http://us.php.net/manual/en/function.escapeshellcmd.php – 2009-06-11 19:06:29

相关问题