fail2banのactionを追加する

【PR】
※IT系の記事は当方環境での実施内容となるため実施する場合は自己責任でお願いいたします。

先日、webminでサービスdown/up通知をLINE Notifyに通知できたため、

これはヤバい!サーバがダウンしたらLINEに通知するサービスをあっという間に作れちゃう | TRAINING GROUNDS (bigfoot.work)

fail2banでipをbanした時にもそれをLINE Notifyに通知できないかなと思っていたのですが、
ばっちりやっておられる方がいました。素晴らしい!

fail2banのban通知をLine Notifyを使ってLINEグループに通知する – Qiita

私はLINEのちょっとキモ目な(笑)スタンプはいらないので、

-F 'stickerPackageId=1' -F 'stickerId=14'

はバッサリカット。

で、actionに追加するときなんですが、

/etc/jail.d配下のオーバーライド用ファイル(例えばjail.local)でactionを指定しなければ、

デフォルトは/etc/fail2ban/jail.confの以下の部分が有効になっているようなので、

# The simplest action to take: ban only
action_ = %(banaction)s[port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
# Choose default action. To change, just override value of 'action' with the
# interpolation to the chosen action shortcut (e.g. action_mw, action_mwl, etc) in jail.local
# globally (section [DEFAULT]) or per specific section
action = %(action_)s

jail.localの中の該当セクションを以下の様にしてactionを追加オーバーライド

※actionは複数行の時は改行で追加

[sshd]
enabled  = true
action = %(action_)s
         send-line[name=sshd]

これで通知来たので、取り合えず動いているみたいです。

ただ、sshdにセットするとうちの場合でもこんな感じで通知がバンバン来てしまうようになるので、

Status for the jail: sshd
|- Filter
|  |- Currently failed: 183
|  |- Total failed:     20604
|  `- Journal matches:  _SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
   |- Currently banned: 209
   |- Total banned:     367

長期BAN用のrecidiveにセットしておくとよいかも。

recidiveなら、sshd以外の理由でBANされたものも含まれますしね。

Tips

Posted by admin