fail2banの設定見直し

  • 投稿日:
  • by
  • カテゴリ:

/var/log/mail.logを見ていると、あるIPアドレスからAuthの警告が出ている。saslの穴を探っているようだ。
前回記事でのインストール方法を見直すこととした。

環境は

さくらVPS(石狩)
debian
postfix + dovecot
fail2ban + iptables

の構成。fail2banは、0.11.2

# fail2ban-client -V
0.11.2
#

まずは、/etc/fail2ban/jail.d/postfix.conf なるファイルを準備、導入。
/etc/fail2ban/jail.localなるファイルを置いて/etc/fail2ban/jail.confをoverrideする機構であるが、/etc/fail2ban/jail.d/ 以下のファイルもIncludeされるので、サービスごとにconfファイルを置くことにした。

[postfix]
mode = aggressive
enabled = true
backend = %(postfix_backend)s
logpath = %(postfix_log)s
findtime = 600
maxretry = 3
bantime = 2d

modeについては/etc/fail2ban/filters.d/postfix.confで定義されているfilterのルールで何を使うかを定義する。"aggressive"だと、(次郎系らーめんでいうところの)「トッピング全部乗せ」のオーダになる。sasl-authだけ気になるのであれば、mode = auth とか。他にも、rblとかddosなどのパラメータを選択可能。
backend は変数の中に、/etc/fail2ban/filters.d/postfix.conf を指すようになってるようだ。fullpathで書いても動いた。backend についても同様。/var/log/mail.log になっている。

さて同様にsshdについても/etc/fail2ban/jail.d/sshd.confを作る。

[sshd]
mode = aggressive
enabled = true
backend = %(sshd_backend)s
logpath = %(sshd_log)s
findtime = 600
maxretry = 3
bantime = 2d

さらに/etc/fail2ban/jail.d/dovecot.conf

[dovecot]
mode = aggressive
enabled = true
backend = %(dovecot_backend)s
logpath = %(dovecot_log)s
findtime = 600
maxretry = 5
bantime = 2d

デフォルトの状態だと、tcpを定義portでrefuseするが、iptableでBANしてもしつこくsshdにattackしてくるのが居る。応急的にiptableでDROPするよう、とりあえず、手動で以下を発出。

# iptables -A INPUT -s 142.93.164.94 -j DROP

ここから、本格的にiptableでDROPとするやりかた。
fail2banのaction書き換えを別途調査したら見つかった。
/etc/ffail2ban/action.d/iptables-common.confに

[INCLUDES]

after = iptables-blocktype.local
iptables-common.local
# iptables-blocktype.local is obsolete

とある。
そこで、/etc/fail2ban/action.d/iptables-common.local なるファイルを作り、

[Init]
blocktype = DROP

と定義する。

ここでfail2banを再起動。

systemctl reload fail2ban

とした。が、ルールが一部更新されなかったので、

/etc/init.d/fail2ban stop
rm /var/lib/fail2ban/fail2ban.sqlite3
/etc/init.d/fail2ban start

で、サービスを止めて、BANのlistをクリアし、再スタートした。

root@ik1-423-43544:~# fail2ban-client status
Status
|- Number of jail: 3
`- Jail list: dovecot, postfix, sshd
root@ik1-423-43544:~# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
| |- Currently failed: 0
| |- Total failed: 0
| `- File list: /var/log/auth.log
`- Actions
|- Currently banned: 1
|- Total banned: 1
`- Banned IP list: 142.93.164.94
root@ik1-423-43544:~# fail2ban-client status postfix
Status for the jail: postfix
|- Filter
| |- Currently failed: 1
| |- Total failed: 2
| `- File list: /var/log/mail.log
`- Actions
|- Currently banned: 5
|- Total banned: 5
`- Banned IP list: 174.138.10.169 193.56.29.119 5.34.207.52 5.34.207.58 77.247.110.151
root@ik1-423-43544:~# fail2ban-client status dovecot
Status for the jail: dovecot
|- Filter
| |- Currently failed: 0
| |- Total failed: 0
| `- File list: /var/log/mail.log
`- Actions
|- Currently banned: 0
|- Total banned: 0
`- Banned IP list:
root@ik1-423-43544:~#

動作しているのを確認した。

# iptables -L

で。iptable で弾いている様子が確認できる。

f2b.png

iptableでDROP定義に変わった。

あと、

less /var/log/fail2ban.log

でfail2banの動作環境を監視すると、findtimeとかbantime、maxretryの調整に役立つ。

(追記 2022/02/07)