Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # phpfirewall 1.0
- #
- # 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.
- #
- #!/usr/bin/php
- <?php
- set_time_limit(0); # ilimitado tiempo de ejecucion
- /*
- iptables -I FORWARD -m tcp -p tcp -m iprange --dst-range 173.252.64.0-173.252.127.255 -j REJECT # bloquear CIDR
- iptables -I FORWARD -p tcp -m string --string lapagina.com --dport 443 --algo bm -j REJECT # por string
- */
- # 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;
- }
- $pathsquid= '/etc/squid/';
- $cidrbd= 'cidr.txt'; # bdd de CIDR's a bloquear
- $ipsdb= 'ipslibres.txt'; # bdd de IPs que tendran navegacion libre
- $cid= array();
- $ip= array();
- # 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 'Se han leido: '. count($cid). ' CIDRs a bloquear.';
- foreach( $cid as $key )
- echo "\n ". $key. " [Bloqueando]";
- echo "\n\n";
- echo 'Se han leido: '. count($ip). ' direcciones IPs a excluir del bloqueo.';
- foreach( $ip as $key )
- echo "\n ". $key. " [Libre]";
- echo "\n\n";
- echo "Reglas IPTables para IP Libres:\n";
- foreach( $cid as $key )
- {
- foreach( $ip as $val )
- echo "\niptables -I FORWARD -m tcp -p tcp -s ". $val. " -m iprange --dst-range ". $key. " -j ACCEPT";
- }
- echo "\n\n";
- echo "Reglas IPTables para los demas:\n";
- foreach( $cid as $key )
- echo "\niptables -I FORWARD -m tcp -p tcp -m iprange --dst-range ". $key. " -j REJECT";
- echo "\n\n";
- unset($cid, $ip);
- exit(0);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement