0x00 Command Injection
因为本次学习DVWA靶机部署在windows下,底层系统是中文GBK编码,需要修改一处配置,不然会出现中文乱码。
在…/DVWA/dvwa/includes目录下,有个dvwaPage.inc.php文件,打开文件在277行修改,将UTF-8改为GBK或者GB2312即可。
Command Injection,即命令注入,是指通过提交恶意构造的参数破坏命令语句结构,从而达到执行恶意命令的目的。PHP命令注入攻击漏洞是PHP应用程序中常见的脚本漏洞之一,国内著名的Web应用程序Discuz!、DedeCMS等都曾经存在过该类型漏洞。
DVWA中是输入IP地址,然后后台会返回ping测的结果,可以输入地址后加上&&、;等连接下一个命令来触发命令执行漏洞。
0x01 Low
源码分析
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// Get input
$target = $_REQUEST[ 'ip' ];
// Determine OS and execute the ping command.
if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
// Windows
$cmd = shell_exec( 'ping ' . $target );
}
else {
// *nix
$cmd = shell_exec( 'ping -c 4 ' . $target );
}
// Feedback for the end user
echo "<pre>{$cmd}</pre>";
}
?>
这里包含两个知识点:
- stristr(a, b)函数,是在a字符串中搜索b字符串内容,如果搜索得到就返回字符串a除了b剩余的部分,否则返回False。
- php_uname()函数,有这些选项,输入“s ”(返回操作系统名称),“n”(返回主机名),“r”(返回版本名称),“v”(返回版本信息), “m”(返回机器类型)。
这里就是判断是windows系统还是Unix系统(包括Linux),然后执行ping命令。可以看出代码并未对命令执行漏洞输入内容进行任何过滤。
解题思路
注入代码:
127.0.0.1&&dir
然后就会变成
ping 127.0.0.1 && dir
前面的命令执行为真时会执行后面的命令,然后将结果一起返回。ping自己一定为真,所以可以利用这个执行后面的系统命令。
0x02 Medium
源码分析
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// Get input
$target = $_REQUEST[ 'ip' ];
// Set blacklist
$substitutions = array(
'&&' => '',
';' => '',
);
// Remove any of the charactars in the array (blacklist).
$target = str_replace( array_keys( $substitutions ), $substitutions, $target );
// Determine OS and execute the ping command.
if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
// Windows
$cmd = shell_exec( 'ping ' . $target );
}
else {
// *nix
$cmd = shell_exec( 'ping -c 4 ' . $target );
}
// Feedback for the end user
echo "<pre>{$cmd}</pre>";
}
?>
这里比Low等级多了个黑名单过滤机制,过滤了&&和;
解题思路
但是依然可以用&或|或||进行测试,只不过|和||这两个需要前面的命令失败才行。 注入代码:
127.0.0.1 & dir
127.0.0.1 &;& dir
111 | dir
111 || dir
这些会变成
ping 127.0.0.1 & dir
ping 127.0.0.1 && dir
ping 111 | dir
ping 111 || dir
第一个命令同Low等级一样,&前的命令为真才会返回后面的执行结果。
第二个命令会去掉;,去掉之后正好形成&&,达到命令指向效果。
第三个命令是要使前面为假,即ping不成功,后面的命令才会执行。
第三个命令也是要使前面为假,后面的命令才会执行。
0x03 High
源码分析
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// Get input
$target = trim($_REQUEST[ 'ip' ]);
// Set blacklist
$substitutions = array(
'&' => '',
';' => '',
'| ' => '',
'-' => '',
'$' => '',
'(' => '',
')' => '',
'`' => '',
'||' => '',
);
// Remove any of the charactars in the array (blacklist).
$target = str_replace( array_keys( $substitutions ), $substitutions, $target );
// Determine OS and execute the ping command.
if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
// Windows
$cmd = shell_exec( 'ping ' . $target );
}
else {
// *nix
$cmd = shell_exec( 'ping -c 4 ' . $target );
}
// Feedback for the end user
echo "<pre>{$cmd}</pre>";
}
?>
黑名单过滤了更多东西:’&’,’;’,’| ‘, ‘-‘,’$’,’(‘,’)’,’`‘,’||’,注意这里的’| ‘是带空格的,所以就是漏洞所在。
解题思路
可以利用|连接命令,但要注意|后面不要带空格。|只会打印后面的结果,不管前面是真是假。 注入代码:
111|dir
就会变成
ping 111|dir
这样就可以执行并返回后面的命令执行结果了。
0x04 Impossible
源码分析
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// Check Anti-CSRF token
checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
// Get input
$target = $_REQUEST[ 'ip' ];
$target = stripslashes( $target );
// Split the IP into 4 octects
$octet = explode( ".", $target );
// Check IF each octet is an integer
if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {
// If all 4 octets are int's put the IP back together.
$target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];
// Determine OS and execute the ping command.
if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
// Windows
$cmd = shell_exec( 'ping ' . $target );
}
else {
// *nix
$cmd = shell_exec( 'ping -c 4 ' . $target );
}
// Feedback for the end user
echo "<pre>{$cmd}</pre>";
}
else {
// Ops. Let the user name theres a mistake
echo '<pre>ERROR: You have entered an invalid IP.</pre>';
}
}
// Generate Anti-CSRF token
generateSessionToken();
?>
先是Anti-CSRF token检验,然后进行stripslashes去除反斜杠\,接着explode根据.号切割成四个数字字符串,用is_numeric校验,也就是必须为127.0.0.1这种四个十进制用点号连接的字符串输入才行。所以不存在黑名单过滤漏洞了,无法利用命令执行漏洞注入命令。
解题思路
无。
0x05 小结
防御方法:
- 严格限制输入,如访问ip类的就是用正则完全匹配格式。
- 类型校验,如果是输入部分是整数类型等,可以用is_numeric等强制转换后检验。
-
黑名单过滤掉常见命令执行关键字,如“ ”、“&”等。
知识扩展:
- command1&command2 command1为真才会执行并只返回command2的结果
- command1&&command2 command1为真才会执行command2,然后返回全部结果
-
command1 command2 不管command1真假都会执行并只返回command2的结果 -
command1 command2 command1为假才会执行command2,然后返回全部结果 - command1;command2 都执行都输出