前言
命令注入执行漏洞(Command Injection), 是程序能够调用函数, 将字符串转化为可执行代码, 且没有考虑到攻击者可以利用字符串, 造成代码执行漏洞。很难通过黑盒查找漏洞,大部分都是根据源代码判断代码执行漏洞。
具体见之前的blog: 漏洞之执行漏洞
下面对四种不同等级的漏洞进行分析:
-
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>";
}
?>
- stristr() 函数
搜索字符串在另一字符串中的第一次出现。该函数是二进制安全的, 且是不区分大小写的。如需进行区分大小写的搜索,要使用 strstr() 函数。
- php_uname()函数
返回运行 PHP 的系统的有关信息。
'a':此为默认。包含序列 "s n r v m" 里的所有模式。
's':操作系统名称。例如: FreeBSD。
'n':主机名。例如: localhost.example.com。
'r':版本名称,例如: 5.1.2-RELEASE。
'v':版本信息。操作系统之间有很大的不同。
'm':机器类型。例如:i386
可以看到, 服务端代码仅仅根据操作系统的不同, 执行相应的命令, 没有进行任何过滤, 导致了严重的命令执行漏洞。
漏洞利用
命令是用分号;分隔的, 所以尝试注入:
;ls /
也可以用&&来执行, &&当第一个命令执行成功时(返回0), 才执行&&后面的命令
baidu.com && pwd
-
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>";
}
?>
代码中利用黑名单过滤的方法来防止命令执行, 但是依然存在命令注入漏洞。
漏洞利用
采用黑名单过滤, 可以理解为一种枚举过滤, 列举出可能出现的漏洞, 并过滤之; 但是很多情况下是枚举不完的, 依旧存在漏洞
比如, 过滤了&&, 但是&依然可以:
cmd1&&cmd2 和 cmd1&cmd2的区别是:
前者是当cmd1执行成功才执行cmd2; 后者是不管cmd1执行成功与否, 都执行cmd2
-
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
$html .= "<pre>{$cmd}</pre>";
}
?>
可以看到, 在High级别中, 将一些主要的命令执行关键字( & ; | ...) 给替换为空了, 但是由于黑名单过滤的局限性, 还是存在漏洞的。
漏洞利用
仔细观察发现, 仅仅是把 | (带一个空格) 给替换为空了, 实际上并没有把 | (管道符)给过滤:
-
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
$html .= "<pre>{$cmd}</pre>";
}
else {
// Ops. Let the user name theres a mistake
$html .= '<pre>ERROR: You have entered an invalid IP.</pre>';
}
}
// Generate Anti-CSRF token
generateSessionToken();
?>
- stripslashes(string) 函数
删除字符串中的反斜杠, 并返回。
- explode(separator,string,limit) 函数
使用一个字符串分割另一个字符串,并返回由字符串组成的数组。即把字符串打散为数组。
- is_numeric(string) 函数
判断string是否是字符串或者字符串数字
可以看到在impossible级别中, 严格限制了只能输入数字(白名单), 有效地防止了命令注入; 加入了Anti-CSRF token防止CSRF攻击。