Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php
- #
- # phpfirewall 1.06-1113
- #
- # M.S.I. Angel Haniel Cantu Jauregui
- # angel.cantu@sie-group.net
- # http://www.sie-group.net/
- #
- # Script que arma las reglas del firewall IPTables, con la finalidad de obtener los rangos de IPs a
- # bloquear, tomando en cuenta la lista de IPs Local (Red LAN) que no tendran bloqueado los servicios.
- #
- <?php
- set_time_limit(0); # ilimitado tiempo de ejecucion
- /*
- iptables -I FORWARD -s segmento/bitmask -d segmento/bitmask -j REJECT
- iptables -I FORWARD -m tcp -p tcp -m iprange --dst-range ipinicio-ipfinal -j REJECT
- iptables -I FORWARD -p tcp -m string --string lapagina.com --dport 443 --algo bm -j REJECT
- */
- # limpia una variable, eliminando salto de linea
- function get_script_clearjump( $var )
- {
- $out='';
- if( strstr($var, "\n") ) # si existe un de linea
- $out= substr($var, 0, -1);
- else $out=$var;
- return $out;
- }
- function string_clean( $buf )
- {
- $s_signos= array( '/[^0-9.-\/]/' ); # buscar simbolos que no sean estos
- $buf= preg_replace( $s_signos, "", $buf ); # re-emplazamos signos
- return $buf;
- }
- $iptables= '/sbin/iptables';
- $input= ' -A INPUT ';
- $output= ' -A OUTPUT ';
- $fw= ' -A FORWARD ';
- $ifw= ' -I FORWARD ';
- $nat= ' -t nat ';
- $pathsquid= '/etc/squid/';
- $cidrbd= 'cidrblock'; # bdd de CIDR's a bloquear
- $ipsdb= 'ipslibres'; # bdd de IPs que tendran navegacion libre
- $cid= array();
- $ip= array();
- $openports=NULL; # no puertos abiertos
- $fwports=NULL; # no puertos a forwardear
- /*
- $openports= array(
- "20:22"=>"tcp,udp", # ftp y ssh
- "80"=>"tcp", # http
- );
- */
- # leyendo bdd de CIDRs
- $fp= fopen($pathsquid.$cidrbd, "r"); # abrimos
- while( ($buf= fgets($fp, 1024))!==FALSE )
- {
- if( string_clean($buf) )
- $cid[]= get_script_clearjump(string_clean($buf));
- }
- fclose($fp);
- # leyeno bdd de IPs
- $fp= fopen($pathsquid.$ipsdb, "r"); # abrimos
- while( ($buf= fgets($fp, 1024))!==FALSE )
- {
- if( string_clean($buf) )
- $ip[]= get_script_clearjump(string_clean($buf));
- }
- fclose($fp);
- echo "\n[squid] ". count($cid). " cidr's se bloquearan";
- foreach( $cid as $key )
- echo "\n\t". $key;
- echo "\n[squid] ". count($ip). " direcciones ip se excluiran del bloqueo";
- foreach( $ip as $key )
- echo "\n\t". $key;
- echo "\n[firewall] Inicializando...";
- # reglas de limpieza e inicializacion de flujos
- # politicas
- # abriendo puertos
- if( is_array($openports) && count($openports) )
- {
- foreach( $openports as $key=>$val )
- {
- $x=0;
- if( strstr(",", $val) ) # si hay delimitador
- $x= explode(",", $val);
- if( is_array($x) && count($x) )
- {
- foreach( $x as $p )
- system( $iptables.$input.'-p '.$p. ' --dport '. $key. ' -j ACCEPT' );
- }
- system( $iptables.$input.'-p '.$val. ' --dport '. $key. ' -j ACCEPT' );
- unset($x);
- }
- }
- # forwardeo de puertos
- #if( is_array($fwports) && count($fwports) )
- # {
- # }
- echo "\n[firewall_squid] aplicando reglas para ip's libres";
- foreach( $cid as $key )
- {
- foreach( $ip as $val )
- system( $iptables.$ifw. '-s '. $val. ' -d '. $key. ' -j ACCEPT' );
- }
- echo "\n[firewall_squid] aplicando reglas generales";
- foreach( $cid as $key )
- system( $iptables.$ifw.'-d '. $key. ' -j REJECT' );
- unset($cid, $ip, $iptables, $input, $output, $fw, $ifw, $nat, $pathsquid);
- exit(0);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement