maurolarrat
(usa Ubuntu)
Enviado em 26/10/2011 - 12:30h
#!/bin/bash
# ========================================================
# === COMANDOS PARA EXECUÇÃO DO FIREWALL NO SHELL:
# === PARA ATIVAR: /etc/network/NG-Firewall.sh ativar
# === PARA desativar: /etc/network/NG-Firewall.sh desativar
#=========================================================
# ========================================================
# === Carregando módulos de iptables e NAT no Kernel.
#=========================================================
echo "Carregando os módulos de Iptables..."
modprobe iptable_filter
modprobe iptable_nat
modprobe iptable_mangle
modprobe ip_nat_ftp
modprobe ip_conntrack
modprobe ip_conntrack_netlink
modprobe ip_conntrack_ftp
modprobe ip_queue
modprobe ip_tables
modprobe nf_conntrack_ipv4
modprobe ipt_LOG
modprobe ipt_MARK
modprobe ipt_MASQUERADE
modprobe ipt_REDIRECT
modprobe ipt_TCPMSS
modprobe ipt_REJECT
modprobe ipt_limit
modprobe ipt_mac
modprobe ipt_mark
modprobe ipt_multiport
modprobe ipt_owner
modprobe ipt_state
modprobe ipt_tos
echo "Módulos carregados."
#=========================================================
# === Declaração e configurações das variáveis de interface de rede.
#=========================================================
echo "Configurando as interfaces de rede, internet e intranet..."
INTRA=eth2
echo " Interface eth2 (INTRANET) com IP 192.168.0.1"
INTER=eth0
echo " Interface eth0 (INTERNET)com IP 200.208.3.178"
LAN=192.168.0.0/24
echo " Intranet local: 192.168.0.0/24"
# ======================= *** ============================
# Inicia as regras do Firewall.
# Este Firewall permite parâmetros de configuração.
# Descrição dos parâmetros:
# desativar :
# Limpa as regras do Firewall (FLUSH).
# Permite acesso à rede sem bloqueios (FULL NAT).
# Configura as Políticas Padrões da tabela filter (ACCEPT).
#=========================================================
#=========================================================
# === Desativar o firewall.
#=========================================================
if [ "$1" = "desativar" ]; then
echo "Desativando o Firewall..."
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
echo "Firewall DESATIVADO. NAT ativado."
#=========================================================
# === Ativa o Firewall.
#=========================================================
elif [ "$1" = "ativar" ]; then
echo "Ativando o Firewall..."
iptables -t filter -P INPUT DROP
iptables -t filter -P FORWARD DROP
iptables -t filter -P OUTPUT ACCEPT
echo "Firewall carregado e executando. NAT ativado."
else
echo "Ativando firewall sem parâmetros. NAT ativado."
fi
# Limpa todas as regras de política.
iptables -F
iptables -X
iptables -Z
iptables -t nat -F
iptables -t nat -X
iptables -t nat -Z
iptables -t mangle -F
iptables -t mangle -X
iptables -t mangle -Z
# Permissão de LOOPBACK para processos do Firewall.
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
# Mascaramento da rede local (Intranet).
iptables -t nat -A POSTROUTING -o $INTER -j MASQUERADE
# Statefull inspection
iptables -I FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# FTP-DATA FTP SSH
iptables -A FORWARD -p tcp --dport 20:22 -j ACCEPT
# DNS (TCP e UDP)
iptables -A FORWARD -p udp --dport 53 -j ACCEPT
iptables -A FORWARD -p tcp --dport 53 -j ACCEPT
##############################
# FAÇA AS SUAS LIBERAÇÕES AQUI
##############################
# E-MAIL TURBOSITE
iptables -I FORWARD -s 201.87.225.0/24 -p all -j ACCEPT
iptables -I FORWARD -s 201.87.225.53 -p all -j ACCEPT
iptables -I FORWARD -s 201.87.225.54 -p all -j ACCEPT
# SMTP (Configurado para a Turbo Site).
iptables -A FORWARD -p tcp --dport 587 -j ACCEPT
iptables -A FORWARD -p tcp --dport 25 -j ACCEPT
iptables -A FORWARD -p udp --dport 587 -j ACCEPT
iptables -A FORWARD -p udp --dport 25 -j ACCEPT
iptables -A FORWARD -p tcp --sport 587 -j ACCEPT
iptables -A FORWARD -p tcp --sport 25 -j ACCEPT
iptables -A FORWARD -p udp --sport 587 -j ACCEPT
iptables -A FORWARD -p udp --sport 25 -j ACCEPT
# POP3 (Configurado para a Turbo Site).
iptables -A FORWARD -p tcp --dport 110 -j ACCEPT
iptables -A FORWARD -p udp --dport 110 -j ACCEPT
iptables -A FORWARD -p tcp --sport 110 -j ACCEPT
iptables -A FORWARD -p udp --sport 110 -j ACCEPT
iptables -A FORWARD -p tcp --dport 143 -j ACCEPT
iptables -A FORWARD -p udp --dport 143 -j ACCEPT
iptables -A FORWARD -p tcp --sport 143 -j ACCEPT
iptables -A FORWARD -p udp --sport 143 -j ACCEPT
# IMAP (Configurado para GMAIL).
# POP3 (Configurado para a Turbo Site).
iptables -A FORWARD -p tcp --dport 993 -j ACCEPT
iptables -A FORWARD -p udp --dport 993 -j ACCEPT
iptables -A FORWARD -p tcp --sport 993 -j ACCEPT
iptables -A FORWARD -p udp --sport 993 -j ACCEPT
# SMTP (Configurado para GMAIL).
# POP3 (Configurado para a Turbo Site).
iptables -A FORWARD -p tcp --dport 465 -j ACCEPT
iptables -A FORWARD -p udp --dport 465 -j ACCEPT
iptables -A FORWARD -p tcp --sport 465 -j ACCEPT
iptables -A FORWARD -p udp --sport 465 -j ACCEPT
# HTTP/HTTPS (COM O SQUID)
#FILTER
iptables -A INPUT -i $INTRA -p tcp -m multiport --dport 3128,3130 -j ACCEPT
iptables -A INPUT -i $INTRA -p tcp -m multiport --dport 80,443 -j ACCEPT
iptables -A FORWARD -p tcp -m multiport --dport 80,443 -j ACCEPT
#NAT Pré-roteamento Squid. DEVE utilizar APPEND (-A) para não bloquear as regras de banco abaixo.
iptables -t nat -A PREROUTING -i $INTRA -p tcp --dport 80 -j REDIRECT --to-port 3128
# HTTP/HTTPS (SEM O SQUID)
#FILTER
#iptables -I FORWARD -i $INTRA -o $INTER -p tcp -m multiport --dport 80,443 -j ACCEPT
#iptables -I FORWARD -i $INTER -o $INTRA -p tcp -m multiport --dport 80,443 -j ACCEPT
# Final do script.