DVWA靶场-Command Injection 命令注入

往期博文:

DVWA靶场-Brute Force Source 暴力破解

靶场环境搭建

https://github.com/ethicalhack3r/DVWA

[网络安全学习篇附]:DVWA 靶场搭建

目录

 

Command Injection 命令注入

Low Command Injection

核心代码

Medium Command Injection

核心代码

High Command Injection

核心代码

Impossible Command Injection

核心代码


Command Injection 命令注入

Low Command Injection

核心代码

<?php

 

if( isset( $_POST'Submit' ]  ) ) {

    // get 方式获取ip

    $target $_REQUEST'ip' ];

 

    // 判断操作系统来指定相应的ping 命令

    if( stristrphp_uname's' ), 'Windows NT' ) ) {

        // Windows 默认ping 4次

        $cmd shell_exec'ping  ' $target );

    }

    else {

        // *nix 手动指定ping 次数

        $cmd shell_exec'ping  -c 4 ' $target );

    }

 

    //输出命令执行的结果

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

}

 

?>

这里直接将target 变量放入 shell_exec()执行,不进行任何过滤,用户端可以直接加入特定的命令,来执行并获取想要的信息。

我们可以以下命令来拼接输入的命令

A;B

A不论正确与否都会执行B

A&B

A后台运行,A和B同时执行

A&&B

A执行成功后才会执行B

A|B

A执行的输出结果作为B命令的参数,A不论正确与否,都会执行B

A||B

A执行失败后才会执行B命令

我们可以

127.0.0.1 ; ipconfig

127.0.0.1 & ipconfig

127.0.0.1 && ipconfig

127.0.0.1 | ipconfig

111 || ipconfig

 

Medium Command Injection

核心代码

<?php

···

    // 设置黑名单

    $substitutions = array(

        '&&' => '',

        ';'  => '',

    );

    // '' 替换黑名单中存在的字符

    $target str_replacearray_keys$substitutions ), $substitutions$target );

 

···

?>

 

 

中级难度设置了黑名单过滤规则,但是他只过滤了两种字符'&&'  ';',前面我们列举了5种,这三种还是可以绕过的

127.0.0.1 & ipconfig

127.0.0.1 | ipconfig

111 || ipconfig

 

High Command Injection

核心代码

<?php

···

 

    // 设置黑名单

    $substitutions = array(

        '&'  => '',

        ';'  => '',

        '| ' => '',

        '-'  => '',

        '$'  => '',

        '('  => '',

        ')'  => '',

        '`'  => '',

        '||' => '',

    );

 

    //替换

    $target str_replacearray_keys$substitutions ), $substitutions$target );

 

···

?>

看上去,似乎敏感的字符都被过滤了,但是| 明显后面有个空格,所以如果我们不使用空格的话依然可以绕过

127.0.0.1 |ipconfig

 

Impossible Command Injection

核心代码

<?php

···

 

    // 以 . 作为分割符 分割target为4个字段

    $octet explode"."$target );

 

    // 检测分割后的4个字段,是否都为数字型

    if( ( is_numeric$octet[0] ) ) && ( is_numeric$octet[1] ) ) && ( is_numeric$octet[2] ) ) && ( is_numeric$octet[3] ) ) && ( sizeof$octet ) == ) ) {

        //如果4个字段都是数字型,还原target 变量

        $target $octet[0] . '.' $octet[1] . '.' $octet[2] . '.' $octet[3];

         }

    else {

        // 提示 输入为无效

        echo '<pre>ERROR: You have entered an invalid IP.</pre>';

    }

 

···

?>

这种类似于白名单的过滤方式,相较于黑名单过滤更加彻底。


参考文章:

https://www.freebuf.com/author/lonehand

https://www.sqlsec.com/2020/05/dvwa.html#toc-heading-19

猜你喜欢

转载自blog.csdn.net/weixin_43252204/article/details/106504342