php处理 xss方法,php实现XSS安全过滤的方法
发布日期:2022-02-08 20:24:01 浏览次数:36 分类:技术文章

本文共 3285 字,大约阅读时间需要 10 分钟。

本文实例讲述了php实现XSS安全过滤的方法。分享给大家供大家参考。具体如下:

function remove_xss($val) {

// remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed

// this prevents some character re-spacing such as

// 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

$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,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,8}([9|10|13]);)‘;

$pattern .= ‘)*‘;

}

$pattern .= $ra[$i][$j];

}

$pattern .= ‘/i‘;

$replacement = substr($ra[$i], 0, 2).‘‘.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;

}

希望本文所述对大家的php程序设计有所帮助。

原文:http://www.jb51.net/article/70238.htm

转载地址:https://blog.csdn.net/weixin_33132553/article/details/116256394 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:linux svn服务器搭建 jb51,详解Linux服务器配置——搭建SVN服务器
下一篇:oracle 查询最高分,SQL查找一个表里,每个班级的最高分。

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月12日 08时59分22秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

spring boot 与 Ant Design of Vue 鉴权体系登录的实现(三十一) 2019-04-27
spring boot 与 Ant Design of Vue 鉴权体系获取用户信息的实现(三十二) 2019-04-27
Druid连接池实现自定义场景的多数据库的连接 2019-04-27
CentOs7命令行(静默)的方式安装oracle数据库 2019-04-27
基于VMware安装CentOs7的镜像 2019-04-27
PL/SQL数据库管理工具的使用 2019-04-27
带你玩转属于自己的spring-boot-starter系列(一) 2019-04-27
带你玩转属于自己自己的spring-boot-starter系列(二) 2019-04-27
带你玩转属于自己的spring-boot-starter系列(三) 2019-04-27
基于SnowFlake算法如何让分库分表中不同的ID落在同一个库的算法的实现 2019-04-27
基于springboot的ShardingSphere5.X的分库分表的解决方案之分库解决方案(二) 2019-04-27
基于springboot的ShardingSphere5.X的分库分表的解决方案之分表解决方案(一) 2019-04-27
基于springboot的ShardingSphere5.X的分库分表的解决方案之关联查询解决方案(三) 2019-04-27
基于springboot的ShardingSphere5.X的分库分表的解决方案之基于seata的分布式事务的解决方案(十五) 2019-04-27
Linux文件管理参考 2019-04-27
FTP文件管理项目(本地云)项目日报(一) 2019-04-27
FTP文件管理项目(本地云)项目日报(二) 2019-04-27
FTP文件管理项目(本地云)项目日报(三) 2019-04-27
FTP文件管理项目(本地云)项目日报(四) 2019-04-27
【C++】勉强能看的线程池详解 2019-04-27