命令注入(Command Injection),是指在某些需要输入数据的位置,还构造了恶意的代码破坏了原先的语句结构。而系统缺少有效的过滤,最终达到破坏数据、信息泄露甚至掌控电脑的目的。许多内容管理系统CMS存在命令注入漏洞。
解决乱码问题
在文本框中输入”ip address”后结果显示为乱码,如下图所示。
解决此问题的方法:在DVWA-master\dvwa\includes目录下找到dvwaPage.inc.php文件中所有的”charset=utf-8”,修改”charset=gb2312”,即可。
LOW级别
在文本框里输入”192.168.0.1 && net user”,得到以下系统中所有的用户
输入”192.168.0.1 && net user LUJIE”得到LUJIE账号的属性,当然还可以增删账号密码等等
代码
<?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>";
}
?>
审计代码可以发现直接针对操作系统类型进行ping,没有对输入的数据作出任何过滤,非常危险。
Medium级别
再次输入上面的&&攻击命令,发现提示参数错误,表明有过滤,但是它只针对了”&&”和”;”进行了过滤。
输入”192.168.0.1 & dir”时,同样可以攻击,表明没有对”&”过滤,”&&”和”&”是有区别的,”&&”是短路运算符,只有前一步执行成功才会执行后一步,而”&”则两个表达式都会执行。
输入”192.168.0.1 &;& net view”时,也是可以的,因为过滤一次后相当于”192.168.0.1&& net view”。
代码
<?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>";
}
?>
High级别
仔细查看代码发现”|”后面有个空格,因此当输入”127.0.0.1|net view”,一样可以攻击,”|”是管道符,意思是将前者处理后的结果作为参数传给后者。
代码
<?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>";
}
?>
代码中对许多敏感符号(”&”、”;”、”||”等)进行了过滤。
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();
?>
stripslashes()去除字符串中的反斜杠,反转义。
CSRF是跨站域请求伪造,加入Anti-CSRFtoken,服务器端对token进行验证,防止黑客利用保存用户验证信息的cookie来伪造请求。
输入数据会根据小数点进行拆分,拆分成四段再组合起来,恶意命令难以入侵。