• DVWA靶场(二、命令执行)


    命令执行介绍

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

    命令执行(low)

    分析以下源代码,其中Shell_exec函数是导致命令执行漏洞的原因,作用是让php执行操作系统的命令。代码中做了一个基于windows还是linux的判断,
    windows和linux的ping有所不同,windows默认发4个包,linux一直发包

    <?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>";
    }
    
    ?> 
    
      通过执行以下命令,达到命令执行目的
      127.0.0.1 || ipconfig           或,前面执行失败才执行后面的
      127.0.0.1 & ipconfig            And,前面执行后接着执行后面的
      127.0.0.1 && ipconfig           与,前面执行成功才执行后面的
      127.0.0.1 | ipconfig            管道符,将前面的输出结果作为后面的输出内容
      127.0.0.1 ; ipconfig            分号,此命令在Linux下才生效,同时执行多条命令
      127.0.0.1 & echo test>c:1.txt  还可以使用重定向(>)在服务器中生成文件,或是使用(<)将准备好的文件读入
    

    将以上几条命令制作成字典进行测试

    命令执行(medium)

    $substitutions = array(
            '&&' => '',
            ';'  => '',
        );
    
        // Remove any of the charactars in the array (blacklist).
        $target = str_replace( array_keys( $substitutions ), $substitutions, $target );
    

    定义了一个黑名单,其中包含数组array中的两个键&&和;,target变量表示将&&和;替换成空,其实就是把&&和;过滤了,但是还可以通过其他符合,比如&绕过黑名单执行

      解决乱码问题:在DVWA-masterdvwaincludes目录下找到dvwaPage.inc.php文件中所有的”charset=utf-8”,修改”charset=gb2312”,即可。
    

    命令执行(high)

    high级别相比于medium级别对符号进行了增加,但是还是有符号没有过滤,如|(这里是|后面不加空格)

        $target = trim($_REQUEST[ 'ip' ]);
    
        // Set blacklist
        $substitutions = array(
            '&'  => '',
            ';'  => '',
            '| ' => '',
            '-'  => '',
            '$'  => '',
            '('  => '',
            ')'  => '',
            '`'  => '',
            '||' => '',
        ); 
    

    命令执行(impossible)

    增加了$octet = explode( ".", $target );通过explode函数以"."为分隔符将$target变量中的IP地址进行分割,分割后会得到一个数组,并复制给变量$octet
    定义一个白名单,只允许输入IP。if判断语句,判断数组中的元素是否为数字,并且判断是否为4个元素,否则不执行。

    <?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、对传入的命令进行严格过滤
    2、在后台对应用的权限进行控制
    3、EscapeShellCmd()函数,把字符串中所有可能瞒过shell而去执行另一个命令的字符进行转义
    4、EscapeShellArg()函数,在给定的字符串两边加上单引号,并把字符串中的单引号转义,这样这个字符就可以安全地作为命令的参数

  • 相关阅读:
    Algorand算法实现(一)
    什么是跨链?为什么要跨链?
    Web配置简单记录(多更新)
    Web前端常用调优
    回文素数
    postman接口请求快速新建
    两台虚拟机如何ping通
    vue动态改变标题
    js正则匹配版本号V1.0.0
    js获取年月日星期时分秒
  • 原文地址:https://www.cnblogs.com/tonywell/p/14013084.html
Copyright © 2020-2023  润新知