DVWA之command

学习学习

Posted by lll-yz on December 1, 2020

Command Injection

命令连接符:

command1 && command2 先执行command1,若command1正确则执行command2,否则不执行command2
command1 & command2 先执行command1,无论command1成不成功都执行command2
command1 || command2 先执行command1,若command1成功则不执行command2,反之执行command2
command1 | command2 只执行command2
注意:”&&”, “&”, “ ” 这三种连接符在Windows和Linux系统下都支持。 “   ” 支持Windows。
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>";
}

?> 

相关函数学习:

  • $_REQUEST HTTP request 变量,默认包括了 $_GET, $_POST, $_COOKIE数组。

  • stristr(string,search,before_search) : stristr函数搜索字符串在另一字符串的第一次出现,返回字符串的剩余部分(从匹配点),如果未找到所搜索的字符串,则返回false。

    参数string规定被搜索的字符串,search规定要搜索的字符串(如果该参数是数字,则搜索匹配该数字对应的ASCII值的字符),可选参数before_search为布尔型,默认为“false”,如果设置为“true”,函数将返回search参数第一次出现之前的字符串部分。

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

  • shell_exec(cmd) : 在外部执行一个命令,参数cmd即为要执行的命令。

    因此得出服务器对操作系统执行Linux系统的ping命令,如果不是Windows NT系统则执行Linux系统的ping命令。

    由于未对IP参数做任何过滤,导致有注入漏洞。

    输入 127.0.0.1 && ipconfig :

    DjNMKU.png

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

?> 
$substitutions = array(
        '&&' => '',
        ';'  => '',
    );

设置了黑名单,将 && 和 ; 替换为空。

1.因为只有 && 与 ; 被过滤,所以可以使用 127.0.0.1 & net user

(net user 查看有哪些用户)

Dj0khd.md.png

2.由于 && 与 ; 都被替换为空,所有也可以用 127.0.0.1 &;& ipconfig 绕过。

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

?> 
可以看到对 “&”, “;”, “ ”, “-“, “$”, “(“, “)”, “`”, “   “都进行了过滤。但是仔细看” “,这里 后有一个空格,所以我们可以使用
127.0.0.1 ipconfig (中间不加空格即可)
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。

  • sizeof(array,mode):计算数组中的单元数目或对象中的属性个数。array必须,规定要计数的数组或对象。mode可选,规定函数模式。0-默认,不检测多维数组。1-检测多维数组。

​ 这里采用了token令牌,用户每次提交表单时都附加提交一个token值,服务器将提交的token值与session或cookie中存储的token值进行比较,相同则提交请求,不同则过滤请求。

 $octet = explode( ".", $target );

对IP进行了严格的过滤,将IP值以 “.” 为分隔符打散成一个数组。

 if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) )

检测数组中的每个元素是否为数字并且数组元素个数是否为4。

限制了我们输入的值必须为正确格式的IP地址。(即只有为”数字.数字.数字.数字”格式输入才能被执行。)

不存在命令注入漏洞。