DVWA靶场实战-Command Injection 命令行注入

Command Injection:

1.漏洞介绍:

  Command Injection,中文叫做命令注入,是指通过提交恶意构造的参数破坏命令语句结构,从而达到执行恶意命令的目的。PHP命令注入攻击漏洞是PHP应用程序常见的脚本漏洞之一。

2.漏洞成因:

  (1)外部参数可控:

    应用程序调用了执行系统命令的函数,比如服务器程序通过system、eval、exec等函数直接或者间接的调用cmd.exe。

  (2)内部拼接命令:

    服务器将输入的恶意参数拼接到正常命令中,从而执行命令造成攻击。

3.漏洞危害:

  黑客如果能够利用命令执行漏洞,那么将像控制自己电脑一样控制,自由的进行操作,比如关闭防火墙、查询密码、开启远程服务等操作。

4.防御措施:

  (1)设计者尽可能少设计使用一些命令执行函数。

  (2)若有必要使用,那么必须对特殊函数做过滤,对用户输入的命令做检查,对客户端提交的变量在进入执行命令前做好过滤和检查等。

5.攻击方法:

  可以用以下命令来拼接输入的命令:

    ·A;BA

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

    ·A&B

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

    ·A&&B

      →A执行成功后才会执行B

    ·A|B

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

    ·A||B

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

6.实战:

  在开始实战前有个注意事项,在Low级别下,尝试用“127.0.0.1&&ipconfig”命令进行注入后,发现会有乱码情况。那么解决的方法如下:

    ①在DVWA的安装目录下(……/WWW/DVWA-master/dvwa/includes)找到文件“dvwaPage.inc.php”

    ②打开这个文件,然后全文查找charest=utf-8,有好几处charest=utf-8,然后全部修改成为charest=gb2312

    ③接下来就不会有乱码了

  接下来正式进入实战环节:

    (1)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

    $html .= "<pre>{$cmd}</pre>";

}

?>

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

首先测试“127.0.0.1”这里加上后台命令后就是“ping 127.0.0.1”

接下去,测试“127.0.0.1&&ipconfig”,执行命令为“ping 127.0.0.1 && ipconfig”,这里结果如下:

最后尝试“127.0.0.1 && ipconfig && systeminfo”,没问题,可以通过构造恶意传参系统命令或者直接传木马。

 (2)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

    $html .= "<pre>{$cmd}</pre>";

}

?>

这里设置了“黑名单过滤规则”,但是只起到了过滤两种字符分别是“&&”和“;”两种字符,但可以想办法绕过。比如“127.0.0.1 & ipconfig”、“127.0.0.1 | ipconfig”、“111 || ipconfig”等

  (3)High:

    代码分析:

<?php

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

    // Get input

    $target = trim($_REQUEST[ 'ip' ]);

    // Set blacklist

    $substitutions = array(

        '&'  => '',

        ';'  => '',

        '| ' => '',

        '-'  => '',

        '$'  => '',

        '('  => '',

        ')'  => '',

        '`'  => '',

        '||' => '',

    );

    // Remove any of the characters 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

    $html .= "<pre>{$cmd}</pre>";

}

?>

这里虽然看上去有有些敏感字符被过滤了,但是“|”的后面明显有个空格,意思就是只要不使用空格还是可以绕过的,比如“127.0.0.1 |ipconfig”就是仍然生效的。当然,ipconfig指令也是可以替换的。

  (4)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

        $html .= "<pre>{$cmd}</pre>";

    }

    else {

        // Ops. Let the user name theres a mistake

        $html .= '<pre>ERROR: You have entered an invalid IP.</pre>';

    }

}

// Generate Anti-CSRF tokengenerateSessionToken();

?>

这里已经严格限制了输入接收的参数只能是“数字.数字.数字.数字”的模式,所以不存在命令注入的漏洞了。

  • 25
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值