DVWA—Command Injection(命令注入)

  命令注入攻击的常见模式为:仅仅需要输入数据的场合,却伴随着数据同时输入了恶意代码,

装载数据的系统对此并未设计良好的过滤过程,导致恶意代码也一并执行,最终导致信息泄露或者正常数据的破坏。

       PHP命令注入攻击漏洞是PHP应用程序中常见的脚本漏洞之一,国内著名的Web应用程序Discuz!、DedeCMS等都曾经存在过该类型漏洞。

现在开始我们的Command Injection

首先打开Command Injection,发现是一个执行ping的界面,推测若没有做好过滤,则很有可能执行其他DOS命令

DOS在一行中执行多条命令需使用的符号:

&&:只有当前面的命令执行成功才执行后面的命令

&:无论怎样执行后面的命令

||:只有当前面的命令执行失败才执行后面的命令

|:将前面命令执行的输出作为后面命令执行的输入

Low

Low级别的源码,未对用户输入ip做任何过滤

 可以看到,low级别的代码接收了用户输入的ip,然后根据服务器是否是Windows NT系统,对目标ip进行不同的ping测试。

但是这里对用户输入的ip并没有进行任何的过滤,所以我们可以进行命令执行漏洞

我们ping一个ip(随便一个能ping得通的就行,比如本机ip 127.0.0.1)

可以看到能ping通

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

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

 比如,我们执行 127.0.0.1 & net user xie /add ,尝试ping完后新建一个用户,我们就可以成功创建用户了。

使用&,查看系统用户

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

可以看到,medium级别的代码在low级别的代码上增加量了对 && 和 ;的过滤,

但并未过滤&,|,||

使用&,查看系统用户

输入” 127.0.0.1&;& net view ”时,也是可以的,因为过滤一次后相当于”127.0.0.1 && net view

High

仔细查看过滤代码发现”|”后面有个空格,因此当输入”127.0.0.1  |net view”,一样可以攻击,”|”是管道符,意思是将前者处理后的结果作为参数传给后者。

也可以使用一个不会执行的命令和|来执行命令

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进行了严格的限制,只有诸如“数字.数字.数字.数字”的输入才会被接收执行,因此不存在命令注入漏洞。

猜你喜欢

转载自www.cnblogs.com/escwq/p/12403541.html