[Rio-pm] Script p/ iptables
Fernando Oliveira
fernandocorrea em gmail.com
Sexta Março 16 09:03:47 PDT 2007
Blz?!
è o seguinte, falta do q fazer é f... estou fazendo um script q facilita a
leitura de arquivo de regras de firewall... é bem básico, se alguém quiser
me ajudar a terminar, eu agradeceria muito...
segue o script:
<code>
#!/usr/bin/perl -n
BEGIN{
$int = '\$INT';
$ext = '\$EXT';
%conhecido = (
"-t" => "filter|nat|mangle" ,
"-[ADIR]" => "INPUT|OUTPUT|FORWARD|PREROUTING|POSTROUTING" ,
"-p" => "\!?(?:tcp|udp|icmp)(?:,(?:tcp|udp|icmp)){0,3}" ,
"-s" => ".*?" ,
"-d" => ".*?" ,
"-j" => "ACCEPT|DROP|LOG|REJECT|QUEUE|RETURN|DNAT|SNAT|MASQUERADE",
"-g" => ".*?" ,
"-i" => ".*?" ,
"-o" => ".*?" ,
"--(?:destination-|source-|[ds])port" => "\\d+?|\\\$.*?" ,
"--to(?:-destination|-source)?" => ".*?" ,
);
}
%hash=();
next if /^#/;
s/([^\\])#.*$/$1/g;
s/(--[ds])(?:estination|ource)-port/$1port/g;
s/(--to)-[ds](?:estinadion|ource)/$1/g;
$iptables = "\\\$".$1 if /^\s*(.*?)=.*?iptables.*?$/;
$iptables ||= "iptables";
if(/^\s*$iptables/)
{
print("Apaga a tablela ", $1 || "filter", $/), next if
/(?:-t\s+(filter|nat|mangle)\s+)?-[FX]\s+/;
s/$int/interna/g;
s/$ext/externa/g;
%hash = /(-.+?)\s+(?:(.+?)\s+)?/g;
CHAVE: for $chave (keys %hash)
{
undef $sim;
for $conh (keys %conhecido)
{
$value = $conhecido{$conh};
if($chave =~ /^$conh$/){
$sim = 1, next CHAVE if $hash{$chave} =~ /^$value$/;
}
}
print "Regra desconhecida na linha $.\n";
last CHAVE;
}
next unless $sim;
if($hash{"-t"} eq "nat")
{
print "Porta ", $hash{"--dport"} || $hash{"--sport"}, ", redirecionad",
($hash{"-A"} eq "PREROUTING"?"o destino":"a origem"), " para o IP ",
$hash{"--to"} || $hash{"--to-destination"} || $hash{"--to-source"}, $/ if
$hash{"--dport"} || $hash{"--sport"};
}
elsif($hash{"-A"} eq "FORWARD")
{
print $hash{"-p"}?"Sob o protocolo \"".$hash{"-p"}."\", ":"",
$hash{"--dport"}?"porta ".$hash{"--dport"}." da ":"", "interface ",
$hash{"-o"}, " para a interface ",$hash{"-i"}, " foi ", ($hash{"-j"} eq
"ACCEPT"?"aceito":"negado"), $/ if $hash{"-j"} =~ /ACCEPT|DROP/;
}
elsif($hash{"-A"} eq "INPUT")
{
print $hash{"-p"}?"Sob o protocolo \"".$hash{"-p"}."\", ":"",
$hash{"--dport"}?"porta ".$hash{"--dport"}." ":"", $hash{"-i"}?"para a
interface ".$hash{"-i"}:$hash{"-d"}?"para o ip
".$hash{"-d"}:"",$hash{"-s"}?" do ip ".$hash{"-s"}:"", " foi ", ($hash{"-j"}
eq "ACCEPT"?"aceito":"negado"), $/ if $hash{"-j"} =~ /ACCEPT|DROP/;
}
elsif($hash{"-A"} eq "OUTPUT")
{
print $hash{"-p"}?"Sob o protocolo \"".$hash{"-p"}."\", ":"",
$hash{"--dport"}?"porta ".$hash{"--dport"}." ":"", $hash{"-o"}?"da interface
".$hash{"-o"}:$hash{"-s"}?"do ip ".$hash{"-s"}:"",$hash{"-d"}?" para o ip
".$hash{"-d"}:"", " foi ", ($hash{"-j"} eq "ACCEPT"?"aceito":"negado"), $/
if $hash{"-j"} =~ /ACCEPT|DROP/;
}
elsif($hash{"-F"} || $hash{"-X"})
{
print "Apagando as regras da tabela ", $hash{"-t"} || "filter", $/;
}
}
</code>
--
[]'s Fernando
-------------- Próxima Parte ----------
Um anexo em HTML foi limpo...
URL: http://mail.pm.org/pipermail/rio-pm/attachments/20070316/fc312947/attachment.html
Mais detalhes sobre a lista de discussão Rio-pm