这里提供一个过滤非法脚本的函数:

function RemoveXSS($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-\x20])/', '', $val);

// straight replacements, the user should never need these since they're normal characters 

   // this prevents like <IMG SRC=&#X40&#X61&#X76&#X61&#X73&#X63&#X72&#X69&#X70&#X74&#X3A&#X61&#X6C&#X65&#X72&#X74&#X28&#X27&#X58&#X53&#X53&#X27&#X29> 

   $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

// &#x0040 @ search for the hex values 

      $val = preg_replace('/(&#[x|X]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{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 .= '(&#[x|X]0{0,8}([9][a][b]);?)?'; 

               $pattern .= '|(&#0{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; 
         } 
      } 
   } 
}  

PHP 一个可以过滤非法脚本的函数的更多相关文章

  1. 【Nginx】开发一个HTTP过滤模块

    与HTTP处理模块不同.HTTP过滤模块的工作是对发送给用户的HTTP响应做一些加工. server返回的一个响应能够被随意多个HTTP过滤模块以流水线的方式依次处理.HTTP响应分为头部和包体,ng ...

  2. 05- Shell脚本学习--函数

    函数可以让我们将一个复杂功能划分成若干模块,让程序结构更加清晰,代码重复利用率更高.像其他编程语言一样,Shell 也支持函数.Shell 函数必须先定义后使用. 函数定义 Shell 函数的定义格式 ...

  3. 一个不错的shell 脚本教程 入门级

    一个很不错的bash脚本编写教程,至少没接触过BASH的也能看懂     建立一个脚本 Linux中有好多中不同的shell,但是通常我们使用bash (bourne again shell) 进行s ...

  4. 使用Filter过滤非法内容

    1.首先,需要编写一个响应的封装器ResponseReplaceWrapper,用它来缓存response中的内容,代码如下: ResponseReplaceWrapper.java package ...

  5. 如何用python的装饰器定义一个像C++一样的强类型函数

        Python作为一个动态的脚本语言,其函数在定义时是不需要指出参数的类型,也不需要指出函数是否有返回值.本文将介绍如何使用python的装饰器来定义一个像C++那样的强类型函数.接下去,先介绍 ...

  6. 程序猿修仙之路--数据结构之你是否真的懂数组? c#socket TCP同步网络通信 用lambda表达式树替代反射 ASP.NET MVC如何做一个简单的非法登录拦截

    程序猿修仙之路--数据结构之你是否真的懂数组?   数据结构 但凡IT江湖侠士,算法与数据结构为必修之课.早有前辈已经明确指出:程序=算法+数据结构  .要想在之后的江湖历练中通关,数据结构必不可少. ...

  7. 一个事件激活多个JavaScript函数

    http://www.cnblogs.com/meil/archive/2006/09/20/509359.html如果你的网页中一个“OnLoad”事件要激活两个以上的JavaScript函数,那怎 ...

  8. js 如何生成一个不重复的ID的函数

    在MongoDB中的ObjectID,可以理解为是一个不会重复的ID,这里有个链接http://www.jb51.net/article/101164.htm感兴趣可以去研究一下. 我今天要做的就是做 ...

  9. SpringBoot过滤XSS脚本攻击

    XSS攻击是什么 XSS攻击全称跨站脚本攻击,是为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS,XSS是一种在web应用中的计算机安 ...

随机推荐

  1. 关于 ES6箭头函数

    转自  http://simplyy.space/article/577c5b0dcbe0a3e656c87c24 多个连续的箭头函数与柯里化 高阶函数 高阶函数定义:将函数作为参数或者返回值是函 ...

  2. 获取Spring-boot系统环境变量方法

    public static ConfigurableApplicationContext context = null; public static void main( String[] args ...

  3. MeasureSpec介绍

    在自定义View和ViewGroup的时候,我们经常会遇到int型的MeasureSpec来表示一个组件的大小,这个变量里面不仅有组件的尺寸大小,还有大小的模式. 这个大小的模式,有点难以理解.在系统 ...

  4. python django第二弹

    每天晚上应该就这样坐着,然后把每天的东西做个总结,或大或小,有的人可能愿意把自己的东西保留在草稿箱,想想我还是把他写出来吧,前几次我发现又遇到了之前遇到的简单的问题,翻看自己之前写的几篇小日记,可以很 ...

  5. Java汉字转成汉语拼音工具类

    Java汉字转成汉语拼音工具类,需要用到pinyin4j.jar包. import net.sourceforge.pinyin4j.PinyinHelper; import net.sourcefo ...

  6. 关于JDK中的总结和基本知识总结

    人机交互的图形化界面(GUI) 命令行方式(CLI  command line interface) JDK有不同的版本(linux,mac os, windows) Java 的跨平台性. 软件放到 ...

  7. 【暴力,STL,水】UVa 1523 - Helicopter

    Since ancient time, people have been dreaming of flying in the sky. Eventually, the dream was realiz ...

  8. Java Concurrency - ReentrantLock

    ReentrantLock 是可重入的互斥锁,它具有与使用 synchronized 方法和语句所访问的隐式监视器锁相同的一些基本行为和语义,但功能更强大. ReentrantLock 将由最近成功获 ...

  9. Xcode Product -> Archive disabled

    You've changed your scheme destination to a simulator instead of "iOS Device". That's why ...

  10. IIS实现301重定向

    301永久重定向对SEO无任何不好的影响,而且网页A的关键词排名和PR级别都会传达给网页B,网站更换了域名,表示本网页永久性转移到另一个地址,对于搜索引擎优化|SEO来说,给搜索引擎一个友好的信息,告 ...