
我想建立一個fail2ban操作,在禁止操作時將流量路由到另一個IP,並在取消禁止操作時刪除路由。
檔案:/etc/fail2ban/action.d/ 中的 iptables-route.conf
# Fail2Ban configuration file
#
#
[INCLUDES]
before = iptables-common.conf
[Definition]
# Option: actionstart
# Notes.: command executed once at the start of Fail2Ban.
# Values: CMD
#
actionstart = <iptables> -N f2b-<name>
<iptables> -A f2b-<name> -j <returntype>
<iptables> -I <chain> -p <protocol> -m multiport --dports <port> -j f2b-<name>
<iptables> -A FORWARD -i ens3 -p tcp -m state --state NEW --dport 80 -j ACCEPT
<iptables> -A FORWARD -i ens3 -p tcp -m state --state NEW --dport 443 -j ACCEPT
# Option: actionstop
# Notes.: command executed once at the end of Fail2Ban
# Values: CMD
#
actionstop = <iptables> -D <chain> -p <protocol> -m multiport --dports <port> -j f2b-<name>
<actionflush>
<iptables> -X f2b-<name>
<iptables> -D FORWARD -i ens3 -p tcp -m state --state NEW --dport 80 -j ACCEPT
<iptables> -D FORWARD -i ens3 -p tcp -m state --state NEW --dport 443 -j ACCEPT
# Option: actioncheck
# Notes.: command executed once before each actionban command
# Values: CMD
#
actioncheck = <iptables> -n -L <chain> | grep -q 'f2b-<name>[ \t]'
# Option: actionban
# Notes.: command executed when banning an IP. Take care that the
# command is executed with Fail2Ban user rights.
# Tags: See jail.conf(5) man page
# Values: CMD
#
actionban = <iptables> -I f2b-<name> 1 PREROUTING -s <ip> -j DNAT --to-destination 188.68.45.124
# Option: actionunban
# Notes.: command executed when unbanning an IP. Take care that the
# command is executed with Fail2Ban user rights.
# Tags: See jail.conf(5) man page
# Values: CMD
#
actionunban = <iptables> -D f2b-<name> -s <ip> -j DNAT --to-destination 188.68.45.124
[Init]
/etc/fail2ban/jail.d/ 中的檔案 apache-route.local:
[apache-route]
enabled = true
filter = apache-probe
port = http,https
banaction = iptables-route.conf
maxretry = 3
findtime = 1500
bantime = 600
logpath = /var/www/*/userdata/logs/*-access.log
我什至無法測試它,因為它給了我以下錯誤:
fail2ban-客戶端重新啟動
在 /etc/fail2ban 下找不到「action.d/iptables-route.conf」的可存取設定檔
無法讀取操作“iptables-route.conf”
監獄“apache-route”中的錯誤。跳過...
我嘗試讓它工作,但我不知道為什麼它會給我這個錯誤
答案1
無法讀取操作“iptables-route.conf”
.conf
只需從操作名稱中刪除:
-banaction = iptables-route.conf
+banaction = iptables-route
順便提一句。你的行為在我看來有點不對勁。為什麼不預設iptables-multiport
指定(覆蓋)chain
和blocktype
?
不確定你在嘗試什麼,但不會是這樣的:
banaction = iptables-multiport[chain=PREROUTING, blocktype="DNAT --to-destination 188.68.45.124"]
做這份工作嗎?
答案2
為什麼不預設 iptables-multiport 指定(覆蓋)鍊和區塊類型?
iptables-multiport 不加入輸出鏈:
-A 轉送 -i ens3 -p tcp -m 狀態 --state 新 --dport 80 -j 接受
-A 轉送 -i ens3 -p tcp -m 狀態 --state 新 --dport 443 -j 接受
所以我決定創建一個自己的操作,添加這些操作並在加載/卸載時刪除它們
我忘記了我還需要實現後路由,但我需要進一步重新考慮以存檔它。
我想要的是:
在禁止時,請求被路由到另一個 IP,其中託管一個頁面,顯示“由於無效請求太多,您被禁止”,而不是僅僅拒絕/丟棄請求