andre_ramos
(usa openSUSE)
Enviado em 09/12/2009 - 13:32h
#!/bin/bash
IPTABLES=/usr/sbin/iptables
NETWORK=eth1
WEB=eth0
iniciar () {
# Limpando as regras
$IPTABLES -F
$IPTABLES -Z
$IPTABLES -t nat -F
$IPTABLES -t mangle -F
# Compatilhamento da conexao
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "Ativando compatilhamento!"
# Politicas de acesso geral
$IPTABLES -P INPUT DROP
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -P FORWARD DROP
# Manter conexoes ja estabelecidas para nao parar
$IPTABLES -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A INPUT -m state --state INVALID -j DROP
# Bloqueia pacotes intencionamente mal-formados
$IPTABLES -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
$IPTABLES -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
$IPTABLES -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
$IPTABLES -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
# Evitando SYN_Flood
echo 1 > /proc/sys/net/ipv4/tcp_syncookies
# Carregamento de Modulos
modprobe ip_nat_ftp
modprobe iptable_nat
modprobe ip_tables
modprobe ip_conntrack
modprobe ipt_MASQUERADE
modprobe ip_conntrack_ftp
# Aceita todo o trafego vindo do loopback e indo pro loopback
$IPTABLES -t filter -A INPUT -i lo -j ACCEPT
# Protecoes contra ataques #
$IPTABLES -A INPUT -m state --state INVALID -j DROP
$IPTABLES -A INPUT -i $NETWORK -s ! 192.168.0.0/24 -j DROP
# Redirecionando o trafego para a porta 3128
$IPTABLES -t nat -A PREROUTING -i $NETWORK -p tcp -d ! 200.201.174.207 --dport 80 -j REDIRECT --to-port 3128
#$IPTABLES -t nat -A PREROUTING -i $NETWORK -p tcp -d ! 200.201.174.207 --dport 443 -j REDIRECT --to-port 3128
$IPTABLES -A INPUT -i eth1 -j ACCEPT
# Bloquear o MSN
$IPTABLES -I FORWARD -s 192.168.0.0/24 -p tcp --dport 1863 -j REJECT
$IPTABLES -I FORWARD -s 192.168.0.0/24 -d loginnet.passport.com -j REJECT
#$IPTABLES -A FORWARD -s 192.168.0.0/24 -p tcp -m -d imo.im -j REJECT
# Liberar MSN para determinados IPS
$IPTABLES -I FORWARD -s 192.168.0.221/24 -p tcp --dport 1863 -j ACCEPT #Humberto
$IPTABLES -I FORWARD -s 192.168.0.220/24 -p tcp --dport 1863 -j ACCEPT #Manuel
$IPTABLES -I FORWARD -s 192.168.0.221/24 -d loginnet.passport.com -j ACCEPT #Humberto
$IPTABLES -I FORWARD -s 192.168.0.220/24 -d loginnet.passport.com -j ACCEPT #Manuel
# Regras para liberar o outlook
$IPTABLES -A FORWARD -p udp -s 192.168.0.0/24 -d 200.175.182.139 --dport 53 -j ACCEPT
$IPTABLES -A FORWARD -p udp -s 200.175.182.139 --sport 53 -d 192.168.0.0/24 -j ACCEPT
$IPTABLES -A FORWARD -p udp -s 192.168.0.0/24 -d 200.175.5.139 --dport 53 -j ACCEPT
$IPTABLES -A FORWARD -p udp -s 200.175.5.139 --sport 53 -d 192.168.0.0/24 -j ACCEPT
$IPTABLES -A FORWARD -p tcp -s 192.168.0.0/24 --dport 25 -j ACCEPT # SMTP
$IPTABLES -A FORWARD -p tcp -s 192.168.0.0/24 --dport 587 -j ACCEPT # SMTP
$IPTABLES -A FORWARD -p tcp -s 192.168.0.0/24 --dport 110 -j ACCEPT # POP3
# Regras para liberar o FTP
$IPTABLES -A INPUT -i $NETWORK -p tcp -s 192.168.0.0/24 --dport 20 -j ACCEPT # FTP
$IPTABLES -A INPUT -i $NETWORK -p tcp -s 192.168.0.0/24 --dport 21 -j ACCEPT # FTP
# INPUT # Pacotes que entram na rede
$IPTABLES -A INPUT -i $NETWORK -p tcp --dport 22 -j ACCEPT # SSH
$IPTABLES -A INPUT -i $NETWORK -p tcp --dport 80 -j ACCEPT # HTTP
$IPTABLES -A INPUT -i $NETWORK -p tcp --dport 443 -j ACCEPT # HTTPS
# FORWARD # Pacotes que circulam na rede
$IPTABLES -A FORWARD -i $NETWORK -p tcp --dport 22 -j ACCEPT # SSH
$IPTABLES -A FORWARD -i $NETWORK -p tcp --dport 8333 -j ACCEPT # HTTP
$IPTABLES -A FORWARD -i $NETWORK -p tcp --dport 443 -j ACCEPT # HTTPS
$IPTABLES -A FORWARD -i $NETWORK -s 192.168.0.0/24 -j ACCEPT
# Mascaramento da rede para acesso externo
$IPTABLES -t nat -A POSTROUTING -o eth0 -j MASQUERADE
#--------------------------------------------------------------------------------------
echo "Firewall Ativado"
}
parar(){
echo "Regras de firewall e compartilhamento desativados"
iptables -F
iptables -t nat -F
iptables -t mangle -F
}
case "$1" in
"start") iniciar ;;
"stop") parar ;;
"restart") parar; iniciar;;
*)echo "Use os paramentros start ou stop"
esac