
Si no tenéis fail2ban, la instalación es realmente simple:
apt-get install fail2ban
Para que fail2ban empiece a funcionar no hay que hacer realmente nada, la configuración por defecto para SSH es que tras 5 intentos fallidos de inicio de sesión la IP sea baneada durante 10 minutos. Ejemplo de mi VM Kali baneada en mi Raspberry:

Cada vez que se produzca un intento de ataque de fuerza bruta, o que básicamente se banee una IP en nuestro sistema podemos enviar una alerta a través de un bot de Telegram, para ello configuraremos el fichero /etc/fail2ban/jail.conf, en este caso lo haremos con SSH, así que buscamos: [sshd] y añadimos las lineas 10 y 11 que se ven a continuación:
[sshd] # To use more aggressive sshd modes set filter parameter "mode" in jail.local: # normal (default), ddos, extra or aggressive (combines all). # See "tests/files/logs/sshd" or "filter.d/sshd.conf" for usage example and details. #mode = normal port = ssh logpath = %(sshd_log)s backend = %(sshd_backend)s action = iptables[name=SSH, port=22, protocol=tcp] telegram
Asegurados que “telegram” esta tabulado, si no dará un fallo.
Ahora creamos el fichero /etc/fail2ban/action.d/telegram.conf con el siguiente contenido:
[Definition] actionstart = /etc/fail2ban/scripts/send_telegram_notif.sh -a start actionstop = /etc/fail2ban/scripts/send_telegram_notif.sh -a stop actioncheck = actionban = /etc/fail2ban/scripts/send_telegram_notif.sh -b <ip> actionunban = /etc/fail2ban/scripts/send_telegram_notif.sh -u <ip> [Init] init = 123
Este fichero es el que se lee cuando se genera una alerta en SSH y como veis llama un script, este pequeño script lo podréis encontrar en mi GitHub como siempre, pero os lo dejo por aquí también, aunque tener en cuenta que en mi GitHub siempre estará actualizado:
#!/bin/bash # Version 1.0 # Send Fail2ban notifications using a Telegram Bot # Add to the /etc/fail2ban/jail.conf: # [sshd] # *** # action = iptables[name=SSH, port=22, protocol=tcp] # telegram # Create a new file in /etc/fail2ban/action.d with the following information: # [Definition] # actionstart = /etc/fail2ban/scripts/send_telegram_notif.sh -a start # actionstop = /etc/fail2ban/scripts/send_telegram_notif.sh -a stop # actioncheck = # actionban = /etc/fail2ban/scripts/send_telegram_notif.sh -b <ip> # actionunban = /etc/fail2ban/scripts/send_telegram_notif.sh -u <ip> # # [Init] # init = 123 # Telegram BOT Token telegramBotToken='YOUR_BOT_TOKEN' # Telegram Chat ID (must include the dash (-23232323) telegramChatID='YOUR_CHAT_ID' function talkToBot() { message=$1 curl -s -X POST https://api.telegram.org/bot${telegramBotToken}/sendMessage -d text="${message}" -d chat_id=${telegramChatID} > /dev/null 2>&1 } if [ $# -eq 0 ]; then echo "Usage $0 -a ( start || stop ) || -b \$IP || -u \$IP" exit 1; fi while getopts "a:b:u:" opt; do case "$opt" in a) action=$OPTARG ;; b) ban=y ip_add_ban=$OPTARG ;; u) unban=y ip_add_unban=$OPTARG ;; \?) echo "Invalid option. -$OPTARG" exit 1 ;; esac done if [[ ! -z ${action} ]]; then case "${action}" in start) talkToBot "Fail2ban has been started" ;; stop) talkToBot "Fail2ban has been stopped" ;; *) echo "Incorrect option" exit 1; ;; esac elif [[ ${ban} == "y" ]]; then talkToBot "The IP: ${ip_add_ban} has been banned" exit 0; elif [[ ${unban} == "y" ]]; then talkToBot "The IP: ${ip_add_unban} has been unbanned" exit 0; else info fi
Tened en cuenta que el script debe tener permisos de ejecución:
chmod +x /etc/fail2ban/scripts/send_telegram_notif.sh
Tras esto, reiniciar el servicio fail2ban:
systemctl restart fail2ban
POC
Realicé varios intentos de inicio de sesión fallidos dese una VM:

Estos intentos fallidos y el momento en el que se banea la IP pueden ser detallados en el fichero de logs de fail2ban /var/log/fail2ban.log:

Y estos son los mensajes que he recibido en mi Chat de Telegram:

Y eso es todo, manteneos atentos 😀