PHP general XSS attack filter function, prevent XSS vulnerability attack in discuz system, filter HT

From , 4 Years ago, written in PHP, viewed 216 times.
URL https://pastebin.vip/view/58191d2a
  1. <?php
  2. /**
  3.  * Usage: Run *every* variable passed in through it.
  4.  * The goal of this function is to be a generic function that can be used to
  5.  * parse almost any input and render it XSS safe. For more information on
  6.  * actual XSS attacks, check out http://ha.ckers.org/xss.html. Another
  7.  * excellent site is the XSS Database which details each attack and how it
  8.  * works.
  9.  *
  10.  * Used with permission by the author.
  11.  * URL: http://quickwired.com/smallprojects/php_xss_filter_function.php
  12.  *
  13.  * License:
  14.  * This code is public domain, you are free to do whatever you want with it,
  15.  * including adding it to your own project which can be under any license.
  16.  *
  17.  * $Id: RemoveXSS.php 2663 2007-11-05 09:22:23Z ingmars $
  18.  *
  19.  * @author      Travis Puderbaugh <kallahar@quickwired.com>
  20.  * @package RemoveXSS
  21.  */
  22. class RemoveXSS {
  23.         /**
  24.          * Wrapper for the RemoveXSS function.
  25.          * Removes potential XSS code from an input string.
  26.          *
  27.          * Using an external class by Travis Puderbaugh <kallahar@quickwired.com>
  28.          *
  29.          * @param       string          Input string
  30.          * @return      string          Input string with potential XSS code removed
  31.          */
  32.         function RemoveXSS($val)        {
  33.                 // remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
  34.                 // this prevents some character re-spacing such as <java\0script>
  35.                 // note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs
  36.                 $val = preg_replace('/([\x00-\x08][\x0b-\x0c][\x0e-\x20])/', '', $val);
  37.                 // straight replacements, the user should never need these since they're normal characters
  38.                 // this prevents like <IMG src="http://www.iamle.com/archives/&#X40&#X61&#X76&#X61&#X73&#X63&#X72&#X69&#X70&#X74&#X3A&#X61&#X6C&#X65&#X72&#X74&#X28&#X27&#X58&#X53&#X53&#X27&#X29>
  39.                 $search = "'abcdefghijklmnopqrstuvwxyz';
  40.                 $search.= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  41.                 $search.= '1234567890!@#$%^&*()';
  42.                 $search.= '~`";:?+/={}[]-_|\'\\';
  43.                 for ($i = 0; $i < strlen($search); $i++) {
  44.                         // ;? matches the ;, which is optional
  45.                         // 0{0,7} matches any padded zeros, which are optional and go up to 8 chars
  46.                         // &#x0040 @ search for the hex values
  47.                         $val = preg_replace('/(&#[x|X]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ;
  48.                         // &#00064 @ 0{0,7} matches '0' zero to seven times
  49.                         $val = preg_replace('/(&#0{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ;
  50.                 }
  51.                 // now the only remaining whitespace attacks are \t, \n, and \r
  52.                 $ra1 = array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
  53.                 $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');
  54.                 $ra = array_merge($ra1, $ra2);
  55.                 $found = true; // keep replacing as long as the previous round replaced something
  56.                 while ($found == true) {
  57.                         $val_before = $val;
  58.                         for ($i = 0; $i < sizeof($ra); $i++) {
  59.                                 $pattern = '/';
  60.                                 for ($j = 0; $j < strlen($ra[$i]); $j++) {
  61.                                         if ($j > 0) {
  62.                                                 $pattern .= '(';
  63.                                                 $pattern .= '(&#[x|X]0{0,8}([9][a][b]);?)?';
  64.                                                 $pattern .= '|(&#0{0,8}([9][10][13]);?)?';
  65.                                                 $pattern .= ')?';
  66.                                         }
  67.                                         $pattern .= $ra[$i][$j];
  68.                                 }
  69.                                 $pattern .= '/i';
  70.                                 $replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2); // add in <> to nerf the tag
  71.                                 $val = preg_replace($pattern, $replacement, $val); // filter out the hex tags
  72.                                 if ($val_before == $val) {
  73.                                         // no replacements were made, so exit the loop
  74.                                         $found = false;
  75.                                 }
  76.                         }
  77.                 }
  78.                 return $val;
  79.         }
  80. }
  81. ?>

Reply to "PHP general XSS attack filter function, prevent XSS vulnerability attack in discuz system, filter HT"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website