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)
上一篇 2021年8月29日 下午5:00
下一篇 2021年8月29日 下午6:00


相关推荐

  • tomcat配置和使用

    tomcat配置和使用tomcat配置:https://blog.csdn.net/qq_39081974/article/details/78803919配置问题解决:https://blog.csdn.net/gyp0307/article/details/821945261、JavaWebJavaweb,是用java技术来解决相关web互联网领域的技术的总称。web包括:web服务器和web客户…

    2022年7月18日
    20
  • 韦根协议及IO模拟韦根34接口

    韦根协议及IO模拟韦根34接口1 写在前面韦根 Wiegand 协议是国际上统一的标准 是由摩托罗拉公司制定的一种通讯协议 它适用于涉及门禁控制系统的读卡器和卡片的许多特性 韦根有许多种类格式 常见有 26 bit 34 bit 37 bit 格式 其中 26 bit 格式是最常用的格式 而标准 26 bit 格式是一个开放式的格式 这就意味着任何人都可以购买某一特定格式的 HID 卡 并且这些特定格式的种类是公开可选的 26 Bi

    2026年3月20日
    2
  • 数据分析sql面试必会6题经典_数据分析师SQL面试必备50题[通俗易懂]

    数据分析sql面试必会6题经典_数据分析师SQL面试必备50题[通俗易懂]以下是SQL面试必备的经典的50道题目,每道题都有博主本人的解题思路和对应的SQL语句。每道题的思路与答案均为博主本人主观理解,仅供参考。环境:MySQL8.0可视化工具:Navicat1、查询课程编号为01的课程比02的课程高的所有学生的学号和成绩解题思路:(1)先把课程为01的学号和成绩找出来as表a(2)再把课程为02的学号和成绩找出来as表b(3)用innerjoin将表a…

    2022年6月28日
    25
  • powershell安装openclaw

    powershell安装openclaw

    2026年3月14日
    2
  • 数组splice_数组concat方法

    数组splice_数组concat方法有三个方法经常会混淆,但是了解它后你会发现很好区分splice方法是数组特有的方法spite方法是字符串特有的方法slice方法是字符串和数组共同都有的方法这里介绍下数组的常用方法数组的splice方法splice()函数用于从当前数组中移除一部分连续的元素。如有必要,还可以在所移除元素的位置上插入一个或多个新的元素。该函数以数组形式返回从当前数组中被移除的元素。该…

    2026年3月10日
    5
  • 序列化和反序列化的详解[通俗易懂]

    序列化和反序列化的详解[通俗易懂]一、基本概念1、序列化和反序列化的定义:  (1)Java序列化就是指把Java对象转换为字节序列的过程    Java反序列化就是指把字节序列恢复为Java对象的过程。  (2)序列化最重要的作用:在传递和保存对象时.保证对象的完整性和可传递性。对象转换为有序字节流,以便在网络上传输或者保存在本地文件中。    反序列化的最重要的作用:根据字节流中保存的对…

    2022年4月20日
    41

发表回复

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

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