0x01 Low级别
黑盒测试
Ping a device:输入IP之后调用系统ping命令,ping相应的IP
从前段的学习中知道,命令可以连接执行。输入192.168.115.129 && net user
最后执行的命令为:ping 192.168.115.129 && net user
源码分析
<?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
echo "<pre>{$cmd}</pre>";
}
?>
stristr() 函数:搜索字符串在另一字符串中的第一次出现。
php_uname:返回运行 PHP 的系统的有关信息 。
服务器通过判断操作系统执行不同ping命令,但是对ip参数没有做任何过滤,造成命令注入。
0x02 Medium级别
黑盒测试
low级别的注入命令已经不好使了,但是命令连接符不止一个,尝试其他如下所示:
A&B | A&&B | A|B | A||B |
---|---|---|---|
AB间无约束关系 | A执行成功,然后执行B | A的输出作为B的输入 | A执行失败,然后执行B |
输入192.168.115.129 & net user或192.168.115.129 | net user 都可以成功执行
源码分析
<?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
echo "<pre>{$cmd}</pre>";
}
?>
Medium采用黑名单机制,将’&&’、’;'转义为空,但同时具备黑名单机制的缺陷。
Tips:网上看到一个大佬的操作,由于将’&&’、’;‘转义为空,可以使用’&;&'进行绕过。
这样一来192.168.115.129 &;& net user 就转义为192.168.115.129 && net user
0x03 High级别
源码分析
<?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>";
}
?>
黑盒测试失败后查看源码的过滤规则。看似完全过滤,仔细查看发现其中有一个’| ‘,抱着试一试的心态输入’ |’,成功注入。
0x04 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();
?>
stripslashes()函数:删除字符串string中的反斜杠,返回已剥离反斜杠的字符串 。
explode(separator,string,limit) 函数:把字符串打散为数组。 参数separator规定在哪里分割字符串;参数string是要分割的字符串;参数limit:规定所返回的数组元素的数目。
is_numeric()函数:用于检测变量是否为数字或数字字符串。
stripslashes()函数:删除字符串string中的反斜杠,返回已剥离反斜杠的字符串 。
explode(separator,string,limit) 函数:把字符串打散为数组。 参数separator规定在哪里分割字符串;参数string是要分割的字符串;参数limit:规定所返回的数组元素的数目。
is_numeric()函数:用于检测变量是否为数字或数字字符串。
Impossible级别加入了Anti-CSRF token,同时对参数进行严格限制,不存在命令注入漏洞。