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-\x20])/', '', $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('/(&#[x|X]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,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 .= '(&#[x|X]0{0,8}([9][a][b]);?)?';
$pattern .= '|(�{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;
}
}
}
}
分享到:
相关推荐
计算峰均比(PAPR)的matlab脚本 可直接调用!
PHP利用正则过滤掉js脚本代码 一、 开发环境 1、环境搭建:Windows 7+Apache 2.4.18+MySQL 5.7.11+PHP 7.1.0。 2、文本编辑器:Sublime 3。 二、主要技术 本案例主要在PHP中使用正则表达式来实现过滤掉JavaScript...
wincc C脚本函数手册zip,wincc C脚本函数手册
MCGS昆仑通态触摸屏的所有脚本函数的说明与范例,非常详细
基恩士PLC脚本函数编程
wincc 常用脚本总结,用于一些复杂变量的链接和画面的启动。
Linux 在Shell脚本中使用函数实例详解 Shell的函数 Shell程序也支持函数。函数能完成一特定的功能,可以重复调用这个函数。 函数格式如下: 函数名() { 函数体 } 函数调用方式: 函数名 参数列表 实例:编写...
SHELL 脚本函数代码的详解 1 函数的创建 2 函数和脚本的不同
Qt工程,c++代码调用vbs脚本函数,一个很简单的demo,参考学习
AutoIt3_脚本函数用法中文说明 在看这本小小的电子书之前,也许您也和当初的我一样,不知道 Au3 是何种东西,也 许您只是有听说过,却一直没有时间来试一试。 结识 Au3 完全是一个偶然,在去年夏天的时候,偶然在...
MCGS组态脚本函数.pdf
TIA博途WINCC的触摸屏VB脚本入门(Len函数获取字符串长度以及Right和Left函数)
javascript过滤危险脚本方法.docx
vbs搞人代码大集合 脚本-vbs VBS总结 vbs 提升管理员权限_启动脚本法 VBS函数列表
MYSQL脚本函数 10进制转换成36进制
介绍Intouch脚本创建,脚本函数使用
一个SQL脚本函数,16进制转换为10进制数
PHP脚本编程中的文件系统函数库PHP脚本编程中的文件系统函数库
05chkconfig 脚本函数1
作者:black world QQ:334064205 php图片上传脚本(合格过滤与验证),限制图片的类型,图片的大小,上传出错等提示