• DVWA Command Injection 解析


    命令注入,即 Command Injection。是指通过提交恶意构造的参数破坏命令语句结构,从而达到执行恶意命令的目的。

    在Web应用中,有时候会用到一些命令执行的函数,如php中system、exec、shell_exec等,当对用户输入的命令没有进行限制或者过滤不严导致用户可以执行任意命令时,就会造成命令执行漏洞。

    Low

    服务端核心代码如下:

    
    <?php
    
    if( isset( $_POST[ 'submit' ] ) ) {
    
        $target = $_REQUEST[ 'ip' ];
    
        // Determine OS and execute the ping command.
        if (stristr(php_uname('s'), 'Windows NT')) { 
        
            $cmd = shell_exec( 'ping  ' . $target );
            echo '<pre>'.$cmd.'</pre>';
            
        } else { 
        
            $cmd = shell_exec( 'ping  -c 3 ' . $target );
            echo '<pre>'.$cmd.'</pre>';
            
        }
        
    }
    ?>
    

    函数首先判断环境下的系统,如果是win则执行第一个命令,若是linux执行的命令加上-c选项,以为linux中ping命令是一直执行的。只有加了-c指定发送的跳数才能停止。
    可以看到在接收用户输入的地方,对用户的输入没有做任何的处理。不难看出这就是一个典型的命令注入漏洞,输入恶意构造的语句 www.baidu.com && ifconfig会把后面的语句也一并执行,如果把命令换成其他恶意的攻击语句,就会造成严重后果

    执行结果:

    image.png

    Medium

    服务端核心代码如下:

    <?php
    
    if( isset( $_POST[ 'submit'] ) ) {
    
        $target = $_REQUEST[ 'ip' ];
    
        // Remove any of the charactars in the array (blacklist).
        $substitutions = array(
            '&&' => '',
            ';' => '',
        );
    
        $target = str_replace( array_keys( $substitutions ), $substitutions, $target );
        
        // Determine OS and execute the ping command.
        if (stristr(php_uname('s'), 'Windows NT')) { 
        
            $cmd = shell_exec( 'ping  ' . $target );
            echo '<pre>'.$cmd.'</pre>';
            
        } else { 
        
            $cmd = shell_exec( 'ping  -c 3 ' . $target );
            echo '<pre>'.$cmd.'</pre>';
            
        }
    }
    
    ?>
    

    Str_replace()函数的作用是以其他字符替换字符串中的一些字符(区分大小写)。
    $target = str_replace( array_keys( $substitutions ), $substitutions, $target );将用户输入的内容,含有“&&”或“;”的替换为空。
    其他的部分基本和low相差不大。
    这里的源码对用户的输入进行了初步的过滤,过滤掉了一些能够同时执行命令的符号,但是我们知道,拥有同样作用的符号不止“&&”和“;”。所以依然可以进行命令注入。

    构造语句: www.baidu.com & ifconfig
    & 是前面命令执行后,接着执行后面的命令

    执行结果:

    image.png

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

    这个级别的源码和medium级别的源码相差不大,只是将更多的符号加入黑名单。通过这样的确实能够有效的防御之前的诸多思路。但是只是做黑名单的话,总是不够安全的,只要黑名单不够完整,就不是很安全。即使你认为名单已经很完整了。可能还有你不知道的存在可以利用。

    黑名单看似过滤了所有的非法字符,但仔细观察到是把”| ”(注意这里|后有一个空格)替换为空字符,但是并没有过滤“|”。

    构造语句:127.0.0.1|net user
    “|”是管道符,表示将Command 1的输出作为Command 2的输入,并且只打印Command 2执行的结果。

    执行结果:

    image.png

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

    stripslashes(string)

    stripslashes函数会删除字符串string中的反斜杠,返回已剥离反斜杠的字符串。

    explode(separator,string,limit)

    把字符串打散为数组,返回字符串的数组。参数separator规定在哪里分割字符串,参数string是要分割的字符串,可选参数limit规定所返回的数组元素的数目。

    is_numeric(string)

    检测string是否为数字或数字字符串,如果是返回TRUE,否则返回FALSE。

    可以看到,Impossible级别的代码加入了Anti-CSRF token,同时对参数ip进行了严格的限制,只有诸如“数字.数字.数字.数字”的输入才会被接收执行,因此不存在命令注入漏洞。

  • 相关阅读:
    安卓开发环境搭建
    css3实现漂亮的按钮链接
    mouseover与mouseenter的区别
    踩了一个在body上添加多个代理事件的坑
    javascript与生活:papago行车记录仪播放器golife无卫星图修复方法
    grunt入门之windows下搭建一个最基本的grunt项目
    一个简单的跨浏览器的弹出窗口的实现
    如何查询mysql中执行效率低的sql语句
    用PHP读写音频文件的信息(支持WMA和MP3)
    PHP功能类[获取客户端IP、页面跳转]
  • 原文地址:https://www.cnblogs.com/sn1per/p/11976627.html
Copyright © 2020-2023  润新知