DVWA使用教程(Command Injection)(二)

DVWA使用教程(Command Injection)(二)

 

DVWA是一个用来练习Web渗透的PHP应用。共有十个模块,分别是

1.Brute Force(爆破)

2.Command Injection(命令注入)

3.CSRF(跨站请求伪造)

4.File Inclusion(文件包含)

5.File Uplod(文件上传)

6.Insecure CAPTCHA(不安全的验证码)

7.SQL Inj(SQL注入)

8.SQL B Inj(SQL盲注)

9.XSS-ref(反射型xss)

10.xss-stored(存储型xss)

 

一、    简介

命令注入(Command Injection)漏洞主要由函数参数过滤不严产生。用户的输入被带到系统命令中执行。

 

二、    功能特点

利用各种调用系统命令的web应用,通过命令拼接、绕过黑名单等方式实现在服务端实现想要实现的系统命令。

 

三、    各防护等级简介

low等级,对命令注入行为毫无设防。

medium等级,对命令注入行为防护不足,防护做法欠考虑。

hight等级,对命令注入行为有一定防护,但有疏忽。

impossible等级,对命令注入行为正确防护。

 

四、基础知识补充

命令区别(&、|、&&、||)

&起到连接命令的作用,不管前一条命令是否执行成功,下一条都会接着执行。

|起到管道的作用,前一条命令的输出,作为下一条命令的输入,只打印最后一条命令的执行结果。

&&和||比较常见,就不多说了。

 

五、low代码模块剖析

概述: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

    echo "<pre>{$cmd}</pre>";

}

?>

 

根据代码可以得知

1.isset函数用来检测变量是否设置,并且不能是 NULL。

2.用户可以完全控制该参数,传参时给Submit赋值即可满足条件继续执行。

3.IP地址输入框,没有经过任何的过滤和检查。

4.IP地址输入框将原封不动传递到ping语句中。

5.php_uname('a'),函数会返回运行 php 的操作系统的相关描述。

6.stristr函数搜索字符串,在另一字符串中的第一次出现,大小写不敏感。返回字符串的剩余部分(从匹配点),如果未找到所搜索的字符串,则返回false。

 

Ping 127.0.0.1&&net user执行结果:

 

六、medium代码模块剖析

概述: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>";

}

?>

根据代码可以得知

1.isset函数用来检测变量是否设置,并且不能是 NULL。

2.用户可以完全控制该参数,传参时给Submit赋值即可满足条件继续执行。

3.用户输入部分使用str_replace()函数过滤,过滤(&& , ;)字符。

4.过滤非法字符是一种黑名单策略,这种策略不安全。

 

执行结果:

Ping 127.0.0.1&net user

 

七、High代码模块剖析

概述:hight等级,对命令注入行为有一定防护,但有疏忽。

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

}

?>

 

根据代码可以得知

1.isset函数用来检测变量是否设置,并且不能是 NULL。

2.用户可以完全控制该参数,传参时给Submit赋值即可满足条件继续执行。

3.用户输入部分使用str_replace()函数过滤,过滤(&& , ;)字符。

4.过滤非法字符是一种黑名单策略,这种策略不安全,与上一个等级相比,仅是丰富了黑名单。

 

执行结果:

Ping 127.0.0.1|net user

 

 

 

八、Impossible代码模块剖析

 

概述: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();

?>

 

根据代码可以得知

1.isset函数用来检测变量是否设置,并且不能是 NULL。

2.用户可以完全控制该参数,传参时给Submit赋值即可满足条件继续执行。

3.用户输入部分使用stripslashes()函数过滤,函数删除字符串内所有反斜杠。

4.explode()函数以.为分割,把字符串切成数组。

5.is_numeric()检测string是否为数字或数字字符串。这样命令中仅能包含数字。

6.Generate Anti-CSRF token,生成反CSRF令牌。给命令盲注带来一定难度。

 

九、小结

有些不显示输出结果的命令,可以再拼接一条延时命令来校验是否成功执行。

比如:ping 127.0.0.1 -n 5>nul  或者使用远程请求来监测,远程命令有 wget和curl。

命令注入关键在绕过。黑名单策略不够安全,攻破只是时间问题。比如whoami命令可以拆成 who”“am”“i 如果攻击者脑洞够大,绕过的技巧五花八门。黑名单试图穷举攻击者的绕过手法,这显然不现实。正确的做法有1.对非法字符进行转义 2.对反斜杠进行过滤。

发布了96 篇原创文章 · 获赞 172 · 访问量 25万+

猜你喜欢

转载自blog.csdn.net/ai_64/article/details/92062825