对网站发动 XSS 攻击的方式有很多种, 仅仅使用 php 的一些内置过滤函数是对付不了的, 即使你将 filter_var,mysql_real_escape_string,htmlentities,htmlspecialchars,strip_tags 这些函数都使用上了也不一定能保证绝对的安全
那么如何预防 XSS 注入? 主要还是需要在用户数据过滤方面得考虑周全, 在这里不完全总结下几个 Tips
1. 假定所有的用户输入数据都是邪恶的
2. 弱类型的脚本语言必须保证类型和期望的一致
3. 考虑周全的正则表达式
4. strip_tagshtmlspecialchars 这类函数很好用
5. 外部的 Javascript 不一定就是可靠的
6. 引号过滤必须要重点注意
7. 除去不必要的 HTML 注释
8. Exploer 求你放过我吧
方法一, 利用 php htmlentities 函数
php 防止 XSS 跨站脚本攻击的方法: 是针对非法的 HTML 代码包括单双引号等, 使用 htmlspecialchars()函数
在使用 htmlspecialchars()函数的时候注意第二个参数, 直接用 htmlspecialchars($string) 的话, 第二个参数默认是 ENT_COMPAT, 函数默认只是转化双引号 (), 不对单引号() 做转义.
所以, htmlspecialchars 函数更多的时候要加上第二个参数, 应该这样用: htmlspecialchars($string,ENT_QUOTES). 当然, 如果需要不转化如何的引号, 用 htmlspecialchars($string,ENT_NOQUOTES).
另外, 尽量少用 htmlentities, 在全部英文的时候 htmlentities 和 htmlspecialchars 没有区别, 都可以达到目的. 但是, 中文情况下, htmlentities 却会转化所有的 html 代码, 连同里面的它无法识别的中文字符也给转化了
htmlentities 和 htmlspecialchars 这两个函数对 之类的字符串支持不好, 都不能转化, 所以用 htmlentities 和 htmlspecialchars 转化的字符串只能防止 XSS 攻击, 不能防止 SQL 注入攻击.
所有有打印的语句如 echo,print 等 在打印前都要使用 htmlentities() 进行过滤, 这样可以防止 Xss, 注意中文要写出 htmlentities($name,ENT_NOQUOTES,GB2312)
方法二, 什么也不多说我们给一个函数
- function xss_clean($data) {
- // Fix &entity\n;
- $data = str_replace(array( & , <, >), array( & amp;, <, >), $data);
- $data = preg_replace(/ ( & # * \w + )[\x00 - \x20] + ;
- /u, $1;, $data);
- $data = preg_replace(/ ( & #x * [0 - 9A - F] + ); * /iu, $1;, $data);
- $data = html_entity_decode($data, ENT_COMPAT, UTF - 8);
- / /Remove any attribute starting with "on"or xmlns $data = preg_replace(# ( < [ ^ >] + ?[\x00 - \x20 "\])(?:on|xmlns)[^>]*+>#iu,$1>,$data);
- // Remove javascript: and vbscript: protocols
- $data=preg_replace(#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\"] * )[\x00 - \x20] * j[\x00 - \x20] * a[\x00 - \x20] * v[\x00 - \x20] * a[\x00 - \x20] * s[\x00 - \x20] * c[\x00 - \x20] * r[\x00 - \x20] * i[\x00 - \x20] * p[\x00 - \x20] * t[\x00 - \x20] * :#iu, $1 = $2nojavascript..., $data);
- $data = preg_replace(# ([a - z] * )[\x00 - \x20] *= ([\"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu,$1=$2novbscript...,$data);
- $data=preg_replace(#([a-z]*)[\x00-\x20]*=([\"] * )[\x00 - \x20] * -moz - binding[\x00 - \x20] * :#u, $1 = $2nomozbinding..., $data);
- // Only works in IE: <span style="width: expression(alert(Ping!));"></span>
- $data = preg_replace(# ( < [ ^ >] + ?) style[\x00 - \x20] *= [\x00 - \x20] * [`\"]*.*?expression[\x00-\x20]*\([^>]*+>#i,$1>,$data);
- $data=preg_replace(#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\"] * . * ?behaviour[\x00 - \x20] * \ ([ ^ >] * +>#i, $1 > , $data); $data = preg_replace(# ( < [ ^ >] + ?) style[\x00 - \x20] *= [\x00 - \x20] * [`\"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu,$1>,$data);
- // Remove namespaced elements (we do not need them)
- $data=preg_replace(#</*\w+:\w[^>]*+>#i,,$data);
- do{// Remove really unwanted tags
- $old_data=$data;
- $data=preg_replace(#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i,,$data);
- }while($old_data!==$data);
- // we are done...
- return $data;
- }"
方法三, php 防注入和 XSS 攻击通用过滤
- function SafeFilter (&$arr)
- {
- $ra=Array(/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/,/script/,/javascript/,/vbscript/,/expression/,/applet/,/meta/,/xml/,/blink/,/link/,/style/,/embed/,/object/,/frame/,/layer/,/title/,/bgsound/,/base/,/onload/,/onunload/,/onchange/,/onsubmit/,/onreset/,/onselect/,/onblur/,/onfocus/,/onabort/,/onkeydown/,/onkeypress/,/onkeyup/,/onclick/,/ondblclick/,/onmousedown/,/onmousemove/,/onmouseout/,/onmouseover/,/onmouseup/,/onunload/);
- if (is_array($arr))
- {
- foreach ($arr as $key => $value)
- {
- if (!is_array($value))
- {
- if (!get_magic_quotes_gpc()) // 不对 magic_quotes_gpc 转义过的字符使用 addslashes(), 避免双重转义
- {
- $value = addslashes($value); // 给单引号 () 双引号 (") 反斜线 (\) 与 NUL(NULL 字符)加上反斜线转义
- }
- $value = preg_replace($ra,,$value); // 删除非打印字符, 粗暴式过滤 xss 可疑字符串
- $arr[$key] = htmlentities(strip_tags($value)); // 去除 HTML 和 PHP 标记并转换为 HTML 实体
- }
- else
- {
- SafeFilter($arr[$key]);
- }
- }
- }
- }
来源: http://www.bubuko.com/infodetail-2499914.html