Debian

fail2ban 動作路由到另一個 ip

  • January 13, 2020

我想創建一個 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”中的錯誤。跳過…

我試圖讓它工作,但我不知道為什麼它會給我這個錯誤

無法讀取操作“iptables-route.conf”

只需.conf從動作名稱中刪除:

-banaction   = iptables-route.conf
+banaction   = iptables-route

順便提一句。你的行為在我看來有點不對勁。為什麼不預設iptables-multiport指定(覆蓋)chainblocktype

不知道你在嘗試什麼,但不會是這樣的:

banaction = iptables-multiport[chain=PREROUTING, blocktype="DNAT --to-destination 188.68.45.124"]

做這份工作?

為什麼不使用指定(覆蓋)鍊和塊類型的預設 iptables-multiport?

iptables-multiport 不添加輸對外連結:

-A FORWARD -i ens3 -p tcp -m state –state NEW –dport 80 -j ACCEPT

-A FORWARD -i ens3 -p tcp -m state –state NEW –dport 443 -j ACCEPT

所以我決定創建一個自己的操作來添加這些並在載入/解除安裝時刪除它們

我忘記了我還需要實現後路由,但我需要進一步重新考慮以存檔它。

我想要的是:

在 Banaction 上,請求被路由到另一個 ip,其中託管了一個頁面,上面寫著“由於過多的無效請求而被禁止”,而不是僅僅拒絕/丟棄請求

引用自:https://serverfault.com/questions/998647