Advertisement
diabliyo

phpfirewall 1.06-1113

Nov 6th, 2013
97
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. # angel.cantu@sie-group.net
  7. # http://www.sie-group.net/
  8. #
  9. # Script que arma las reglas del firewall IPTables, con la finalidad de obtener los rangos de IPs a
  10. # bloquear, tomando en cuenta la lista de IPs Local (Red LAN) que no tendran bloqueado los servicios.
  11. #
  12.  
  13. <?php
  14. set_time_limit(0); # ilimitado tiempo de ejecucion
  15.  
  16. /*
  17. iptables -I FORWARD -s segmento/bitmask -d segmento/bitmask -j REJECT
  18. iptables -I FORWARD -m tcp -p tcp -m iprange --dst-range ipinicio-ipfinal -j REJECT
  19. iptables -I FORWARD -p tcp -m string --string lapagina.com --dport 443 --algo bm -j REJECT
  20. */
  21.  
  22. # limpia una variable, eliminando salto de linea
  23. function get_script_clearjump( $var )
  24.     {
  25.     $out='';
  26.     if( strstr($var, "\n") ) # si existe un de linea
  27.         $out= substr($var, 0, -1);
  28.     else $out=$var;
  29.     return $out;
  30.     }
  31.  
  32. function string_clean( $buf )
  33.     {  
  34.     $s_signos= array( '/[^0-9.-\/]/' ); # buscar simbolos que no sean estos
  35.     $buf= preg_replace( $s_signos, "", $buf ); # re-emplazamos signos
  36.     return $buf;
  37.     }
  38.  
  39. $iptables= '/sbin/iptables';
  40. $input= ' -A INPUT ';
  41. $output= ' -A OUTPUT ';
  42. $fw= ' -A FORWARD ';
  43. $ifw= ' -I FORWARD ';
  44. $nat= ' -t nat ';
  45. $pathsquid= '/etc/squid/';
  46. $cidrbd= 'cidrblock'; # bdd de CIDR's a bloquear
  47. $ipsdb= 'ipslibres'; # bdd de IPs que tendran navegacion libre
  48. $cid= array();
  49. $ip= array();
  50. $openports=NULL;    # no puertos abiertos
  51. $fwports=NULL;  # no puertos a forwardear
  52. /*
  53. $openports= array(
  54.     "20:22"=>"tcp,udp", # ftp y ssh
  55.     "80"=>"tcp", # http
  56.     );
  57. */
  58.  
  59. # leyendo bdd de CIDRs
  60. $fp= fopen($pathsquid.$cidrbd, "r"); # abrimos
  61. while( ($buf= fgets($fp, 1024))!==FALSE )
  62.     {
  63.     if( string_clean($buf) )
  64.         $cid[]= get_script_clearjump(string_clean($buf));
  65.     }
  66. fclose($fp);
  67.  
  68. # leyeno bdd de IPs
  69. $fp= fopen($pathsquid.$ipsdb, "r"); # abrimos
  70. while( ($buf= fgets($fp, 1024))!==FALSE )
  71.     {
  72.     if( string_clean($buf) )
  73.         $ip[]= get_script_clearjump(string_clean($buf));
  74.     }
  75. fclose($fp);
  76.  
  77. echo "\n[squid] ". count($cid). " cidr's se bloquearan";
  78. foreach( $cid as $key )
  79.     echo "\n\t". $key;
  80.  
  81. echo "\n[squid] ". count($ip). " direcciones ip se excluiran del bloqueo";
  82. foreach( $ip as $key )
  83.     echo "\n\t". $key;
  84.  
  85. echo "\n[firewall] Inicializando...";
  86. # reglas de limpieza e inicializacion de flujos
  87.  
  88. # politicas
  89.  
  90. # abriendo puertos
  91. if( is_array($openports) && count($openports) )
  92.     {
  93.     foreach( $openports as $key=>$val )
  94.         {
  95.         $x=0;
  96.         if( strstr(",", $val) ) # si hay delimitador
  97.             $x= explode(",", $val);
  98.        
  99.         if( is_array($x) && count($x) )
  100.              {
  101.              foreach( $x as $p )
  102.                 system( $iptables.$input.'-p '.$p. ' --dport '. $key. ' -j ACCEPT' );
  103.              }
  104.         system( $iptables.$input.'-p '.$val. ' --dport '. $key. ' -j ACCEPT' );
  105.         unset($x);
  106.         }
  107.     }
  108.  
  109. # forwardeo de puertos
  110. #if( is_array($fwports) && count($fwports) )
  111. #   {
  112. #   }
  113.  
  114. echo "\n[firewall_squid] aplicando reglas para ip's libres";
  115. foreach( $cid as $key )
  116.     {
  117.     foreach( $ip as $val )
  118.         system( $iptables.$ifw. '-s '. $val. ' -d '. $key. ' -j ACCEPT' );
  119.     }
  120.  
  121. echo "\n[firewall_squid] aplicando reglas generales";
  122. foreach( $cid as $key )
  123.     system( $iptables.$ifw.'-d '. $key. ' -j REJECT' );
  124.  
  125. unset($cid, $ip, $iptables, $input, $output, $fw, $ifw, $nat, $pathsquid);
  126. exit(0);
  127. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement