DVWA靶场——Command Injection(命令注入)

概述

Command Injection,即命令注入,是指通过提交恶意构造的参数破坏命令语句结构,从而达到执行恶意命令的目的。PHP命令注入攻击漏洞是PHP应用程序中常见的脚本漏洞之一,国内著名的Web应用程序Discuz!、DedeCMS等都曾经存在过该类型漏洞。

命令执行分隔符:(注:关于分隔符大家可以到网上搜索它们的用法)

  Windows:&&  ||  &  

  Linux:  &&  ||  &  ;

  |:  可作为管道符pipe用于命令执行,其实根本是pipe的作用

  &&:  从前往后按顺序执行,遇到出错的命令,后面的不执行

  ||:  从前往后按顺序执行,遇到成功的命令,后面的不执行

  &:  同时执行所有命令

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

?> 

可以看到源代码中,通过stristrphp_uname两个函数来判断操作系统,执行不同的ping命令。但并没有对ip参数做任何的过滤等安全措施,导致了严重的命令注入漏洞!

由于它并没有做任何的安全措施,所以我们可以利用任意的命令执行分隔符来获取我们想要的信息。

www.baidu.com&&pwd

输入payload后,可以看到不仅获取到了百度ping后的信息,还获取了靶场所在的目录,可见危害之大。

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

?> 

在源代码中,可以看到它已经开始设置黑名单限制了,过滤了"&&"";"字符,但也只是过滤了这两个,我们可以通过更换别的命令执行符号来执行获取信息,比如:“&”。

127.0.0.1&pwd

在源码中,它把"&&"";"字符过滤为空字符,我们可以利用它在这里的漏洞来绕过执行payload

127.0.0.1&;&pwd

因为";"会转换为空字符,所以payload就会变为127.0.0.1& &pwd,这就会绕过它的限制,变成为一个可执行的payload。

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

?> 

可以看到源代码中,high级别相比于medium级别的黑名单更加完善了,但这黑名单还是有一定的局限性,我们依然可以绕过。

注意看,源代码中的'| '字符它里面的|后面是跟一个空字符的,而不是'|'字符(拿'| ''|'比较一下就能看出了),这也就成了这个黑名单中的”漏网之鱼“,我们可以利用这个漏洞来进行绕过。

payload:

127.0.0.1|pwd

high级别源代码中,它把这个"||"分隔符拉进了黑名单,但并没有把旁边带有空格的" || "分隔符拉进黑名单,我们可以完全可以利用这个漏洞来进行绕过

payload:

127.0.0.1 || net user

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

?> 

Impossible级别黑名单完善的更加彻底,已经类似于白名单了,使用了stripslashesexplodeis_numeric函数,还加入了Anti-CSRF token进行了严格的限制。

原文地址:https://www.cnblogs.com/QingR/p/13403127.html