Iptables (firewall.sh)

Script IPtables com dmz e loadbalance com 2 links

Categoria: Segurança

Software: Iptables

[ Hits: 17.748 ]

Por: Gabriel


Pessoal, depois de muito tempo de pesquisa e testes cheguei ao objeto final do meu firewall, o script iptables. Fiz dezenas de testes e não apresentou nenhum erro até onde pude verificar.

Então gostaria de dar a minha contribuição para a comunidade e também de aprimorá-lo se possível.

Grande abraço a todos.


#!/bin/bash
/etc/init.d/bind9 restart
/etc/init.d/squid restart

# carrega modulos
/sbin/modprobe iptable_nat
/sbin/modprobe ip_conntrack
/sbin/modprobe ip_conntrack_ftp
/sbin/modprobe ip_nat_ftp
/sbin/modprobe ipt_LOG
/sbin/modprobe ipt_REJECT
/sbin/modprobe ipt_MASQUERADE

# habilita roteamento no kernel
echo 1 >/proc/sys/net/ipv4/ip_forward

# protecao contra spoofing
echo "1" > /proc/sys/net/ipv4/conf/all/rp_filter

# protecao contra spoofing 2
echo 0 > /proc/sys/net/ipv4/conf/all/accept_source_route
echo 1 > /proc/sys/net/ipv4/tcp_syncookies
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses

# limpa as tabelas existentes
iptables -F
iptables -t mangle -F
iptables -t nat -F
iptables -X

# politica padrao
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP

# conexoes preestabelecidas
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED,NEW -j ACCEPT
iptables -A FORWARD -m state --state ESTABLISHED,RELATED,NEW -j ACCEPT

# libera interface loopback
iptables -A INPUT -i lo -j ACCEPT

# Registro de logs
iptables -A INPUT -p tcp --dport 333 --syn -j LOG --log-prefix="[TENTATIVA ACESSO FWLOGWATCH]"
iptables -A INPUT -p tcp --dport 23 --syn -j LOG --log-prefix="[TENTATIVA ACESSO TELNET]"
iptables -A INPUT -p tcp --dport 10000 --syn -j LOG --log-prefix="[TENTATIVA ACESSO WEBMIN]"
iptables -A FORWARD -m multiport -p tcp --dport 5800,5900,6000 -j LOG --log-prefix="[ACESSO VNC]"
iptables -A INPUT -p tcp --dport 22 --syn -j LOG --log-prefix="[TENTATIVA ACESSO SSH]"
iptables -A INPUT -p tcp --dport 2222 --syn -j LOG --log-prefix="[TENTATIVA ACESSO SSH]"
iptables -A INPUT -p tcp --dport 21 --syn -j LOG --log-prefix="[TENTATIVA ACESSO FTP]"

################################################################################ REGRAS DE SEGURANÇA
#
###############################################################################

# Protege contra port scanners
iptables -A INPUT -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 5/s -j ACCEPT

# proteção contra traceroute
iptables -A INPUT -p udp -s 0/0 -i eth3 --dport 33435:33525 -j REJECT

# Protecoes contra pacotes invalidos
iptables -A INPUT -m state --state INVALID -j REJECT

###############################################################################
# REGRAS PARA INPUT
#
###############################################################################
# liberando Servidor DNS
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT

# libera proxy squid pelo navegador
iptables -A INPUT -s 192.168.0.0/24 -p tcp --dport 3128 -j ACCEPT
iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 3128 -j ACCEPT
iptables -A INPUT -s 192.168.2.0/24 -p tcp --dport 3128 -j ACCEPT
iptables -A INPUT -s 192.168.3.0/24 -p tcp --dport 3128 -j ACCEPT
iptables -A INPUT -s 192.168.4.0/24 -p tcp --dport 3128 -j ACCEPT

# libera ping para rede do piso 1 
iptables -A INPUT -s 192.168.1.0/24 -p icmp --icmp-type 8 -j ACCEPT

# libera ssh para piso 1 e bloqueia todo o resto
#iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT

# libera ssh externamente
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# libera acesso ao WEBMIN para o piso 1
iptables -A INPUT -p tcp --dport 10000 -j ACCEPT

# libera acesso ao fwlogwatch para o piso 1
iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 333 -j ACCEPT

# bloqueia todo o resto
iptables -A INPUT -p tcp --syn -j DROP
iptables -A INPUT -p tcp -j DROP

################################################################################ REGRAS VLANS e DMZ (FORWARD)
#
###############################################################################

# libera portas ctvoicer
iptables -A FORWARD -d 192.168.0.8/24 -p tcp -m multiport --dport 3050,10014,10010 -j ACCEPT

#liberado ping para rede piso 1 (qualquer destino)
iptables -A FORWARD -s 192.168.1.0/24 -p icmp --icmp-type echo-request -j ACCEPT
iptables -A FORWARD -s 192.168.1.0/24 -p icmp --icmp-type echo-reply -j ACCEPT

# libera portas rede piso 1
iptables -A FORWARD -i eth0.10 -p tcp -m multiport --dport 53,137,138,139,110,25,22,2222,995,465,5800,5900,6000 -j ACCEPT
iptables -A FORWARD -i eth0.10 -p udp -m multiport --dport 53,137,138,139,110,25,22,995,465 -j ACCEPT

# regras para o webserver
iptables -A FORWARD -d 192.168.0.253/24 -p tcp -m multiport --dport 80,8080 -j ACCEPT
iptables -A FORWARD -d 192.168.0.253/24 -p udp -m multiport --dport 80,8080 -j ACCEPT
iptables -A FORWARD -s 192.168.1.0/24 -d 192.168.1.0/24 -p tcp -m multiport --dport 137,138,139 -j ACCEPT
iptables -A FORWARD -s 192.168.1.0/24 -d 192.168.1.0/24 -p udp -m multiport --dport 137,138,139 -j ACCEPT

# regras para servidor de email
iptables -A FORWARD -d 192.168.0.254/24 -p tcp -m multiport --dport 995,465,110,25,143 -j ACCEPT
iptables -A FORWARD -d 192.168.0.254/24 -p udp -m multiport --dport 995,465,110,25,143 -j ACCEPT

# regras para o asterisk
iptables -A FORWARD -d 192.168.0.250/24 -p tcp --dport 5060 -j ACCEPT
iptables -A FORWARD -d 192.168.0.250/24 -p udp --dport 5060 -j ACCEPT
iptables -A FORWARD -d 192.168.0.250/24 -p udp --dport 10000:20000 -j ACCEPT

# regras para servidor samba
iptables -A FORWARD -i eth0 -d 192.168.0.127/24 -p tcp -m multiport --dport 137,138,139 -j ACCEPT
iptables -A FORWARD -i eth0 -d 192.168.0.127/24 -p udp -m multiport --dport 137,138,139 -j ACCEPT

# regras para serv-adm2
iptables -A FORWARD -d 192.168.0.252/24 -p tcp --dport 3389 -j ACCEPT
iptables -A FORWARD -d 192.168.0.252/24 -p udp --dport 3389 -j ACCEPT

# regras de forward para vnc piso 1
iptables -A FORWARD -s 192.168.1.0/24 -p tcp -m multiport --dport 5800,5900,6000 -j ACCEPT

###############################################################################
# REGRAS PARA NAT
#
###############################################################################

# redirecionando acesso ao servidor VOIP
iptables -t nat -A PREROUTING -d 200.195.YYY.YYY -j DNAT --to 192.168.0.250

# redirecionado pop e smtp
iptables -t nat -A PREROUTING -d 200.195.ZZZ.ZZZ -p tcp -m tcp --dport 110 -j DNAT --to-destination 192.168.0.254:110
iptables -t nat -A PREROUTING -d 200.195.ZZZ.ZZZ -p tcp -m tcp --dport 25 -j DNAT --to-destination 192.168.0.254:25

# redirecionando acesso ao servidor web via rede local e internet
iptables -t nat -A PREROUTING -s 200.195.KKK.KKK -p tcp --dport 80 -j DNAT --to 192.168.0.253

# redireciona acesso terminal service para serv-adm
iptables -t nat -A PREROUTING -d 200.195.ZZZ.ZZZ -p tcp --dport 3389 -j DNAT --to 192.168.0.252

# redireciona acesso vnc
iptables -t nat -A PREROUTING -d 200.139.XXX.XXX -p tcp --dport 5900 -j DNAT --to 192.168.0.8

# acesso vnc gabriel
iptables -t nat -A PREROUTING -d 200.139.XXX.XXX -p tcp --dport 6000 -j DNAT --to 192.168.1.2

# acesso vnc gerson
iptables -t nat -A PREROUTING -d 200.139.XXX.XXX -p tcp --dport 6001 -j DNAT --to 192.168.1.3

# ativando proxy transparente
#iptables -t nat -A PREROUTING -p tcp -s 192.168.0.0/24 --dport 80 -j REDIRECT --to-ports 3128

# ativando masquerade
iptables -t nat -A POSTROUTING -p all -s 192.168.1.2 -o eth3 -j SNAT --to-source 200.139.XXX.XXX

# ativando SNAT
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -m multiport -p tcp --dport 53,110,25,22,2222,995,465,5800,5900,6000 -o eth3 -j SNAT --to-source 200.139.XXX.XXX
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -m multiport -p udp --dport 53,110,25,22,995,465 -j SNAT --to-source 200.139.XXX.XXX
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -p icmp -o eth3 -j SNAT --to-source 200.139.XXX.XXX

###############################################################################

# priorizando pacotes da rede
iptables -t mangle -A PREROUTING -p tcp --dport 5060 -j TOS --set-tos 16
iptables -t mangle -A PREROUTING -p udp --dport 1000:20000 -j TOS --set-tos 8
iptables -t mangle -A PREROUTING -p tcp --dport 80 -j TOS --set-tos 8
iptables -t mangle -A PREROUTING -p udp --dport 80 -j TOS --set-tos 8
iptables -t mangle -A OUTPUT -o eth3 -p tcp --dport 5060 -j TOS --set-tos 16
iptables -t mangle -A OUTPUT -o eth3 -p udp --dport 10000:20000 -j TOS --set-tos 8
iptables -t mangle -A OUTPUT -o eth3 -p tcp --dport 80 -j TOS --set-tos 8
iptables -t mangle -A OUTPUT -o eth3 -p udp --dport 80 -j TOS --set-tos 8

# balanceamento dos links gvt e copel realizado por serviços
#link1 copel 8mb
#link2 gvt 2mb

#echo 10 link1 >>/etc/iproute2/rt_tables
#echo 20 link2 >>/etc/iproute2/rt_tables

#iptables -t mangle -A PREROUTING -p tcp --dport 443 -j MARK --set-mark 3
#iptables -t mangle -A PREROUTING -p tcp --dport 80 -j MARK --set-mark 3
#iptables -t mangle -A PREROUTING -p tcp --dport 5060 -j MARK --set-mark 3
#iptables -t mangle -A PREROUTING -p tcp --dport 21 -j MARK --set-mark 4
#iptables -t mangle -A PREROUTING -p tcp --dport 25 -j MARK --set-mark 4
#iptables -t mangle -A PREROUTING -p tcp --dport 110 -j MARK --set-mark 4
#iptables -t mangle -A PREROUTING -p tcp --dport 5800:6000 -j MARK --set-mark 4
#iptables -t mangle -A PREROUTING -p tcp --dport 3306 -j MARK --set-mark 4
#ip rule add fwmark 3 table link1
#ip rule add fwmark 4 table link2
#ip route add default via 200.195.XXX.XXX table link1
#ip route add default via 200.139.XXX.XXX table link2
  


Comentários
[1] Comentário enviado por cpaynes em 21/05/2010 - 18:37h

dae veioo .. bom o script ... parabens pela contribuicao ...

abracos ..

[2] Comentário enviado por luizvieira em 21/05/2010 - 23:02h

excelente!
[ ]'s

[3] Comentário enviado por pedroavelar em 23/05/2013 - 16:15h

quero editar o firewall aqui em minha casa, mas quem local ?
Procurando na internet achei esse caminho
/etc/rc.d/rc.firewall
esse caminho está correto??

[4] Comentário enviado por gabrielramos em 24/05/2013 - 00:01h

Olá amigão. Nossa!!! tinha até me esquecido desse script ja... hehehe
Seguinte, o local depende da distribuição. Por exemplo no CentOS fica /etc/sysconfig/iptables e ja tem o script de inicialização automatico no /etc/init.d/iptables
Ja no caso do Debian não há um local padrão, pelo menos até a versão 5.X voce pode criar na mão, colocar em qualquer lugar, por ex. no /root/ e lançar no o caminha no /etc/rc.local que ja resolve.

[5] Comentário enviado por bignatanael em 26/03/2020 - 12:11h

Bom Dia Família no meu caso gostaria ajuda poder implementar este script no meu computador pessoal o cero e usar o ip do servidor dhcp gateway sou novato nesta área tenho noções básica somente estou com um problema em minha interface no ifconfig

root@natanaelba-LNV13x5:/home/natanael-b-a# ifconfig
as0t0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST> mtu 1500
inet ip netmask 255.255.254.0 destination ip
inet6 fe80::2ab:7105:dc5d:5897 prefixlen 64 scopeid 0x20<link>
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 200 (Não Especificado)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 337 bytes 50988 (50.9 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

as0t1: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST> mtu 1500
inet ip netmask 255.255.254.0 destination ip
inet6 fe80::aff6:1ed6:73dc:a755 prefixlen 64 scopeid 0x20<link>
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 200 (Não Especificado)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 337 bytes 50988 (50.9 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

as0t2: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST> mtu 1500
inet ip netmask 255.255.254.0 destination ip
inet6 fe80::23f1:8759:d443:7263 prefixlen 64 scopeid 0x20<link>
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 200 (Não Especificado)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 337 bytes 50988 (50.9 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

as0t3: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST> mtu 1500
inet ip netmask 255.255.254.0 destination ip
inet6 fe80::86ed:6bfc:96b1:d161 prefixlen 64 scopeid 0x20<link>
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 200 (Não Especificado)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 337 bytes 50988 (50.9 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

as0t4: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST> mtu 1500
inet ip netmask 255.255.254.0 destination ip
inet6 fe80::c464:2870:ad85:92bb prefixlen 64 scopeid 0x20<link>
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 200 (Não Especificado)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 337 bytes 50988 (50.9 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

as0t5: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST> mtu 1500
inet ip netmask 255.255.254.0 destination ip
inet6 fe80::618:fffd:3d9a:82fd prefixlen 64 scopeid 0x20<link>
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 200 (Não Especificado)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 337 bytes 50988 (50.9 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

as0t6: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST> mtu 1500
inet ip netmask 255.255.254.0 destination ip
inet6 fe80::b0f8:6ed2:61b0:2f34 prefixlen 64 scopeid 0x20<link>
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 200 (Não Especificado)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 337 bytes 50988 (50.9 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

as0t7: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST> mtu 1500
inet ip netmask 255.255.254.0 destination ip
inet6 fe80::4143:6058:b255:efe8 prefixlen 64 scopeid 0x20<link>
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 200 (Não Especificado)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 337 bytes 50988 (50.9 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet ip netmask 255.255.0.0 broadcast ip
ether 02:42:a5:32:3f:b1 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

enp2s0f2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet ip netmask 255.255.255.0 broadcast ip
inet6 fe80::adb0:74ed:6466:a50a prefixlen 64 scopeid 0x20<link>
ether 2e:f6:be:93:84:01 txqueuelen 1000 (Ethernet)
RX packets 194640 bytes 244787916 (244.7 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 109144 bytes 10971478 (10.9 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Loopback Local)
RX packets 3555 bytes 237789 (237.7 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3555 bytes 237789 (237.7 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Somente tem um computador por cabo rede nesta internet e tem um celular com wi fi conectado ja andei fechando portas no meu computador mas nocelular tem portas abertas ip gostaria ajudar pra resolver estou achando que estão tentando invadir minha mauina esta portas não era pra estar aberta e tem um perfil fake face tentando pescar meus dados


Contribuir com comentário

  



Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts