專案

一般

個人檔案

動作

如何為 Redmine 配置 Fail2ban

安裝

如果尚未安裝,請使用 apt-get 安裝 fail2ban(或使用 yum、pacman 或其他套件管理器)

 apt-get install fail2ban

配置

在 /etc/fail2ban/filter.d/ 中建立一個名為 redmine.conf 的新檔案
說明:我們在這裡建立新的篩選器來匹配失敗的登入嘗試

# redmine configuration file
#
# Author: David Siewert
#
# $Revision$
#
[INCLUDES]

# Read common prefixes. If any customizations available -- read them from
# common.local
before = common.conf

[Definition]

datepattern = %%Y-%%m-%%d %%H:%%M:%%S %%Z$
failregex = Failed [-/\w]+ for .* from <HOST>

# Option:  ignoreregex
# Notes.:  regex to ignore. If this regex matches, the line is ignored.
# Values:  TEXT
#
ignoreregex =

# Source:
#http://www.fail2ban.org/wiki/index.php/MANUAL_0_8

將以下行添加到 /etc/fail2ban/jail.conf 中的某處
說明:我們在這裡啟用新建立的篩選器並添加一些配置選項

[redmine]
enabled  = true
filter   = redmine
port     = 80,443
#backend  = polling
action   = iptables-allports[name=redmine]
logpath  = /var/log/redmine/default/production.log
maxretry = 5
findtime = 7320
bantime  = 7320

備註 1
如果您的網路伺服器使用當地時間,則需要根據與 UTC 時間的差異相應地增加 findtime 和 bantime
在我的配置範例中,我使用了 2 小時 + 2 分鐘 = 3600 * 2 +120 = 7320,
因為我的本地伺服器時間總是早 2 小時,並且我額外添加了 2 分鐘以防萬一

備註 2
如果您沒有在生產環境中使用 Redmine,則還需要更改 logpath

備註 3
如果此配置不起作用,您可以嘗試取消註釋「backend = polling」

測試

如果您進行任何更改,可以使用以下命令檢查語法

fail2ban-regex /var/log/redmine/default/production.log /etc/fail2ban/filter.d/redmine.conf 

最後啟動/重新啟動 fail2ban

/etc/init.d/fail2ban restart

並測試您是否可以讓自己被禁止訪問 :)
備註:您的失敗登入嘗試次數需要與 /etc/fail2ban/jail.conf 中的 maxretry 值相符

Jan Catrysse將近 3 年前 更新 · 5 個版本