DVWA靶场系列(一)—— Command Injection(命令注入)

 #免责声明:
本文属于个人学习笔记,仅用于学习,禁止使用于任何违法行为,任何违法行为与本人无关。

漏洞原理

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

产生原因:

        1.外部参数可控

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

        2.内部拼接命令

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

漏洞危害

黑客如果能够利用命令执行漏洞,那么黑客就可以像电脑用户控制自己电脑一样,自由地对电脑进行操作,比如开启防火墙、添加路由、开启远程服务等等操作。

防御措施

  1. 设计者尽可能少设计使用一些命令执行函数。
  2. 若有必要使用,那么必须对特殊函数做过滤,对用户输入的命令做检查,对客户端提交的变量在进入执行命令前做好过滤和检查等。

靶场实战

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

?> 

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

漏洞复现

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

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难度

源码分析

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

?> 

 这里设置了黑名单过滤规则,但是只过滤了两种字符'&&'  ';'

漏洞复现

前面列举了5种,这三种还是可以绕过的:

127.0.0.1 & ipconfig

127.0.0.1 | ipconfig

111 || ipconfig

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

?> 

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

漏洞复现

127.0.0.1 |ipconfig 

 

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

?> 

 这里加入了Anti-CSRF token,同时对参数ip进行了严格的限制,只有诸如“数字.数字.数字.数字”的输入才会被接收执行,因此不存在命令注入漏洞。这个确实已经结合业务场景来进行约束了。

又是朴实无华的一天!!!!!!!!!!!!!!!! 

参考文章:https://blog.csdn.net/zy15667076526/article/details/109705286

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值