在 centos 7 中,我想要阻止来自源 IP(例如 3.3.3.3)的流量,为此我应用以下规则:
# firewall-cmd --permanent --zone=drop --add-source=3.3.3.0/24
# firewall-cmd --reload
执行该命令后,我检查了我的规则是否适用:
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:
在这里您可以看到所有其他活动规则的输出:
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:
在路由表中,公共区域列在为 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
因此该规则适用,但我仍然在 centos 机器上的监听端口 (5060) 上收到来自此源 IP 的流量 (UDP)。我的规则有什么问题?
更新 正如迈克尔提到的,有一个 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
更新 在下面的梯形图(sngrep)中,您可以看到选项 Ping(5060)由我的应用程序应答,因此防火墙规则没有影响。
更新2 错误再次出现,现在我重新加载过滤规则,但 IP 不适用于删除列表。我发现如果接口有流量负载,就会出现此问题。关闭服务器并重新启动后,规则将应用。但如果服务器有负载,我也需要一种方法来应用删除规则。
答案1
我遇到了相同/类似的问题。经过数小时的故障排除,我得出以下观察结果。
iptables 规则链(针对“公共”区域)的顺序是:
IN_public_log
IN_public_deny
IN_public_allow
这意味着“拒绝”规则在“允许”规则之前处理 - 因此这对于理解规则匹配的顺序非常重要。我不知道这个顺序是否可以改变。
我遇到了同样的问题,发出firewalld-cmd --reload
似乎不会影响 SIP 数据包是否被丢弃或接受,但重新启动可以解决问题。
但是,我发现这个firewalld-cmd --complete-reload
命令似乎效果更好 - 虽然我认为这会删除任何现有会话。但至少我可以更改防火墙规则,而不必重新启动才能使其完全/正确应用。
我还注意到,即使 SIP 消息被阻止,sngrep 似乎仍然能够捕获和显示 SIP 消息,但它显示的消息计数为 1,并且没有响应消息(因为它实际上已被阻止)。
更新:我知道 sngrep (自 0.1.0 起) 使用 libpcap - 请参阅https://github.com/irontec/sngrep/wiki根据这篇文章,libpcap 在数据包被“防火墙”处理之前会处理(入站)数据包。我假设“防火墙”在这种情况下也可以指防火墙。请参阅tcpdump 能看到被 iptables 丢弃的数据包吗?
注意:CentOS 7 似乎附带了防火墙版本 0.4.4.4。最新版本是 0.6.0,但我还不知道如何升级它。我希望新版本可以解决/修复上述问题。
答案2
在尝试使规则生效时,我以两种不同的方式创建阻止规则。第一种方法是将 IP 放入放置区,方法是:
firewall-cmd --permanent --zone=drop --add-source=3.3.3.0/24
并忘记了重新加载命令,因此规则不适用,接下来我在防火墙中创建此条目。
firewall-cmd --permanent --zone=public --add-rich-rule='rule family=ipv4 source address=3.3.3.0/24 reject'
我无法让规则与防火墙中的两个条目一起工作。清除防火墙中与 3.3.3.0/24 网络相关的所有条目,然后重新创建带有放置区域的规则,它对我来说就可以正常工作了。
答案3
您缺少一些基础知识。有接口,接口属于一个区域(默认情况下为公共区域)。该区域的规则在属于它的所有接口上实施。您有两个接口eno1(在区域内内部的) 和en4(在区域内民众)。正如 Nasir Riley 所建议的,您必须在所需接口所属的区域上实施规则。您正在尝试与区域有关的事情“降低“但该区域中没有接口。来自 Red Hat 文档降低区:
“所有传入的网络数据包都会被丢弃;没有回复。只有传出的网络连接才有可能。”
因此,您可以将所需的接口放入此区域,但首先删除您添加的规则。然后彻底检查RHEL 7 安全指南,有很多关于firewalld 的信息。