更改 IPTABLE CHAIN 的讀取順序(使用 Fail2Ban)

更改 IPTABLE CHAIN 的讀取順序(使用 Fail2Ban)

我有一些使用 iptables 和 failure2ban 的經驗。兩者都按其應有的方式工作,但我想優化當 IP 和連接埠探測發生時資料包「丟棄」的方式。

Fail2Ban 在阻止嘗試存取各種連接埠(即 SSH、MySQL 等)的 IP 方面做得很好。

然而,一旦 IP 在特定連接埠(即 SSH 的連接埠 22)上被阻止,即使 Fail2Ban 已向 iptables 新增了「DROP - all」條件,仍可透過 ICMP 存取主機。

我可能是錯的,但我認為這與 iptables 讀取 Fail2Ban CHAIN 的順序有關。

這就是iptables -L揭示的內容(IP 和 DNS 已被替換):

user@ SERVER > iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
fail2ban-SSH  tcp  --  anywhere             anywhere            tcp dpt:ssh
ACCEPT     all  --  10.10.10.1/25  anywhere
fail2ban-SSH  all  --  anywhere             anywhere
RH-Firewall-1-INPUT  all  --  anywhere             anywhere

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
fail2ban-SSH  all  --  anywhere             anywhere
RH-Firewall-1-INPUT  all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain RH-Firewall-1-INPUT (2 references)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     icmp --  anywhere             anywhere            icmp any
ACCEPT     esp  --  anywhere             anywhere
ACCEPT     ah   --  anywhere             anywhere
ACCEPT     udp  --  anywhere             224.0.0.251         udp dpt:mdns
ACCEPT     udp  --  anywhere             anywhere            udp dpt:ipp
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ipp
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ftp
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:http
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

Chain fail2ban-SSH (3 references)
target     prot opt source               destination
DROP       all  --  badip.baddomain.org  anywhere
DROP       all  --  299.299.299.11         anywhere
DROP       all  --  prober.hackers.com  anywhere
RETURN     all  --  anywhere             anywhere

另外,這是我的iptables文件作為參考

# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:fail2ban-SSH - [0:0]
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
#
#
#
-A INPUT -j fail2ban-SSH
-A FORWARD -j fail2ban-SSH
-A INPUT -j RH-Firewall-1-INPUT 
-A FORWARD -j RH-Firewall-1-INPUT 
-A RH-Firewall-1-INPUT -i lo -j ACCEPT 
-A RH-Firewall-1-INPUT -i eth0 -j ACCEPT 
-A RH-Firewall-1-INPUT -p icmp -m icmp --icmp-type any -j ACCEPT 
-A RH-Firewall-1-INPUT -p esp -j ACCEPT 
-A RH-Firewall-1-INPUT -p ah -j ACCEPT 
-A RH-Firewall-1-INPUT -d 224.0.0.251 -p udp -m udp --dport 5353 -j ACCEPT 
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT 
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT 
-A RH-Firewall-1-INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT 
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT 
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT 
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited 
COMMIT

如您所見,有一行允許 ICMP:

ACCEPT     icmp --  anywhere             anywhere            icmp any

這是故意這樣做的,因為我需要合法用戶能夠 ping 某些伺服器。

您可以在我的 iptables 檔案中看到,我在其他規則之前添加了“fail2ban-SSH”鏈,希望它能在所有其他規則之前被讀取,但這不起作用。

我的目標是刪除來自 Fail2Ban 出於任何原因阻止的 IP 的任何請求,包括 ICMP 請求。

有沒有辦法設定 iptables 在所有其他鍊和規則之前讀取 Fail2Ban 規則,以便我可以真正阻止所有連接埠和協定上的 IP?

答案1

如果我正確理解你的問題,你的 SSH 監獄中的 IP 應該被阻止訪問系統上的所有端口,並且不應該能夠 ping 通你。所有其他 IP 應該能夠 ping 通。

要禁止某個 IP 來自所有端口,您需要設定 SSH 監獄以使用 iptables-allports 操作配置。可以在/etc/fail2ban/action.d/iptables-blocktype.conf中配置是否使用DROP、REJECT等

[sshd]
enabled  = true
action   = iptables-allports[name=sshd]
filter   = sshd
logpath  = /var/log/auth.log
maxretry = 5
findtime = 300
bantime  = 7200

如果您還想阻止此特定 IP 對您執行 ping 操作,同時允許不在 SSH 監獄中的其他人對您執行 ping 操作,則需要向 SSH 監獄添加另一個操作。

  • 複製 iptables-allports.conf 和 iptables-blocktype.conf。
  • 為檔案指定新名稱,例如:iptables-blockping.conf 和 iptables-blocktype-ping.conf。
  • 開啟 iptables-blockping.conf 並更新 [INCLUDES] 部分以指向 iptables-blocktype-ping.conf。
  • 開啟 iptables-blocktype-ping.conf 並將區塊類型變更為REJECT --reject-with icmp-host-prohibited

Jails 可以有多個操作,因此直接在 iptables-allports[name=sshd] 下方列出新操作設定檔 iptables-blockping.conf 的名稱。

這應該適合您的目的 - SSH 監獄中的 IP 將在 iptables 中具有特定條目來拒絕 ping 請求。這些規則將在您允許 ping 的規則之後讀取。

相關內容