Command Execution命令执行漏洞,或称为命令注入漏洞。
安全级别为Low时,输入正确的IP地址,提交后返回ping 命令的结果
如果输入的值是非IP地址的值,则返回的是不正常结果
现在我们输入正确的ip地址以及连接命令,如输入
127.0.0.1 && net user
返回结果如下:
说明除了执行ping 命令外,还执行了net user命令,造成任意命令执行漏洞。
查看漏洞源代码
(1)Low Level Sourcecode
//Low Security Level
if( isset( $_POST[ 'submit' ] ) ) {
$target = $_REQUEST[ 'ip' ];
// Determine OS and execute the ping command.
if (stristr(php_uname('s'), 'Windows NT')) {
$cmd = shell_exec( 'ping ' . $target );
echo '
'.$cmd.'
';
} else {
$cmd = shell_exec( 'ping -c 3 ' . $target );
echo '
'.$cmd.'
';
}
}
?>
(2)Medium Level Sourcecode
<?php //Medium Security Level if( isset( $_POST[ 'submit'] ) ) { $target = $_REQUEST[ 'ip' ]; // Remove any of the charactars in the array (blacklist). $substitutions = array( '&&' => '',
';' => '',
);
$target = str_replace( array_keys( $substitutions ), $substitutions, $target );
// Determine OS and execute the ping command.
if (stristr(php_uname('s'), 'Windows NT')) {
$cmd = shell_exec( 'ping ' . $target );
echo '
'.$cmd.'
';
} else {
$cmd = shell_exec( 'ping -c 3 ' . $target );
echo '
'.$cmd.'
';
}
}
?>
(3)High Level Sourcecode
?php
//High Security Level
if( isset( $_POST[ 'submit' ] ) ) {
$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')) {
$cmd = shell_exec( 'ping ' . $target );
echo '
'.$cmd.'
';
} else {
$cmd = shell_exec( 'ping -c 3 ' . $target );
echo '
'.$cmd.'
';
}
}
else {
echo '
ERROR: You have entered an invalid IP
';
}
}
?>
综合比较三个不用等级代码发现:
同样的处理操作:判断操作系统类型,若是windows类型,执行ping ip命令;若是非windows类型,执行ping -c 3 ip 命令
Low级别:输入的ip值不经任何处理,直接作为参数传入,造成的结果就是使用命令连接字符可以输入任意命令来执行
Medium级别:相当于加了一个黑名单,将输入ip值中的 && ; 变成空,然后作为参数输入,可以有效过滤&&连接字符,但是对于黑名单之外的例如 || 字符没有过滤,依然可以构造命令注入。|| 使用就是当前边的命令执行失败后执行后边的命令。
构造命令aaaa || net user 执行结果如图:
High级别:对获取的ip值,先去下划线处理,然后根据’.’来分成数组,判断是否分成四份且每一份是数字的,然后还原回去,对ip值进行ping操作,否则判定输入ip值为非法ip格式。经过这样的处理,输入的只能是ip格式的参数,确保了执行输入参数的安全性。
命令注入漏洞防御:
(1)谨慎使用像exec(),shell_exec(),system(),passthru()等命令
(2)确保输入的参数跟命令匹配,对参数进行转义、过滤、黑白盒限制等
(3)如果有必要,避免使用系统执行函数
(4)设置safe_mode = on