Foros del Web » Programando para Internet » PHP »

Inyección sql y xss

Estas en el tema de Inyección sql y xss en el foro de PHP en Foros del Web. Bueno, mi consulta es sobre esta función: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código PHP: Ver original /**      * Removes all XSS attacks that came in the ...
  #1 (permalink)  
Antiguo 13/05/2011, 02:27
Avatar de C2am  
Fecha de Ingreso: enero-2009
Ubicación: Rosario, Argentina
Mensajes: 2.005
Antigüedad: 15 años, 3 meses
Puntos: 306
Inyección sql y xss

Bueno, mi consulta es sobre esta función:
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.     private 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.             // &#00064 @ 0{0,7} matches '0' zero to seven times
  30.             $val = preg_replace('/(&#0{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{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.     }

que publicó GatorV en :http://www.forosdelweb.com/f18/funci...5/#post3497564

1- ¿Es necesario usarla junto con otra que bloquee el ataque inyección sql?
Pregunto esto porque no alcanzo a comprender del todo la función.

2- Cuando la aplico me tira un error :
Código HTML:
Ver original
  1. Parse error: syntax error, unexpected T_PRIVATE in D:\AppServ\www\misitio\rejcdsde\filtro2.php on line 11

El cual deja de aparecer cuando quito "private" de la función.
__________________
El mundo nada puede contra un hombre que canta en la miseria.
-- Ernesto Sábato--
  #2 (permalink)  
Antiguo 13/05/2011, 02:37
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: Inyección sql y xss

Puedes usarla como función regular, solo es cuestión de quitar el private, ya que eso es para OOP. Lo otro esa función va a ser necesaría si no usaras codificación utf8 (aunque yo mismo la uso hasta en esa codificación porque me acostumbré). Eso evita que te ingresen datos en forma de hexadecimal engañando al sistema. Te recomiendo esta lectura que te ayudará a comprender mejor cómo esa función evita ataques XSS http://shiflett.org/blog/2006/jan/ad...-escape-string
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos
  #3 (permalink)  
Antiguo 13/05/2011, 11:51
Avatar de C2am  
Fecha de Ingreso: enero-2009
Ubicación: Rosario, Argentina
Mensajes: 2.005
Antigüedad: 15 años, 3 meses
Puntos: 306
Respuesta: Inyección sql y xss

muchas gracias abimaelrc, me sirvió de mucho tu respuesta.
Un saludo
__________________
El mundo nada puede contra un hombre que canta en la miseria.
-- Ernesto Sábato--

Etiquetas: seguridad, xss
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.
Respuesta




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