DVWA Command Injection 攻略
●初级
进行黑盒测试一下:
127.0.0.1
返回了ping命令结果,从TTL值来看是windows系统。
尝试执行多条命令
127.0.0.1&whoami
查看源代码:
<?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>";
}
?>
代码中存在PHP危险函数shell_exec(),且代码中没有进行任何的过滤,存在RCE漏洞
●中级
127.0.0.1&whoami
查看源代码:
<?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>";
}
?>
代码中存在PHP危险函数shell_exec(),代码中又设置了黑名单,过滤了&&和;,但是可以绕过黑名单,依旧存在RCE漏洞
●高级
127.0.0.1|whoami
查看源代码:
<?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
$html .= "<pre>{$cmd}</pre>";
}
?>
代码中存在PHP危险函数shell_exec(),代码中又设置了黑名单,过滤了很多符号,但是还可以使用|绕过黑名单,依旧存在RCE漏洞
●完全防御难度分析
代码分析:
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// 检查防范CSRF的令牌
checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
// input
$target = $_REQUEST[ 'ip' ];
$target = stripslashes( $target ); //去除反斜杠
$octet = explode( ".", $target );
//explode()函数使用一个字符串分割另一个字符串,并返回由字符串组成的数组
// 将IP分割为4个八进制
// 检查每个八进制是否为整数
if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {
//is_numeric()函数用于判断括号内的值是否为数字或数字字符串,如果是返回true否则返回false
//同时sizeof()函数判断分割的数组是否为4
// 如果4个八进制都是整数,把IP放回去
$target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];
// 判断操作系统,执行ping命令
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. 让用户知道有个错误
echo '<pre>ERROR: You have entered an invalid IP.</pre>';
}
}
// 生成防范CSRF的令牌
generateSessionToken();
?>
代码审计:
1、这里增加了令牌检查,防范CSRF(跨站请求伪造),同时对参数使用了stripslashes( )函数去除了反斜杠
2、使用explode()函数将IP根据"."分块,然后is_numeric()函数检查每一块是否为数字或数字字符串,限制了其他字符,同时还有sizeof()函数确定是否分割的数组只有4块,导致注入的不可能性
相关函数说明:
stripslashes( ), 删除反斜杠
addslashes() 函数,返回在预定义字符(单引号,双引号,反斜杠)之前添加反斜杠的字符串。
explode()函数,把字符串分割为数组。
is_numeric()函数,用于检测变量是否为数字或数字字符串。返回值为布尔类型。