php system执行shell,php命令执行函数–shell_exec()、passthru()、exec()、system()与防止命令执行漏洞函数...

一、原型

1、shell_exec()

string shell_exec ( string $cmd )     执行命令,并将结果作为字符串返回。

返回值:如果执行失败,则返回NULL。执行成功则返回执行结果字符串。

注意:This function is disabled when PHP is running in safe mode

2、passthru()

void passthru ( string $command [, int &$return_var ] )

没有返回值,函数直接将执行结果返回给浏览器。函数第二个参数就是执行状态码:返回0表示成功,返回1表示失败。

3、exec()

string exec(string command, string [array], int [return_var]);

command – 需要执行的命令

array –  是输出值填充的数组(每一行作为数组的一项)

return_var–是返回值0或1,如果返回0则执行成功,返回1则执行失败。

执行不成功时的方案:一个技巧就是使用管道命令, 使用 2>&1, 命令就会输出shell执行时的错误到$output变量, 输出该变量即可分析。

如:exec(‘convert a.jpg b.jpg’, $output, $return_val);改为:exec(‘convert a.jpg b.jpg  2>&1’, $output, $return_val); print_r($output);

4、system()

string system ( string $command [, int &$return_var ] )

return_var :命令执行状态码。返回0表示成功,返回1表示失败。

返回值:返回执行结果的最后一行,如果失败返回FALSE.

二、区别

1、shell_exec() 只返回,不输出。

2、passthru()  只输出,不返回。     有状态码

3、exec()  返回最后一行结果,所有结果可以保存到一个返回的数组里面。有状态码。

4、system()   输出返回最后一行结果。  有状态码

三、代码示例

1、shell_exec()

$output = shell_exec('ls -lart');

echo "

$output
";

?>

2、passthru()

passthru ('echo $PATH');

?>

3、exec()

$cmd = exec( ‘ping  ‘ . $target ,$result,$code);

$html .= ‘

’.$cmd.'
’;

if ($code)

{

$html .=’shibai’;

}

else

{

$html .=’chenggong’;

}

4、system()

echo '

';// Outputs all the result of shellcommand “ls”, and returns

// the last output line into $last_line. Stores the return value

// of the shell command in $retval.

$last_line = system(‘ls’, $retval);// Printing additional info

echo ‘


Last line of the output: ‘ . $last_line . ‘
Return value: ‘ . $retval;

?>

四、防止命令执行漏洞函数

存在命令执行漏洞的原因是,未对输入命令进行过滤,导致可以shell命令的组合运行(即同时运行多个shell命令)。

shell命令的组合运行主要有一下几种(Linux下,windows下可能不适用):管道操作、重定向、逻辑分隔。

(1)管道操作:将一端的命令输出交给另一端的命令处理。格式:    命令1  |  命令2

如:ps aux | grep httpd

(2) 重定向:改变执行命令时的默认输入输出。

类型                               操作符                             用途

重定向输入                     

重定向输出                     >   或>>                      将输出结果覆盖、追加到指定文件(>覆盖、>>追加)

重定向标准错误输出     2>或 2>>                 将错误信息覆盖或追加到指定文件

重定向混合输出             &> 或 &>>               将标准输出和错误信息覆盖或追加到指定文件

(3)逻辑分割:处理多条命令之间的逻辑关系

逻辑与                      &&      两条命令都要执行

逻辑或                     ||           若第一条命令执行成功,则不执行第二条命令(即只要有一条命令成功就不再继续执行命令)

顺序执行               ;              执行完第一条命令后执行第二条命令

—————————————————————————————————————————–

1、escapeshellcmd()

string escapeshellcmd ( string $command )

escapeshellcmd() escapes any characters in a string that might be used to trick a shell command into executing arbitrary commands. This function should be used to make sure that any data coming from user input is escaped before this data is passed to the exec() or system() functions, or to the backtick operator.

Following characters are preceded by a backslash: #&;`|*?~<>^()[]{}$\, \x0A and \xFF. ‘ and “ are escaped only if they are not paired. In Windows, all these characters plus % are replaced by a space instead.

// We allow arbitrary number of arguments intentionally here.

$command = './configure '.$_POST['configure_options'];$escaped_command = escapeshellcmd($command);system($escaped_command);

?>

2、escapeshellarg()

string escapeshellarg ( string $arg )

escapeshellarg() adds single quotes around a string and quotes/escapes any existing single quotes allowing you to pass a string directly to a shell function and having it be treated as a single safe argument. This function should be used to escape individual arguments to shell functions coming from user input. The shell functions include exec(), system() and the backtick operator.

system('ls '.escapeshellarg($dir));

?>

escapeshellarg()函数是为了从用作系统命令的参数的用户输入中移出含有潜在危险的字符而设计的。执行时,这个函数将在字符两边添加单引号,并转义原来字符串中的单引号

说明:escapeshellarg这个函数,它给整个参数加上引号并且escape所有需要的字符,让整个参数正确包含在一对引号中间。而 escapeshellcmd只是escape了几个必要的字符(|>之类的),并不能保证参数本身也是正确的,从这个意义上来说 escapeshellarg更安全一些。

escapeshellcmd()和escapeshellarg()类似,只是它只转义对底层操作系统有特殊意义的字符。和 escapeshellarg()不同,escapeshellcmd()不会处理内容中的空白格。举个实例,当使用escapeshellcmd()转 义时,字符$string = “‘hello, world!’;evilcommand”将变为:’hello, world’;evilcommand。如果这个字符串用作系统调用的参数它将仍然不能得到正确的结果,因为shell将会把它分别解释为两个分离的参 数: ‘hello 和 world’;evilcommand。如果用户输入用于系统调用的参数列表部分,escapeshellarg()是一个更好的选择。

总结:一般命令语句用escapeshellcmd()来过滤,而命令参数用escapeshellarg()来进行过滤。

五、其他参考文献

1、http://www.webzhishi.com/aboutphp-exec/       exec()函数用法

2、http://leeon.me/a/compare-php-system-executing-command-system-exec-passthru

3、http://wenku.baidu.com/view/9b6fc50979563c1ec5da71d2.html

4、http://developer.51cto.com/art/200911/163774.htm     shell_exec()函数说明

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值