Tom_Skunk
(usa Ubuntu)
Enviado em 20/10/2009 - 11:20h
#!/bin/bash
#
#---------------------------------
# INICIA O FIREWALL
#---------------------------------#
case "$1" in
start)
I_LAN=eth0
I_NET=ppp0
# PORTAS="20,21,22,25,53,80,106,110,445,563,2390,3389"
REDE="192.168.1.0/24"
DNSPROVEDOR="xxx.xxx.xxx.xxx"
# Carrega os modulos do iptables
modprobe ip_tables
modprobe iptable_nat
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
# Limpa as regras do iptables
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P OUTPUT ACCEPT
iptables -t mangle -P PREROUTING ACCEPT
iptables -t mangle -P OUTPUT ACCEPT
# Flushing em todas as regras
iptables -F
iptables -t nat -F
iptables -t mangle -F
# Apaga chains que nao sao default
iptables -X
iptables -t nat -X
iptables -t mangle -X
# Define o bloqueio por padrao
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
#iptables -t nat -P PREROUTING DROP
#iptables -t nat -P POSTROUTING DROP
#Abre uma faixa de portas para internet
# iptables -A INPUT -m multiport -p tcp --port $PORTAS -j ACCEPT
# Configuracao para o VOIP
iptables -A INPUT -m udp -p udp --dport 10000:20000 -j ACCEPT
iptables -A OUTPUT -m udp -p udp --dport 10000:20000 -j ACCEPT
iptables -A FORWARD -m udp -p udp --dport 10000:20000 -j ACCEPT
iptables -A INPUT -p udp --dport 5060 -j ACCEPT
iptables -A FORWARD -p udp --dport 5060 -j ACCEPT
iptables -A OUTPUT -p udp --dport 5060 -j ACCEPT
# Roteamento da porta 3389 - Windows Terminal Server
iptables -A INPUT -p tcp --dport 3389 -j ACCEPT
iptables -A FORWARD -p tcp --dport 3389 -j ACCEPT
iptables -A INPUT -p tcp -i eth1 --dport 3389 -j ACCEPT
iptables -A INPUT -p udp -i eth1 --dport 3389 -j ACCEPT
iptables -A INPUT -p tcp -i eth0 --dport 3389 -j ACCEPT
iptables -A INPUT -p udp -i eth0 --dport 3389 -j ACCEPT
iptables -A INPUT -p tcp -i ppp0 --dport 3389 -j ACCEPT
iptables -A INPUT -p udp -i ppp0 --dport 3389 -j ACCEPT
# iptables -t nat -A PREROUTING -i $I_NET -p tcp --dport 3389 -j DNAT --to 192.168.1.110:3389
# Habilita o repasse de pacotes
echo "1" > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o $I_NET -j MASQUERADE
# Bloquear MSN, além do bloqueio no squid
iptables -A FORWARD -s $REDE -p tcp --dport 1863 -j REJECT
iptables -A FORWARD -s $REDE -p tcp --dport 5190 -j REJECT
# Desvia para o Squid
iptables -t nat -A PREROUTING -i $I_LAN -p tcp --dport 80 -j REDIRECT --to-port 3128
# Fecha as portas 1863 / 5190 para impedir que o MSN faça login.
iptables -I FORWARD -p tcp --dport 1863 -j DROP
iptables -I FORWARD -p tcp --dport 5190 -j DROP
#Abre uma faixa de portas para internet
# iptables -A INPUT -m multiport -p tcp --port $PORTAS -j ACCEPT
#iptables -A INPUT -m multiport -p tcp --port 10000:20000 -j ACCEPT
# Libera Bradesco
iptables -t nat -A PREROUTING -i eth0 -d 200.155.88.15 -p tcp --dport 80 -j ACCEPT
# Libera Caixa
iptables -t nat -A PREROUTING -i eth0 -d 200.201.166.200 -p tcp --dport 80 -j ACCEPT
# Libera Itau
iptables -t nat -A PREROUTING -i eth0 -d 200.246.143.40 -p tcp --dport 80 -j ACCEPT
# Micro win2003srv
#iptables -t nat -A PREROUTING -p tcp -m tcp --dport 3380 -s 0/0 -j DNAT --to-destination 192.168.1.110:3389
#iptables -t nat -A PREROUTING -p udp -m udp --dport 3380 -s 0/0 -j DNAT --to-destination 192.168.1.110:3389
#Abre uma faixa para loopback
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A INPUT -p tcp --syn -s 127.0.0.1/255.0.0.0 -j ACCEPT
iptables -A INPUT -p tcp -s $REDE -j ACCEPT
# Aceita FTP
iptables -A FORWARD -p tcp --dport 20 -j ACCEPT
iptables -A FORWARD -p tcp --dport 21 -j ACCEPT
# Aceita SSH
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A FORWARD -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 22 -j ACCEPT
# Aceita SMTP
iptables -A FORWARD -p tcp --dport 25 -j ACCEPT
# Aceita DNS
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A FORWARD -p tcp --dport 53 -j ACCEPT
iptables -A FORWARD -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
# Aceita HTTP
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A FORWARD -p tcp --dport 80 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT
# Aceita Hamachi
iptables -A INPUT -p tcp --sport 6352 -j ACCEPT
iptables -A FORWARD -p tcp --sport 6352 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 6352 -j ACCEPT
iptables -A INPUT -p udp --sport 6352 -j ACCEPT
iptables -A FORWARD -p udp --sport 6352 -j ACCEPT
iptables -A OUTPUT -p udp --sport 6352 -j ACCEPT
iptables -A OUTPUT -d 0/0 -p tcp --dport 5800:5999 -j ACCEPT
iptables -A INPUT -d 0/0 -p tcp --dport 5800:5900 -j ACCEPT
iptables -A FORWARD -d 0/0 -p tcp --dport 5800:5999 -j ACCEPT
# Aceita POP3
iptables -A FORWARD -p tcp --dport 110 -j ACCEPT
# Aceita Proxy
iptables -A INPUT -p tcp --dport 3128 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 3128 -j ACCEPT
########################## IP Masquerading ###################################
# Masq FTP
iptables -t nat -A POSTROUTING -s $REDE -p tcp --dport 20 -j MASQUERADE
iptables -t nat -A POSTROUTING -s $REDE -p tcp --dport 21 -j MASQUERADE
# Masq SSH
iptables -t nat -A POSTROUTING -s $REDE -p tcp --dport 22 -j MASQUERADE
# Masq POP3
iptables -t nat -A POSTROUTING -s $REDE -p tcp --dport 110 -j MASQUERADE
#Abre portas para UltraVNC
iptables -A INPUT -m multiport -p tcp --port 5900 -j ACCEPT
iptables -A INPUT -m multiport -p udp --port 5900 -j ACCEPT
#Protege contra synflood
iptables -N syn-flood
iptables -A INPUT -i $I_NET -p tcp --syn -j syn-flood
iptables -A syn-flood -m limit --limit 1/s --limit-burst 4 -j RETURN
iptables -A FORWARD -p tcp --syn -m limit --limit 1/s -j ACCEPT
iptables -A syn-flood -j DROP
#Bloqueia Traceroute
iptables -A INPUT -p udp -i $I_NET --dport 33435:33525 -j DROP
# Protecao contra worms
iptables -A FORWARD -p tcp --dport 135 -i $I_LAN -j DROP
# Protege contra os "Ping of Death"
iptables -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT
# Protege contra os ataques do tipo "Syn-flood, DoS, etc"
iptables -A FORWARD -p tcp -m limit --limit 1/s -j ACCEPT
# Protege contra todos os pacotes danificados e ou suspeitos ...
# iptables -A INPUT -m unclean -j DROP
# iptables -A FORWARD -m unclean -j DROP
# Permitir repasse (NAT,DNAT,SNAT) de pacotes estabelecidos e os relatados ...
iptables -A INPUT -m state --state ESTABLISHED,RELATED -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 NEW,INVALID -j DROP
iptables -A FORWARD -m state --state NEW,INVALID -j DROP
iptables -A OUTPUT -m state --state NEW,INVALID -j DROP
# Protege contra port scanners avancados (Ex.: nmap)
iptables -A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT
# Protege contra pacotes que podem procurar e obter informacoes da rede interna ...
iptables -A FORWARD --protocol tcp --tcp-flags ALL SYN,ACK -j DROP
# Protecoes contra ataques
iptables -A INPUT -m state --state INVALID -j DROP
# Performance - Setando acesso a web com delay minimo
iptables -t mangle -A OUTPUT -o $I_NET -p tcp --dport 53 -j TOS --set-tos Minimize-Delay
iptables -t mangle -A OUTPUT -o $I_NET -p tcp --dport 80 -j TOS --set-tos Minimize-Delay
# Deixa passar as portas UDP do servidores DNS, e rejeita o restante
iptables -A INPUT -i $I_NET -p udp -s $DNSPROVEDOR -j ACCEPT
iptables -A INPUT -i $I_NET -p udp -s $DNSPROVEDOR -j ACCEPT
#iptables -A INPUT -i $I_NET -p udp -j DROP
# Bloqueia qualquer tentativa de conexao de fora para dentro por TCP
iptables -A INPUT -i $I_NET -p tcp --syn -j DROP
# Responde pacotes icmp especificados e rejeita o restante
iptables -A INPUT -i $I_NET -p icmp --icmp-type host-unreachable -j ACCEPT
iptables -A INPUT -i $I_NET -p icmp --icmp-type source-quench -j ACCEPT
iptables -A INPUT -i $I_NET -p icmp -j REJECT --reject-with icmp-host-unreachable
# libera conexoes de fora pra dentro
iptables -A INPUT -p tcp --destination-port 80 -j ACCEPT
iptables -A INPUT -p tcp --destination-port 443 -j ACCEPT
iptables -A INPUT -p tcp --destination-port 20 -j ACCEPT
iptables -A INPUT -p tcp --destination-port 21 -j ACCEPT
iptables -A INPUT -p tcp --destination-port 22 -j ACCEPT
# libera conexoes de dentro pra fora:
iptables -A OUTPUT -p tcp --destination-port 80 -j ACCEPT
iptables -A OUTPUT -p tcp --destination-port 3306 -j ACCEPT
iptables -A OUTPUT -p tcp --destination-port 22 -j ACCEPT
iptables -A OUTPUT -p tcp --destination-port 20 -j ACCEPT
iptables -A OUTPUT -p tcp --destination-port 21 -j ACCEPT
iptables -A OUTPUT -p tcp --destination-port 86 -j ACCEPT
iptables -A OUTPUT -p tcp --destination-port 5190 -j ACCEPT
iptables -A OUTPUT -p tcp --destination-port 443 -j ACCEPT
# compartilha a web na rede interna
iptables -t nat -A POSTROUTING -s $REDE -o $I_NET -j MASQUERADE
echo 1 > /proc/sys/net/ipv4/ip_forward
# Fecha todas as portas abaixo de 10000
# iptables -A INPUT -i $I_NET -p tcp --dport :10000 -j DROP
# bloqueia o resto
iptables -A INPUT -p tcp --syn -j DROP
iptables -A OUTPUT -p tcp --syn -j DROP
iptables -A FORWARD -p tcp --syn -j DROP
# bloqueia ping
# iptables -A FORWARD -p icmp --icmp-type echo-request -j DROP
echo "0" > /proc/sys/net/ipv4/icmp_echo_ignore_all
;;
#---------------------------------
# INTERROMPE O FIREWALL
#---------------------------------
stop)
# Limpa as regras do iptables
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P OUTPUT ACCEPT
iptables -t mangle -P PREROUTING ACCEPT
iptables -t mangle -P OUTPUT ACCEPT
# Flushing em todas as regras
iptables -F
iptables -t nat -F
iptables -t mangle -F
# Apaga chains que nao sao default
iptables -X
iptables -t nat -X
iptables -t mangle -X
;;
#---------------------------------
# CHECA O STATUS DO FIREWALL
#---------------------------------
status)
clear
echo
echo
iptables -L
echo
;;
#---------------------------------
# REINICIA O FIREWALL
#---------------------------------
restart)
$0 stop
$0 start
echo
;;
#---------------------------------
# IMPRIME AS OPCOES NO CONSOLE
#---------------------------------#
*)
clear
echo
echo
echo "Uso correto : firewall.rc ( start|stop|status|restart)"
echo
echo
exit 1
echo
;;
esac
exit 0