dvwa实战第二篇:Command Injection

最近被编译原理搞得神魂颠倒,没时间更新dvwa…

今天写dvwa的第二篇:command injection命令注入。
在开始之前,先对centos进行设置:关闭selinux。
(我看到很多教程好像都没有提到关闭selinux,实测在没有关闭的情况下,命令注入后没有反馈,centos7给出警告如下:
在这里插入图片描述
因此首先需要关闭selinux,具体操作如下:
centos 7 终端输入vim /etc/sysconfig/selinux,将SELINUX=enforcing改为SELINUX=disabled,保存后重启centos。
回到dvwa,先把dvwa安全系数调至low,用连接符连接个简单的:127.0.0.1&whoami:
在这里插入图片描述
这里需要学习连接符的使用:

& 前面的语句为假则执行后面的语句,前面为真则都执行
&& 前面的语句为假则出错且后面语句不执行,前面为真则都执行
| 前面的语句为假则出错且后面语句不执行,前面为真则执行后面语句
|| 前面的语句为假则两个语句都执行,前面为真则只执行前面语句

在这里插入图片描述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>";
}

?>

可以看到,并没有对任何符号进行过滤。
下面看看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>";
}

?>

可以看到,输入框过滤了&&和;,那么这次用||进行命令注入,输入test|echo ‘helloworld’执行:
在这里插入图片描述
此外,dvwa还提供了high和impossible两种防御方案,high的代码原理差不多,通过设置黑名单过滤的方式阻止命令注入,impossible的php还没读懂,查资料得知Impossible防御中加入了Anti-CSRF token,也只允许数字和’.‘输入,因此不存在命令注入漏洞。①

参考资料:
①:https://www.freebuf.com/articles/web/116714.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值