摘要
包含Command Injection四个级别(Low,Medium,High,Impossible),浅述了前三个级别的漏洞及其利用方式,解读了Impossible的代码
Low
简要:没有对输入做任何限制
漏洞:存在注入漏洞
方案:使用 &&
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// 获取输入信息,没有处理输入信息,存在漏洞,例如输入127.0.0.1&&echo 'hello world!'
$target = $_REQUEST[ 'ip' ];
// 使用php_uname('s')获取OS信息,并使用stristr匹配,成功返回true,失败返回false
// 使用shell_exec()执行命令
if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
// Windows
$cmd = shell_exec( 'ping ' . $target );
}
else {
// *nix
$cmd = shell_exec( 'ping -c 4 ' . $target );
}
// 显示执行结果
echo "<pre>{$cmd}</pre>";
}
?>
Medium
简要:设置了置换列表,禁止使用&&,;
漏洞:存在注入漏洞
方案 1:使用 &(置换列表并没有置换 &,仍可以127.0.0.1&echo ‘hello world!’。注:&是与运算,&&是且运算)
方案 2:使用 &;&(置换列表会将 ; 置换为 ‘’ ,这时127.0.0.1&;&echo ‘hello world!’ 就会变成 127.0.0.1&&echo ‘hello world!’)
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
$target = $_REQUEST[ 'ip' ];
// 设置置换列表
$substitutions = array(
'&&' => '',
';' => '',
);
// 将输入中存在的列表的键改为其对应的值
$target = str_replace( array_keys( $substitutions ), $substitutions, $target );
// 使用php_uname('s')获取OS信息,并使用stristr匹配,成功返回true,失败返回false
// 使用shell_exec()执行命令
if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
// Windows
$cmd = shell_exec( 'ping ' . $target );
}
else {
// *nix
$cmd = shell_exec( 'ping -c 4 ' . $target );
}
// 显示执行结果
echo "<pre>{$cmd}</pre>";
}
?>
High
简要:在Medium的基础上,完善了置换列表
漏洞:注入漏洞
方案:见注释
<?php
$substitutions = array(
'&' => '',
';' => '',
'| ' => '', // ' |'没有过滤,可以利用:127.0.0.1 |echo 'hello world!'(注:|管道符将命令隔开,其左边命令执行的输出成为右边命令的输入)
'-' => '',
'$' => '',
'(' => '',
')' => '',
'`' => '',
'||' => '',
);
?>
Impossible 代码浅析
简要:通过对输入的字符串进行拆分处理,判断字符串是否为 IP地址,从源头上解决注入漏洞
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// Check Anti-CSRF token
// 加入token认证机制
checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
// 获取输入信息
$target = $_REQUEST[ 'ip' ];
$target = stripslashes( $target ); // 去掉反斜杆 '\'
// 将$target当作IP地址对待,以 . 拆分为数组
$octet = explode( ".", $target );
// 利用is_numeric()分别判断拆分出的数组的前四个值是否为数字
if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {
// 如果都是数字,拼接这些数字,还原成IP地址
$target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];
// 使用php_uname('s')获取OS信息,并使用stristr匹配,成功返回true,失败返回false
// 使用shell_exec()执行命令
if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
// Windows
$cmd = shell_exec( 'ping ' . $target );
}
else {
// *nix
$cmd = shell_exec( 'ping -c 4 ' . $target );
}
// 显示执行结果
echo "<pre>{$cmd}</pre>";
}
else {
// 输入的字符串不是IP地址,反馈错误信息
echo '<pre>ERROR: You have entered an invalid IP.</pre>';
}
}
// Generate Anti-CSRF token
generateSessionToken();
?>
本文详细剖析了命令注入漏洞的四个等级(Low、Medium、High、Impossible),并提供了每个等级的漏洞利用方法及代码示例。从简单的无限制输入到复杂的输入验证策略,展示了如何逐步提高应用程序的安全性。
7148

被折叠的 条评论
为什么被折叠?



