vaini
(usa Debian)
Enviado em 16/01/2012 - 19:27h
segue meu firewall para ver se mais alguem tem sugestões
#!/bin/sh
#"::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
#"| Script de Firewall - IPTABLES"
#"| Criado por: Fernando Vaini"
#"| Uso: /etc/init.d/firewall start|stop|restart|allow|proxy|help"
#"::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
#Variaveis
PATH=/sbin:/bin:/usr/sbin:/usr/bin
IPTABLES="/sbin/iptables"
PROGRAMA="/etc/init.d/firewall"
WAN=eth0
LAN=eth1
REDE="192.168.0.0/24"
#Carregando modulos do iptables
modprobe iptable_nat
modprobe iptable_mangle
modprobe iptable_filter
modprobe ip_tables
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_conntrack_irc
modprobe ip_nat_ftp
modprobe ip_nat_irc
modprobe ip_queue
modprobe ipt_limit
modprobe ipt_LOG
modprobe ipt_mac
modprobe ipt_MARK
modprobe ipt_MASQUERADE
modprobe ipt_multiport
modprobe ipt_owner
modprobe ipt_recent
modprobe ipt_REJECT
modprobe ipt_state
modprobe ipt_tcpmss
modprobe ipt_tos
case "$1" in
start)
#cabecalho do firewall exibido na tela
echo "==============================================================================="
echo "| CARREGANDO AS REGRAS DO FIREWALL IPTABLES |"
echo "==============================================================================="
#limpando tudo
$IPTABLES -t filter -F
$IPTABLES -t nat -F
$IPTABLES -t mangle -F
$IPTABLES -t filter -X
$IPTABLES -t nat -X
$IPTABLES -t mangle -X
#politica padrao
$IPTABLES -t filter -P INPUT DROP
$IPTABLES -t filter -P OUTPUT ACCEPT
$IPTABLES -t filter -P FORWARD DROP
#ativando roteamento de pacote
echo "1" > /proc/sys/net/ipv4/ip_forward
$IPTABLES -t nat -A POSTROUTING -o $WAN -j MASQUERADE
$IPTABLES -t nat -A PREROUTING -i $LAN -p tcp --dport 80 -j REDIRECT --to 3128
#evita problemas de conexao quando o ip externo for dinamico (programa diald)
echo "1" > /proc/sys/net/ipv4/ip_dynaddr
#bloqueio a syn-flood, ip spoofing, port scanner e icmp falsos respectivamente
echo "1" > /proc/sys/net/ipv4/tcp_syncookies
echo "1" > /proc/sys/net/ipv4/conf/all/rp_filter
echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
echo "1" > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
#permite que conexoes ja criadas pela LAN retornem sem criar novas regras
$IPTABLES -t filter -A INPUT -m state --state ESTABLISHED,RELATED,NEW -j ACCEPT
$IPTABLES -t filter -A OUTPUT -m state --state ESTABLISHED,RELATED,NEW -j ACCEPT
$IPTABLES -t filter -A FORWARD -m state --state ESTABLISHED,RELATED,NEW -j ACCEPT
#bloqueando synfloods e port scanners por forward respectivamente
$IPTABLES -t filter -A FORWARD -p tcp --syn -m limit --limit 1/s -j ACCEPT
$IPTABLES -t filter -A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT
$IPTABLES -t filter -A FORWARD --protocol tcp --tcp-flags ALL SYN,ACK -j DROP
#liberando acesso ao servidor
$IPTABLES -t filter -A INPUT -i lo -j ACCEPT
$IPTABLES -t filter -A OUTPUT -o lo -j ACCEPT
#liberando passagem de pacotes entre interfaces
$IPTABLES -t filter -A FORWARD -i $LAN -o $WAN -j ACCEPT
#criando logs
$IPTABLES -A INPUT -i $WAN -p tcp --dport 3389 -j LOG --log-level 7 --log-prefix "Firewall - TS:"
$IPTABLES -A INPUT -i $WAN -p tcp --dport 1226 -j LOG --log-level 7 --log-prefix "Firewall - SSH:"
$IPTABLES -A INPUT -i $WAN -p tcp --dport 1433 -j LOG --log-level 7 --log-prefix "Firewall - SISTEMA:"
$IPTABLES -A INPUT -i $WAN -p tcp --dport 80 -j LOG --log-level 7 --log-prefix "Firewall - HTTP:"
$IPTABLES -A INPUT -i $WAN -p tcp --dport 110 -j LOG --log-level 7 --log-prefix "Firewall - POP:"
$IPTABLES -A INPUT -i $WAN -p tcp --dport 25 -j LOG --log-level 7 --log-prefix "Firewall - SMTP:"
$IPTABLES -A INPUT -i $WAN -p tcp --dport 143 -j LOG --log-level 7 --log-prefix "Firewall - IMAP:"
$IPTABLES -A INPUT -i $WAN -p tcp --dport 3128 -j LOG --log-level 7 --log-prefix "Firewall - SQUID:"
$IPTABLES -A INPUT -i $WAN -p tcp --dport 10000 -j LOG --log-level 7 --log-prefix "Firewall - WEBMIN:"
$IPTABLES -A INPUT -i $WAN -p icmp -j LOG --log-level 7 --log-prefix "Firewall - PING:"
#aceitando e protegendo ping
echo "0" > /proc/sys/net/ipv4/icmp_echo_ignore_all
$IPTABLES -t filter -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT
$IPTABLES -t filter -A INPUT -p icmp -m state --state INVALID -j DROP
$IPTABLES -t filter -A OUTPUT -p icmp -m state --state INVALID -j DROP
$IPTABLES -t filter -A FORWARD -p icmp -m state --state INVALID -j DROP
#liberandos portas para acesso interno
$IPTABLES -t filter -A INPUT -i $LAN -p tcp -m multiport --dports 80,3128,10000 -j ACCEPT
$IPTABLES -t filter -A INPUT -i $LAN -p udp -m multiport --dports 80,3128,10000 -j ACCEPT
#liberando samba para a rede interna
$IPTABLES -t filter -A INPUT -i $LAN -s $REDE -p tcp -m multiport --dports 137,138,139,445 -j ACCEPT
$IPTABLES -t filter -A INPUT -i $LAN -s $REDE -p udp -m multiport --dports 137,138,139,445 -j ACCEPT
$IPTABLES -t filter -A OUTPUT -o $LAN -s $REDE -p tcp -m multiport --dports 137,138,139,445 -j ACCEPT
$IPTABLES -t filter -A OUTPUT -o $LAN -s $REDE -p udp -m multiport --dports 137,138,139,445 -j ACCEPT
#liberando acesso externo ao webmin
$IPTABLES -t filter -A INPUT -i $WAN -p tcp --dport 10000 -j ACCEPT
#liberando DNS
$IPTABLES -t filter -A OUTPUT -p udp --dport 53 -j ACCEPT
$IPTABLES -t filter -A FORWARD -i $LAN -p udp --dport 53 -j ACCEPT
#liberando HTTPS
$IPTABLES -t filter -A OUTPUT -p tcp --dport 443 -j ACCEPT
$IPTABLES -t filter -A FORWARD -i $LAN -p tcp --dport 443 -j ACCEPT
#liberando SMTP/POP para a LAN
$IPTABLES -t filter -A FORWARD -i $LAN -p tcp --dport 25 -j ACCEPT
$IPTABLES -t filter -A FORWARD -i $LAN -p tcp --dport 110 -j ACCEPT
#liberando entrada/retorno do SSH
$IPTABLES -t filter -A INPUT -p tcp --dport 1226 -j ACCEPT
$IPTABLES -t filter -A FORWARD -p tcp --dport 1226 -j ACCEPT
#liberando TS
$IPTABLES -t filter -A INPUT -p tcp --dport 3389 -j ACCEPT
$IPTABLES -t filter -A FORWARD -p tcp --dport 3389 -j ACCEPT
$IPTABLES -t nat -A PREROUTING -i $WAN -p tcp --dport 3389 -j DNAT --to 192.168.0.2:3389
#$IPTABLES -t nat -A PREROUTING -i $WAN -p tcp -m mac --mac-source xx:xx:xx:xx:xx:xx --dport 3389 -j DNAT --to 192.168.0.2:3389
#liberando servidor das cameras
$IPTABLES -t filter -A INPUT -p tcp --dport 2550 -j ACCEPT
$IPTABLES -t filter -A FORWARD -p tcp --dport 2550 -j ACCEPT
$IPTABLES -t nat -A PREROUTING -i $WAN -p tcp --dport 2550 -j DNAT --to 192.168.0.3:2550
#liberando entrada/retorno do sistema interno
$IPTABLES -t filter -A INPUT -p tcp --dport 1433 -j ACCEPT
$IPTABLES -t filter -A FORWARD -p tcp --dport 1433 -j ACCEPT
$IPTABLES -t nat -A PREROUTING -i $WAN -p tcp --dport 1433 -j DNAT --to 192.168.0.2:1433
$IPTABLES -t filter -A INPUT -p tcp --dport 4096 -j ACCEPT
$IPTABLES -t filter -A FORWARD -p tcp --dport 4096 -j ACCEPT
$IPTABLES -t nat -A PREROUTING -i $WAN -p tcp --dport 4096 -j DNAT --to 192.168.0.2:4096
$IPTABLES -t filter -A INPUT -p tcp --dport 61199 -j ACCEPT
$IPTABLES -t filter -A FORWARD -p tcp --dport 61199 -j ACCEPT
$IPTABLES -t nat -A PREROUTING -i $WAN -p tcp --dport 61199 -j DNAT --to 192.168.0.2:61199
#liberando acesso pelo radmin
$IPTABLES -t filter -A INPUT -p tcp --dport 60002 -j ACCEPT
$IPTABLES -t filter -A FORWARD -p tcp --dport 60002 -j ACCEPT
$IPTABLES -t nat -A PREROUTING -i $WAN -p tcp --dport 60002 -j DNAT --to 192.168.0.2:61145
$IPTABLES -t filter -A INPUT -p tcp --dport 60003 -j ACCEPT
$IPTABLES -t filter -A FORWARD -p tcp --dport 60003 -j ACCEPT
$IPTABLES -t nat -A PREROUTING -i $WAN -p tcp --dport 60003 -j DNAT --to 192.168.0.3:61145
$IPTABLES -t filter -A INPUT -p tcp --dport 60020 -j ACCEPT
$IPTABLES -t filter -A FORWARD -p tcp --dport 60020 -j ACCEPT
$IPTABLES -t nat -A PREROUTING -i $WAN -p tcp --dport 60020 -j DNAT --to 192.168.0.20:61145
$IPTABLES -t filter -A INPUT -p tcp --dport 60021 -j ACCEPT
$IPTABLES -t filter -A FORWARD -p tcp --dport 60021 -j ACCEPT
$IPTABLES -t nat -A PREROUTING -i $WAN -p tcp --dport 60021 -j DNAT --to 192.168.0.21:61145
$IPTABLES -t filter -A INPUT -p tcp --dport 60030 -j ACCEPT
$IPTABLES -t filter -A FORWARD -p tcp --dport 60030 -j ACCEPT
$IPTABLES -t nat -A PREROUTING -i $WAN -p tcp --dport 60030 -j DNAT --to 192.168.0.30:61145
$IPTABLES -t filter -A INPUT -p tcp --dport 60031 -j ACCEPT
$IPTABLES -t filter -A FORWARD -p tcp --dport 60031 -j ACCEPT
$IPTABLES -t nat -A PREROUTING -i $WAN -p tcp --dport 60031 -j DNAT --to 192.168.0.31:61145
$IPTABLES -t filter -A INPUT -p tcp --dport 60032 -j ACCEPT
$IPTABLES -t filter -A FORWARD -p tcp --dport 60032 -j ACCEPT
$IPTABLES -t nat -A PREROUTING -i $WAN -p tcp --dport 60032 -j DNAT --to 192.168.0.32:61145
$IPTABLES -t filter -A INPUT -p tcp --dport 60033 -j ACCEPT
$IPTABLES -t filter -A FORWARD -p tcp --dport 60033 -j ACCEPT
$IPTABLES -t nat -A PREROUTING -i $WAN -p tcp --dport 60033 -j DNAT --to 192.168.0.33:61145
$IPTABLES -t filter -A INPUT -p tcp --dport 60034 -j ACCEPT
$IPTABLES -t filter -A FORWARD -p tcp --dport 60034 -j ACCEPT
$IPTABLES -t nat -A PREROUTING -i $WAN -p tcp --dport 60033 -j DNAT --to 192.168.0.33:61145
$IPTABLES -t filter -A INPUT -p tcp --dport 60035 -j ACCEPT
$IPTABLES -t filter -A FORWARD -p tcp --dport 60035 -j ACCEPT
$IPTABLES -t nat -A PREROUTING -i $WAN -p tcp --dport 60035 -j DNAT --to 192.168.0.35:61145
$IPTABLES -t filter -A INPUT -p tcp --dport 60045 -j ACCEPT
$IPTABLES -t filter -A FORWARD -p tcp --dport 60045 -j ACCEPT
$IPTABLES -t nat -A PREROUTING -i $WAN -p tcp --dport 60045 -j DNAT --to 192.168.0.45:61145
#liberando msn para determinado ip e bloqueando o restante
$IPTABLES -t filter -A FORWARD -s 192.168.0.20 -p tcp --dport 1863 -j ACCEPT
$IPTABLES -t filter -A FORWARD -s 192.168.0.20 -p tcp --dport 5190 -j ACCEPT
$IPTABLES -t filter -A FORWARD -s 192.168.0.21 -p tcp --dport 1863 -j ACCEPT
$IPTABLES -t filter -A FORWARD -s 192.168.0.21 -p tcp --dport 5190 -j ACCEPT
$IPTABLES -t filter -A FORWARD -s $REDE -p tcp --dport 1863 -j REJECT
$IPTABLES -t filter -A FORWARD -s $REDE -p tcp --dport 5190 -j REJECT
#setando alta prioridade para a porta 1433
$IPTABLES -t mangle -A INPUT -p tcp --dport 1433 -j TOS --set-tos 16
$IPTABLES -t mangle -A OUTPUT -p tcp --dport 1433 -j TOS --set-tos 16
$IPTABLES -t mangle -A FORWARD -p tcp --dport 1433 -j TOS --set-tos 16
$IPTABLES -t mangle -A PREROUTING -p tcp --dport 1433 -j TOS --set-tos 16
echo "==============================================================================="
echo "| REGRAS DO FIREWALL CARREGADAS COM SUCESSO |"
echo "==============================================================================="
#fim das regras
;;
stop)
echo -e "Desligando o Firewall \c"
for passo in $(seq 50); do echo -e ".\c"; sleep 0.01; done; echo -e " [ {TTEXTO}33[01;32mOK{TTEXTO}33[00;37m ]"
$IPTABLES -F
$IPTABLES -F INPUT
$IPTABLES -F OUTPUT
$IPTABLES -F FORWARD
$IPTABLES -t mangle -F
$IPTABLES -t nat -F
$IPTABLES -t nat -F
$IPTABLES -X
$IPTABLES -Z
$IPTABLES -P INPUT ACCEPT
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -P FORWARD ACCEPT
echo
echo "==============================================================================="
echo "| FIREWALL DESLIGADO |"
echo "==============================================================================="
echo
;;
restart)
$PROGRAMA stop
$PROGRAMA start
;;
allow)
$PROGRAMA stop
modprobe iptable_nat
$IPTABLES -F
$IPTABLES -t nat -F
$IPTABLES -X
echo "1" > /proc/sys/net/ipv4/ip_forward
$IPTABLES -t nat -A POSTROUTING -o $WAN -j MASQUERADE
echo
echo "==============================================================================="
echo "| FIREWALL DESLIGADO, NAVEGACAO LIBERADA SEM PROXY |"
echo "==============================================================================="
echo
;;
proxy)
$PROGRAMA stop
modprobe iptable_nat
$IPTABLES -F
$IPTABLES -t nat -F
$IPTABLES -X
echo "1" > /proc/sys/net/ipv4/ip_forward
$IPTABLES -t nat -A POSTROUTING -o $WAN -j MASQUERADE
$IPTABLES -t nat -A PREROUTING -p tcp -i $LAN --dport 80 -j REDIRECT --to 3128
echo
echo "==============================================================================="
echo "| FIREWALL DESLIGADO, NAVEGACAO LIBERADA PELO PROXY |"
echo "==============================================================================="
echo
;;
help)
echo
echo "==============================================================================="
echo "|Parametros aceitos pelo firewall: {start|stop|restart|allow|proxy|help} |"
echo "| |"
echo "|start: |"
echo "| Carrega o Firewall com todas as regras de seguranca |"
echo "|stop: |"
echo "| Desliga as regras de seguranca do Firewall e bloqueia a navegacao |"
echo "|restart: |"
echo "| Descarrega/Carrega todas as regras do Firewall |"
echo "|allow: |"
echo "| Desliga as regras de seguranca do Firewall e habilita a navegacao |"
echo "| transparente, sem nenhum bloqueio de Firewall ou Proxy |"
echo "|proxy: |"
echo "| Desliga as regras de seguranca do Firewall e habilita a navegacao |"
echo "| por Proxy, mas sem bloqueio de Firewall |"
echo "|help: |"
echo "| Exibe a tela de ajuda |"
echo "==============================================================================="
echo
;;
*)
echo "{TTEXTO}33[01;31mATENCAO \c";tput sgr0
echo "Argumento Invalido!!!"
echo "Uso Correto: firewall {start|stop|restart|allow|proxy|help}" >&2
exit 1
esac
exit 0
#