Advertisement
diabliyo

phpfirewall v1.03-1113

Nov 3rd, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.33 KB | None | 0 0
  1. # phpfirewall 1.0
  2. #
  3. # M.S.I. Angel Haniel Cantu Jauregui
  4. # angel.cantu@sie-group.net
  5. # http://www.sie-group.net/
  6. #
  7. # Script que arma las reglas del firewall IPTables, con la finalidad de obtener los rangos de IPs a
  8. # bloquear, tomando en cuenta la lista de IPs Local (Red LAN) que no tendran bloqueado los servicios.
  9. #
  10.  
  11. #!/usr/bin/php
  12.  
  13. <?php
  14. set_time_limit(0); # ilimitado tiempo de ejecucion
  15.  
  16. /*
  17. iptables -I FORWARD -m tcp -p tcp -m iprange --dst-range 173.252.64.0-173.252.127.255 -j REJECT # bloquear CIDR
  18. iptables -I FORWARD -p tcp -m string --string lapagina.com --dport 443 --algo bm -j REJECT # por string
  19. */
  20.  
  21. # limpia una variable, eliminando salto de linea
  22. function get_script_clearjump( $var )
  23.     {
  24.     $out='';
  25.     if( strstr($var, "\n") ) # si existe un de linea
  26.         $out= substr($var, 0, -1);
  27.     else $out=$var;
  28.     return $out;
  29.     }
  30.  
  31. function string_clean( $buf )
  32.     {  
  33.     $s_signos= array( '/[^0-9.-]/' ); # buscar simbolos que no sean estos
  34.     $buf= preg_replace( $s_signos, "", $buf ); # re-emplazamos signos
  35.     return $buf;
  36.     }
  37.  
  38. $pathsquid= '/etc/squid/';
  39. $cidrbd= 'cidr.txt'; # bdd de CIDR's a bloquear
  40. $ipsdb= 'ipslibres.txt'; # bdd de IPs que tendran navegacion libre
  41. $cid= array();
  42. $ip= array();
  43.  
  44. # leyendo bdd de CIDRs
  45. $fp= fopen($pathsquid.$cidrbd, "r"); # abrimos
  46. while( ($buf= fgets($fp, 1024))!==FALSE )
  47.     {
  48.     if( string_clean($buf) )
  49.         $cid[]= get_script_clearjump(string_clean($buf));
  50.     }
  51. fclose($fp);
  52.  
  53. # leyeno bdd de IPs
  54. $fp= fopen($pathsquid.$ipsdb, "r"); # abrimos
  55. while( ($buf= fgets($fp, 1024))!==FALSE )
  56.     {
  57.     if( string_clean($buf) )
  58.         $ip[]= get_script_clearjump(string_clean($buf));
  59.     }
  60. fclose($fp);
  61.  
  62. echo 'Se han leido: '. count($cid). ' CIDRs a bloquear.';
  63. foreach( $cid as $key )
  64.     echo "\n ". $key. " [Bloqueando]";
  65. echo "\n\n";
  66.  
  67. echo 'Se han leido: '. count($ip). ' direcciones IPs a excluir del bloqueo.';
  68. foreach( $ip as $key )
  69.     echo "\n ". $key. " [Libre]";
  70. echo "\n\n";
  71.  
  72. echo "Reglas IPTables para IP Libres:\n";
  73. foreach( $cid as $key )
  74.     {
  75.     foreach( $ip as $val )
  76.         echo "\niptables -I FORWARD -m tcp -p tcp -s ". $val. " -m iprange --dst-range ". $key. " -j ACCEPT";
  77.     }
  78. echo "\n\n";
  79.  
  80. echo "Reglas IPTables para los demas:\n";
  81. foreach( $cid as $key )
  82.     echo "\niptables -I FORWARD -m tcp -p tcp -m iprange --dst-range ". $key. " -j REJECT";
  83. echo "\n\n";
  84.  
  85. unset($cid, $ip);
  86. exit(0);
  87. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement