zoukankan      html  css  js  c++  java
  • 代码审计-DVWA-命令注入

    首先说明,我水平不高,这是我在学习代码审计过程中写的记录笔记,难免有不正之处,还望指出。
    Windows 10
    php7.2.10 + apache

    DVWA代码审计

    命令执行

    low

     <?php 
    if (isset($_POST['Submit'])) {
        $target = $_REQUEST['ip'];
        if (stristr(php_uname('s'),'Windows NT')) {
            $cmd = shell_exec('ping '.$target);
        }else{
            $cmd = shell_exec('ping -c 4 '.$target);
        }
        echo "<pre>{$cmd}</pre>";
    }
      ?>
    

    1、首先使用预定义变量$_POST[]来接收从form表单中传来的数据,使用isset()判断该值是否传入。
    2、将接受数据中的ip的值赋给变量target
    3、进行主机系统判断,使用php_uname()获取服务器操作系统信息,win10系统中php_uname('s')返回的是Windows NT 。
    4、stristr(a,b,c)函数搜索字符串b在另一字符串a中的第一次出现,并返回字符串剩余部分,且不区分大小写。如果不存在返回空,c的值为true或false,默认false,true返回字符串之前部分。
    5、通过if进行判断是否为空后,使用shell_exec()执行并将所有输出流作为字符串返回。注意ping后面有空格。
    6、通过分析可以看到,此执行过程无任何过滤。

    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、在将ip的值赋给变量target后,定义了一个数组substitutions,该数组的功能是将&&;替换为空。
    2、使用字符串替换函数str_replace(a,b,c),将c中的a替换为b。
    3、array_keys($subtitutions),返回的是数组的key,&&和;
    4、将target变量中的&&或;替换为空并重新赋值。
    5、就是说中难度过滤了&&和;

    high

    //其他内容跟中难度都一样,只是丰富了替换数组
    $substitutions = array(
            '&'  => '',
            ';'  => '',
            '| ' => '',
            '-'  => '',
            '$'  => '',
            '('  => '',
            ')'  => '',
            '`'  => '',
            '||' => '',
        ); 
    

    impose

    <?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、使用checkToken()验证index.php页面的token,防止csrf。
    2、stripslashes()函数删除由addslashes()添加的反斜杠。
    3、explode()函数,将target字符串以'.'为分隔符打散为数组,因为正确的ip地址是以'.'为分隔符。
    4、下面通过if判断,数组前4个是否是数字,是,再将前4个以'.'进行拼接;否,则输出用户输入有误。
    5、generateSessionToken();用来生成token。
    6、像最后这一种通过是不会存在命令注入的。因为它限制用户必须输入满足判定条件格式的内容,而满足该格式的只有IP地址。至于输入的ip地址范围不正确,那就不是该程序处理的事了。

  • 相关阅读:
    MyBatis中#{}和${}的区别
    springBoot 配置详解
    模板方法模式
    记录一次linux挂载数据盘
    缓存击穿,缓存穿透,缓存雪崩
    Echart折线值相加问题
    mybatis plus主键生成策略
    BigDecimal精度损失
    cent0s6安装nginx小程序https
    Centos6 java运行环境部署
  • 原文地址:https://www.cnblogs.com/ahtoh/p/12091003.html
Copyright © 2011-2022 走看看