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


相关推荐

  • RenderControl输出html遇到的问题

    RenderControl输出html遇到的问题当我直接在后台用GridView gv=new GridView();gv.RenderControl(htmlwrite);没有问题,但是如果我从工具直接拖一个GridView到设计页面,在后台gv.RenderControl(htmlwrite);就会出现Control GridViewTotal of type GridView must be placed inside a f

    2022年7月20日
    15
  • layui弹出层提交表单![通俗易懂]

    layui弹出层提交表单![通俗易懂]本文章使用layui框架,提交表单,如果使用其他的框架请右上角!首先设置弹出层如下图:layer.open({ type:2, title:"信息編輯", area:[‘45%’,’35%’], shade:0, sha…

    2022年5月6日
    590
  • 调和数列举例_数列专项训练

    调和数列举例_数列专项训练算法训练调和数列问题时间限制:1.0s内存限制:512.0MB时间限制:1.0s内存限制:512.0MB问题描述输入一个实数x,求最小的n使得,1/2+1/3+1/4+…+1/(n+

    2022年8月5日
    6
  • Python新手进阶教程之1、海龟作图——用Python绘图(1)「建议收藏」

    Python新手进阶教程之1、海龟作图——用Python绘图(1)「建议收藏」1.1海龟的作用使用海龟作图,我们不仅能够只用几行代码就创建出令人影响深刻的视觉效果,而且还可以跟海龟看看几行代买如何影响到它的移动。者能够帮助我们理解代码的逻辑。1.2第一个海龟程序让我们使用海龟作图来编写第一个程序。在一个新的IDLE窗口中输入如下的代码并将其保存为Square.py#Square.py-画一个正方形importturtlet=turtle.Pen()foriinrange(100):t.forward(x)t.left(90)

    2022年6月28日
    31
  • 数据ETL是指什么

    数据ETL是指什么

    2021年10月28日
    55
  • 计算机win7卡顿如何解决方法,win7电脑出现卡顿怎么办_win7电脑卡顿严重解决方法…[通俗易懂]

    计算机win7卡顿如何解决方法,win7电脑出现卡顿怎么办_win7电脑卡顿严重解决方法…[通俗易懂]现在已经是网络时代了,不管我们是在工作、生活还是学习我们大家都离不开电脑。win7系统也是一款比较经典的系统,不管怎么样我们电脑使用久了,都会出现卡顿的问题,有时候打开一个文件,程序反应非常慢。那么win7电脑出现卡顿我们要怎么办呢?接下来小编就跟大家分享一下win7电脑卡顿严重解决方法。win7电脑卡顿严重解决方法:方法一:1、首先鼠标右键点击“计算机”,选择“属性”。2、在属性页面,点击“高级…

    2025年11月2日
    2

发表回复

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

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