我有一个Ubuntu20.04服务器,每天在我的postfix服务器上接收来自同一个IP的数百个SMTP AUTH请求。我已经安装了fail2ban,但具有讽刺意味的是,它没有禁止IP。
我的/etc/fail2ban/jail.local文件是('d位是个人和商业IP):
[postfix-flood-attack]
enabled = true
bantime = 1h
filter = postfix-flood-attack
action = iptables-multiport[name=postfix, port="http,https,smtp,submission,pop3,pop3s,imap,imaps,sieve", protocol=tcp]
logpath = /var/log/mail.log
ignoreip = <snip> 127.0.0.1/8
maxretry = 3
[postfix]
enabled = true
maxretry = 3
bantime = 1h
filter = postfix[mode=aggressive]
logpath = /var/log/mail.log
ignoreip = <snip> 127.0.0.1/8
[dovecot]
enabled = true
port = pop3,pop3s,imap,imaps
filter = dovecot
logpath = /var/log/mail.log
maxretry = 3
ignoreip = <snip> 127.0.01/8有争议的监狱是postfix-flood-attack,从本教程的底部那里带走的。/etc/fail2ban/filter.d/postfix-flood-attack.conf文件是:
[Definition]
failregex = lost connection after AUTH from (.*)\[<HOST>\]
ignoreregex =我的日志消息看起来就像
Aug 15 13:54:45 ikana postfix/smtps/smtpd[268729]: connect from unknown[193.35.48.18]
Aug 15 13:54:46 ikana postfix/smtps/smtpd[268729]: Anonymous TLS connection established from unknown[193.35.48.18]: TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)
Aug 15 13:54:50 ikana postfix/smtps/smtpd[268729]: warning: unknown[193.35.48.18]: SASL PLAIN authentication failed:
Aug 15 13:54:50 ikana postfix/smtps/smtpd[268729]: lost connection after AUTH from unknown[193.35.48.18]
Aug 15 13:54:50 ikana postfix/smtps/smtpd[268729]: disconnect from unknown[193.35.48.18] ehlo=1 auth=0/1 commands=1/2
Aug 15 13:54:50 ikana postfix/smtps/smtpd[268729]: connect from unknown[193.35.48.18]
Aug 15 13:54:51 ikana postfix/smtps/smtpd[268729]: Anonymous TLS connection established from unknown[193.35.48.18]: TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)
Aug 15 13:54:57 ikana postfix/smtps/smtpd[268729]: lost connection after AUTH from unknown[193.35.48.18]
Aug 15 13:54:57 ikana postfix/smtps/smtpd[268729]: disconnect from unknown[193.35.48.18] ehlo=1 auth=0/1 commands=1/2
Aug 15 13:54:57 ikana postfix/smtps/smtpd[268729]: connect from unknown[193.35.48.18]
Aug 15 13:54:58 ikana postfix/smtps/smtpd[268729]: Anonymous TLS connection established from unknown[193.35.48.18]: TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)
Aug 15 13:55:04 ikana postfix/smtps/smtpd[268729]: lost connection after AUTH from unknown[193.35.48.18]
Aug 15 13:55:04 ikana postfix/smtps/smtpd[268729]: disconnect from unknown[193.35.48.18] ehlo=1 auth=0/1 commands=1/2
Aug 15 13:55:04 ikana postfix/smtps/smtpd[268734]: connect from unknown[193.35.48.18]
Aug 15 13:55:05 ikana postfix/smtps/smtpd[268734]: Anonymous TLS connection established from unknown[193.35.48.18]: TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)
Aug 15 13:55:09 ikana postfix/smtps/smtpd[268734]: warning: unknown[193.35.48.18]: SASL PLAIN authentication failed:
Aug 15 13:55:09 ikana postfix/smtps/smtpd[268734]: lost connection after AUTH from unknown[193.35.48.18]
Aug 15 13:55:09 ikana postfix/smtps/smtpd[268734]: disconnect from unknown[193.35.48.18] ehlo=1 auth=0/1 commands=1/2根据fail2ban-regex的说法,这应该是可行的,但是IP并没有被禁止。命令fail2ban-regex /var/log/mail.log /etc/fail2ban/filter.d/postfix-flood-attack.conf的输出是:
Running tests
=============
Use failregex filter file : postfix-flood-attack, basedir: /etc/fail2ban
Use log file : /var/log/mail.log
Use encoding : UTF-8
Results
=======
Failregex: 5356 total
|- #) [# of hits] regular expression
| 1) [5356] lost connection after AUTH from (.*)\[<HOST>\]
`-
Ignoreregex: 0 total
Date template hits:
|- [# of hits] date format
| [37949] {^LN-BEG}(?:DAY )?MON Day %k:Minute:Second(?:\.Microseconds)?(?: ExYear)?
`-
Lines: 37949 lines, 0 ignored, 5356 matched, 32593 missed
[processed in 1.43 sec]
Missed line(s): too many to print. Use --print-all-missed to print all 32593 lines所以它找到了5356个原木的匹配点,而且从来不禁止任何一个。通常在默认的10分钟查找时间内有8次尝试。将-v选项与fail2ban-regex结合使用的代码片段显示了以下与时间戳匹配的内容:
...
193.35.48.18 Thu Aug 15 13:50:55 2019
193.35.48.18 Thu Aug 15 13:51:02 2019
193.35.48.18 Thu Aug 15 13:51:10 2019
193.35.48.18 Thu Aug 15 13:51:15 2019
193.35.48.18 Thu Aug 15 13:54:50 2019
193.35.48.18 Thu Aug 15 13:54:57 2019
193.35.48.18 Thu Aug 15 13:55:04 2019
193.35.48.18 Thu Aug 15 13:55:09 2019
193.35.48.18 Thu Aug 15 13:58:40 2019
193.35.48.18 Thu Aug 15 13:58:48 2019
193.35.48.18 Thu Aug 15 13:58:54 2019
193.35.48.18 Thu Aug 15 13:58:59 2019
...发布于 2020-08-15 15:36:46
配置看起来不错,但是在fail2ban-regex的输出中有一个重要的细节需要注意:它决定日期为2019年。考虑到日志在问题中的样子,这在一开始似乎相当愚蠢。事实证明,这在某种程度上是fail2ban的一个已知问题,他们称之为Z-发行。在将服务器配置为使用特定时区之后,您需要重新启动一组服务,或者重新启动整个系统以使其正常生效。虽然我不记得这是多久了,我想我从来没有重新启动我的服务器,因为我配置了它的时区。
systemctl restart syslog重新启动syslog服务后,fail2ban识别出正确时区中的日志行.Fail2ban立即在配置的查找时间中识别日志消息,并禁止了已困扰我的服务器数天的IP。我猜Fail2ban要求syslog提供时区信息,而不是使用自启动故障2ban-server以来在机器上设置的内容。
我希望这能帮助其他人解决类似的问题。
发布于 2020-08-16 13:14:37
当您使用标准日期格式时,导致fail2ban假定这些日期为2019年的模糊情况不会发生。您可以完全避免使用ISO 8601的问题--在2020年,您可能没有任何理由坚持采用向后兼容的日志格式。
此外,在Ubuntu中,您很可能可以完全跳过日期格式化/解析,方法是指示fail2ban直接使用systemd日志,这将提供来自没有时区信息的历代普通偏移量(在本地监狱配置中尝试[DEFAULT]块中的backend = systemd )。
https://serverfault.com/questions/1030249
复制相似问题