High难度
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// Get input [判断里面内容是否为空,检查是否存在名为 “Submit” 的 POST 变量。]
$target = trim($_REQUEST[ 'ip' ]); //[去除方括号里面的字符串开头与结尾的空格]
// Set blacklist
$substitutions = array(
'&' => '',
';' => '',
'| ' => '',
'-' => '',
'$' => '',
'(' => '',
')' => '',
'`' => '',
'||' => '',
); //[定义了关联数组 $substitutions。]
// Remove any of the charactars in the array (blacklist).
$target = str_replace( array_keys( $substitutions ), $substitutions, $target );
//[先通过array_keys返回上面定义的关联数组键名,之后根据键名去寻找,如果$target传递出来的字符串有和键名一样的东西,根据$substitutions这个数组里面的值进行替换]
// Determine OS and execute the ping command.
if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
// Windows [查询字符串首次出现的位置,忽略它的大小写,且判断是不是windows操作系统]
$cmd = shell_exec( 'ping ' . $target );
} //[是windows系统,则执行ping这个命令,拼接后面的变量,$target为定义的变量IP]
else {
// *nix
$cmd = shell_exec( 'ping -c 4 ' . $target );
} //[如果不是windows系统,执行ping这个命令,只ping4次,拼接后面的变量,$target为定义的变量IP]
// Feedback for the end user
echo "<pre>{$cmd}</pre>"; //[输出cmd这个参数,并使用 pre 标签包围以保留输出格式]
}
?>
Impossible难度
<?php
if( isset( $_POST[ 'Submit' ] ) ) { //[判断里面内容是否为空,检查是否存在名为 “Submit” 的 POST 变量。]
// Check Anti-CSRF token
checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
// [不管后面的输出是啥,检查我们是否具有资格使其继续往下执行代码]
// Get input
$target = $_REQUEST[ 'ip' ]; //[去除方括号里面的字符串开头与结尾的空格]
$target = stripslashes( $target ); //[将解释器给的字符在成盘之前全部给去掉,stripslashes可以将这些字符删除,防止 SQL 注入攻击和其他安全问题]
// Split the IP into 4 octects
$octet = explode( ".", $target ); //[用点分隔$target这个变量,返回的值定义成$octet]
// Check IF each octet is an integer
if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {
//[判断所有的$octet是否为数字或者数字字符串,并且判断数组的个数是否等于4,以上条件全部显示为正确才会继续运行,否则直接结束]
// If all 4 octets are int's put the IP back together.
$target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];
//[将一个 IP 地址每段的数值合并为一个字符串]
// Determine OS and execute the ping command.
if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
// Windows [查询字符串首次出现的位置,忽略它的大小写,且判断是不是windows操作系统]
$cmd = shell_exec( 'ping ' . $target );
} //[是windows系统,则执行ping这个命令,拼接后面的变量,$target为定义的变量IP]
else {
// *nix
$cmd = shell_exec( 'ping -c 4 ' . $target );
} [如果不是windows系统,执行ping这个命令,只ping4次,拼接后面的变量,$target为定义的变量IP]
// Feedback for the end user
echo "<pre>{$cmd}</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
generateSessionToken(); //[函数生成会话令牌,用于防御 CSRF 攻击。]]
?>
特殊函数罗列
这里面出现了很多的函数,对于初学者来说很不友好,特别是组合在一起更加不友好,所以我们可以将函数单独罗列出来,并单独举个例子,这样感觉会更容易记忆跟理解
trim
trim — 去除字符串首尾处的空白字符(或者其他字符)
代码举例
<?php
$a=" 123 abc ";
echo trim($a);
?>
代码输出效果
这边可以看到在写入代码时我们在字符串首尾处添加的空白字符被去除了,但是字符串中间添加的空白字符不受影响
array_keys
array_keys — 返回数组中部分的或所有的键名
代码举例
<?php
$substitutions = array(
'&' => '',
';' => '',
'| ' => '',
'-' => '',
'$' => '',
'(' => '',
')' => '',
'`' => '',
'||' => '',
);
$arr1=array_keys($substitutions);
for($a=0;$a<sizeof($substitutions);$a++){
echo $arr1[$a]."<br \>";
}
?>
代码输出效果
只输出了上面数组的所有键名
str_replace
str_replace — 子字符串替换
代码举例
<?php
$substitutions = array(
'ipconfig' =>'',
'whoami' =>'',
'pwd' =>'',
);
$arr1=array_keys($substitutions);
$a="whoamiabcipconfig123pwd";
echo $a."<br \>";
echo str_replace($arr1,$substitutions,$a);
?>
代码输出效果
通过上下输出的效果对比,下面定义的变量内容如果跟上面数组键名相同的话,就会被替换成空白字符
php_uname
php_uname — 返回运行 PHP 的系统的有关信息
代码举例
<?php
echo php_uname('s');
?>
代码输出效果
返回该操作系统的内核名称
explode
explode — 使用一个字符串分割另一个字符串
代码举例
<?php
$target="127.0.0.1&whoami";
$arr1=explode(".",$target);
for($a=0;$a<sizeof($arr1);$a++){
echo $arr1[$a]."<br \>";
}
?>
代码输出效果
输出了用小数点隔离的字符串
is_numeric
is_numeric — 检测变量是否为数字或数字字符串
代码举例
<?php
var_dump(is_numeric("123"));
var_dump(is_numeric("12a"));
?>
代码输出效果
可以看到上面输出为数字的话,输出结果为ture,反之则为false
以上就是我对这些代码的个人看法与理解,新手小白一枚,有什么不对的地方希望大佬可以指正一下,望多多包涵!