elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Trabajando con las ramas de git (tercera parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web
| | |-+  PHP (Moderador: #!drvy)
| | | |-+  Ejemplos scripts PHP para Banear IP y Rangos de IP's
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Ejemplos scripts PHP para Banear IP y Rangos de IP's  (Leído 10,244 veces)
el-brujo
ehn
***
Desconectado Desconectado

Mensajes: 21.580


La libertad no se suplica, se conquista


Ver Perfil WWW
Ejemplos scripts PHP para Banear IP y Rangos de IP's
« en: 1 Junio 2012, 20:58 pm »

Código
  1. <?php
  2. /* Listado con las IPs */
  3. $ban_ip_list = array('68.180.206.184', '64.233.167.99', '207.46.232.182');
  4.  
  5. /* Listado con el rango de IP. Use the '*' as the range selector */
  6. $ban_ip_range = array('69.*.83.197');
  7.  
  8. /* Ip Visitante */
  9. $user_ip = $_SERVER['REMOTE_ADDR'];
  10.  
  11. /* Mensaje que verá si está baneado */
  12. $msg = 'You do not have permission to access this page.';
  13.  
  14. /* Message to output if the IP is in the ban list */
  15.  
  16.    if(in_array($user_ip, $ban_ip_list))
  17. {
  18.  exit($msg);
  19. }
  20.  
  21. /* Check if the Visitor's IP is in our range's list */
  22.  
  23. if(!empty($ban_ip_range))
  24. {
  25. foreach($ban_ip_range as $range)
  26. {
  27. $range = str_replace('*','(.*)', $range);
  28.  
  29.    if(preg_match('/'.$range.'/', $user_ip))
  30. {
  31.  exit($msg);
  32. }
  33. }
  34. }
  35. ?>

Explicación de cómo banear correctamente un rango:

Ayuda:Cómo bloquear un rango
http://es.wikipedia.org/wiki/Ayuda:C%C3%B3mo_bloquear_un_rango

Usando mejor siempre el sufijo /24 para bloquear 256 ip's


Código
  1. <?
  2. $banned[0]="x.x.x.x";
  3. if (in_array($_SERVER['REMOTE_ADDR'],$banned))
  4. {
  5. header("HTTP/1.1 403 Forbidden");
  6. }
  7. ?>


Código
  1. <?php
  2. $ipArray = file('ip.txt');
  3. foreach ($ipArray as $ipTest) {
  4.  if (substr_count($_SERVER['REMOTE_ADDR'],trim($ipTest)) != "0") {
  5.    header('location: /banned.htm');  // the banned display page
  6.    die();
  7.  }
  8. }
  9. ?>


Código
  1. <?
  2. $targetAddr = "123.123..*..*";  //yes is two dots
  3.  
  4. //this code will match any class of 123.123.x.x,
  5. //you can also do "123.123.123..*" to do anything that is 123.123.123.x
  6.  
  7. if (ereg($targetAddr, $_SERVER['REMOTE_ADDR'])) {
  8.    //remote address match, do something or don't do anything
  9. } else {
  10.   //do whatever you want to address that doesn't match
  11. }
  12. ?>



Código
  1. <?php
  2.  
  3. $ip = $_SERVER['REMOTE_ADDR'];
  4. $ips_baneadas = array('10.0.0.1',’192.0.0.1’);
  5. $contador = count($ips_baneadas);
  6.  
  7.  
  8. for ($i=0; $i<$contador; $i++) {
  9. if($ip == $ips_baneadas[$i]) { die("Tu Ip no esta permitida . $ip" ; } }
  10.  
  11.  
  12. ?>

Mirando un listado de ip's un fichero txt

Código
  1. <?php
  2. $count_blocks = 0;
  3. $ip_lines = file("includes/ban.txt");
  4. $count = count($ip_lines);
  5. foreach($ip_lines as $single_line){
  6. $ip = explode("#", $single_line);
  7. if($ip[1] == $_SERVER['REMOTE_ADDR']){
  8.      die("Ip baneada.");
  9. }
  10. }
  11. ?>


Código
  1. <?php
  2. $banned_ip = array();
  3. $banned_ip[] = '111.111.111.111';
  4. $banned_ip[] = '111.111.111.112';
  5. $banned_ip[] = '111.111.111.113';
  6. $banned_ip[] = '111.111.111.114';
  7.  
  8. foreach($banned_ip as $banned) {
  9. $ip = $_SERVER['REMOTE_ADDR'];
  10. if($ip == $banned){
  11. echo "You have been banned!";
  12. exit();
  13. }
  14. }
  15. // rest of PHP Script here!
  16. ?>




Código
  1. <?php
  2. function ban($range = array(), $ip = '')
  3. {
  4.        $ip = longip(trim($ip));
  5.        if($ip == FALSE)
  6.        {
  7.                return FALSE;
  8.        }
  9.        if(empty($ip))
  10.        {
  11.                return FALSE;
  12.        }
  13.        foreach($range AS $key => $val)
  14.        {
  15.                $temp = explode('-', $val);
  16.                if(empty($temp[0]))
  17.                {
  18.                        return FALSE;
  19.                }
  20.                else
  21.                {
  22.                        $start_ip = longip(trim($temp[0]));
  23.                        if($start_ip == FALSE)
  24.                        {
  25.                                return FALSE;
  26.                        }
  27.                }
  28.                if(empty($temp[1]))
  29.                {
  30.                        if($ip == $start_ip)
  31.                        {
  32.                                return TRUE;
  33.                        }
  34.                }
  35.                else
  36.                {
  37.                        $stop_ip = longip(trim($temp[1]));
  38.                        if($stop_ip == FALSE)
  39.                        {
  40.                                return FALSE;
  41.                        }
  42.                }
  43.                if($start_ip <= $ip && $ip <= $stop_ip)
  44.                {
  45.                        return TRUE;
  46.                }
  47.        }
  48.        return FALSE;
  49. }
  50. function longip($ip)
  51. {
  52.        if(empty($ip))
  53.        {
  54.                return FALSE;
  55.        }
  56.        $block = explode('.', $ip);
  57.        if(count($block) != 4)
  58.        {
  59.                return FALSE;
  60.        }
  61.        $i = 3;
  62.        $block_ip = 0;
  63.        foreach($block as $k => $v)
  64.        {
  65.                $v = filter_var($v, FILTER_VALIDATE_INT);
  66.                if($v < 0)
  67.                {
  68.                        $v = 0;
  69.                }
  70.                if($v > 255)
  71.                {
  72.                        $v = 255;
  73.                }
  74.                $block_ip += pow(256, $i)*$v;
  75.                $i--;
  76.        }
  77.        return $block_ip;
  78. }
  79.  
  80. //usage
  81. $block_range = array(   '192.168.1.0 - 192.168.2.255',
  82.                        '192.168.4.0 - 192.168.4.255',
  83.                        '192.168.6.1'
  84.                );
  85.  
  86. $ip = '192.168.3.255';
  87.  
  88. if(ban($block_range, '192.168.6.1'))
  89. {
  90.        echo "BAN\n";
  91. }
  92. else
  93. {
  94.        echo "UNBAN\n";
  95. }
  96.  
  97. ?>


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines