Advertisement
Guest User

Untitled

a guest
May 19th, 2011
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.12 KB | None | 0 0
  1. function removeXSS($val) {
  2.  
  3.      // remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
  4.  
  5.      // this prevents some character re-spacing such as <java\0script>
  6.  
  7.      // note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs
  8.  
  9.      $val = preg_replace('/([\x00-\x08][\x0b-\x0c][\x0e-\x20])/', '', $val);
  10.  
  11.      
  12.  
  13.      //REMOVE SQL INJECTION
  14.  
  15.      $val = preg_replace(sql_regcase("/(\n|\r|%0a|%0d|Content-Type:|bcc:|to:|cc:|Autoreply:|from|select|insert|truncate|delete|where|drop table|show tables|#|\*|--|\\\\)/"), "", $val);
  16.  
  17.      
  18.  
  19.      //$val = stripslashes($val);
  20.  
  21.      //$val = strip_tags($val); # Remove tags HTML e PHP.
  22.  
  23.      //$val = addslashes($val); # Adiciona barras invertidas à uma string.
  24.  
  25.      
  26.  
  27.      
  28.  
  29.      //$val = preg_replace('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '', $val);
  30.  
  31.  
  32.  
  33.      // straight replacements, the user should never need these since they're normal characters
  34.  
  35.      // this prevents like <IMG SRC=&#X40&#X61&#X76&#X61&#X73&#X63&#X72&#X69&#X70&#X74&#X3A&#X61&#X6C&#X65&#X72&#X74&#X28&#X27&#X58&#X53&#X53&#X27&#X29>
  36.  
  37.      $search = 'abcdefghijklmnopqrstuvwxyz';
  38.  
  39.      $search.= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  40.  
  41.      $search.= '1234567890!@#$%^&*()';
  42.  
  43.      $search.= '~`";:?+/={}[]-_|\'\\';
  44.  
  45.  
  46.  
  47.      for ($i = 0; $i < strlen($search); $i++) {
  48.  
  49.              // ;? matches the ;, which is optional
  50.  
  51.              // 0{0,7} matches any padded zeros, which are optional and go up to 8 chars
  52.  
  53.  
  54.  
  55.              // &#x0040 @ search for the hex values
  56.  
  57.              $val = preg_replace('/(&#[x|X]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ;
  58.  
  59.              // &#00064 @ 0{0,7} matches '0' zero to seven times
  60.  
  61.              $val = preg_replace('/(&#0{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ;
  62.  
  63.      }
  64.  
  65.      
  66.  
  67.      
  68.  
  69.  
  70.  
  71.      // now the only remaining whitespace attacks are \t, \n, and \r
  72.  
  73.      $ra1 = array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title');
  74.  
  75.      $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');
  76.  
  77.      $ra = array_merge($ra1, $ra2);
  78.  
  79.  
  80.  
  81.          $found = true; // keep replacing as long as the previous round replaced something
  82.  
  83.          while ($found == true) {
  84.  
  85.                  $val_before = $val;
  86.  
  87.                  for ($i = 0; $i < sizeof($ra); $i++) {
  88.  
  89.                          $pattern = '/';
  90.  
  91.                          for ($j = 0; $j < strlen($ra[$i]); $j++) {
  92.  
  93.                                  if ($j > 0) {
  94.  
  95.                                          $pattern .= '(';
  96.  
  97.                                          $pattern .= '(&#[x|X]0{0,8}([9][a][b]);?)?';
  98.  
  99.                                          $pattern .= '|(&#0{0,8}([9][10][13]);?)?';
  100.  
  101.                                          $pattern .= ')?';
  102.  
  103.                                  }
  104.  
  105.                                  $pattern .= $ra[$i][$j];
  106.  
  107.                          }
  108.  
  109.                          $pattern .= '/i';
  110.  
  111.                          $replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2); // add in <> to nerf the tag
  112.  
  113.                          $val = preg_replace($pattern, $replacement, $val); // filter out the hex tags
  114.  
  115.                          if ($val_before == $val) {
  116.  
  117.                                  // no replacements were made, so exit the loop
  118.  
  119.                                  $found = false;
  120.  
  121.                          }
  122.  
  123.                  }
  124.  
  125.          }
  126.  
  127.  
  128.  
  129.      return trim($val);
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement