PHP中跨站脚本的过滤RemoveXss函数

PHP中跨站脚本的过滤RemoveXss函数

RemoveXss函数主要用于跨站脚本的过滤 //Remove the exploer'bug XSS 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; }

转载于:https://my.oschina.net/sharesuiyue/blog/269152

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109822.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java实现单例模式的9种方法

    Java实现单例模式的9种方法人工智能,零基础入门!http://www.captainbed.net/inner一.什么是单例模式因进程需要,有时我们只需要某个类同时保留一个对象,不希望有更多对象,此时,我们则应考虑单例模式的设计。二.单例模式的特点1、单例模式只能有一个实例。2、单例类必须创建自己的唯一实例。3、单例类必须向其他对象提供这一实例。三.单例模式VS静态类在知道了什么是…

    2022年7月8日
    18
  • 总结几个对象转数组的方法是_js将对象转为数组

    总结几个对象转数组的方法是_js将对象转为数组目标效果:obj={0:’a’,1:’b’,2:’c’}=&gt;ayy=[‘a’,’b’,’c’]1、Array.from(object)注:1️⃣object中必须有length属性,返回的数组长度取决于length长度2️⃣key值必须是数值2、Object.values(object)注:与第一种不同的…

    2022年9月12日
    0
  • 怎么新建pytest的ini文件_bikini

    怎么新建pytest的ini文件_bikini前言pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行查看pytest.ini的配置选项pytest-h找到以下

    2022年7月31日
    38
  • sendip linux发包工具

    sendip linux发包工具第一步:安装sendip工具sudoapt-getupdatesudoapt-getinstallsendipmansendip//可以查看sendip的使用方法第二步:使用开启两台虚拟机,在其中一台执行sendip命令,在另一台抓包分析sendip命令格式:sendip网络层传输层数据domainsendip-v-p***-is***-id***-p***-f/-d***

    2022年9月4日
    2
  • 三次握手四次挥手例题(tcp三次握手原理)

    在面试中,三次握手和四次挥手可以说是问的最频繁的一个知识点了,我相信大家也都看过很多关于三次握手与四次挥手的文章,今天的这篇文章,重点是围绕着面试,我们应该掌握哪些比较重要的点,哪些是比较被面试官给问到的,我觉得如果你能把我下面列举的一些点都记住、理解,我想就差不多了。三次握手当面试官问你为什么需要有三次握手、三次握手的作用、讲讲三次三次握手的时候,我想很多人会这样回答:首先很多人会先讲下握…

    2022年4月10日
    113
  • whith ~ as 用法[通俗易懂]

    whith ~ as 用法[通俗易懂]个人理解withself.client.get("/",catch_response=True)asresponse:其实就是response=self.clien

    2022年8月3日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号