Fail2ban et notifications Telegram

Salut, lors d’une ré-install j’ai galéré à retrouver la source donc je fais une petite sauvegarde ici.

Je pars du principe que Fail2ban est correctement installé ainsi que votre bot telegram.

On va commencer par copier le fichier de conf « jail » afin de ne pas trop modifier une conf qui marche :
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
Le fichier « jail.local » va prendre le dessus sur « jail.conf », concrètement cela veut dire qu’en cas de paramètres qui se contredisent Fail2ban va choisir ceux du fichier « jail.local ».

Ensuite on édite le fichier « jail.local » et a chaque module où l’on souhaite des notifications telegram on ajoute une ligne « action ». Exemple avec ssh :
[sshd]
port = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s
action = telegram

Cette ligne signifie qu’à chaque action en rapport avec sshd Fail2ban va appeler le fichier de conf telegram présent dans le dossier « /etc/fail2ban/action.d/ ».

Du coup on va créer ce fameux fichier :
sudo nano /etc/fail2ban/action.d/telegram.conf
# Fail2Ban configuration file
#
# Author: MushaGH
#
#
[Definition]
# Option: actionstart
# Notes.: command executed once at the start of Fail2Ban.
# Values: CMD
#
actionstart = /etc/fail2ban/scripts/fail2ban-telegram.sh start
# Option: actionstop
# Notes.: command executed once at the end of Fail2Ban
# Values: CMD
#
actionstop = /etc/fail2ban/scripts/fail2ban-telegram.sh stop
# Option: actioncheck
# Notes.: command executed once before each actionban command
# Values: CMD
#
actioncheck =
# Option: actionban
# Notes.: command executed when banning an IP. Take care that the
# command is executed with Fail2Ban user rights.
# Tags: See jail.conf(5) man page
# Values: CMD
#
actionban = /etc/fail2ban/scripts/fail2ban-telegram.sh ban
# Option: actionunban
# Notes.: command executed when unbanning an IP. Take care that the
# command is executed with Fail2Ban user rights.
# Tags: See jail.conf(5) man page
# Values: CMD
#
actionunban = /etc/fail2ban/scripts/fail2ban-telegram.sh unban
#
[Init]
init = 123

Comme on peut le voir, les actions appellent un script que l’on va donc créer sans oublier de renseigner le token telegram et l’id de la conversation :
sudo mkdir /etc/fail2ban/scripts/
sudo nano /etc/fail2ban/scripts/fail2ban-telegram.sh
#!/bin/bash
#
# Sends text messages using Telegram
# to alert webmaster of banning.
#
# Require one argument, one of the following
# start
# stop
# ban
# unban
# Optional second argument: Ip for ban/unband
#
#
# Display usage information
function show_usage {
echo "Usage: $0 action "
echo "Where action start, stop, ban, unban"
echo "and IP is optional passed to ban, unban"
exit
}
#
#
# Send notification
function send_msg {
apiToken=<put your api key here>
chatId=<put your chat id here>

url="https://api.telegram.org/bot$apiToken/sendMessage"
#

curl -s -X POST $url -d chat_id=$chatId -d text="$1"
exit
}
#
#
# Check for script arguments
if [ $# -lt 1 ]
then
show_usage
fi
#
#
# Take action depending on argument
if [ "$1" = 'start' ]
then
msg='Fail2ban+just+started.'
send_msg $msg
elif [ "$1" = 'stop' ]
then
msg='Fail2ban+just+stoped.'
send_msg $msg
elif [ "$1" = 'ban' ]
then
msg=$([ "$2" != '' ] && echo "Fail2ban+just+banned+$2" || echo 'Fail2ban+just+banned+an+ip.' )
send_msg $msg
elif [ "$1" = 'unban' ]
then
msg=$([ "$2" != '' ] && echo "Fail2ban+just+unbanned+$2" || echo "Fail2ban+just+unbanned+an+ip." )
send_msg $msg
else
show_usage
fi

Puis on rend le script exécutable :
sudo chmod +x /etc/fail2ban/scripts/fail2ban-telegram.sh

Pour finir on relance le service :
sudo service fail2ban restart
Et là normalement un joli message devrait arriver annonçant que le service démarre !


Taggé , , , , , , .Mettre en favori le Permaliens.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée.

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.