LoginSignup
3
1

More than 1 year has passed since last update.

Raspberry Piでrsyslogを使ってsshguardの不正アクセスブロックをLINEに通知する

Last updated at Posted at 2022-09-16

/etc/rsyslog.d/に適当なconfを作成

line-notification.conf
module(load="omprog")
if ($msg contains "Blocking") then {
    action(
        type="omprog"
        template="RSYSLOG_TraditionalFileFormat"
        binary="***.sh"
    )
}

sshguardが不正アクセスIPをブロックしたときにrsyslogに以下が通知される。

/var/log/auth.log
*** ** **:**:** raspberrypi sshguard[***]: Blocking "***" for *** secs (* attacks in * secs, after * abuses over * secs.)

したがって、line-notification.confに"Blocking"がmsgに含まれるとshellが走るように設定する。
LINEに通知するshellスクリプトを適当に作成。

***.sh
#!/bin/bash

while read line; do
        msg1=$(echo ${line} | sed -e "s/\"//g")
        curl -v https://api.line.me/v2/bot/message/push \
        -H 'Content-Type: application/json' \
        -H 'Authorization: Bearer {***}' \
        -d @- <<EOS
{"to": "***","messages":[{"type":"text","text":"${msg1}"}]}
EOS

done

sedで/var/log/auth.logにあるIPアドレス部分が””で囲われているのを置換してやらないとjson書式が崩れるので、何かしらの置換が必要。
LINEに渡すデータ部分(-d以下)はシングル・ダブルクォーテーションの扱いが難しいので@でヒアドキュメントをパイプでつなげた。

結構雑な部分が多いけど出力できた。

3
1
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
3
1