high
源码
Command Injection Source
<?php
if( isset( $_POST[ 'Submit' ] ) ) {//isset函数判断内容是否为空
// 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>";
}
?>
相关函数解析
trim函数——去除字符串首尾处的空白字符(或者其他字符)
首位都存在空白字符
查看输出结果
arry_keys函数——返回数组中部分的或所有的键名
for循环,输出数组内的键名
str_replace函数——子字符串替换
如果传递过来的$target参数中含有 $substitutions 中的内容,那么会根据内容来进行替换
当没用添加str_replace函数时,$target字符串中的内容为正常输出的内容
添加了str_replace函数之后,输出的内容会过滤掉数组中定义的内容
输出的内容已经过滤掉了ipconfig等一些命令
stristr函数——查看字符是否首次出现并忽略大小写
这里stristr函数的作用是判断系统是否为windows nt 不区分大小写,如果是则执行命令
php_uname函数——返回运行 PHP 的系统的有关信息
代码分析
设置了黑名单$ ; |+ - ( ) 反引号 ||
。
黑名单中漏掉了关键字符|
。
impossible
源码
Command Injection Source
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// Check Anti-CSRF token
checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
// Get input
$target = $_REQUEST[ 'ip' ];
$target = stripslashes( $target );
// Split the IP into 4 octects
$octet = explode( ".", $target );
// 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 ) ) {
// If all 4 octets are int's put the IP back together.
$target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];
// 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>";
}
else {
// Ops. Let the user name theres a mistake
echo '<pre>ERROR: You have entered an invalid IP.</pre>';
}
}
// Generate Anti-CSRF token
generateSessionToken();
?>
相关函数解析
explode函数——使用一个字符串分割另一个字符串
用.将$target的内容分割
is_numeric函数——检查内容是否为数字或字符串
if判断语句
if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {
这里需要判断由.分割得到的$octet数组的内容是否都为数字,且一共只有四个,如果都成立的话那么执行命令
因为 $target通过post传递的参数应该是一个ip地址,ip地址由.分割得到四个纯数字数组