美文网首页
DVWA笔记之Command Injection命令行注入

DVWA笔记之Command Injection命令行注入

作者: inspireboom | 来源:发表于2017-08-16 15:03 被阅读0次

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

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

    php_uname(mode)

    这个函数会返回运行php的操作系统的相关描述,参数mode可取值”a” (此为默认,包含序列”s n r v m”里的所有模式),”s ”(返回操作系统名称),”n”(返回主机名),” r”(返回版本名称),”v”(返回版本信息), ”m”(返回机器类型)。

    linux和windows 都 可以用 && 、 & 、; 等 执行其他 命令

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

    可知,medium增加了过滤&&与;字符
    但是可以用&;&,因为过滤;,形成&&,也可以只用&,&与&&区别是前者是不管前一个命令是否执行都执行后面命令,而&&是只有前一个命令执行才执行后一个命令,也可以用 | 、 ||执行,| 和|| 与&之间刚刚好相反,||是前面没执行下,执行后面命令。

    Hign服务端代码:

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

    high的代码完善了过滤的字符。但是好像|还是没过滤。所以|还是 可以绕过。

    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中的反斜杠,返回已剥离反斜杠的字符串。
    目前impossible 我没办法绕过。

    相关文章

      网友评论

          本文标题:DVWA笔记之Command Injection命令行注入

          本文链接:https://www.haomeiwen.com/subject/cyrvrxtx.html