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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • cocos2dx luajit_如何还原文件打开方式

    cocos2dx luajit_如何还原文件打开方式最近对一款游戏的lua脚本提取,发现提取出来的脚本都是LJ格式的文件。如图:image在网上找了好几个工具都没什么用,最后在github找到了luajit-decomp-master,还原出来的伪代码基本能看懂,但是还是有残缺。如果不明确的逻辑和数据还是要结合ida动态调试。可以看看效果原lua源码functioniter(a,i)i=i+1…

    2022年9月28日
    1
  • python allure报告_Pytest+Allure 定制报告

    python allure报告_Pytest+Allure 定制报告前言:最近在研究接口自动化的框架,好的测试报告在整个测试框架起到至关重要的部分。终于被我发现一个超好用的报告框架,不仅报告美观,而且方便CI集成。就是它,就是它:AllureTestReport!!!先上一张报告效果图:python版本及必要库python3.5pytest3.3.3pytest-allure-adaptor1.7.9一、环境配置安装Python依赖库:pip3…

    2022年7月26日
    21
  • Mustache 使用心得总结

    Mustache 使用心得总结

    2021年12月15日
    43
  • sql mysql创建 视图索引_SQLServer中在视图上使用索引(转载)「建议收藏」

    sql mysql创建 视图索引_SQLServer中在视图上使用索引(转载)「建议收藏」在SQLServer中,视图是一个保存的T-SQL查询。视图定义由SQLServer保存,以便它能够用作一个虚拟表来简化查询,并给基表增加另一层安全。但是,它并不占用数据库的任何空间。实际上,在你查询它之前,视图并不做任何事情。索引视图在SQLServer2000和2005中,你能够给视图增加索引。但是,如果视图只是一个保存在数据库中的查询定义,在运行前没有自己的数据,你如何给那个定义建立…

    2022年7月22日
    17
  • PoolBoy

    PoolBoy

    2022年1月11日
    90
  • matlab支持向量回归,支持向量回归 MATLAB代码

    matlab支持向量回归,支持向量回归 MATLAB代码支持向量回归MATLAB代码(2013-05-3116:30:35)标签:教育支持向量机和神经网络都可以用来做非线性回归拟合,但它们的原理是不相同的,支持向量机基于结构风险最小化理论,普遍认为其泛化能力要比神经网络的强。大量仿真证实,支持向量机的泛化能力强于神经网络,而且能避免神经网络的固有缺陷——训练结果不稳定。本源码可以用于线性回归、非线性回归、非线性函数拟合、数据建模、预测、分类等多种应…

    2022年6月6日
    95

发表回复

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

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