Command injection(命令注入)
介绍:
命令执行漏洞的产生原因一般就是将用户输入未经过滤或者过滤不严就直接当作系统命令进行执行,我们可以通过批处理中的一些技巧来一次执行多条命令,这样就可以执行任意命令。在命令执行中,常用的命令连接符号有五个:&& & || | ;
&&:
前一个指令执行成功,后面的指令才继续执行,就像进行与操作一样
||
:前一个命令执行失败,后面的才继续执行,类似于或操作
&
:直接连接多个命令
|
:管道符,将前一个命令的输出作为下一个命令的输入
;
:直接连接多个命令
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
echo "<pre>{$cmd}</pre>";
}
?>
漏洞利用
window和linux系统都可以用&&来执行多条命令
192.168.5.5 && net user
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
echo "<pre>{$cmd}</pre>";
}
?>
可以看到,相比Low级别的代码,服务器端对ip参数做了一定过滤,即把”&&” 、”;”删除,本质上采用的是黑名单机制,因此依旧存在安全问题。
漏洞利用
1、192.168.5.5&net user因为被过滤的只有”&&”与” ;”,所以”&”不会受影响。
这里需要注意的是”&&”与”&”的区别:
(1)Command 1&&Command 2
先执行Command 1,执行成功后执行Command 2,否则不执行Command 2
(2)Command 1&Command 2
先执行Command 1,不管是否成功,都会执行Command 2
2、由于使用的是str_replace把”&&” 、”;”替换为空字符,因此可以采用以下方式绕过:
192.168.5.5&;&ipconfig
这是因为”192.168.5.5&;&ipconfig”中的” ;”会被替换为空字符,这样一来就变成了”192.168.5.5&& ipconfig” ,会成功执行。
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>";
}
?>
相比Medium级别的代码,High级别的代码进一步完善了黑名单,但由于黑名单机制的局限性,我们依然可以绕过。
漏洞利用
黑名单看似过滤了所有的非法字符,但仔细观察到是把”| ”(注意这里|后有一个空格)替换为空字符,于是 ”|”成了“漏网之鱼”。
192.168.5.5 | net user