Buckminster
(usa Debian)
Enviado em 07/11/2013 - 17:35h
Bom que resolveu. Mas vou te enviar igual.
Testa assim como abaixo, com calma, mas antes dê uma lida em todo o script, tem observações:
#!/bin/sh
#
#Variaveis
REDE_DIGITAL="192.168.2.0/24"
IP_NET="185.52.36.73"
IF_NET="eth0"
LOG=4
IF_DIGITAL="eth1"
IF_CAMERAS="eth2"
#FIREWALL
#Desliga IP FORWARD
echo 0 > /proc/sys/net/ipv4/ip_forward
##Apagando e restaurando as chains e tabelas
############################################
iptables -Z # Zera as regras das chains
iptables -F # Remove as regras das chains
iptables -X # Apaga as chains
#
##Proteção contra ping, SYN Cookie, IP Spoofing e proteções do kernel
#####################################################################
echo 1 > /proc/sys/net/ipv4/tcp_syncookies #Syn Flood-DoS
echo 1 > /proc/sys/net/ipv4/conf/all/rp_filter #Ip Spoofing
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts #Sem ping e port scanners
echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses #Sem resposta remota
for i in /proc/sys/net/ipv4/conf/*; do
echo 0 > $i/accept_redirects #Sem redirecionar rotas
echo 0 > $i/accept_source_route #Sem traceroute
echo 1 > $i/log_martians #Loga pacotes suspeitos no kernel
echo 1 > $i/rp_filter #Ip Spoofing
echo 0 > $i/secure_redirects; done #Sem redirecionamento de pacotes
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all #Sem ping e tracert
#
#Modulos
modprobe iptable_nat
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe xt_recent
#
##Definindo políticas padrões
#############################
iptables -P INPUT DROP # iptables a política padrão da chain INPUT é proibir tudo
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
#
##Liberando a Loopback
######################
iptables -A INPUT -i lo -j ACCEPT # adiciona regra na chain INPUT para liberar a loopback
#
##Regras de segurança na internet e de aceitação de pacotes
###########################################################
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m state --state NEW ! -i $IF_NET -j DROP # << Interface de entrada da internet
iptables -A FORWARD -m state --state NEW ! -i $IF_NET -j DROP
iptables -A INPUT -m state --state INVALID -j DROP
iptables -A FORWARD -m state --state INVALID -j DROP
#Libera Maquinas permitidas sem LOG
iptables -A FORWARD -s 192.168.2.254 -j ACCEPT
iptables -A FORWARD -s 192.168.2.253/24 -d 200.228.16.49 -j ACCEPT
# iptables -A FORWARD -s 192.168.2.253/24 -p tcp -d 192.168.2.73 --dport 21 -j RETURN
# iptables -A FORWARD -s 192.168.2.253/24 -p tcp -d 192.168.2.73 --dport 80 -j RETURN
# iptables -A FORWARD -s 192.168.2.253/24 -p tcp -d 192.168.2.73 --dport 443 -j RETURN
iptables -A FORWARD -s 192.168.2.253/24 -p tcp -m tcp --dport 21 -j DROP
iptables -A FORWARD -s 192.168.2.253/24 -p tcp -m tcp --dport 80 -j DROP
iptables -A FORWARD -s 192.168.2.253/24 -p tcp -m tcp --dport 443 -j DROP
iptables -A FORWARD -s 192.168.2.253/24 -p tcp -m tcp --dport 8080 -j DROP
#Evita spoof
# iptables -A INPUT ! -i lo -s 127.0.0.0/8 -j DROP
#Libera a interface local
# iptables -A INPUT -i lo -j ACCEPT
#Bloqueio a placa de rede das Camereas
iptables -A FORWARD -i $IF_CAMERAS ! -d 192.168.2.253 -j DROP
#Libera as interfaces de rede
iptables -A INPUT -i $IF_DIGITAL -j ACCEPT
iptables -A INPUT -i tun0 -j ACCEPT
#Libera foward
iptables -A FORWARD -s 192.168.2.0/24 -j ACCEPT
# Redirecionamento
iptables -t nat -A PREROUTING -i $IF_DIGITAL -p tcp --dport 80 -j REDIRECT --to-port 3128 # << AQUI DEVE IR A PLACA DA REDE INTERNA, NÃO SEI SE IF_DIGITAL É A TUA PLACA DA REDE INTERNA.
iptables -t nat -A PREROUTING -i $IF_DIGITAL-p tcp --dport 443 -j REDIRECT --to-port 3128 # aqui também.
#Faz mascaramento para acesso a net
iptables -t nat -A POSTROUTING -o $IF_NET -j MASQUERADE
# COMENTA TODAS ESSAS REGRAS A PARTIR DAQUI...
#Dropa pacotes com flags invalidas
iptables -A INPUT -i $IF_NET -p tcp --tcp-flags ALL FIN,URG,PSH -m limit --limit 3/m -j LOG --log-level $LOG --log-prefix "Iptables_-_DROP tcp flag: "
iptables -A INPUT -i $IF_NET -p tcp --tcp-flags SYN,RST SYN,RST -m limit --limit 3/m -j LOG --log-level $LOG --log-prefix "Iptables_-_DROP tcp flag: "
iptables -A INPUT -i $IF_NET -p tcp --tcp-flags SYN,FIN SYN,FIN -m limit --limit 3/m -j LOG --log-level $LOG --log-prefix "Iptables_-_DROP tcp flag: "
iptables -A INPUT -i $IF_NET -p tcp --tcp-flags ALL FIN -m limit --limit 3/m -j LOG --log-level $LOG --log-prefix "Iptables_-_DROP tcp flag: "
iptables -A INPUT -i $IF_NET -p tcp --tcp-flags ALL ALL -m limit --limit 3/m -j LOG --log-level $LOG --log-prefix "Iptables_-_DROP tcp flag: "
iptables -A INPUT -i $IF_NET -p tcp --tcp-flags ALL NONE -m limit --limit 3/m -j LOG --log-level $LOG --log-prefix "Iptables_-_DROP tcp flag: "
iptables -A INPUT -i $IF_NET -p tcp --tcp-flags FIN,RST FIN,RST -m limit --limit 3/m -j LOG --log-level $LOG --log-prefix "Iptables_-_DROP tcp flag: "
iptables -A INPUT -i $IF_NET -p tcp --tcp-flags ACK,FIN FIN -m limit --limit 3/m -j LOG --log-level $LOG --log-prefix "Iptables_-_DROP tcp flag: "
iptables -A INPUT -i $IF_NET -p tcp --tcp-flags ACK,PSH PSH -m limit --limit 3/m -j LOG --log-level $LOG --log-prefix "Iptables_-_DROP tcp flag: "
iptables -A INPUT -i $IF_NET -p tcp --tcp-flags ACK,URG URG -m limit --limit 3/m -j LOG --log-level $LOG --log-prefix "Iptables_-_DROP tcp flag: "
iptables -A INPUT -i $IF_NET -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
iptables -A INPUT -i $IF_NET -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
iptables -A INPUT -i $IF_NET -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
iptables -A INPUT -i $IF_NET -p tcp --tcp-flags ACK,FIN FIN -j DROP
iptables -A INPUT -i $IF_NET -p tcp --tcp-flags ACK,PSH PSH -j DROP
iptables -A INPUT -i $IF_NET -p tcp --tcp-flags ACK,URG URG -j DROP
#Dropa pacotes "novos" sem a flag SYN
iptables -A INPUT -i $IF_NET -p tcp ! --syn -m state --state NEW -m limit --limit 1/h -j LOG --log-prefix "Iptables_-_DROP sem SYN: "
iptables -A INPUT -i $IF_NET -p tcp ! --syn -m state --state NEW -j DROP
#Dropa icmp
iptables -A INPUT -i tun0 -p icmp -j ACCEPT
iptables -A INPUT -i $IF_NET -p icmp -m limit --limit 1/h -j LOG --log-level $LOG --log-prefix "Iptables_-_DROP icmp: "
iptables -A INPUT -i $IF_NET -p icmp -j DROP
################################################### ATÉ AQUI.
#Habilita portas
iptables -A INPUT -p tcp --dport 21 -j ACCEPT #FTP - OFFICIAL
iptables -A INPUT -s $REDE_DIGITAL -p tcp --dport 22 -j ACCEPT #SSH_INTERNO
iptables -A INPUT -p udp --dport 53 -j ACCEPT #DNS (query)
iptables -A INPUT -p tcp --dport 53 -j ACCEPT #DNS (transferencia)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT #HTTP
iptables -A INPUT -p tcp --dport 110 -j ACCEPT #POP3
iptables -A INPUT -p tcp --dport 143 -j ACCEPT #IMAP
iptables -A INPUT -p tcp --dport 443 -j ACCEPT #HTTPS
iptables -A INPUT -p udp --dport 1194 -j ACCEPT #Tunel0
iptables -A INPUT -s $REDE_DIGITAL -p tcp --dport 3000 -j ACCEPT #Ntop interno
#iptables -A INPUT -s $REDE_DIGITAL -p tcp --dport 3128 -j ACCEPT #Squid
#SMTP
iptables -A INPUT -p tcp --dport 25 --syn -m limit --limit 1/s --limit-burst 10 -j ACCEPT
iptables -A INPUT -p tcp --dport 25 --syn -j DROP
iptables -A INPUT -p tcp --dport 25 -j ACCEPT
#SMTP - 587
iptables -A INPUT -p tcp --dport 587 --syn -m limit --limit 1/s --limit-burst 10 -j ACCEPT
iptables -A INPUT -p tcp --dport 587 --syn -j DROP
iptables -A INPUT -p tcp --dport 587 -j ACCEPT
#POP3
iptables -A INPUT -p tcp --dport 110 --syn -m limit --limit 1/s --limit-burst 10 -j ACCEPT
iptables -A INPUT -p tcp --dport 110 --syn -j DROP
iptables -A INPUT -p tcp --dport 110 -j ACCEPT
#IMAP
iptables -A INPUT -p tcp --dport 143 --syn -m limit --limit 1/s --limit-burst 10 -j ACCEPT
iptables -A INPUT -p tcp --dport 143 --syn -j DROP
iptables -A INPUT -p tcp --dport 143 -j ACCEPT
#Redireciona portas
# iptables -t nat -A PREROUTING -d $IP_NET -p tcp --dport 21 -j DNAT --to 192.168.2.73 #FTP
# iptables -t nat -A PREROUTING -d $IP_NET -p tcp --dport 22 -j DNAT --to 192.168.2.73 #SSH
# iptables -t nat -A PREROUTING -d $IP_NET -p tcp --dport 25 -j DNAT --to 192.168.2.73 #SMTP
# iptables -t nat -A PREROUTING -d $IP_NET -p tcp --dport 587 -j DNAT --to 192.168.2.73 #SMTP
# iptables -t nat -A PREROUTING -d $IP_NET -p udp --dport 53 -j DNAT --to 192.168.2.73 #DNS (Query)
# iptables -t nat -A PREROUTING -d $IP_NET -p tcp --dport 53 -j DNAT --to 192.168.2.73 #DNS (Transferencia)
# iptables -t nat -A PREROUTING -d $IP_NET -p tcp --dport 80 -j DNAT --to 192.168.2.73 #HTTP
# iptables -t nat -A PREROUTING -d $IP_NET -p tcp --dport 110 -j DNAT --to 192.168.2.73 #POP3
# iptables -t nat -A PREROUTING -d $IP_NET -p tcp --dport 143 -j DNAT --to 192.168.2.73 #IMAP
# iptables -t nat -A PREROUTING -d $IP_NET -p tcp --dport 443 -j DNAT --to 192.168.2.73 #HTTPS
#Evita spamware bloqueando a porta 25
iptables -A FORWARD -d 192.168.2.73 -p tcp --dport 25 -j ACCEPT
iptables -A FORWARD -s 192.168.2.73 -p tcp --dport 25 -j ACCEPT
iptables -A FORWARD -p tcp --dport 25 --syn -m limit --limit 1/m -m state --state NEW -j LOG --log-level $LOG --log-prefix "Iptables_-_Access Spam: "
iptables -A FORWARD -p tcp --dport 25 -j DROP
iptables -A FORWARD -d 192.168.2.73 -p tcp --sport 25 -j ACCEPT
iptables -A FORWARD -s 192.168.2.73 -p tcp --sport 25 -j ACCEPT
iptables -A FORWARD -p tcp --sport 25 --syn -m limit --limit 1/m -m state --state NEW -j LOG --log-level $LOG --log-prefix "Iptables_-_Access Spam: "
#Evita spyware bloqueando a porta 110
iptables -A FORWARD -d 192.168.2.73 -p tcp --dport 110 -j ACCEPT
iptables -A FORWARD -s 192.168.2.73 -p tcp --dport 110 -j ACCEPT
iptables -A FORWARD -p tcp --dport 110 --syn -m limit --limit 1/m -m state --state NEW -j LOG --log-level $LOG --log-prefix "Iptables_-_Access Spy: "
iptables -A FORWARD -p tcp --dport 110 -j DROP
iptables -A FORWARD -d 192.168.2.73 -p tcp --sport 110 -j ACCEPT
iptables -A FORWARD -s 192.168.2.73 -p tcp --sport 110 -j ACCEPT
iptables -A FORWARD -p tcp --sport 110 --syn -m limit --limit 1/m -m state --state NEW -j LOG --log-level $LOG --log-prefix "Iptables_-_Access Spy: "
iptables -A FORWARD -p tcp --sport 110 -j DROP
#Recent Porta 110
iptables -I FORWARD -p tcp --dport 22 -i eth0 -m state --state NEW -m recent --set --name SSH;
iptables -I FORWARD -p tcp --dport 22 -i eth0 -m state --state NEW -m recent --name SSH --update --seconds 60 --hitcount 15 -j DROP;
iptables -I FORWARD -p tcp --dport 25 -i eth0 -m state --state NEW -m recent --set --name SMTP;
iptables -I FORWARD -p tcp --dport 25 -i eth0 -m state --state NEW -m recent --name SMTP --update --seconds 60 --hitcount 15 -j DROP;
iptables -I FORWARD -p tcp --dport 587 -i eth0 -m state --state NEW -m recent --set --name SMTP;
iptables -I FORWARD -p tcp --dport 587 -i eth0 -m state --state NEW -m recent --name SMTP --update --seconds 60 --hitcount 15 -j DROP;
iptables -I FORWARD -p tcp --dport 110 -i eth0 -m state --state NEW -m recent --set --name POP3;
iptables -I FORWARD -p tcp --dport 110 -i eth0 -m state --state NEW -m recent --name POP3 --update --seconds 60 --hitcount 15 -j DROP;
iptables -I FORWARD -p tcp --dport 143 -i eth0 -m state --state NEW -m recent --set --name IMAP;
iptables -I FORWARD -p tcp --dport 143 -i eth0 -m state --state NEW -m recent --name IMAP --update --seconds 60 --hitcount 15 -j DROP;
echo 1 > /proc/sys/net/ipv4/ip_forward
Veja bem: em "Proteção contra ping, SYN Cookie..." você pode comentar o que não quiser.
Nas políticas padrões o ideal é sempre bloquear as chains INPUT e FORWARD e depois ir liberando somente o que você quer.
Teste em pelo menos duas máquinas com o IP do Squid setado nos navegadores.
A política padrão FORWARD deve ser DROP por questões de segurança.
E a liberar a loopback não influencia tanto assim na segurança, pois a loopback é uma placa de rede virtual.
Fala uma coisa, a placa de rede IF_DIGITAL é a placa da rede interna?