DVWA靶场通关实录-Command Injection篇

0x01 简介

Command Injection漏洞,也就是我们常说的RCE(远程命令/脚本执行)漏洞。常用的连接符如下:


0x02 Command Injection -Low

当前页面给到了一个ping命令的回显


image.png

尝试各种连接符的使用:

  1. 分号前面的语句为真127.0.0.1;whoami,两条命令均会执行
    image.png
  2. 分号前面的语句为假,;whoami,仍会执行后面的语句
    image.png
  3. |前面的语句为真127.0.0.1|whoami,只执行了后面的语句
    image.png
  4. |前面的语句为假|whoami,仍然只执行了后面的语句
    image.png
  5. ||前面的语句为真127.0.0.1||whoami,只执行了前面的语句
    image.png
  6. ||前面的语句为假||whoami,执行了后面的语句
    image.png
  7. &前面的语句为真127.0.0.1&whoami,只执行了前面的语句
    image.png
  8. &前面的语句为假&whoami,执行了后面的语句
    image.png
  9. &&前面的语句为真127.0.0.1&&whoami,两条语句都执行
    image.png
  10. &&前面的语句为假&&whoami,两条语句都不执行
    image.png
  11. 命令替换 ||echo ·whoami·
    image.png

    总结
    Windows系统:
    • |:只执行后面的语句。
    • ||:如果前面的语句执行失败,则执行后面的语句。
    • &:两条语句都执行,如果前面的语句为假则执行后面的语句,如果前面的语句为真则不执行后面的语句。
    • &&:如果前面的语句为假,则直接出错,也不再执行后面的语句;前面的语句为真则两条命令都执行,前面的语句只能为真。
      Linux系统:
    • ;:执行完前面的语句再执行后面的语句,当有一条命令执行失败时,不会影响其它语句的执行。
    • |(管道符):只执行后面的语句。
    • ||(逻辑或):只有前面的语句执行出错时,执行后面的语句。
    • &(后台任务符):两条语句都执行,如果前面的语句为假则执行后面的语句,如果前面的语句为真则不执行后面的语句。
    • &&(逻辑与):如果前面的语句为假则直接出错,也不再执行后面的语句;前面的语句为真则两条命令都执行,前面的语句只能为真。
    • ``(命令替换):当一个命令被解析时,它首先会执行反引号之间的操作。例 echo whoami

代码如下:

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

?>


0x03 Command Injection -Medium

使用||whoami命令直接执行成功


image.png

查看代码看下这关做了什么限制

<?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等级是对&& 和 ; 字符进行了转空操作,本质上属于黑名单限制。但我们仍可以使用||、|、&等连接符进行命令执行


0x04 Command Injection -High

|whoami语句直接执行成功,但||whoami执行失败,猜测是做了更多的黑名单过滤,看下源代码。


image.png
Command Injection Source
vulnerabilities/exec/source/high.php
<?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>";
}

?>

黑名单里确实多了不少限制,但|后面有个空格,而我使用的命令|whoami没有空格,从而绕过了黑名单的限制。


0x05 RCE漏洞防御方法

  • 不执行外部的应用程序或命令
    尽可能的使用自定义函数或者函数库来避免使用外部应用程序或命令,确实要使用如system、eval等函数的话,要确认参数。
  • 使用 escapeshellarg 函数处理用户输入的相关参数
  • 使用 safe_mode_exec_dir 限制可执行命令的文件路径
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Command Injection,即命令注入,是指通过提交恶意构造的参数破坏命令语句结构,从而达到执行恶意命令的...
    id_rsa阅读 922评论 0 1
  • Command Injection(命令注入),是指通过提交恶意构造的参数破坏命令语句结构,从而达到执行恶意命令的...
    网络安全自修室阅读 1,445评论 0 0
  • Brute Force - Low burpsuite暴力破解image-20210704144258650.pn...
    Asson阅读 316评论 0 0
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 126,151评论 2 7
  • 16宿命:用概率思维提高你的胜算 以前的我是风险厌恶者,不喜欢去冒险,但是人生放弃了冒险,也就放弃了无数的可能。 ...
    yichen大刀阅读 6,120评论 0 4