#DVWA靶场(medium)

Command Injection

源代码:

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
Command Injection Source
vulnerabilities/exec/source/medium.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>";
}

?>

函数介绍:

stristr()函数

查找 “world” 在 “Hello world!” 中的第一次出现,并返回字符串的剩余部分:

1
2
3
4
<?php
echo stristr("Hello world!","WORLD");
?>
//输出:world!

php_uname(mode)函数定义和用法:

1
2
3
4
5
6
7
这个函数会返回运行php的操作系统的相关描述,参数mode可取值:
”a”(此为默认,包含序列”s n r v m”里的所有模式),
”s”(返回操作系统名称),
”n”(返回主机名),
“r”(返回版本名称),
”v”(返回版本信息),
”m”(返回机器类型)。

3. Exec命令的用途

3.1 改变Shell

使用exec命令可以改变当前的shell。例如,如果你正在使用bash shell,但想切换到ksh shell,可以使用以下命令:

1
exec ksh

3.2 在同一进程中运行多个命令

exec命令还可以用于在同一进程中运行多个命令,这是通过在exec命令之后添加多个命令并使用分号隔开实现的:

1
exec command1 ; command2 ; command3

3.3 重定向输入输出

exec命令也可以用于重定向输入和输出。例如,你可以使用exec命令将所有的标准输出重定向到一个文件:

1
exec > outputfile

或者,你也可以将所有的标准输入从一个文件中获取:

1
exec < inputfile

过滤了“&&” and “;”

所以