No centos 7 quero bloquear o tráfego de um IP de origem (exemplo 3.3.3.3) e para isso aplico a seguinte regra:
# firewall-cmd --permanent --zone=drop --add-source=3.3.3.0/24
# firewall-cmd --reload
Após esse comando, verifiquei se minha regra se aplica por:
firewall-cmd --list-all --zone=drop
drop (active)
target: DROP
icmp-block-inversion: no
interfaces:
sources: 3.3.3.0/24
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
Aqui você pode ver o resultado de todas as outras regras ativas:
firewall-cmd --list-all --zone=public
public (active)
target: default
icmp-block-inversion: no
interfaces: eno4
sources:
services: dhcpv6-client ssh
ports: 5060/udp 16384-32768/udp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
firewall-cmd --list-all --zone=internal
internal (active)
target: default
icmp-block-inversion: no
interfaces: eno1
sources:
services: mdns dhcpv6-client samba-client ssh
ports: 80/tcp 60024/tcp 5080/udp 8080/tcp 5060/tcp 161/udp 5080/tcp 5060/udp 16384-32768/udp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
Na tabela de rotas a zona pública está listada naquela que roteia o tráfego para 3.3.3.0/24.
ip route
default via 1.1.1.1 dev eno4
10.1.1.0/24 via 10.1.1.1 dev eno1
Então essa regra está se aplicando, mas ainda recebo tráfego (UDP) deste IP de origem em uma porta de escuta (5060) na minha máquina centos. O que há de errado com minha regra?
ATUALIZAR como Michael mencionou, há uma saída de iptables -nvL
iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
46M 33G ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
53342 3122K ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
315K 35M INPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0
315K 35M INPUT_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
315K 35M INPUT_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
85 3916 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID
1908 87293 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
0 0 FORWARD_direct all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FORWARD_IN_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FORWARD_IN_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FORWARD_OUT_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FORWARD_OUT_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 46M packets, 21G bytes)
pkts bytes target prot opt in out source destination
46M 21G OUTPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0
Chain FORWARD_IN_ZONES (1 references)
pkts bytes target prot opt in out source destination
0 0 FWDI_public all -- eno4 * 0.0.0.0/0 0.0.0.0/0 [goto]
0 0 FWDI_internal all -- eno1 * 0.0.0.0/0 0.0.0.0/0 [goto]
0 0 FWDI_public all -- + * 0.0.0.0/0 0.0.0.0/0 [goto]
Chain FORWARD_IN_ZONES_SOURCE (1 references)
pkts bytes target prot opt in out source destination
0 0 FWDI_drop all -- * * 3.3.3.0/24 0.0.0.0/0
0 0 FWDI_drop all -- * * 3.3.3.0/24 0.0.0.0/0
Chain FORWARD_OUT_ZONES (1 references)
pkts bytes target prot opt in out source destination
0 0 FWDO_public all -- * eno4 0.0.0.0/0 0.0.0.0/0 [goto]
0 0 FWDO_internal all -- * eno1 0.0.0.0/0 0.0.0.0/0 [goto]
0 0 FWDO_public all -- * + 0.0.0.0/0 0.0.0.0/0 [goto]
Chain FORWARD_OUT_ZONES_SOURCE (1 references)
pkts bytes target prot opt in out source destination
0 0 FWDO_drop all -- * * 0.0.0.0/0 3.3.3.0/24
0 0 FWDO_drop all -- * * 0.0.0.0/0 3.3.3.0/24
Chain FORWARD_direct (1 references)
pkts bytes target prot opt in out source destination
Chain FWDI_drop (2 references)
pkts bytes target prot opt in out source destination
0 0 FWDI_drop_log all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FWDI_drop_deny all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FWDI_drop_allow all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0
Chain FWDI_drop_allow (1 references)
pkts bytes target prot opt in out source destination
Chain FWDI_drop_deny (1 references)
pkts bytes target prot opt in out source destination
Chain FWDI_drop_log (1 references)
pkts bytes target prot opt in out source destination
Chain FWDI_internal (1 references)
pkts bytes target prot opt in out source destination
0 0 FWDI_internal_log all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FWDI_internal_deny all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FWDI_internal_allow all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
Chain FWDI_internal_allow (1 references)
pkts bytes target prot opt in out source destination
Chain FWDI_internal_deny (1 references)
pkts bytes target prot opt in out source destination
Chain FWDI_internal_log (1 references)
pkts bytes target prot opt in out source destination
Chain FWDI_public (2 references)
pkts bytes target prot opt in out source destination
0 0 FWDI_public_log all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FWDI_public_deny all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FWDI_public_allow all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
Chain FWDI_public_allow (1 references)
pkts bytes target prot opt in out source destination
Chain FWDI_public_deny (1 references)
pkts bytes target prot opt in out source destination
Chain FWDI_public_log (1 references)
pkts bytes target prot opt in out source destination
Chain FWDO_drop (2 references)
pkts bytes target prot opt in out source destination
0 0 FWDO_drop_log all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FWDO_drop_deny all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FWDO_drop_allow all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0
Chain FWDO_drop_allow (1 references)
pkts bytes target prot opt in out source destination
Chain FWDO_drop_deny (1 references)
pkts bytes target prot opt in out source destination
Chain FWDO_drop_log (1 references)
pkts bytes target prot opt in out source destination
Chain FWDO_internal (1 references)
pkts bytes target prot opt in out source destination
0 0 FWDO_internal_log all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FWDO_internal_deny all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FWDO_internal_allow all -- * * 0.0.0.0/0 0.0.0.0/0
Chain FWDO_internal_allow (1 references)
pkts bytes target prot opt in out source destination
Chain FWDO_internal_deny (1 references)
pkts bytes target prot opt in out source destination
Chain FWDO_internal_log (1 references)
pkts bytes target prot opt in out source destination
Chain FWDO_public (2 references)
pkts bytes target prot opt in out source destination
0 0 FWDO_public_log all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FWDO_public_deny all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 FWDO_public_allow all -- * * 0.0.0.0/0 0.0.0.0/0
Chain FWDO_public_allow (1 references)
pkts bytes target prot opt in out source destination
Chain FWDO_public_deny (1 references)
pkts bytes target prot opt in out source destination
Chain FWDO_public_log (1 references)
pkts bytes target prot opt in out source destination
Chain INPUT_ZONES (1 references)
pkts bytes target prot opt in out source destination
272K 32M IN_public all -- eno4 * 0.0.0.0/0 0.0.0.0/0 [goto]
42964 2736K IN_internal all -- eno1 * 0.0.0.0/0 0.0.0.0/0 [goto]
0 0 IN_public all -- + * 0.0.0.0/0 0.0.0.0/0 [goto]
Chain INPUT_ZONES_SOURCE (1 references)
pkts bytes target prot opt in out source destination
0 0 IN_drop all -- * * 3.3.3.0/24 0.0.0.0/0
0 0 IN_drop all -- * * 3.3.3.0/24 0.0.0.0/0
Chain INPUT_direct (1 references)
pkts bytes target prot opt in out source destination
Chain IN_drop (2 references)
pkts bytes target prot opt in out source destination
0 0 IN_drop_log all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 IN_drop_deny all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 IN_drop_allow all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0
Chain IN_drop_allow (1 references)
pkts bytes target prot opt in out source destination
Chain IN_drop_deny (1 references)
pkts bytes target prot opt in out source destination
Chain IN_drop_log (1 references)
pkts bytes target prot opt in out source destination
Chain IN_internal (1 references)
pkts bytes target prot opt in out source destination
42964 2736K IN_internal_log all -- * * 0.0.0.0/0 0.0.0.0/0
42964 2736K IN_internal_deny all -- * * 0.0.0.0/0 0.0.0.0/0
42964 2736K IN_internal_allow all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
Chain IN_internal_allow (1 references)
pkts bytes target prot opt in out source destination
0 0 ACCEPT udp -- * * 0.0.0.0/0 224.0.0.251 udp dpt:5353 ctstate NEW
201 15678 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:137 ctstate NEW
627 144K ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:138 ctstate NEW
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 ctstate NEW
969 50388 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 ctstate NEW
39727 2400K ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:60024 ctstate NEW
7 4458 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:5080 ctstate NEW
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080 ctstate NEW
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:5060 ctstate NEW
1252 90788 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:161 ctstate NEW
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:5080 ctstate NEW
0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:5060 ctstate NEW
140 28000 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpts:16384:32768 ctstate NEW
Chain IN_internal_deny (1 references)
pkts bytes target prot opt in out source destination
Chain IN_internal_log (1 references)
pkts bytes target prot opt in out source destination
Chain IN_public (2 references)
pkts bytes target prot opt in out source destination
272K 32M IN_public_log all -- * * 0.0.0.0/0 0.0.0.0/0
272K 32M IN_public_deny all -- * * 0.0.0.0/0 0.0.0.0/0
272K 32M IN_public_allow all -- * * 0.0.0.0/0 0.0.0.0/0
5 196 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
Chain IN_public_allow (1 references)
pkts bytes target prot opt in out source destination
1184 70012 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 ctstate NEW
388 271K ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:5060 ctstate NEW
268K 31M ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpts:16384:32768 ctstate NEW
Chain IN_public_deny (1 references)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 3.3.3.0/24 0.0.0.0/0 reject-with icmp-port-unreachable
0 0 REJECT all -- * * 3.3.3.0/24 0.0.0.0/0 reject-with icmp-port-unreachable
Chain IN_public_log (1 references)
pkts bytes target prot opt in out source destination
Chain OUTPUT_direct (1 references)
pkts bytes target prot opt in out source destination
ATUALIZAR No diagrama ladder a seguir (sngrep) você pode ver que a opção Ping (5060) é respondida pela minha aplicação, portanto a regra do firewall não afetou.
ATUALIZAÇÃO 2 O erro ocorre novamente e agora recarrego a regra de filtro e o IP não se aplica à lista suspensa. Eu descobri que o problema ocorre se a interface tiver carga de tráfego. Após desligar o servidor e reiniciar a regra é aplicada. Mas preciso aplicar uma regra de queda também se o servidor estiver carregado.
Responder1
Eu tive os mesmos problemas/semelhantes. Após horas de solução de problemas, tenho as seguintes observações.
A ordem da cadeia de regras do iptables (para zona 'pública') é:
IN_public_log
IN_public_deny
IN_public_allow
O que significa que as regras de 'negação' são processadas antes das regras de 'permissão' - portanto, isso é significativo para entender a ordem em que as regras são correspondidas. Não sei se esta ordem pode ser alterada.
Me deparei com o mesmo problema, pois a emissão de um firewalld-cmd --reload
não parecia afetar o fato de os pacotes SIP serem descartados ou aceitos, mas uma reinicialização resolveu o problema.
No entanto, encontrei o firewalld-cmd --complete-reload
comando e parece estar funcionando melhor - embora eu ache que isso eliminará todas as sessões existentes. Mas pelo menos posso alterar as regras do firewalld e não precisar reinicializar para que sejam aplicadas total/corretamente.
Também notei que o sngrep ainda parece ser capaz de capturar e exibir a mensagem SIP mesmo que esteja bloqueada, mas a mostra com uma contagem de mensagens de 1 e não há mensagem de resposta (porque na verdade foi bloqueada).
ATUALIZAR:Eu entendo que sngrep (desde 0.1.0) usa libpcap - vejahttps://github.com/irontec/sngrep/wiki. De acordo com esta postagem, o libpcap processa os pacotes (de entrada) antes de serem processados pelo 'firewall'. Presumo que 'firewall' neste caso também possa significar firewalld. VerO tcpdump verá pacotes que estão sendo descartados pelo iptables?
Nota: CentOS 7 parece vir com firewalld versão 0.4.4.4. O mais recente é 0.6.0, mas ainda não sei como atualizá-lo. Espero que as versões mais recentes possam resolver/corrigir os problemas acima.
Responder2
Ao tentar fazer a regra funcionar, crio a regra de bloqueio de duas maneiras diferentes. A primeira forma foi colocar o IP na zona de lançamento com:
firewall-cmd --permanent --zone=drop --add-source=3.3.3.0/24
e esqueça o comando reload, então a regra não se aplica, em seguida crio esta entrada no firewall.
firewall-cmd --permanent --zone=public --add-rich-rule='rule family=ipv4 source address=3.3.3.0/24 reject'
E não consigo fazer a regra funcionar com ambas as entradas no firewall. Depois de limpar o firewall de todas as entradas relacionadas à rede 3.3.3.0/24 e recriar a regra com a zona de lançamento, funciona para mim.
Responder3
Você está perdendo alguns fundamentos. Existem interfaces e as interfaces pertencem a uma zona (pública por padrão). As regras para essa zona são implementadas em todas as interfaces que pertencem a ela. Você tem duas interfaceseno1(na zonainterno) een4(na zonapúblico). Como sugeriu Nasir Riley, você deve implementar a regra na zona à qual pertence a interface desejada. Você está tentando algo a ver com a zona "derrubar" mas não há interface nessa zona. Da documentação da Red Hat para oderrubarzona:
"Todos os pacotes de rede recebidos são descartados; não há resposta. Somente conexões de rede de saída são possíveis."
Portanto, você pode colocar a interface desejada nesta zona, mas primeiro remova a regra que você adicionou. E vá completamenteGuia de segurança RHEL 7, há muitas informações sobre firewalld .