DVWA 黑客攻防演练(三)命令行注入(Command Injection)

栏目: 服务器 · Apache · 发布时间: 7年前

内容简介:文章会讨论 DVWA 中低、中、高、不可能级别的命令行注入这里的需求是在服务器上可以 ping 一下其他的服务器Hacker 试下输入 192.168.31.130; cat /etc/apache2/apache2.conf;

文章会讨论 DVWA 中低、中、高、不可能级别的命令行注入

这里的需求是在服务器上可以 ping 一下其他的服务器

DVWA 黑客攻防演练(三)命令行注入(Command Injection)

低级

Hacker 试下输入 192.168.31.130; cat /etc/apache2/apache2.conf;

瞬间爆炸, 竟然是直接执行 shell 的结果。再看看代码

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

?>

竟然没有任何的参数校验!这主机安全可以说是形同虚设。 Hacker 竟然不废任何力气,就得到一个 weshell 了!接下来的攻击主要是就看攻击者的想象力了,什么改掉你的文件,什么创建个新页面做负载均衡,批量将 php 重名了。。。

中级

中级代码有做了一点参数校验

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

?>

会将有 ;&& 都移除了。

Hacker 试了一下 || cat /etc/apache2/apache2.conf 。。。注入成功。而  || 的意思是第一条命令执行失败就会执行下一条。。。

Hacker 又试了一下 & cat /etc/apache2/apache2.conf 。。。注入也成功。  & 是后台执行,然后再执行新的代码。

主要是过滤太少的参数了。

高级

高级的话就过滤的参数比较多。

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

?>

|cat /etc/apache2/apache2.conf 就可以了,因为 substitutions 中的  | 后面有个空格,  |+<space> 才会被替换 。。。有点无语

不可能

&amp;lt;?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] ) ) &amp;amp;&amp;amp; ( is_numeric( $octet[1] ) ) &amp;amp;&amp;amp; ( is_numeric( $octet[2] ) ) &amp;amp;&amp;amp; ( is_numeric( $octet[3] ) ) &amp;amp;&amp;amp; ( 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 "&amp;lt;pre&amp;gt;{$cmd}&amp;lt;/pre&amp;gt;";
    }
    else {
        // Ops. Let the user name theres a mistake
        echo '&amp;lt;pre&amp;gt;ERROR: You have entered an invalid IP.&amp;lt;/pre&amp;gt;';
    }
}

// Generate Anti-CSRF token
generateSessionToken();

?&amp;gt;

不能级别,添加了 token 用来对付 CSRF 跨域请求攻击。还对参数进行真正的验证,只有 ip 参数才能够 ping。如果是我写的话可能会用估计会用正则表达式。这个方式验证也可以了。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

信息烟尘

信息烟尘

戴维·申克 / 黄锫坚 / 江西教育出版社 / 2002 / 14.50元

今天,我们被大量的信息淹没了:传真、电子邮件、各种新闻、消息和铺天盖地的广告,正如人们以前预示的那样:出现了一个令人鼓舞的信息时代,媒体专家兼网络评论员戴维·申克透过这些繁荣的表象,揭示了大量的无用的信息对我们造成的干扰,或者说,“信息烟尘”对我们个人的健康(包括精神上的和肉体上的)及对社会造成的极大危害。这《信息烟尘:在信息爆炸中求生存》宣告了“信息时代”神话的破灭。一起来看看 《信息烟尘》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具