Firewall - IPTables (rc.firewall)
Firewall com máxima segurança
Categoria: Segurança
Software: Firewall - IPTables
[ Hits: 27.606 ]
Por: Sérgio Abrantes Junior
Esse firewall possui política DROP tanto para entrada (INPUT) quando saída de rede interna (FORWARD).
Essas regras estão em pleno funcionamento em cliente.
O arquivo está comentado e, ao meu ver, é de fácil compreensão.
Qualquer dúvida estou aí.
Sérgio Abrantes
[]'s
#!/bin/bash # # /etc/rc.d/rc.firewall # # Start/stop/restart Firewall # # To make Firewall start automatically at boot, make this # file executable: chmod 755 /etc/rc.d/rc.firewall # #Autor: Sérgio Abrasntes Junior sergioabrantes@gmail.com #Data de criação 15/09/05 #Data de modificação 15/12/07 # Variáveris # LanExt=200.193.xx.xx LanInt=192.168.4.1 Rede=192.168.4.0/24 # Modulos # /sbin/modprobe ip_tables /sbin/modprobe ip_conntrack /sbin/modprobe iptable_filter /sbin/modprobe iptable_mangle /sbin/modprobe iptable_nat /sbin/modprobe ipt_LOG /sbin/modprobe ipt_limit /sbin/modprobe ipt_state /sbin/modprobe ipt_REDIRECT /sbin/modprobe ipt_owner /sbin/modprobe ipt_REJECT /sbin/modprobe ipt_MASQUERADE /sbin/modprobe ip_conntrack_ftp /sbin/modprobe ip_nat_ftp #################### ### Função START ### #################### firewall_start() { echo "Iniciando o Firewall" # Limpa as regras # iptables -X iptables -Z iptables -F INPUT iptables -F OUTPUT iptables -F FORWARD iptables -F -t nat iptables -F -t mangle # Politicas padrao # iptables -t filter -P INPUT DROP iptables -t filter -P OUTPUT ACCEPT iptables -t filter -P FORWARD DROP iptables -t nat -P PREROUTING ACCEPT iptables -t nat -P OUTPUT ACCEPT iptables -t nat -P POSTROUTING ACCEPT iptables -t mangle -P PREROUTING ACCEPT iptables -t mangle -P OUTPUT ACCEPT # Manter conexoes jah estabelecidas para nao parar 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 # Aceita todo o trafego vindo do loopback e indo pro loopback iptables -t filter -A INPUT -i lo -j ACCEPT ####################### ### LOG DO FIREWALL ### ####################### #iptables -A INPUT -d $LanExt -p tcp --dport 22 -j LOG --log-level 6 --log-prefix "FIREWALL: SSH EXT 22" #iptables -A INPUT -d $LanExt -p tcp --dport 21 -j LOG --log-level 6 --log-prefix "FIREWALL: FTP EXT 21" #iptables -A INPUT -d $LanInt -p tcp --dport 22 -j LOG --log-level 6 --log-prefix "FIREWALL: SSH INT 22" #iptables -A INPUT -d $LanInt -p tcp --dport 21 -j LOG --log-level 6 --log-prefix "FIREWALL: FTP INT 21" ############################### # Proteções # ############################### # Protege contra os "Ping of Death" iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 20/m -j ACCEPT iptables -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 20/m -j ACCEPT # Protege contra port scanners avançados (Ex.: nmap) iptables -A INPUT -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 20/m -j ACCEPT # Bloqueando tracertroute iptables -A INPUT -p udp -s 0/0 -i eth0 --dport 33435:33525 -j REJECT # Protecoes contra ataques iptables -A INPUT -m state --state INVALID -j REJECT ############################### # TABELA Input # ############################### ### Destino Externo ### # Liberando Porta 22 (SSH) #iptables -A INPUT -d $LanExt -p tcp --dport 22 -j LOG --log-level 6 --log-prefix "FIREWALL: SSH EXT 2222" iptables -A INPUT -d $LanExt -p tcp --dport 22 -j ACCEPT # Liberando Porta 21 (ftp) #iptables -A INPUT -d $LanExt -p tcp --dport 21 -j LOG --log-level 6 --log-prefix "FIREWALL: FTP EXT 21" iptables -A INPUT -d $LanExt -p tcp --dport 21 -j ACCEPT ### Destino Interno ### # Liberando Porta 22 (SSH) #iptables -A INPUT -d $LanInt -p tcp --dport 22 -j LOG --log-level 6 --log-prefix "FIREWALL: SSH INT 22" iptables -A INPUT -d $LanInt -p tcp --dport 22 -j ACCEPT # Liberando porta 3128 (Squid) iptables -A INPUT -d $LanInt -p tcp --dport 3128 -j ACCEPT # Liberando Porta 80 (http) #iptables -A INPUT -d $LanInt -p tcp --dport 80 -j LOG --log-level 6 --log-prefix "FIREWALL: HTTP INT 80" iptables -A INPUT -d $LanInt -p tcp --dport 80 -j ACCEPT # Liberando Porta 21 (ftp) #iptables -A INPUT -d $LanInt -p tcp --dport 21 -j LOG --log-level 6 --log-prefix "FIREWALL: FTP INT 21" iptables -A INPUT -d $LanInt -p tcp --dport 21 -j ACCEPT # Liberando porta 3000 (NTOP) iptables -A INPUT -d $LanInt -p tcp --dport 3000 -j ACCEPT ############################### # TABELA Forward # ############################### # Libera computador das regras do firewall iptables -A FORWARD -s 192.168.4.13 -p tcp -j ACCEPT iptables -A FORWARD -s 192.168.4.13 -p udp -j ACCEPT ### MSN ### # Libera msn para o IP # # nome iptables -A FORWARD -s 192.168.4.11 -p tcp --dport 1863 -j ACCEPT # Bloqueio de MSN # #iptables -A FORWARD -s 192.168.4.0 -p tcp --dport 1863 -j DROP #iptables -A FORWARD -s 192.168.4.0 -d loginnet.passport.com -j DROP #iptables -A FORWARD -s 198.164.4.0/24 -p tcp --dport 1863 -j DROP #iptables -A FORWARD -s 198.164.4.0/24 -d loginnet.passport.com -j DROP #iptables -A FORWARD -s 198.164.4.0/24 -d messenger.hotmail.com -j DROP #iptables -A FORWARD -s 198.164.4.0/24 -d webmessenger.msn.com -j DROP #iptables -A FORWARD -p tcp --dport 1080 -j DROP #iptables -A FORWARD -s 198.164.4.0/24 -p tcp --dport 1080 -j DROP #iptables -A FORWARD -p tcp --dport 1863 -j DROP #iptables -A FORWARD -d 64.4.13.0/24 -j DROP # Liberando Porta 2222 (SSH) iptables -A FORWARD -s $Rede -p tcp --dport 2222 -j ACCEPT # Liberando Porta 22 (SSH) iptables -A FORWARD -s $Rede -p tcp --dport 22 -j ACCEPT # Liberando Porta 110 (pop-3) iptables -A FORWARD -s $Rede -p tcp --dport 110 -j ACCEPT # Liberando Porta 995 (spop-3) iptables -A FORWARD -s $Rede -p tcp --dport 995 -j ACCEPT # Liberando Porta 25 (smtp) iptables -A FORWARD -s $Rede -p tcp --dport 25 -j ACCEPT # Liberando Porta 465 (smtp-s) iptables -A FORWARD -s $Rede -p tcp --dport 465 -j ACCEPT # Liberando Porta 2121 (ftp) iptables -A FORWARD -s $Rede -p tcp --dport 2121 -j ACCEPT # Liberando Porta 21 (ftp) iptables -A FORWARD -s $Rede -p udp --dport 21 -j ACCEPT iptables -A FORWARD -s $Rede -p udp --dport 20 -j ACCEPT # Liberando porta 53 (DNS) iptables -A FORWARD -s $Rede -p tcp --dport 53 -j ACCEPT iptables -A FORWARD -s $Rede -p udp --dport 53 -j ACCEPT # Regras forward para o funcionamento de redirecionamento de portas (NAT) # Redirecionando porta 5900 (VNC) #iptables -A FORWARD -p tcp --dport 5900 -j ACCEPT #ptables -A FORWARD -p tcp --dport 5800 -j ACCEPT ############################### ######### TABELA NAT ## ####### ############################### # Redireconamento de portas # VNC Para algum micro (192.168.1.31 = nome da pessoa) #iptables -t nat -A PREROUTING -d $LanExt -p tcp --dport 5900 -j DNAT --to 192.168.0.77:5900 # Mascaramento de rede para acesso externo # iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE #Bloqueia todo o resto #iptables -A INPUT -p tcp -j LOG --log-level 6 --log-prefix "FIREWALL: GERAL " iptables -A INPUT -p tcp --syn -j DROP iptables -A INPUT -p tcp -j DROP iptables -A INPUT -p udp -j DROP } ################## ### Função STOP ## ################## firewall_stop() { echo "Parando firewall e funcionando apenas com mascaramento" # Limpa as regras # iptables -X iptables -Z iptables -F INPUT iptables -F OUTPUT iptables -F FORWARD iptables -F -t nat iptables -F -t mangle # Politicas padrao # iptables -t filter -P INPUT ACCEPT iptables -t filter -P OUTPUT ACCEPT iptables -t filter -P FORWARD ACCEPT iptables -t nat -P PREROUTING ACCEPT iptables -t nat -P OUTPUT ACCEPT iptables -t nat -P POSTROUTING ACCEPT iptables -t mangle -P PREROUTING ACCEPT iptables -t mangle -P OUTPUT ACCEPT # Manter conexoes jah estabelecidas para nao parar 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 # Aceita todo o trafego vindo do loopback e indo pro loopback iptables -t filter -A INPUT -i lo -j ACCEPT ############################### # TABELA Forward # ############################### ### MSN ### # Libera msn para o IP # # nome #iptables -A FORWARD -s 192.168.0.34 -p tcp --dport 1863 -j ACCEPT # nome #iptables -A FORWARD -s 192.168.0.5 -p tcp --dport 1863 -j ACCEPT # Bloqueio de MSN # #iptables -A FORWARD -s 192.168.1.0 -p tcp --dport 1863 -j DROP #iptables -A FORWARD -s 192.168.1.0 -d loginnet.passport.com -j DROP #iptables -A FORWARD -s 198.164.1.0/24 -p tcp --dport 1863 -j DROP #iptables -A FORWARD -s 198.164.1.0/24 -d loginnet.passport.com -j DROP #iptables -A FORWARD -s 198.164.1.0/24 -d messenger.hotmail.com -j DROP #iptables -A FORWARD -s 198.164.1.0/24 -d webmessenger.msn.com -j DROP #iptables -A FORWARD -p tcp --dport 1080 -j DROP #iptables -A FORWARD -s 198.164.1.0/24 -p tcp --dport 1080 -j DROP #iptables -A FORWARD -p tcp --dport 1863 -j DROP #iptables -A FORWARD -d 64.4.13.0/24 -j DROP ############################### ######### TABELA NAT ## ####### ############################### # Mascaramento de rede para acesso externo # iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE echo "Regras Limpas e Firewall desabilitado" } firewall_restart() { echo "Reiniciando Firewall" firewall_stop sleep 3 firewall_start echo "Firewall Reiniciado" } case "$1" in 'start') firewall_start echo "Firewall Iniciado" ;; 'stop') firewall_stop ;; 'restart') firewall_restart ;; *) echo "Opções possíveis:" echo "rc.firewall start" echo "rc.firewall stop" echo "rc.firewall restart" esac
Compartilhando a tela do Computador no Celular via Deskreen
Como Configurar um Túnel SSH Reverso para Acessar Sua Máquina Local a Partir de uma Máquina Remota
Configuração para desligamento automatizado de Computadores em um Ambiente Comercial
Como renomear arquivos de letras maiúsculas para minúsculas
Imprimindo no formato livreto no Linux
Vim - incrementando números em substituição
Efeito "livro" em arquivos PDF
Como resolver o erro no CUPS: Unable to get list of printer drivers
Não to conseguindo resolver este problemas ao instalar o playonelinux (1)
Excluir banco de dados no xampp (1)
[Python] Automação de scan de vulnerabilidades
[Python] Script para analise de superficie de ataque
[Shell Script] Novo script para redimensionar, rotacionar, converter e espelhar arquivos de imagem
[Shell Script] Iniciador de DOOM (DSDA-DOOM, Doom Retro ou Woof!)
[Shell Script] Script para adicionar bordas às imagens de uma pasta