php实现XSS安全过滤的方法


Posted in PHP onJuly 29, 2015

本文实例讲述了php实现XSS安全过滤的方法。分享给大家供大家参考。具体如下:

function remove_xss($val) {
  // remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
  // this prevents some character re-spacing such as <java\0script>
  // note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs
  $val = preg_replace('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '', $val);
  // straight replacements, the user should never need these since they're normal characters
  // this prevents like <IMG SRC=@avascript:alert('XSS')>
  $search = 'abcdefghijklmnopqrstuvwxyz';
  $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  $search .= '1234567890!@#$%^&*()';
  $search .= '~`";:?+/={}[]-_|\'\\';
  for ($i = 0; $i < strlen($search); $i++) {
   // ;? matches the ;, which is optional
   // 0{0,7} matches any padded zeros, which are optional and go up to 8 chars
   // @ @ search for the hex values
   $val = preg_replace('/(&#[xX]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ;
   // @ @ 0{0,7} matches '0' zero to seven times
   $val = preg_replace('/(�{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ;
  }
  // now the only remaining whitespace attacks are \t, \n, and \r
  $ra1 = array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
  $ra2 = array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload');
  $ra = array_merge($ra1, $ra2);
  $found = true; // keep replacing as long as the previous round replaced something
  while ($found == true) {
   $val_before = $val;
   for ($i = 0; $i < sizeof($ra); $i++) {
     $pattern = '/';
     for ($j = 0; $j < strlen($ra[$i]); $j++) {
      if ($j > 0) {
        $pattern .= '(';
        $pattern .= '(&#[xX]0{0,8}([9ab]);)';
        $pattern .= '|';
        $pattern .= '|(�{0,8}([9|10|13]);)';
        $pattern .= ')*';
      }
      $pattern .= $ra[$i][$j];
     }
     $pattern .= '/i';
     $replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2); // add in <> to nerf the tag
     $val = preg_replace($pattern, $replacement, $val); // filter out the hex tags
     if ($val_before == $val) {
      // no replacements were made, so exit the loop
      $found = false;
     }
   }
  }
  return $val;
}

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
php设计模式 Composite (组合模式)
Jun 26 PHP
php读取mysql乱码,用set names XXX解决的原理分享
Dec 29 PHP
用Php编写注册后Email激活验证的实例代码
Mar 11 PHP
php判断GIF图片是否为动画的方法
Sep 04 PHP
19个Android常用工具类汇总
Dec 30 PHP
WordPress中用于检索模版的相关PHP函数使用解析
Dec 15 PHP
深入理解PHP中mt_rand()随机数的安全
Oct 12 PHP
PHP对称加密算法(DES/AES)类的实现代码
Nov 14 PHP
Laravel框架基于ajax和layer.js实现无刷新删除功能示例
Jan 17 PHP
Laravel中Kafka的使用详解
Mar 24 PHP
如何判断微信付款码和支付宝付款码
Apr 01 PHP
详解Laravel制作API接口
May 31 PHP
php检查字符串中是否有外链的方法
Jul 29 #PHP
php数组比较实现查找连续数的方法
Jul 29 #PHP
PHP实现XML与数据格式进行转换类实例
Jul 29 #PHP
PHP获取某个月最大天数(最后一天)的方法
Jul 29 #PHP
discuz图片顺序混乱解决方案
Jul 29 #PHP
php计算title标题相似比的方法
Jul 29 #PHP
PHP实现简单实用的验证码类
Jul 29 #PHP
You might like
加速XP搜索功能堪比vista
2007/03/22 PHP
php入门学习知识点七 PHP函数的基本应用
2011/07/14 PHP
深入PHP运行环境配置的详解
2013/06/04 PHP
浅析php插件 Simple HTML DOM 用DOM方式处理HTML
2013/07/01 PHP
基于PHP的简单采集数据入库程序
2014/07/30 PHP
php如何把表单内容提交到数据库
2019/07/08 PHP
phpstorm激活码2020附使用详细教程
2020/09/25 PHP
符合W3C网页标准的iframe标签的使用方法
2007/07/19 Javascript
Exjs 入门篇
2010/04/07 Javascript
JS获取并操作iframe中元素的方法
2013/03/21 Javascript
JavaScript极简入门教程(一):基础篇
2014/10/25 Javascript
jQuery简单动画变换效果实例分析
2016/07/04 Javascript
jQuery元素属性操作实例(设置、获取及删除元素属性)
2016/09/08 Javascript
Angular2 组件通信的实例代码
2017/06/23 Javascript
引入JavaScript时alert弹出框显示中文乱码问题
2017/09/16 Javascript
js实现磁性吸附的示例
2020/10/26 Javascript
[19:14]DOTA2 HEROS教学视频教你分分钟做大人-维萨吉
2014/06/24 DOTA
[05:09]2016国际邀请赛中国区预选赛淘汰赛首日精彩回顾
2016/06/29 DOTA
[01:06:26]全国守擂赛第二周 Team Coach vs DeMonsTer
2020/04/28 DOTA
Python单例模式的两种实现方法
2017/08/14 Python
Python中的单行、多行、中文注释方法
2018/07/19 Python
Python3 无重复字符的最长子串的实现
2019/10/08 Python
pytorch实现特殊的Module--Sqeuential三种写法
2020/01/15 Python
Python基于内置库pytesseract实现图片验证码识别功能
2020/02/24 Python
SIDESTEP荷兰:在线购买鞋子
2019/11/18 全球购物
美国踏板车和轻便摩托车销售网站:Mega Motor Madness
2020/02/26 全球购物
Huda Beauty官方商店:化妆和美容产品
2020/09/05 全球购物
优秀的2014年两会精神解读
2014/03/17 职场文书
安全生产专项整治方案
2014/05/06 职场文书
合作协议书范文
2014/08/20 职场文书
新闻学专业职业生涯规划范文:我的人生我做主
2014/09/12 职场文书
学生不参加考试检讨书
2015/02/19 职场文书
党员身份证明材料
2015/06/19 职场文书
PHP实现考试倒计时功能代码
2021/04/16 PHP
Python排序算法之插入排序及其优化方案详解
2021/06/11 Python
python井字棋游戏实现人机对战
2022/04/28 Python