Foros del Web » Programando para Internet » PHP »

Funcion para evitar XSS y Sql injection

Estas en el tema de Funcion para evitar XSS y Sql injection en el foro de PHP en Foros del Web. Que funcion creen ustedes que es mejor? Código PHP: function  no_injection ( $string ) {     if( get_magic_quotes_gpc ())          $string  =  stripslashes ( $string ); ...
  #1 (permalink)  
Antiguo 28/07/2010, 08:54
 
Fecha de Ingreso: febrero-2009
Mensajes: 20
Antigüedad: 15 años, 1 mes
Puntos: 2
Pregunta Funcion para evitar XSS y Sql injection

Que funcion creen ustedes que es mejor?

Código PHP:
function no_injection($string)
{
    if(
get_magic_quotes_gpc())
        
$string stripslashes($string);
    return 
$string;

Código PHP:
function no_injection2($string){

$string striptags($string);
$string htmlspecialchars($string);
$string trim($string);
$string stripslashes($string);
$string mysql_real_escape_string($string);

return 
$string;


lo utilizaria en todos los $post["algo"];


conocen alguna funcion efectiva?

saludos y gracias!
  #2 (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
Respuesta: Funcion para evitar XSS y Sql injection

Yo uso la siguiente:
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.     }
  #3 (permalink)  
Antiguo 28/07/2010, 09:18
 
Fecha de Ingreso: junio-2010
Ubicación: Venezuela, Zulia
Mensajes: 686
Antigüedad: 13 años, 9 meses
Puntos: 55
Respuesta: Funcion para evitar XSS y Sql injection

y como se usaria exactamente esta funcion, osea de que forma se implementa en cada pagina
  #4 (permalink)  
Antiguo 28/07/2010, 10:04
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
Respuesta: Funcion para evitar XSS y Sql injection

¿A que te refieres?
  #5 (permalink)  
Antiguo 28/07/2010, 11:34
 
Fecha de Ingreso: junio-2010
Ubicación: Venezuela, Zulia
Mensajes: 686
Antigüedad: 13 años, 9 meses
Puntos: 55
Respuesta: Funcion para evitar XSS y Sql injection

osea que si para que funcione uno debe hacer esto en cada pagina
imaginemos que el script se llame seguridad.php
Código PHP:
<?
include("seguridad.php");

$valor1=filterXSS($_POST["cedula"]);

?>
seria asi que se hace, o de que forma

tambien veo este codigo

Código PHP:
#
$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); 
y nose como se aplicaria en ese caso
  #6 (permalink)  
Antiguo 28/07/2010, 11:41
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
Respuesta: Funcion para evitar XSS y Sql injection

Pues es una función para filtrar el texto que viene desde el usuario para evitar que inyecten XSS, algo que había pedido el que incio el tema.

Te recomiendo que leas un poco en Google sobre que son los ataques XSS.

Saludos.
  #7 (permalink)  
Antiguo 28/07/2010, 13:55
Avatar de ONahuelO  
Fecha de Ingreso: junio-2009
Ubicación: Gualeguaychú, Argentina
Mensajes: 144
Antigüedad: 14 años, 9 meses
Puntos: 4
Respuesta: Funcion para evitar XSS y Sql injection

GatorV esa funcion funciona bien en PHP5?
  #8 (permalink)  
Antiguo 28/07/2010, 14:12
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 14 años, 10 meses
Puntos: 1517
Respuesta: Funcion para evitar XSS y Sql injection

Sí. Trata para que veas.
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos

Etiquetas: evitar, funcion, injection, sql, 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.
Tema Cerrado




La zona horaria es GMT -6. Ahora son las 14:32.