LoginSignup
4
0

More than 5 years have passed since last update.

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

Last updated at Posted at 2017-03-24

Line NotifyというLINEのサービスを使うと、コマンドベースで任意のグループにメッセージを送れるようになります。
そこで、不正アクセスなどを検知したfail2banが、特定のIPアドレスをbanしたときに、LINEグループに通知するようにしてみました。

このような感じに、スタンプの送信も可能です。
linenotify4.png

バージョン

CentOS 7.3
fail2ban 0.9.6

LINE側の準備

Line Notifyはブラウザ上で操作します。 また通知用のLINEグループを作成するため、スマートフォンとブラウザの両方で操作が必要になります。

LINEアプリに通知用のグループ作成

  1. Line Notifyをブラウザで開き、自分のLINEアカウントでログインします。
  2. 自分のLINEアプリのトークに、LINE Notifyから通知が来ていることを確認してください。
  3. fail2ban通知用のグループを自身のLINEアプリ上に作成します。 今回は、fail2banというグループ名で作成しました。
  4. 作ったグループのメンバーにLINE Notifyを追加します。 これで、LINEアプリ側の準備は整いました。

Line Notifyの通知用トークン作成

  1. 次に、ブラウザ上のLine Notifyマイページ にいきます。
    linenotify1.png

  2. Generate tokenをクリックます。
    linenotify3.png

  3. トークン用の設定画面が出るので、トークン名と事前に作成したfail2ban用のグループを選択します。今回は、トークン名はTESTとしています。 Ganarate tokenをクリックします。
    linenotify.png

  4. トークンが画面に出てくるので、コピーしてとっておきます。
    linenotify5.png

fail2banの設定

fail2banにはLINE通知用のアクションは無いため、新規に作成します。 作成したactionをjailの定義に組み込みます。

actionの作成

/etc/fail2ban/action.d下にLINE通知用のactionファイルを新規に作成します。 今回は、send-line.confというファイルにしています。

vi /etc/fail2ban/action.d/send-line.conf 

設定内容は、これだけです。
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxは、Line Notifyで取得したトークンです。差し替えてください。

/etc/fail2ban/action.d/send-line.conf
[Definition]

actionban = curl -X POST -H 'Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' -F 'message=[Fail2Ban] <name>: banned <ip>' -F 'stickerPackageId=1' -F 'stickerId=14' https://notify-api.line.me/api/notify

[Init]
name = default
  • message : テキストメッセージ
  • stickerPackageId : スタンプグループID
  • stickerId : スタンプID

スタンプグループIDやスタンプIDの一覧は、以下URLにて公開されています。
Lineスタンプ一覧:ttps://devdocs.line.me/files/sticker_list.pdf

テキスト通知だけで良いのであれば、stickerPackageIdstickerIdを適宜外してください。

jailの設定

今回は、sshdのjail設定に、先ほど作成したLINE通知設定を組み込んでみました。

vi /etc/fail2ban/jail.d/02-enable-jail.conf

sshdのセクションのactionsend-line[name=sshd]を追加します。

/etc/fail2ban/jail.d/02-enable-jail.conf
[sshd]
enabled = true
action  = firewallcmd-multiport[name=sshd, port="ssh", protocol=tcp, blocktype=DROP]
          sendmail-whois-ipjailmatches[name=sshd, sender=fail2ban@xxx.co.jp, dest=root@xxx.co.jp]
          send-line[name=sshd]

これで、準備は完了です。

確認

sshdのjailをリロードしてみます。

# fail2ban-client reload sshd

うまくいけば、ban通知がLINEに来るはずです。
linenotify4.png

参考

LINE Engineers' Blog: http://developers.linecorp.com/blog/ja/?p=3819

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0