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


相关推荐

  • 程序员java_java多线程的实现方式

    程序员java_java多线程的实现方式引言:“作为一名工作了十五年的老程序员,我深知编程行业的不容易,不仅需要应对高强度的工作,还需要学习大量的技术知识,而且不像医生、律师这些知识相对稳定的行业越老越吃香,软件行业的技术每隔一段时间就会更新换代,让你清零,逼着你从头再来。所谓“活到老,学到老”,用到程序员身上再合适不过了。在不断学习的过程中,我“痛恨”那些采用bottom-up方式来讲解技术的资料和文章,一上来就是技术细节、安装步骤、配置方法,让初学者晕头转向、不知所云,看完了以后也不知道为什么有这个东西、解决了什么问题、它有什么来龙去

    2022年9月23日
  • 微博java SDK介绍及使用说明

    微博java SDK介绍及使用说明

    2021年9月4日
  • 网站推荐:11个相似图片搜索网站(以图找图)

    网站推荐:11个相似图片搜索网站(以图找图)你想凭着一张现有图片找出它的原始图片,或者是凭着一张小的缩略图找出原始大图吗?下面的十款搜索引擎可以帮你实现,以图找图,以图搜图,以图片搜索相似的图片。一:http://tineye.com/Tineye是典型的以图找图搜索引擎,输入本地硬盘上的图片或者输入图片网址,即可自动帮你搜索相似图片,搜索准确度相对来说还比较令人满意。TinEye是加拿大Idée公司研发…

    2022年7月17日
  • 运行疑难解答时出错0x8000FFFF_vs2010由于缺少调试目标

    运行疑难解答时出错0x8000FFFF_vs2010由于缺少调试目标发生错误,需要终止调试。有关更多详细信息,请参阅Microsoft帮助和支持网站。HRESULT=0x8000ffff。ErrorCode=0x0。工程目录太深会导致上述错误发生,把整个工程文件往外移即可

    2022年9月24日
  • 十年研发经验工程师的嵌入式学习书籍大推荐(转帖)

    十年研发经验工程师的嵌入式学习书籍大推荐(转帖)从事嵌入式研发行业十年,认为学习就是要不断的吸纳知识,在研发过程中,经常会遇到一些问题,这种发现问题并解决问题的过程就是进步。为什么选择学习嵌入式?嵌入式系统无疑是当前最热门最有发展前途的IT应用领域之一,同时也是当今IT领域仅存的几个金领职位之一。当前的中国IT人才面临严重的“后继乏人”,而且这种缺口由于培训缺乏、教育模式等原因造成的,而缺口最大的,就是高级IT人才。如果你从事的IT培训不专业…

    2022年6月4日
  • C#数组反转

    C#数组反转staticvoidMain(string[]arr){string[]names={“老杨1″,”老李2″,”老王3″,”老牛4″,”老虎5″,”老磁6”};for(inti=0;i<names.Length/2;i++){…

    2022年6月8日

发表回复

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

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