Bài viết này dựa trên 2 bài đăng rất hay của anh Mark StoneBarry deGraaff. Nếu được kết hợp với bài viết Antispam best pratice của anh Mark Stone, bạn sẽ có 1 hệ thống Zimba có khả năng chống spam khá tốt.

Fail2ban là một chương trình được viết bằng ngôn ngữ Python nhằm tìm ra các IP có khả năng được hacker dùng để cố gắng truy cập vào hệ thống của bạn bằng cách quét qua các log file. Nếu tìm thấy các hành động đáng ngờ trong 1 khoảng thời gian định trước, fail2ban sẽ chặn các IP này bằng cách sử dụng iptables hoặc bảng định tuyến. Các luật để tìm ra IP đáng ngờ trong file log được chứa trong các file filter, các hành động tương ứng để chặn IP được viết trong các file jail.

Cấu hình fail2ban.

1. Nếu bạn đang dùng single server (cả mta và mailstore trên cùng 1 server), bạn cần thêm 127.0.0.1 và địa chỉ IP của server vào danh sách trustedIP.

sudo -u zimbra - 
zmprov mcf +zimbraMailTrustedIP 127.0.0.1 +zimbraMailTrustedIP {IP of Server} 
zmcontrol restart

2. Nếu bạn dùng multiserver, bạn cần thêm các ip của mailbox server và proxy server vào.

sudo -u zimbra - 
zmprov mcf +zimbraHttpThrottleSafeIPs {IP of Mailbox-1} 
zmprov mcf +zimbraHttpThrottleSafeIPs {IP of Mailbox-2} 
zmprov mcf +zimbraMailTrustedIP {IP of Proxy-1} 
zmprov mcf +zimbraMailTrustedIP {IP of Proxy-2} 
zmcontrol restart

3. Cài đặt fail2ban

Trên redhat hoặc centos 7/8

yum install epel-release -y 
yum install fail2ban -y

Trên Ubuntu 18/20

apt-get clean all ; apt-get update 
apt-get install fail2ban -y

4. Tạo file jail.local để thiết lập lại một số thông số mặc định của fail2ban

nano /etc/fail2ban/jail.local
[DEFAULT]
# "ignoreip" can be a list of IP addresses, CIDR masks or DNS hosts.
# Fail2ban will not ban a host which matches an address in this list.
# Several addresses can be defined using space (and/or comma) separator.
#ignoreip = 127.0.0.1/8 ::1 10.137.26.29/32
ignoreip = 127.0.0.1/8 IP-ADDRESS-OF-ZIMBRA-SERVER/32
banaction = route

Bạn điền các IP của Zimbra servers vào danh sách ignoreip trong file này. Bạn cũng có thể whitelist các IP khác nếu muốn.

5. Tạo file jail cho các dịch vụ zimbra

nano /etc/fail2ban/jail.d/zimbra.local

[zimbra-smtp]
enabled = true
filter = zimbra-smtp
port = 25,465,587
logpath = /var/log/zimbra.log
maxretry = 3
findtime = 86400
bantime = 86400
action = route

[zimbra-mail]
enabled = true
filter = zimbra-mail
port = 80,443,7071,9071
logpath = /opt/zimbra/log/mailbox.log
maxretry = 5
findtime = 86400
bantime = 86400
action = route

[zimbra-nginx]
enabled = true
filter = zimbra-nginx
port = 80,443
logpath = /opt/zimbra/log/nginx.log
maxretry = 1
findtime = 86400
bantime = 86400
action = route

Bạn tinh chỉnh maxretry, findtime và bantime cho phù hợp với môi trường của bạn.

Nếu bạn muốn dùng fail2ban cho ssh, tạo file sshd.local

nano /etc/fail2ban/jail.d/sshd.local 
[sshd] enabled = true 
port = 22 
maxretry = 3 
findtime = 600 
bantime = 3600 

6. Tạo các file filter ứng với các dịch vụ của zimbra ở trên.

– Tạo filter cho (web) mail service.

nano /etc/fail2ban/filter.d/zimbra-mail.conf 

[Definition]
failregex = .*ip=;.*authentication failed for .*$

ignoreregex =

– Tạo filter cho smtp service.

nano /etc/fail2ban/filter.d/zimbra-smtp.conf

[Definition]
failregex = postfix\/submission\/smtpd\[\d+\]: warning: .*\[\]: SASL \w+ authentication failed: authentication failure$
postfix\/smtps\/smtpd\[\d+\]: warning: .*\[\]: SASL \w+ authentication failed: authentication failure$
postfix\/submission\/smtpd\[\d+\]: warning: non-SMTP command from \w+\[\]:.*$
postfix\/smtpd\[\d+\].*amazonaws\.com\[\]:.*@yahoo\.com>.*helo=<\[127\.0\.0\.1\]>$
postfix\/smtpd\[\d+\].*amazonaws\.com\[\]:.*@yahoo\.com>.*helo=<127\.0\.0\.1>$
postfix\/smtpd\[\d+\].*unknown\[\]:.*helo=<\[192\.168.*\]>$
postfix\/smtpd\[\d+\].*unknown\[\]:.*helo=<192\.168.*>$

ignoreregex =

– Tạo filter cho nginx service

nano /etc/fail2ban/filter.d/zimbra-nginx.conf

[Definition]
failregex = client\s+sent\s+plain\s+HTTP\s+request\s+to\s+HTTPS\s+port\s+while\s+reading\s+client\s+request\s+headers,\s+client:\s+,\s+server
client\s+sent\s+invalid\s+method\s+while\s+reading\s+client\s+request\s+line,\s+client:\s+,\s+server

ignoreregex =

7. Khởi động dịch vụ fail2ban

systemctl restart fail2ban
systemctl status fail2ban
systemctl enable fail2ban

8. Tiến hành kiểm tra thử 1 filter rule, ví dụ cho nginx.

fail2ban-regex /opt/zimbra/log/nginx.log /etc/fail2ban/filter.d/zimbra-nginx --print-all-matched

9. Cho mục đích bảo trì fail2ban, chúng tôi khuyến nghị dùng “vacuum” cho sqlite3 database định kỳ. Thêm vào cho crontab với quyền root:

0 2 * * 0 /usr/bin/sqlite3 /var/lib/fail2ban/fail2ban.sqlite3 'vacuum;'

10. Kiểm tra trạng thái jail của fail2ban

fail2ban-client status

11. Kiểm tra routing table.

route -n

12. Ban và unban 1 địa chỉ IP

fail2ban-client set “Jail-Name” banip “IP-Address”

ví dụ: fail2ban-client set sshd banip 10.137.26.29

fail2ban-client set “Jail-Name” unbanip “Banned IP-Address”

ví dụ: fail2ban-client set sshd unbanip 10.137.26.29

Unban tất cả: fail2ban-client unban –all