zoukankan      html  css  js  c++  java
  • WEB漏洞---命令注入

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

      查看命令注入的流程:

        1;查看是否调用系统命令。

        2;函数以及函数的参数是否可控。

        3;是否拼接命令注入。

      下面我们使用dvwa来做测试。

        A;我们先将安全等级调整为“low”

      1;查看源代码

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

      2;代码分析:

        核心函数shell_exec(),该函数的功能就是通过shell执行命令并将完整输出作为字符串返回。整个代码看下来并没有对输入的参数做一些过滤。几乎可以判定存在命令注入的漏洞。

      3;漏洞利用:

        使用&&来执行多条命令(Window和linux都可以使用)输入命令“127.0.0.1 && ifconfig ”这个时候我们发现ifconfig 命令也成功被执行且返回效果。

       B;下面我们将安全等级调整为“Medium”

        1;查看代码

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

        2;代码分析:

          这里我们发现代码中采用了黑名单的方法过滤了字符串“&&”,“;”也就说类似于127.0.0.1 && ifconfg 这样使用被过滤的连接符号就无法在成功执行。

          我们都知道,采用黑名单并不是绝对安全的,比如上面代码当中就没有过滤“&”那么下面我们就可以利用这点。

        3;漏洞利用:

          输入127.0.0.1 & ifconfig

       C;安全等级“High”

        1;分析代码,这里我们可以看到只是补充了黑名单。黑名单在过滤的过程中一般会出现遗漏关键字的问题。另外就是关键字过滤不当的问题。比如下面代码我们发现黑名单当中'| ' 这么符号的过滤,我们仔细发现后面有一个空格,也就是说如果我们输入的参数管道符不带空格,代码就无法匹配到。

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

        2;漏洞利用:

          注入代码:127.0.0.1|ifconfig 我们可以发现命令ifconfig可以被成功执行。

      C;Impossible

        1;查看代码

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

        2;代码分析:

          这段代码一共进行了两点防护,第一是对token进行了验证。第二对输入数据进行了严格的限定(这里功能是执行PING命令。所以严格设定了输入形式为IP 的形式)

          注释:token可以预防csrf的攻击。预防原理是

          1;当客户端请求页面时,服务器会生成一个随机数Token,并且将Token放置到session当中。然后将Token发给客户端(一般通过构造hidden表单)

          2; 客户端提交请求时,Token会随着表单一起提交到服务器端。 
    然后,如果应用于"anti csrf攻击",则服务器端会对Token值进行验证,判断是否和session中的Token值相等,若相等,则可以证明请求有效,不是伪造的。 

  • 相关阅读:
    [Android编程心得]Debug的一些技巧
    理解WebKit和Chromium: Chromium for Android
    java compiler level does not match the version of the installed java project facet
    [Android编程心得]Activity的生命周期函数
    GDAL中的SURF算法
    SURF算法学习心得
    遥感图像处理流程
    软件界面建模浅析
    IsPostBack深入探讨
    自定义应用程序配置文件(app.config)
  • 原文地址:https://www.cnblogs.com/aaron456-rgv/p/12245934.html
Copyright © 2011-2022 走看看