江边闲话集

08/09/2017

Web安全- Command Injection

Filed under: 一技之长 — 张太国 @ 10:58

Command Injection,是指Web App调用系统命令去实现某些功能,同时因为不安全的web请求数据(例如Form,Cookie,HTTP header等)利用该功能造成一些安全影响。

实例

下面的例子便于理解。

本例子来源https://www.owasp.org/index.php/Command_Injection

下面的PHP代码会引起Command Injection:

<?php
print("Please specify the name of the file to delete");
print("<p>");
$file=$_GET['filename'];
system("rm $file");
?>

我们看看请求和返回。

请求

http://127.0.0.1/delete.php?filename=bob.txt;id

响应

Please specify the name of the file to delete

uid=33(www-data) gid=33(www-data) groups=33(www-data) 

我们可以看到,如果执行web的function id足够大的话,是极有可能把不应该的数据删除了,例如filename为/,同时我们也可以看到该id所在的group。

解决方案

可以尽量使用API。例如上面要删除一个文件,可以使用php语言的api去删除文件,而不是调用OS的rm命令。

如果没有相关的API去调用,一定要调用这些command,务必做好命令的验证。就个人经验看,验证允许的command比验证不允许的command要简单一些。

Powered by WordPress