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
//http://blog.qita.in
   $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 [email protected]:alert(‘XSS‘)>
   $search = ‘abcdefghijklmnopqrstuvwxyz‘;
   $search .= ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ‘;
   $search .= ‘[email protected]#$%^&*()‘;
   $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;
}
时间: 2024-11-10 18:37:20

php XSS安全过滤代码的相关文章

PHP通用的XSS攻击过滤函数,Discuz系统中 防止XSS漏洞攻击,过滤HTML危险标签属性的PHP函数

XSS攻击在最近很是流行,往往在某段代码里一不小心就会被人放上XSS攻击的代码,看到国外有人写上了函数,咱也偷偷懒,悄悄的贴上来... 原文如下: The goal of this function is to be a generic function that can be used to parse almost any input and render it XSS safe. For more information on actual XSS attacks, check out h

XSS安全性过滤

XSS攻击很多发生在用户在可以输入的地方输入了不友好的内容,根本处理方法是在输入内容中进行过滤 PHP或者java,基本都有现成的jar包或者php框架,调用自动过滤用户的输入内容,避免了XSS 防御的方式有以下几种: 1.HttpOnly防止劫取Cookie HttpOnly最早由微软提出,至今已经成为一个标准.浏览器将禁止页面的Javascript访问带有HttpOnly属性的Cookie.很多网站将用来认证的cookies设置为httponly 很多简单的xss攻击就是输入js来获取coo

XSS攻击过滤处理

关于XSS攻击 XSS是一种经常出现在web应用中的计算机安全漏洞,它允许恶意web用户将代码植入到提供给其它用户使用的页面中. XSS漏洞的危害 网络钓鱼,包括盗取各类用户账号: 窃取用户cookies资料,从而获取用户隐私信息,或利用用户身份进一步对网站执行操作: 劫持用户(浏览器)会话,从而执行任意操作,例如进行非法转账.强制发表日志.发送电子邮件等: 强制弹出广告页面.刷流量等: 网页挂马: 进行恶意操作,例如任意篡改页面信息.删除文章等: 进行大量的客户端攻击,如DDoS攻击: 获取客

PHP 文件系统管理函数与 preg_replace() 函数过滤代码

案例:在带行号的代码至文件 crop.js 中.用两种方法去掉代码前面的行号,带行号的代码片段: 1.$(function(){ 2. //初始化图片区域 3. var myimg = new Image(); 4. myimg.src = $("#mypic2").attr("src"); 5. //输出图片数据 6. $("#showSize").html(myimg.width + "×" + myimg.height)

Python Django开发中XSS内容过滤问题的解决

from:http://stackoverflow.com/questions/699468/python-html-sanitizer-scrubber-filter 通过下面这个代码就可以把内容过滤成干净的HTML内容,说明,这个代码来自上面Stackoverflow的回答 Use lxml.html.clean! It's VERY easy! from lxml.html.clean import clean_html print clean_html(html) <html> <

C#敏感关键词过滤代码

System.Text.StringBuilder sb = new System.Text.StringBuilder(text.Length);             string filterText = "需要过滤的脏字 以|分开";//脏字 可根据自己的方式用分隔符                string[] filterData = filterText.Split('|');             foreach (var item in filterData)

文本XSS攻击过滤

在FCK或百度编辑器等常用富文本编辑器中,通常是会被XSS攻击 处理方法: 文本框模拟输入了以下文本 <span style="dispaly:none" onclick="atk();">我是一个span</span> <p style="dispaly:none" onclick="atk();">我是一个p</p> <b style="dispaly:non

DWM1000 帧过滤代码实现

帧过滤功能可以在同一个环境内组建多个网络而不干扰(非频段不同),可以通过PANID(网络ID)区分不同网络,不同网络中的模块无法直接通信, 再之,利用短地址,网络中可以同时有多个模块发送信息,而接收端会根据信息短地址不同而自动过滤. 1 发送端部分,组建发送结构体,按照MAC 数据结构组织. srd_msg_dsss msg_f ; // ranging message frame with 16-bit addresses int psduLength = 0; //set frame typ

XSS - html过滤

JS 根据白名单过滤HTML http://jsxss.com/zh/index.html   方案一: java的一个方案, 可以参考:  http://winnie825.iteye.com/blog/1170833 用xml保存一些过滤信息 <?xml version="1.0" encoding="UTF-8"?> <XSSConfig>     <!-- 是否进行header校验 -->     <isCheckH