Firewall com 3 interfaces...

1. Firewall com 3 interfaces...

Fabio Alexandre Pszepiura
pszepiura

(usa Ubuntu)

Enviado em 28/09/2009 - 16:59h

Boa tarde pessoal tudo bem?

Estou precisando de um help. Tenho um script de firewall que esta funcionando adequadamente.

- A eth3 (LAN) é responsavel pela conexão a minha rede interna.
- A eth1 (ADSL) é conectada a um roteador ADSL.
Toda a navegação na internet passa pelo proxy e sai pela eth1.

Porém surgiu a necessidade de aceitar algumas conexões externas de rede vindas da eth0 (FRAME) que é um link dedicado. Vou usar essa conexão para acessar alguns servidores remotamente, vou criar um servidor web, e também será usado por dois distemas especificos.

Então o que eu precisava era ter em meu firewall o seguinte.
- eth0 - todas as conexões externas - vnc, terminal service, ftp, etc...
- eth1 - navegação na internet através do proxy.
- eth3 - conectada a rede local

Meu script está da seguinte forma...

Navegar eu estou conseguindo, mas não consigo fazer conexões externas...

#!/bin/sh
#Configuração do Firewall através do iptables
clear
#Declaração de variaveis
PATH=/sbin:/bin:/usr/sbin:/usr/bin
IPTABLES="/sbin/iptables"
MACLIBERADOS="/home/scripts/macs.liberados"
PROGRAMA="/home/scripts/firewall.sh"
REDIRECT="/home/scripts/redirecionamentos"
PORTSLIB="/home/scripts/portas.liberadas"
SITESNEGADOS=/home/scripts/acls/sites.bloqueados

#Interfaces de Rede
LAN=eth3
ADSL=eth1
FRAME=eth0

REDE="192.168.0.0/24"
IP_LAN=192.168.0.199
IP_ADSL=192.168.0.99
IP_FRAME=201.XXX.XXX.001

MSK_LAN=255.255.255.0
MSK_ADSL=255.255.255.0
MSK_FRAME=255.255.255.248

GW_ADSL=192.168.0.1
GW_FRAME=201.000.000.000

# configura as interfaces
#interface LAN
ifconfig $LAN $IP_LAN netmask $MSK_LAN up

#interface ADSL
ifconfig $ADSL $IP_ADSL netmask $MSK_ADSL up

#interface FRAME
#ifconfig $FRAME down
ifconfig $FRAME $IP_FRAME netmask $MSK_FRAME up

route add default $ADSL
route add default gw $GW_ADSL


# Os diversos módulos do iptables são chamdos através do modprobe
modprobe ip_tables
modprobe iptable_nat
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_LOG
modprobe ipt_REJECT
modprobe ipt_MASQUERADE
modprobe ipt_state
modprobe ipt_multiport
modprobe iptable_mangle
modprobe ipt_tos
modprobe ipt_limit
modprobe ipt_mark
modprobe ipt_MARK

case "$1" in
start)

echo "====================================================================|"
echo "|: INICIANDO A CONFIGURAÇÃO DO FIREWALL NETFILTER ATRAVÉS :|"
echo "|: DO IPTABLES :|"
echo "====================================================================|"

$IPTABLES -F
$IPTABLES -F INPUT
$IPTABLES -F OUTPUT
$IPTABLES -F FORWARD
$IPTABLES -t mangle -F
$IPTABLES -t nat -F
$IPTABLES -X

$IPTABLES -P INPUT DROP
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -P FORWARD DROP

# ativar o redirecionamento no arquivo ip_forward
echo "1" > /proc/sys/net/ipv4/ip_forward
echo "\nAtivado o redirecionamento no arquivo ip_forward"
echo "ON .................................................[ OK ]"


#habilitando o fluxo interno entre os processos
$IPTABLES -I INPUT -i lo -j ACCEPT
$IPTABLES -I OUTPUT -o lo -j ACCEPT
echo "\nAtivado o fluxo interno entre os processos"
echo "ON .................................................[ OK ]\n"

#liberar as portas principais do servidor
for i in `cat $PORTSLIB`; do
SRVC=`echo $i | cut -d '=' -f 1`
PRT=`echo $i | cut -d '=' -f 2`
$IPTABLES -A INPUT -p tcp --dport $PRT -j ACCEPT
$IPTABLES -A FORWARD -p tcp --dport $PRT -j ACCEPT
$IPTABLES -A OUTPUT -p tcp --sport $PRT -j ACCEPT
echo "\tPorta Liberada: $SRVC-$PRT"
done
$IPTABLES -I INPUT -m state --state ESTABLISHED -j ACCEPT
$IPTABLES -I INPUT -m state --state RELATED -j ACCEPT
$IPTABLES -I OUTPUT -p icmp -o $ADSL -j ACCEPT
$IPTABLES -I INPUT -p icmp -j ACCEPT
echo "Ativado as portas abertas para estabelecer conexões"
echo "Ativado a liberação das portas principais do servidor $HOSTNAME"
echo "ON .................................................[ OK ]\n"


for i in `cat $REDIRECT`; do
PROTO=`echo $i | cut -d ';' -f 1` #recebe o protocolo a ser redirecionado
PORTA=`echo $i | cut -d ';' -f 2` #recebe a porta a ser redirecionado
IP=`echo $i | cut -d ';' -f 3` #recebe o ip a ser redirecionado
SERVICO=`echo $i | cut -d ';' -f 4` #recebe o nome do serviço
HOST=`echo $i | cut -d ';' -f 5` #recebe o nome do host
$IPTABLES -A FORWARD -p $PROTO --dport $PORTA -j ACCEPT
$IPTABLES -t nat -A PREROUTING -p $PROTO -i $FRAME --dport $PORTA -j DNAT --to $IP
echo "\tAtivado o serviço $SERVICO para o $HOST"
done # fim do comando laço for
echo "Ativado o redirecionamento das portas da WAN para LAN"
echo "ON .................................................[ OK ]\n"

#bloquear acesso de sites negados a rede interna
for i in `cat $SITESNEGADOS`; do
$IPTABLES -t filter -I FORWARD -s $REDE -d $i -j DROP
$IPTABLES -t filter -I FORWARD -s $i -d $REDE -j DROP
$IPTABLES -t filter -A INPUT -s $i -j DROP
$IPTABLES -t filter -A OUTPUT -d $i -j DROP
echo "\tAcesso ao site negado: $i"
done
echo "Ativado o bloqueio de envio de pacotes com origem aos sites negados"
echo "ON .................................................[ OK ]\n"

#amarrar ip ao mac
for i in `cat $MACLIBERADOS`; do
IPSOURCE=`echo $i | cut -d ';' -f 1`
MACSOURCE=`echo $i | cut -d ';' -f 2`
HOST=`echo $i | cut -d ';' -f 3`
$IPTABLES -t filter -A FORWARD -d 0/0 -s $IPSOURCE -m mac --mac-source $MACSOURCE -j ACCEPT
$IPTABLES -t filter -A FORWARD -d $IPSOURCE -s 0/0 -j ACCEPT
$IPTABLES -t filter -A INPUT -s $IPSOURCE -d 0/0 -m mac --mac-source $MACSOURCE -j ACCEPT
$IPTABLES -t filter -A OUTPUT -s $IPSOURCE -d 0/0 -j ACCEPT
$IPTABLES -t filter -A OUTPUT -s 0/0 -d $IPSOURCE -j ACCEPT
echo "\tLiberado para rede: $HOST - $IPSOURCE"
done
$IPTABLES -t filter -I INPUT -s $REDE -j DROP
$IPTABLES -t filter -I FORWARD -s $REDE -d 0/0 -j DROP
$IPTABLES -t filter -I FORWARD -s 0/0 -d $REDE -j DROP
$IPTABLES -t filter -I OUTPUT -d $REDE -j DROP
echo "Ativado a amarração do ip ao mac"
echo "ON .................................................[ OK ]"


#proxy transparente
$IPTABLES -t nat -A PREROUTING -i $LAN -p tcp --dport 80 -j REDIRECT --to-port 3128
echo "\nProxy Transparente ativado"
echo "ON .................................................[ OK ]"

# ativar o mascaramento
$IPTABLES -t nat -A POSTROUTING -o $ADSL -j MASQUERADE
echo
echo "====================================================================|"
echo "|: TERMINADA A CONFIGURAÇÃO DO FIREWALL NETFILTER ATRAVÉS :|"
echo "|: DO IPTABLES :|"
echo "====================================================================|"
echo -e "FIREWALL ATIVADO - SISTEMA PREPARADO\n\n"
;;

stop)
$IPTABLES -F
$IPTABLES -F INPUT
$IPTABLES -F OUTPUT
$IPTABLES -F FORWARD
$IPTABLES -t mangle -F
$IPTABLES -t nat -F
$IPTABLES -X
$IPTABLES -Z

$IPTABLES -P INPUT ACCEPT
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -P FORWARD ACCEPT


echo "FIREWALL DESCARREGADO - SISTEMA LIBERADO"
;;

restart)
$PROGRAMA stop
$PROGRAMA start
;;
*)
echo "Use: $N {start|stop|restart}" >&2
echo "\n{TEXTO}33[01;31mATENÇÃO";tput sgr0
echo "\nVocê não colocou nenhum argumento ou algum que seja conhecido, então Por Padrão será dado em 5 segundos um restart no firewall.\n\n\n"
sleep 5
$PROGRAMA restart
exit 1
esac
exit 0





  


2. Re: Firewall com 3 interfaces...

Genesco Sousa
gesousa

(usa Ubuntu)

Enviado em 28/09/2009 - 17:26h

Bom não conseguir entender bem este seu firewall, pelo que vi vc redirecionou todo o trafico em vez criar as tabelas de roteamento com i ip route e criar a marcação das portas no iptables para fazer o redirecionamento...

acho que seria mais fácil da uma lida nos artigos abaixo, que vc terá uma ideia de como fazer isso:

http://www.vivaolinux.com.br/artigo/Roteamento-de-entrada-saida-com-iproute-e-iptables
http://www.vivaolinux.com.br/artigo/Firewall-Linux-Roteamento-avancado-usando-iproute2-e-iptables-%2...






Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts