DVWA高难度代码审计
代码
<?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>";
}
?>
1 isset
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
isset()
是一个判断里面内容是否为空的函数,若是里面为空则不执行if里面的语句,反之则执行语句。
$_POST[ 'Submit' ]
是一种请求方法,submit
则是代表用户提交的数据。
2 trim
// Get input
$target = trim($_REQUEST[ 'ip' ]);
定义一个target的变量,($_REQUEST[ 'ip' ]);
将获取客户端发送过来的请求中的 ‘ip’ 键对应的值。如果 ‘ip’ 键存在并且有值,该函数将返回该值;否则,它将返回一个空字符串。
3 array
// Set blacklist
$substitutions = array(
'&' => '',
';' => '',
'| ' => '',
'-' => '',
'$' => '',
'(' => '',
')' => '',
'`' => '',
'||' => '',
);
定义一个substitutions的数组变量,该数组包含了可以在正则表达式中进行替换的特殊字符。数组里面的键为各种特殊字符替换为空。
4 str_replace
// Remove any of the charactars in the array (blacklist).
$target = str_replace( array_keys( $substitutions ), $substitutions, $target );
将变量target
的中含有$substitutions
这个变量数组中键key的特殊符号替换为变量数组的值value,用来赋值给变量target
。
5 stristr
// Determine OS and execute the ping command.
if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
stristr
函数用来查找在 php_uname('s')
中的 ‘Windows NT’。stristr
函数是一个在 PHP 中的 String
类中的函数,它用于查找字符串在另一个字符串中的第一次出现的位置。
如果找到了 ‘Windows NT’ 这个字符串,那么代码就会进入一个 if
语句,执行在 if
语句中的代码。
这里的 if
语句使用了 php_uname('s')
函数来获取当前运行的 PHP 服务器的类型。php_uname('s')
函数会返回一个包含系统类型的字符串,包括操作系统的名称。
6 shell_exec
// Windows
$cmd = shell_exec( 'ping ' . $target );
}
这句代码用于执行一个 ping
命令来检查指定的 URL 是否可访问。
shell_exec()
是一个 PHP 内置函数,用于执行 shell 命令
'ping ' . $target
即是用小数点拼接前后两个动作,而变量$target = $_REQUEST[ 'ip' ];
所以这句代码的意思就是ping一下ip。
当然这是针对windows的ping操作语法。
else {
// *nix
$cmd = shell_exec( 'ping -c 4 ' . $target );
这句代码与上句代码有些细微的改变。就是多了一个参数-c 4
。
因为在linux中ping的语法不同,若是不限定ping的次数,会一直ping下去。所以这里针对*nux使用ping -c 4
来限定ping的次数。
7 echo
// Feedback for the end user
echo "<pre>{$cmd}</pre>";
}
输出包含 ping
命令结果的 HTML 格式的内容,其中 {$cmd}
是 shell_exec()
函数返回的命令结果。
php
// Feedback for the end user
echo “
{$cmd}”;
}
输出包含 `ping` 命令结果的 HTML 格式的内容,其中 `{$cmd}` 是 `shell_exec()` 函数返回的命令结果。