為什麼firewalld不應用我的丟棄規則?

為什麼firewalld不應用我的丟棄規則?

在 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

這意味著「拒絕」規則在「允許」規則之前處理 - 因此這對於理解規則的匹配順序非常重要。不知道這個順序能不能改。

我遇到了同樣的問題,發出 afirewalld-cmd --reload似乎並不影響 SIP 封包是否被丟棄或接受,但重新啟動解決了它。

然而,我發現了這個firewalld-cmd --complete-reload命令,它似乎工作得更好 - 儘管我認為這會刪除任何現有的會話。但至少我可以更改防火牆規則,而不必重新啟動即可完全/正確應用它。

我還注意到,sngrep 似乎仍然能夠捕獲並顯示 SIP 訊息,即使它被阻止,但它顯示的訊息計數為 1,並且沒有回應訊息(因為它實際上被阻止)。

更新:我了解 sngrep (自 0.1.0 起)使用 libpcap - 請參閱https://github.com/irontec/sngrep/wiki。根據這篇文章,libpcap 在(入站)資料包被「防火牆」處理之前對其進行處理。我認為在這種情況下“防火牆”也可以指防火牆。看tcpdump 會看到 iptables 丟棄的封包嗎?

注意:CentOS 7 似乎附帶了firewalld 版本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

你缺乏一些基礎知識。有接口,接口屬於一個區域(預設為公共)。該區域的規則在屬於該區域的所有介面上實施。你有兩個介面諾1(在區域內內部的) 和恩4(在區域內民眾)。正如 Nasir Riley 所建議的,您必須在所需介面所屬的區域上實施規則。你正在嘗試與該區域有關的事情”降低「但該區域中沒有介面。來自 Red Hat 文檔降低區:

“任何傳入的網路資料包都會被丟棄;沒有回應。只能進行傳出網路連線。”

因此,您可以將所需的介面放入該區域,但先刪除已新增的規則。並且徹底地去RHEL 7 安全指南,有很多關於firewalld的資訊。

相關內容