Advertisement
diabliyo

phpfirewall 1.06-1113

Nov 6th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.23 KB | None | 0 0
  1. #!/usr/bin/php
  2. #
  3. # phpfirewall 1.06-1113
  4. #
  5. # M.S.I. Angel Haniel Cantu Jauregui
  6. # http://www.sie-group.net/
  7. #
  8. # Script que arma las reglas del firewall IPTables, con la finalidad de obtener los rangos de IPs a
  9. # bloquear, tomando en cuenta la lista de IPs Local (Red LAN) que no tendran bloqueado los servicios.
  10. #
  11.  
  12. <?php
  13. set_time_limit(0); # ilimitado tiempo de ejecucion
  14.  
  15. /*
  16. iptables -I FORWARD -s segmento/bitmask -d segmento/bitmask -j REJECT
  17. iptables -I FORWARD -m tcp -p tcp -m iprange --dst-range ipinicio-ipfinal -j REJECT
  18. iptables -I FORWARD -p tcp -m string --string lapagina.com --dport 443 --algo bm -j REJECT
  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. $iptables= '/sbin/iptables';
  39. $input= ' -A INPUT ';
  40. $output= ' -A OUTPUT ';
  41. $fw= ' -A FORWARD ';
  42. $ifw= ' -I FORWARD ';
  43. $nat= ' -t nat ';
  44. $pathsquid= '/etc/squid/';
  45. $cidrbd= 'cidrblock'; # bdd de CIDR's a bloquear
  46. $ipsdb= 'ipslibres'; # bdd de IPs que tendran navegacion libre
  47. $cid= array();
  48. $ip= array();
  49. $openports=NULL;    # no puertos abiertos
  50. $fwports=NULL;  # no puertos a forwardear
  51. /*
  52. $openports= array(
  53.     "20:22"=>"tcp,udp", # ftp y ssh
  54.     "80"=>"tcp", # http
  55.     );
  56. */
  57.  
  58. # leyendo bdd de CIDRs
  59. $fp= fopen($pathsquid.$cidrbd, "r"); # abrimos
  60. while( ($buf= fgets($fp, 1024))!==FALSE )
  61.     {
  62.     if( string_clean($buf) )
  63.         $cid[]= get_script_clearjump(string_clean($buf));
  64.     }
  65. fclose($fp);
  66.  
  67. # leyeno bdd de IPs
  68. $fp= fopen($pathsquid.$ipsdb, "r"); # abrimos
  69. while( ($buf= fgets($fp, 1024))!==FALSE )
  70.     {
  71.     if( string_clean($buf) )
  72.         $ip[]= get_script_clearjump(string_clean($buf));
  73.     }
  74. fclose($fp);
  75.  
  76. echo "\n[squid] ". count($cid). " cidr's se bloquearan";
  77. foreach( $cid as $key )
  78.     echo "\n\t". $key;
  79.  
  80. echo "\n[squid] ". count($ip). " direcciones ip se excluiran del bloqueo";
  81. foreach( $ip as $key )
  82.     echo "\n\t". $key;
  83.  
  84. echo "\n[firewall] Inicializando...";
  85. # reglas de limpieza e inicializacion de flujos
  86.  
  87. # politicas
  88.  
  89. # abriendo puertos
  90. if( is_array($openports) && count($openports) )
  91.     {
  92.     foreach( $openports as $key=>$val )
  93.         {
  94.         $x=0;
  95.         if( strstr(",", $val) ) # si hay delimitador
  96.             $x= explode(",", $val);
  97.        
  98.         if( is_array($x) && count($x) )
  99.              {
  100.              foreach( $x as $p )
  101.                 system( $iptables.$input.'-p '.$p. ' --dport '. $key. ' -j ACCEPT' );
  102.              }
  103.         system( $iptables.$input.'-p '.$val. ' --dport '. $key. ' -j ACCEPT' );
  104.         unset($x);
  105.         }
  106.     }
  107.  
  108. # forwardeo de puertos
  109. #if( is_array($fwports) && count($fwports) )
  110. #   {
  111. #   }
  112.  
  113. echo "\n[firewall_squid] aplicando reglas para ip's libres";
  114. foreach( $cid as $key )
  115.     {
  116.     foreach( $ip as $val )
  117.         system( $iptables.$ifw. '-s '. $val. ' -d '. $key. ' -j ACCEPT' );
  118.     }
  119.  
  120. echo "\n[firewall_squid] aplicando reglas generales";
  121. foreach( $cid as $key )
  122.     system( $iptables.$ifw.'-d '. $key. ' -j REJECT' );
  123.  
  124. unset($cid, $ip, $iptables, $input, $output, $fw, $ifw, $nat, $pathsquid);
  125. exit(0);
  126. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement