Foros del Web » Programando para Internet » PHP »

[Aporte] Funcion para evitar XSS

Estas en el tema de [Aporte] Funcion para evitar XSS en el foro de PHP en Foros del Web. @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código PHP: Ver original /**      * Removes all XSS attacks that came in the input.      *      * ...
  #1 (permalink)  
Antiguo 28/07/2010, 08:56
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 10 meses
Puntos: 2135
Pregunta [Aporte] Funcion para evitar XSS

Código PHP:
Ver original
  1. /**
  2.      * Removes all XSS attacks that came in the input.
  3.      *
  4.      * Function taken from:
  5.      *
  6.      * http://quickwired.com/smallprojects/php_xss_filter_function.php
  7.      *
  8.      * @param mixed $val The Value to filter
  9.      * @return mixed
  10.      */
  11.     function filterXSS($val) {
  12.         // remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
  13.         // this prevents some character re-spacing such as <java\0script>
  14.         // note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs
  15.         $val = preg_replace('/([\x00-\x08][\x0b-\x0c][\x0e-\x20])/', '', $val);
  16.  
  17.         // straight replacements, the user should never need these since they're normal characters
  18.         // 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>
  19.         $search = 'abcdefghijklmnopqrstuvwxyz';
  20.         $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  21.         $search .= '1234567890!@#$%^&*()';
  22.         $search .= '~`";:?+/={}[]-_|\'\\';
  23.         for ($i = 0; $i < strlen($search); $i++) {
  24.             // ;? matches the ;, which is optional
  25.             // 0{0,7} matches any padded zeros, which are optional and go up to 8 chars
  26.  
  27.             // &#x0040 @ search for the hex values
  28.             $val = preg_replace('/(&#[x|X]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ;
  29.             // @ @ 0{0,7} matches '0' zero to seven times
  30.             $val = preg_replace('/(�{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ;
  31.         }
  32.  
  33.         // now the only remaining whitespace attacks are \t, \n, and \r
  34.         $ra1 = Array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
  35.         $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');
  36.         $ra = array_merge($ra1, $ra2);
  37.  
  38.         $found = true; // keep replacing as long as the previous round replaced something
  39.         while ($found == true) {
  40.             $val_before = $val;
  41.             for ($i = 0; $i < sizeof($ra); $i++) {
  42.                 $pattern = '/';
  43.                 for ($j = 0; $j < strlen($ra[$i]); $j++) {
  44.                     if ($j > 0) {
  45.                         $pattern .= '(';
  46.                         $pattern .= '(&#[x|X]0{0,8}([9][a][b]);?)?';
  47.                         $pattern .= '|(�{0,8}([9][10][13]);?)?';
  48.                         $pattern .= ')?';
  49.                 }
  50.                 $pattern .= $ra[$i][$j];
  51.              }
  52.              $pattern .= '/i';
  53.              $replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2); // add in <> to nerf the tag
  54.              $val = preg_replace($pattern, $replacement, $val); // filter out the hex tags
  55.              if ($val_before == $val) {
  56.                 // no replacements were made, so exit the loop
  57.                 $found = false;
  58.              }
  59.           }
  60.         }
  61.  
  62.         return $val;
  63.     }

Última edición por GatorV; 14/11/2012 a las 17:00
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Tema Cerrado




La zona horaria es GMT -6. Ahora son las 09:04.