文章目录
High等级
代码中一些特殊的函数
1)trim()
去除字符串首尾处的空白字符(或者其他字符)
2)array_keys()
— 返回数组中部分的或所有的键名
3)str_replace ()
子字符串替换
4)php_uname ()
返回运行 PHP 的系统的有关信息
5)stristr ()
strstr() 函数的忽略大小写版本
查看代码
<?php
//判断上传的submit参数是否为空
if( isset( $_POST[ 'Submit' ] ) ) {
// Get input
//trim 去除字符串首尾处的空白字符(或者其他字符)
//获取用户提交的IP地址,并去除其首尾的空格,并将其保存在变量 $target中
$target = trim($_REQUEST[ 'ip' ]);
// Set blacklist 设置黑名单
//设置黑名单,过滤特殊字符
$substitutions = array(
'&' => '',
';' => '',
'| ' => '',
//仔细观察发现有一个过滤是’| ‘,这个过滤是加了空格的,说明这个过滤其实是没用的,只需要’|’后面直接加入参数,不保留空格,我们依然可以用这个命令连接符进行命令注入
'-' => '',
'$' => '',
'(' => '',
')' => '',
'`' => '',
'||' => '',
);
// Remove any of the charactars in the array (blacklist). 删除数组中的任何字符(黑名单)。
//str_replace:子字符串替换
//array_keys:返回数组中的数字或者字符串的键名
//通过array_keys返回用户输入的数据里面的黑名单,通过str_replace作过滤
$target = str_replace( array_keys( $substitutions ), $substitutions, $target );
// Determine OS and execute the ping command. 判断操作系统,执行ping命令。
//stristr:忽略大小写版本
//php_uname 返回运行 PHP 的系统的有关信息
//检查服务器操作系统,并根据操作系统类型执行ping命令。
if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
// Windows
//如果操作系统为Windows,则使用 shell_exec()函数执行ping命令
$cmd = shell_exec( 'ping ' . $target );
}
else {
// *nix
//则,在nix系统中使用 ping -c 4 命令来执行。
$cmd = shell_exec( 'ping -c 4 ' . $target );
}
// Feedback for the end user 对最终用户的反馈
//将 ping 命令的输出结果返回给客户端
echo "<pre>{$cmd}</pre>";
}
Impossible等级
代码中一些特殊的函数
1)explode()
使用一个字符串分割另一个字符串
2)is_numeric()
检测变量是否为数字或数字字符串,它返回true或false。
查看代码
<?php
//判断submit参数的是否为空
if( isset( $_POST[ 'Submit' ] ) ) {
// Check Anti-CSRF token 检查Anti-CSRF令牌
//检查并验证表单的CSRF令牌的有效性,以防止跨站点请求伪造(CSRF)攻击。
checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
// Get input
//stripcslashes 反引用一个使用 addcslashes() 转义的字符串
//获取用户提交的IP地址,并使用 stripslashes()函数对其进行解除转义,并将其保存在变量 $target中
$target = $_REQUEST[ 'ip' ];
$target = stripslashes( $target );
。
// Split the IP into 4 octects 将IP分割为4个字节
//explode 使用一个字符串分割另一个字符串
//将 IP 地址拆分为四个“八位字节”,并检查每个“八位字节”的值是否为整数。
$octet = explode( ".", $target );
// Check IF each octet is an integer 检查每个八位是否为整数
//is_numeric 检测变量是否为数字或数字字符串
//检查IP地址的每个八位字节是否为数字,并且该IP地址是否由四个八位字节组成。仅当所有八位字节均为数字,且该IP地址由四个八位字节组成时,该条件语句才成立,否则将不会执行该条件语句内包含的代码。
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. 如果所有八位字节都是int(整数),则将IP重新组合在一起。
$target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];
// Determine OS and execute the ping command.判断操作系统,执行ping命令。
//检测运行PHP脚本的操作系统是否为Windows操作系统
if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
// Windows
//如果操作系统为Windows,则使用 shell_exec()函数执行ping命令
$cmd = shell_exec( 'ping ' . $target );
}
else {
// *nix
//否则,在*nix系统中使用 ping -c 4 命令来执行。
$cmd = shell_exec( 'ping -c 4 ' . $target );
}
// Feedback for the end user 对最终用户的反馈
//将ping 命令的输出结果返回给客户端
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 生成反csrf令牌
//生成一个随机的会话令牌字符串。
generateSessionToken();
?>