DVWA--COMMAND INJECTION

命令注入攻击的常见模式为:仅仅需要输入数据的场合,却伴随着数据同时输入了恶意代码,

而装载数据的系统对此并未设计良好的过滤过程,导致恶意代码也一并执行,最终导致信息泄露或者正常数据的破坏。

   PHP命令注入攻击漏洞是PHP应用程序中常见的脚本漏洞之一,国内著名的Web应用程序Discuz!、DedeCMS等都曾经存在过该类型漏洞。

现在开始我们的Command Injection

首先打开Command Injection,发现是一个执行ping的界面,推测若没有做好过滤,则很有可能执行其他DOS命令

DOS在一行中执行多条命令需使用的符号:

&&:只有当前面的命令执行成功才执行后面的命令

&:无论怎样总执行后面的命令

||:只有当前面的命令执行失败才执行后面的命令

|:将前面命令执行的输出作为后面命令执行的输入

Low

Low级别的源码,未对用户输入ip做任何过滤
`<?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>";

}

?> `
可以看到,low级别的代码接收了用户输入的ip,然后根据服务器是否是Windows NT系统,对目标ip进行不同的ping测试。
但是这里对用户输入的ip并没有进行任何的过滤,所以我们可以进行命令执行漏洞
首先我们可以ping一个正常的id比如127.0.0.1
可以看到ping的结果

接下来我们尝试输入 127.0.0.1 & ipconfig ,在操作系统中," & 、&& 、| 、 || "都可以作为命令连接符使用,我们在ping完后再执行ipconfig 命令查看ip信息

可以看到,成功执行。然后我们就可以继续执行我们的命令了。把ipconfig换成其他的系统命令
比如以下指令都可执行成功
127.0.0.1&&ipconfig
127.0.01|ipconfig
127.0.0.1||ipconfig
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>";

}

?> `

Medium级别相比Low级别的代码,服务器端对ip参数做了一定过滤,把”&&”“ ;”;”替换为空;

Medium级别的过滤方式本质上采用的是黑名单机制,相比白名单依旧存在安全问题;
比如我们像low输入127.0.0.1&&ipconfig
我们会发现无法成功ping。
但是我们输入
127.0.0.1&ipconfig

我们可以ping成功,这是因为后台没有对&进行过滤。
同样
127.0.01|ipconfig
127.0.0.1||ipconfig
依然可以ping成功
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>";

}

?> `

简单分析一下代码
'&' => '',

';' => '',

'| ' => '', #“| ”后面有个空格;

'-' => '',

'$' => '',

'(' => '',

')' => '',

'`' => '',

'||' => '
服务器对IP参数做了限制,过滤了如上字符,将如上字符都替换为空;
注:源码中是将“| ”替换为空,并不是“|”,所以可以采用“|”的方式绕过;
比如我们简单尝试"&","| "等方式实现命令行注入
我们可以发现并不可以ping成功
但是我们输入127.0.0.1|pwd

我们可以发现可以ping成功
这样‘|’就成为漏网之鱼
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();

?>
`
相当于前面几个这一个Impossible级别的黑名单完善的更加彻底
使用了stripslashes、explode、is_numeric函数,还加入了Anti-CSRF token,进行了严格的限制
这样我们没有可乘之机了。

posted @ 2020-08-06 17:32  renletao  阅读(111)  评论(0编辑  收藏  举报