kn007的个人博客
♥ You are here: Home > 软件与网络 > VPS > [Postfix进阶]利用Fail2ban防止爆破和攻击

[Postfix进阶]利用Fail2ban防止爆破和攻击

by | 51 Comments

提醒:本文最后更新于 3429 天前,文中所描述的信息可能已发生改变,请仔细核实。


分享一个Fail2ban过滤器,它可以对那些针对Postfix的恶意扫描及攻击,进行自动加入黑名单。

据我分析邮件日志,主要是有一些恶意访客和安全软件,尝试relay测试,看看能不能利用你的服务器发送邮件;还有就是端口扫描,经常有一些超时连接。

针对此,我对那些无法通过认证的,尝试连接后无动作超时的,还有一些故意浪费资源(helo之后无动作),做了策略。24小时内同IP,2次任意匹配规则,一周内无法访问服务器。

使用很简单,编辑/etc/fail2ban/jail.conf,添加如下配置:

[postfix-ddos]
enabled  = true
filter   = postfix-ddos
action   = iptables-allports[name=MAIL, protocol=all]
logpath  = /var/log/maillog
maxretry = 2
findtime = 86400
bantime  = 604800

然后在/etc/fail2ban/filter.d中,创建一个文件,命名为postfix-ddos.conf,添加如下内容:

# Fail2Ban filter for Postfix DDOS attacks
#

[INCLUDES]

before = common.conf

[Definition]

_daemon = postfix/(submission/)?smtp(d|s)

failregex = ^%(__prefix_line)sNOQUEUE: reject: RCPT from \S+\[<HOST>\]: 450 4\.7\.1 Client host rejected: cannot find your hostname, (\[\S*\]); from=<\S*> to=<\S+> proto=ESMTP helo=<\S*>$
            ^%(__prefix_line)sNOQUEUE: reject: RCPT from \S+\[<HOST>\]: 450 4\.7\.1 : Helo command rejected: Host not found; from=<> to=<> proto=ESMTP helo= *$
            ^%(__prefix_line)sNOQUEUE: reject: RCPT from \S+\[<HOST>\]: 454 4\.7\.1 Service unavailable; Client host \[\S+\] blocked using .* from=<\S*> to=<\S+> proto=ESMTP helo=<\S*>$
            ^%(__prefix_line)sNOQUEUE: reject: VRFY from \S+\[<HOST>\]: 550 5\.1\.1 .*$
            ^%(__prefix_line)sNOQUEUE: reject: RCPT from \S+\[<HOST>\]: 504 5\.5\.2 .*$
            ^%(__prefix_line)sNOQUEUE: reject: RCPT from \S+\[<HOST>\]: 554 5\.7\.1 .*$
            ^%(__prefix_line)simproper command pipelining after \S+ from [^[]*\[<HOST>\]:?$
            ^%(__prefix_line)slost connection after \S+ from [^[]*\[<HOST>\]:?$
            ^%(__prefix_line)swarning: [-._\w]+\[<HOST>\]: SASL ((?i)LOGIN|PLAIN|(?:CRAM|DIGEST)-MD5) authentication failed(: [ A-Za-z0-9+/:]*={0,2})?\s*$
            ^%(__prefix_line)sstatistics: max connection rate \S+ for \(smtp:<HOST>\):?$

ignoreregex = lost connection after .* from unknown\[unknown\]$
              authentication failed: Connection lost to authentication server$
              statistics: max connection rate .* for \(smtp:unknown\).*$

[Init]

journalmatch = _SYSTEMD_UNIT=postfix.service


# Author: kn007

重启Fail2ban即可生效。需要注意的是确保你的Postfix日志写入位置是/var/log/maillog

同时,我在Github也建立了一个repo:kn007/fail2ban-filters。这个repo后续会分享我自用的一些fail2ban过滤器,也欢迎大家进行PR完善它。

值得一提的是,目前Wordpress评论回复通知邮件提醒同时包含text/plain与text/html两个版本,这使得HTML版本的邮件也能获得满分。并且全新的邮件风格,具有不错的兼容性(响应式)。

更多Postfix相关文章,详见《进阶的Postfix小结》。

转载请注明转自: kn007的个人博客 的《[Postfix进阶]利用Fail2ban防止爆破和攻击

donate
有所帮助?

Comments

51 Comments立即评论
  1. 沙发 :smile: :twisted:

    1. MOD回复

      @World: 欢迎 :smile:

  2. 回复

    厉害,大神

    1. MOD回复

      @孵化器: 谬赞了。

  3. 地板 :shock:

    1. MOD回复

      @咕噜噜: 天气冷,小心着凉 :x

  4. 虽然不懂,但必须支持!

    1. MOD回复

      @文栋说自媒体: 谢谢支持 ;-)

  5. 回复

    厉害了我的博主,果然很geek,和我这得一位author很像。。。请问要不要加友链呢?邮件联系我。

    1. MOD回复

      @nash.zhao: 先友后链。。谢谢

  6. 完全看不懂,过来凑热闹。。 :!:

    1. MOD回复

      @郑永: :cool: 欢迎

  7. 是很深奥的东西。 :smile:

    1. MOD回复

      @夏日草博客: 额,没有吧

  8. 我用的也是 Postfix,不过只是用来发回复通知邮件……

    1. MOD回复

      @zwwooooo: 我也基本上是,不过我有收信,所以端口外露,要保护起来 :idea:

  9. 回复

    最近按照博主的教程搭建了 Postifx dovecot +rainloop 感觉好舒服啊 莫名地爽~

    不过我是 CentOS 6.8 安装 fail2ban 发现 文件 /etc/fail2ban/jail.conf 里面自带了 postfix 还有 Dovecot 端口的保护,是不是一般配置好/etc/fail2ban/jail.conf 中的 bantime 等参数

    比如:
    [postfix-sasl]
    port = smtp,465,submission,imap3,imaps,pop3,pop3s

    这样按照这些配置启动fail2ban 就能达到一定保护效果了?

    请教一下: 在 /etc/fail2ban/filter.d 下也有 postfix.conf 还有 dovecot.conf 等配置文件, 是不是意味着我无需再添加 postfix-ddos.conf ?
    :oops:

    1. MOD回复

      @luker: 有一定保护作用,我的跟他们的不大一样。安装理论肯定是越多越好。

    2. 回复

      @kn007: 恩 现在搞懂了,其实你这个配置更好更全面 谢谢 !

      不过这些规则让我自己来写是有点困难了,请教一下如果要自己写需要哪方面知识 谢谢 :!:

    3. MOD回复

      @Luker: 正则。

  10. 回复

    在centos 7下面用你的postfix-ddos.conf没搞定,明天再看看啥原因。

    1. MOD回复

      @Trier: 没搞定指什么

    2. 回复

      @kn007: 在jail.conf加入你的配置后就启动不起来:
      [root@localhost fail2ban]# systemctl status fail2ban.service
      ● fail2ban.service - Fail2Ban Service
      Loaded: loaded (/usr/lib/systemd/system/fail2ban.service; enabled; vendor preset: disabled)
      Active: failed (Result: start-limit) since Sat 2017-07-22 23:42:14 CEST; 9s ago
      Docs: man:fail2ban(1)
      Process: 12891 ExecStart=/usr/bin/fail2ban-client -x start (code=exited, status=255)
      Main PID: 12848 (code=exited, status=255)

      Jul 22 23:42:14 localhost.localdomain systemd[1]: Failed to start Fail2Ban Service.
      Jul 22 23:42:14 localhost.localdomain systemd[1]: Unit fail2ban.service entered failed state.
      Jul 22 23:42:14 localhost.localdomain systemd[1]: fail2ban.service failed.
      Jul 22 23:42:14 localhost.localdomain systemd[1]: fail2ban.service holdoff time over, scheduling restart.
      Jul 22 23:42:14 localhost.localdomain systemd[1]: start request repeated too quickly for fail2ban.service
      Jul 22 23:42:14 localhost.localdomain systemd[1]: Failed to start Fail2Ban Service.
      Jul 22 23:42:14 localhost.localdomain systemd[1]: Unit fail2ban.service entered failed state.
      Jul 22 23:42:14 localhost.localdomain systemd[1]: fail2ban.service failed.
      删掉后就能正常启动了。

      现在能正常启动的使用的全是默认配置。

    3. MOD回复

      @Trier: 看日志,不是启动状态。

    4. MOD回复

      @Trier: 安装好fail2ban,把文件放进去,增加个配置就行了。

    5. 回复

      @kn007: 是的,就是增加启用DDOS的过滤,我正检查是不是因为CentOS 7的原因。

    6. MOD回复

      @Trier: 我也CentOS 7,正常

    7. 回复

      @kn007: 多谢大神指导,你的回复超快啊!

      一样的做法,昨天晚上就不行,今天就没问题,而且有问题的时候也没有记录下来日志,下面是fail2ban的全部日志:
      2017-07-22 23:44:31,630 fail2ban.server [12931]: INFO Changed logging target to /var/log/fail2ban.log for Fail2ban v0.9.6
      2017-07-22 23:44:31,631 fail2ban.database [12931]: INFO Connected to fail2ban persistent database '/var/lib/fail2ban/fail2ban.sqlite3'
      2017-07-22 23:44:31,656 fail2ban.database [12931]: WARNING New database created. Version '2'
      2017-07-23 08:24:13,924 fail2ban.transmitter [12931]: WARNING Command ['status', 'ssh-iptables'] has failed. Received UnknownJailException('ssh-iptables',)
      2017-07-23 08:24:24,401 fail2ban.transmitter [12931]: WARNING Command ['status', 'ssh-iptables'] has failed. Received UnknownJailException('ssh-iptables',)
      2017-07-23 08:31:57,652 fail2ban.server [12931]: INFO Stopping all jails
      2017-07-23 08:31:57,654 fail2ban.server [12931]: INFO Exiting Fail2ban
      2017-07-23 08:31:58,028 fail2ban.server [14049]: INFO Changed logging target to /var/log/fail2ban.log for Fail2ban v0.9.6
      2017-07-23 08:31:58,030 fail2ban.database [14049]: INFO Connected to fail2ban persistent database '/var/lib/fail2ban/fail2ban.sqlite3'
      2017-07-23 08:31:58,032 fail2ban.jail [14049]: INFO Creating new jail 'postfix-ddos'
      2017-07-23 08:31:58,156 fail2ban.jail [14049]: INFO Jail 'postfix-ddos' uses pyinotify {}
      2017-07-23 08:31:58,214 fail2ban.jail [14049]: INFO Initiated 'pyinotify' backend
      2017-07-23 08:31:58,219 fail2ban.filter [14049]: INFO Added logfile = /var/log/maillog
      2017-07-23 08:31:58,221 fail2ban.filter [14049]: INFO Set maxRetry = 2
      2017-07-23 08:31:58,222 fail2ban.filter [14049]: INFO Set jail log file encoding to UTF-8
      2017-07-23 08:31:58,223 fail2ban.actions [14049]: INFO Set banTime = 604800
      2017-07-23 08:31:58,225 fail2ban.filter [14049]: INFO Set findtime = 86400
      2017-07-23 08:31:58,335 fail2ban.server [14049]: INFO Jail postfix-ddos is not a JournalFilter instance
      2017-07-23 08:31:58,359 fail2ban.jail [14049]: INFO Jail 'postfix-ddos' started

      最后还是感谢你的网站,感谢大神! ;-)

    8. MOD回复

      @Trier: 日志说导致启动不了,是因为ssh-iptables这一项。

    9. 回复

      @kn007: 我对iptables不是很懂。但我想有可能的原因是这样:
      在装opendkim的时候,我完全按照你的脚本执行的,这样相当于在CentOS 7上使用了CentOS 6的源进行安装的。然后我就出现了fail2因为python 2.6dependency的问题。
      最后发现了这个问题后,改成CentOS 7 epel的源之后就安装fail2ban成功。

      因为过程中我做过多次的yum update,所以机器上的软件版本可能存在一些混乱,昨天的最后我又进行了yum update操作,有可能因此而将iptables相关的版本更新就能用了。

      别的我想不出原因了。

      感谢回复!

    10. MOD回复

      @Trier: 客气了,源无需使用centos6的,centos7出现问题,就做修改就行了,一般变动不大。

;-):|:x:twisted::smile::shock::sad::roll::razz::oops::o:mrgreen::lol::idea::grin::evil::cry::cool::arrow::???::?::!: