CentOS 7 Fail2ban 抵檔重複登入失敗的 IP - MIS 腳印
MIS 腳印 logo

MIS 腳印

記錄 IT 學習的軌跡

CentOS 7 Fail2ban 抵檔重複登入失敗的 IP

架設對外的 Mail Server,經常在 /var/log/maillog 發現許多登入失敗的訊息,表示有人在重複測試密碼,這時就可使用 Fail2ban 軟體來抵檔登入失敗次數太多的不正常 IP。

CentOS

於 /var/log/maillog 發現很多登入 Postfix 失敗的訊息,這其實是有人在使用暴力破解的方式來不正當取得密碼:

vim /var/log/maillog
Jun 19 15:20:40 mail postfix/smtpd[14705]: warning: unknown[181.214.206.170]: SASL LOGIN authentication failed: authentication failure
Jun 19 15:20:41 mail postfix/smtpd[14705]: disconnect from unknown[181.214.206.170]
Jun 19 15:20:49 mail postfix/smtpd[14840]: warning: unknown[181.214.206.195]: SASL LOGIN authentication failed: authentication failure

安裝

須先安裝 epel 的軟體套件:

yum install epel-release

再安裝 fail2ban:

yum install fail2ban

設定

新增 jail.local 檔案,來自訂 fail2ban 設定:

vim /etc/fail2ban/jail.local
[DEFAULT]
# 禁止時間 (秒)
bantime  = 86400
# 禁止方式
banaction = iptables-multiport

# 要監聽的服務
[postfix-sasl]
enabled = true

[dovecot]
enabled = true

啟用

立即啟用 fail2ban,並開機自動啟動:

systemctl start fail2ban
systemctl enable fail2ban

查看 Ban (禁止) 態狀

查看 postfix-sasl 服務的態狀與被禁止的 IP:

fail2ban-client status postfix-sasl
Status for the jail: postfix-sasl
|- Filter
|  |- Currently failed: 7
|  |- Total failed:     1580
|  `- Journal matches:  _SYSTEMD_UNIT=postfix.service
`- Actions
   |- Currently banned: 2
   |- Total banned:     86
   # 目前被禁止的 IP
   `- Banned IP list:   181.214.206.170 181.214.206.195

查看 iptables 服務,確實拒絕了上述被禁止的 IP:

iptables -L -n
... 以上省略 ...

# 對應上述 jail.local 自訂的設定,所監聽的服務
Chain f2b-dovecot (1 references)
target     prot opt source               destination
RETURN     all  --  0.0.0.0/0            0.0.0.0/0

Chain f2b-postfix-sasl (1 references)
target     prot opt source               destination
# 拒絕的 IP 規則
REJECT     all  --  181.214.206.195      0.0.0.0/0            reject-with icmp-port-unreachable
REJECT     all  --  181.214.206.170      0.0.0.0/0            reject-with icmp-port-unreachable

查看 Log 檔

狀態說明:

  • Unban:之前被禁止的 IP,但禁止時間到了所以解除。
  • Ban:被禁止的 IP。
  • Found:登入失敗的 IP。
vim /var/log/fail2ban.log
2018-06-20 13:57:47,106 fail2ban.actions        [16338]: NOTICE  [postfix-sasl] Unban 181.214.206.170
2018-06-20 13:59:58,379 fail2ban.actions        [16338]: NOTICE  [postfix-sasl] Ban 181.214.206.170
2018-06-20 14:00:02,993 fail2ban.filter         [16338]: INFO    [postfix-sasl] Found 193.15.218.238

參考

發表迴響