背景
这里讲解HTML注入和iFrame注入,其他的本质都是HTML的改变。那么有人会问,XSS与HTML注入有啥区别呢?其实本质上都是没有区别的,改变前端代码,来攻击客户端,但是XSS可以理解为注入了富文本语言程序代码,而HTML注入只注入了超文本标记语言,不涉及富文本程序代码的问题。这里的iframe注入实际上就是一个阅读器,可以阅读通过协议加载的活服务器本地的文件。
HTML注入
在这里不在区分GET和POST,没有任何意义,只看一下反射和存储。
没有防御--简单级
一般防御--中等级
直接编码就可以绕过,不编码会被转义成HTML实体字符
完善防御--高等级
安全
代码分析
function htmli($data)
{
switch($_COOKIE["security_level"])
{
case "0" :
$data = no_check($data);
break;
case "1" :
$data = xss_check_1($data);
break;
case "2" :
$data = xss_check_3($data);
break;
default :
$data = no_check($data);
break;;
}
return $data;
}
function xss_check_1($data)
{
// Converts only "<" and ">" to HTLM entities
$input = str_replace("<", "<", $data);
$input = str_replace(">", ">", $input);
// Failure is an option
// Bypasses double encoding attacks
// <script>alert(0)</script>
// %3Cscript%3Ealert%280%29%3C%2Fscript%3E
// %253Cscript%253Ealert%25280%2529%253C%252Fscript%253E
$input = urldecode($input);
return $input;
}
function xss_check_2($data)
{
// htmlentities - converts all applicable characters to HTML entities
return htmlentities($data, ENT_QUOTES);
}
function xss_check_3($data, $encoding = "UTF-8")
{
// htmlspecialchars - converts special characters to HTML entities
// '&' (ampersand) becomes '&'
// '"' (double quote) becomes '"' when ENT_NOQUOTES is not set
// "'" (single quote) becomes ''' (or ') only when ENT_QUOTES is set
// '<' (less than) becomes '<'
// '>' (greater than) becomes '>'
return htmlspecialchars($data, ENT_QUOTES, $encoding);
}
function xss_check_4($data)
{
// addslashes - returns a string with backslashes before characters that need to be quoted in database queries etc.
// These characters are single quote ('), double quote ("), backslash (\) and NUL (the NULL byte).
// Do NOT use this for XSS or HTML validations!!!
return addslashes($data);
}
函数xss_check_1只过滤<>转移为HTML实体,但是对编码的解码放在了后面,如果把编码解码放前面呢就可以也过滤了,我们来试试。
function xss_check_1($data)
{
// Converts only "<" and ">" to HTLM entities
$input = urldecode($input);
$input = str_replace("<", "<", $data);
$input = str_replace(">", ">", $input);
// Failure is an option
// Bypasses double encoding attacks
// <script>alert(0)</script>
// %3Cscript%3Ealert%280%29%3C%2Fscript%3E
// %253Cscript%253Ealert%25280%2529%253C%252Fscript%253E
//$input = urldecode($input);
return $input;
}
xss_check_3这个函数则使用了htmlspecialchars函数能转移编码后的实体,他的作用很大,能很好的防御这类攻击。而存储型的在中级和高级难度中都使用了这个函数,所以只有初级才可以把存储的信息直接输出到页面。
iFrame 注入
在这里可以成为阅读器(www目录下所有静态文件的阅读器)和跳转和一个可以诱导客户访问某站点的一个点,利用:大小size设置为0时可以理解为无交互的刷访问、前端挖矿、挂黑链、诱导下载等等。
现象
代码分析:
<?php
if($_COOKIE["security_level"] == "1" || $_COOKIE["security_level"] == "2")
{
?>
<iframe frameborder="0" src="robots.txt" height="<?php echo xss($_GET["ParamHeight"])?>" width="<?php echo xss($_GET["ParamWidth"])?>"></iframe>
<?php
}
else
{
?>
<iframe frameborder="0" src="<?php echo xss($_GET["ParamUrl"])?>" height="<?php echo xss($_GET["ParamHeight"])?>" width="<?php echo xss($_GET["ParamWidth"])?>"></iframe>
<?php
}
?>
可以看到,当最低级的事后iframe位置可控,中高级则写死,不可控,则无法利用。又因为height和width两个参数,想构造XSS需要闭合",在xss_check_3过滤了",所以高级没戏,中、低级的确实可以。