DVWA之命令注入漏洞(Command injection)

目录

LOW

medium

high

Impossible


命令执行漏洞的原理:在操作系统中,“&、|、||”都可以作为命令连接符使用,用户通过浏览器提交执行命令,由于服务器端没有针对执行函数做过滤,将用户的输入作为系统命令的参数拼接到命令行中,在没有过滤用户输入的情况下,造成命令执行(注入)漏洞。

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
	$html .= "<pre>{$cmd}</pre>";
}

?>

       可以看到,low级别的代码接收了用户输入的ip,然后根据服务器是否是Windows NT系统,对目标ip进行不同的ping测试。但是这里对用户输入的ip并没有进行任何的过滤,所以我们可以进行命令执行漏洞

这里ping一下真实机的ip

如果这里出现了乱码的情况,解决办法:在DVWA-master\dvwa\includes目录下找到dvwaPage.inc.php文件中所有的”charset=utf-8”,修改”charset=gb2312”,即可。

       接着尝试输入,192.168.43.52|ipconfig,在操作系统中,"  &  、&& 、|  、 ||   "都可以作为命令连接符使用,我们在ping完后再执行ipconfig 命令查看ip信息

可以看到,成功执行。然后我们就可以继续执行我们的命令了。把ipconfig换成其他的系统命令

这里我们可以尝试着进行添加一个系统的账号df,这里因为没有防护软件所以直接添加成功了

192.168.43.52&net user df 123456qwer.. /add

medium

查看源码可知,在低级的基础上增加了对 && 和 ; 的过滤,我们直接不使用 &&和;就好了

这里使用 &,&&和&的区别在于,&&是执行完前面的命令然后执行后面的命令,&是不管前面的命令是否值执行,后面的都执行

high

可以看到,High级别的代码进行了黑名单过滤,把一些常见的命令连接符给过滤了。黑名单过滤看似安全,但是如果黑名单不全是话,是很容易进行绕过的。我们仔细看黑名单过滤中的|,我们发现,'| ' 后面有个空格,因为我们只要输入|后面不跟空格一样可以绕过

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(string) : 该函数会删除字符串string中的反斜杠,返回已剥离反斜杠的字符串。

explode(separator,string,limit): 该函数把字符串打散为数组,返回字符串的数组。参数separator规定在哪里分割字符串,参数string是要分割的字符串,可选参数limit规定所返回的数组元素的数目。

is_numeric(string): 该检测string是否为数字或数字字符串,如果是返回TRUE,否则返回FALSE。

可以看到,Impossible级别的代码加入了Anti-CSRF token,同时对参数ip进行了严格的限制,只有诸如“数字.数字.数字.数字”的输入才会被接收执行,因此不存在命令注入漏洞。
 

——心,若没有栖息的地方,到哪都是流浪

猜你喜欢

转载自blog.csdn.net/qq_44159028/article/details/114642831
今日推荐