一、Command Injection(命令执行)
命令执行漏洞是指服务器没有对执行的命令进行过滤,用户可以随意执行系统命令。
1.漏洞原理
由于开发人员编写源码,没有针对代码中可执行的特殊函数入口做过滤,导致客户端可以提交恶意构造语句提交,并交由服务器端执行。命令注入攻击中WEB服务器没有过滤类似system(),eval(),exec()等函数是该漏洞攻击成功的最主要原因。
2.命令链接符
&& (逻辑与):只有 && 前面的命令执行成功后,它后面的命令才被执行
|| (逻辑或):只有 || 前面的命令执行失败后,它后面的命令才被执行
& (后台任务符):同时执行所有命令
| (管道符):当第一条命令失败时,它仍然会执行第二条命令
; 命令按照顺序(从左到右)被执行,并且可以用分号进行分隔。当有一条命令执行失败时,不会中断其它命令的执行
利用:可尝试进行反弹shell,搭建服务执行wget下载webshell
常见的cmd命令:
whoami 查看当前用户名
ipconfig 查看网卡信息
shutdown -s -t 0 关机
net user [username] [password] /add 增加一个用户名为username密码为password的新用户
type [file_name] 查看file_name文件内容
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>";
}
?>
我们可以看到,low级别的代码接收了用户输入的ip,根据服务器是否是Windows NT系统,对目标ip进行不同的ping测试。但这里对用户输入的ip并没有进行任何的过滤,所以我们可以进行命令执行漏洞的测试。
查看本机ip地址
ping一下百度
执行 127.0.0.1 & net user xie /add ,尝试ping完后新建一个用户,哈哈哈,结果被火绒给拦截了
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>";
}
?>
medium级别在代码上增加量了对 && 和 ;的过滤,但这并不会影响其他符号正常使用。
渗透测试解决方法:
白盒测试:可以看到源代码,查看源代码过滤机制
黑盒测试:看不到源代码,依次尝试常见命令连接符
我们用127.0.0.1 & 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>";
}
?>
我们可以看到,High级别的代码进行了过滤,把一些常见的命令连接符给过滤了。看似都过滤了,
但注意看,发现'I ' I后边一个空格,这是写代码的时候粗心大意。I可以利用。
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) : 该函数会删除字符串string中的反斜杠 \
explode(separator,string,limit): 该函数把字符串打散为数组,返回字符串的数组。
参数separator规定在哪里分割字符串,参数string是要分割的字符串,可选参数limit规定所返回的数组元素的数目。
比如127.0.0.1,分成127 0 0 1 分别判定他们是不是数字,不是的直接不允许。
为数字 就允许通过,在拼成127.0.0.1
is_numeric(string): 该检测string是否为数字或数字字符串,如果是返回TRUE,否则返回FALSE。
可以看到,Impossible级别的代码加入了Anti-CSRF token,同时对参数ip进行了严格的限制,只有诸如“数字.数字.数字.数字”的输入才会被接收执行,因此不存在命令注入漏洞。