博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
过滤XSS(跨站脚本攻击)的函数和防止svn版本库被浏览
阅读量:6076 次
发布时间:2019-06-20

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

别处看到的php去除xss的函数,自己备份下,以备不时之需。

php过滤xss函数

alert("hello world"); * @return 处理后的字符串 * @Recoded By Androidyue **/ 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
// 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; } //测试一下效果 //echo RemoveXSS("
") ; ?>

javascript过滤xss

过滤xss只是一种防君子不防小人的方法,可以两种方式,第一种是将尖括号和引号转义掉,如下面大代码:

function(cont){
cont = cont.replace(/&/g, '&'); cont = cont.replace(//g, '>'); cont = cont.replace(/\'/g, ''').replace(/\"/g, '"'); return cont; };

第二种是动态创建一个dom,然后将str作为innerText,之后动态的取innerHTML出来,就可以是经过转义的内容了,

注:感谢 Jackmasa 指出问题,其实这里还是会有问题,详细见文章:《》

apache防止svn版本库被浏览

前几天炒的比较热的svn版本库被爆的问题,自己查了下找到了简单的解决的svn版本库被浏览方式,也是类似的禁用.svn的浏览

apache禁用svn

Order allow,deny Deny from all

nginx禁用svn

location ~ /.svn/ {

deny all;
}

转载地址:http://bhxgx.baihongyu.com/

你可能感兴趣的文章
参数请求post, get , delete中的基本使用(2)
查看>>
【封装】二叉树相关算法的实验验证
查看>>
3/31下午
查看>>
linux GTK 安装
查看>>
111
查看>>
关于Expression Tree和IL Emit的所谓的&quot;性能差别&quot“.NET研究”;
查看>>
计算几何
查看>>
【搜索】调手表
查看>>
快放弃的第二天手动微笑
查看>>
实例属性和类属性的理解
查看>>
SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
查看>>
实验一 网络对抗
查看>>
关于typedef的用法总结(转)
查看>>
iOS面试用到的一些知识点和技术
查看>>
[树形DP]Luogu P1131 [ZJOI2007]时态同步
查看>>
C++值传递与引用传递
查看>>
sharepoint2010 沙盒解决方案与场解决方案之间的差异
查看>>
Javascript学习二:事件
查看>>
页面局部定时刷新
查看>>
Luogu P4316 绿豆蛙的归宿//期望
查看>>