XSS已知CSS (Cross Site Script) ,跨站点脚本攻击。它指的是恶意攻击者Web插入恶意网页html代码,当用户浏览网页。其中嵌入Web里面html代码运行,从而实现了一些人的攻击的目的。

例如,在那里get收到链接回加盟?id=19"><div+style%3Dwidth%3Aexpression(alert(42873))>

会导致页面错乱,甚至还会有弹出框!

以下是thinkphp里面的一段代码,用于过滤xss

ThinkPHP\Code\ThinkPHP\Common\extend.php

<?

php

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-\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{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{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
//echo RemoveXSS("<script language='javascript'>alert('hello world');</script>") ;
? >

另一种工具:HTML Purifier,上述文件的效率比翻了一倍。要了解如何葬礼,且听下回分解。

php xss过滤的更多相关文章

  1. dedecms功能性函数封装(XSS过滤、编码、浏览器XSS hack、字符操作函数)

    dedecms虽然有诸多漏洞,但不可否认确实是一个很不错的内容管理系统(cms),其他也不乏很多功能实用性的函数,以下就部分列举,持续更新,不作过多说明.使用时需部分修改,你懂的 1.XSS过滤. f ...

  2. Asp.net Mvc中利用ValidationAttribute实现xss过滤

    在网站开发中,需要注意的一个问题就是防范XSS攻击,Asp.net mvc中已经自动为我们提供了这个功能.用户提交数据时时,在生成Action参数的过程中asp.net会对用户提交的数据进行验证,一旦 ...

  3. XSS过滤JAVA过滤器filter 防止常见SQL注入

    Java项目中XSS过滤器的使用方法. 简单介绍: XSS : 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩 ...

  4. 如何在springboot项目中进行XSS过滤

    简单介绍 XSS : 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意 ...

  5. spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,guava限流,定时任务案例, 发邮件

    本文介绍spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例 集成swagger--对于做前后端分离的项目,后端只需要提供接口访问,swagger提供了接口 ...

  6. python(Django之组合搜索、JSONP、XSS过滤 )

    一.组合搜索 二.jsonp 三.xss过滤 一.组合搜索 首先,我们在做一个门户网站的时候,前端肯定是要进行搜索的,但是如果搜索的类型比较多的话,怎么做才能一目了然的,这样就引出了组合搜索的这个案例 ...

  7. 04: 使用BeautifulSoup封装的xss过滤模块

    目录: 1.1 xss攻击简介 1.2 xss攻击解决方法 1.1 xss攻击简介返回顶部 1.简介 1. 跨站脚本(cross site script)为了避免与样式css混淆,所以简称为XSS. ...

  8. Python开发【Django】:组合搜索、JSONP、XSS过滤

    组合搜索 做博客后台时,需要根据文章的类型做不同的检索 1.简单实现 关联文件: from django.conf.urls import url from . import views urlpat ...

  9. Bypass xss过滤的测试方法

    0x00 背景 本文来自于<Modern Web Application Firewalls Fingerprinting and Bypassing XSS Filters>其中的byp ...

  10. xss 过滤

    一. xss过滤 用户通过Form获取展示在终端, 提交数据,Form验证里面加入xss验证(对用户提交的内容验证是否有关键标签) from django.conf.urls import url f ...

随机推荐

  1. Android开源项目大全 - 工具类

    主要包括那些不错的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多媒体相关及其他. 一.依赖注入DI 通过依赖注入减少Vie ...

  2. haskell,lisp,erlang你们更喜欢哪个?

    haskell,lisp,erlang你们更喜欢哪个? haskell,lisp,erlang你们更喜欢哪个?

  3. Samba &amp; Nginx - Resource temporarily unavailable

    先说说本人的开发环境:Win7 + Editplus + VMware(Centos+Samba+Nginx).用Samba在Centos上把web文件夹(如www)共享,然后在Win7上訪问这个文件 ...

  4. CF 519E(树上倍增求lca)

    传送门:A and B and Lecture Rooms 题意:给定一棵树,每次询问到达点u,v距离相等的点有多少个. 分析:按情况考虑: 1.abs(deep[u]-deep[v])%2==1时, ...

  5. 为什么使用 React? Edit on GitHub

    为什么使用 React? React 是一个 Facebook 和 Instagram 用来创建用户界面的 JavaScript 库.很人多认为 React 是 MVC 中的 V(视图). 我们创造 ...

  6. Android 框架炼成 教你怎样写组件间通信框架EventBus

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/41096639 .本文出自:[张鸿洋的博客] 1.概述 关于Eventbus的介绍 ...

  7. Eclipse上运行Python,使用PyDev

    转自:http://www.ibm.com/developerworks/cn/opensource/os-cn-ecl-pydev/index.html 级别: 初级 郑 伟芳 (zhengwf@c ...

  8. 什么样的企业造什么样的软件最easy成功?

    事件1: 一般软件企业按功能分,大体分业务应用型软件和系统工具型软件. 按市场分,应用型软件企业较多,直接贴近生活:系统工具类较少,间接贴近大众较少. 事件2: 软件企业中,当中中小型企业老板存在非常 ...

  9. 使用OGG&quot;Loading data from file to Replicat&quot;的方法应该注意的问题:replicat进程是前台进程

    使用OGG的 "Loading data from file to Replicat"的方法应该注意的问题:replicat进程是前台进程 因此.最好是在vncserver中调用该 ...

  10. Git管理工具对照(GitBash、EGit、SourceTree)

    Git管理工具对照(GitBash.EGit.SourceTree) GitBash是採用命令行的方式对版本号进行管理,功能最为灵活强大,可是由于须要手动输入希望改动的文件名称,所以相对繁琐. EGi ...