Ubuntu で Postfix の SMTP AUTH を設定して fail2ban で認証に失敗したIPアドレスをブロックする

Ubuntu 18.04 の Postfix で次のようにして SMTP AUTH を有効にしました。

/etc/postfix/master.cf

submission inet n       -       n       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_recipient_restrictions=
  -o smtpd_relay_restrictions=permit_sasl_authenticated,reject

/etc/postfix/main.cf

smtpd_sasl_local_domain = $myhostname
broken_sasl_auth_clients = yes
smtpd_sasl_security_options = noanonymous

/etc/postfix/sasl/smtpd.conf

pwcheck_method: saslauthd
mech_list: PLAIN LOGIN

/etc/default/saslauthd (変更部分のみ)

START=yes

設定反映

# systemctl reload postfix

で、 fail2ban というパッケージをインストールすると、SMTP AUTH に何回か失敗するとそのクライアントの IPアドレスがブロックされるようになります。10分経つとブロックが解除されます。

/etc/fail2ban/jail.d/postfix.conf (作成)

[postfix-sasl]
enabled = true

設定反映

# systemctl reload fail2ban

回数やブロック解除までの時間等は /etc/fail2ban/jail.conf で指定できます。

# "bantime" is the number of seconds that a host is banned.
bantime  = 10m

# A host is banned if it has generated "maxretry" during the last "findtime"
# seconds.
findtime  = 10m

# "maxretry" is the number of failures before a host get banned.
maxretry = 5

ログは /var/log/fail2ban.log に出力されます。こんな感じ。

2019-01-14 23:29:00,987 fail2ban.filter         [1566]: INFO    [postfix-sasl] Found x.x.x.x - 2019-01-14 23:29:00
2019-01-14 23:29:14,921 fail2ban.filter         [1566]: INFO    [postfix-sasl] Found x.x.x.x - 2019-01-14 23:29:14
2019-01-14 23:29:29,230 fail2ban.filter         [1566]: INFO    [postfix-sasl] Found x.x.x.x - 2019-01-14 23:29:29
2019-01-14 23:29:39,035 fail2ban.filter         [1566]: INFO    [postfix-sasl] Found x.x.x.x - 2019-01-14 23:29:39
2019-01-14 23:29:39,189 fail2ban.actions        [1566]: NOTICE  [postfix-sasl] Ban x.x.x.x
2019-01-14 23:39:40,070 fail2ban.actions        [1566]: NOTICE  [postfix-sasl] Unban x.x.x.x

なお fail2ban をインストールするだけで sshd についても有効になります。

/etc/fail2ban/jail.d/defaults-debian.conf

[sshd]
enabled = true

これを false にするかコメントアウトすれば sshd について無効にできます。