====== sshguard ======
Alternative à fail2ban pour protéger son accès ssh à la bruteforce.
-> https://linuxiac.com/how-to-secure-ssh-server-with-sshguard/
apt install sshguard
Fichier de config par défaut ''/etc/sshguard/sshguard.conf''
#### REQUIRED CONFIGURATION ####
# Full path to backend executable (required, no default)
BACKEND="/usr/libexec/sshguard/sshg-fw-nft-sets"
# Shell command that provides logs on standard output. (optional, no default)
# Example 1: ssh and sendmail from systemd journal:
LOGREADER="LANG=C journalctl -afb -p info -n1 -o cat SYSLOG_FACILITY=4 SYSLOG_FACILITY=10"
#### OPTIONS ####
# Block attackers when their cumulative attack score exceeds THRESHOLD.
# Most attacks have a score of 10. (optional, default 30)
THRESHOLD=30
# Block attackers for initially BLOCK_TIME seconds after exceeding THRESHOLD.
# Subsequent blocks increase by a factor of 1.5. (optional, default 120)
BLOCK_TIME=120
# Remember potential attackers for up to DETECTION_TIME seconds before
# resetting their score. (optional, default 1800)
DETECTION_TIME=1800
# IP addresses listed in the WHITELIST_FILE are considered to be
# friendlies and will never be blocked.
WHITELIST_FILE=/etc/sshguard/whitelist
Explication des options
* ''BACKEND'' : Définit l'interface avec votre pare-feu. Ici, il utilise ''nftables'' avec des "sets" (très performant pour gérer de longues listes d'IP).
* ''LOGREADER'' : La source des logs. La commande ''journalctl'' surveille en temps réel les messages d'authentification (facility 4 pour auth, 10 pour authpriv).
* ''THRESHOLD'' (Seuil) : Le score cumulé avant le bannissement. Si une tentative échouée vaut 10, l'attaquant est banni à sa 3ème erreur ($3 \times 10 = 30$).
* ''BLOCK_TIME'' : Durée initiale du bannissement (en secondes).
* ''DETECTION_TIME'' : Fenêtre de temps durant laquelle sshguard mémorise les échecs d'une IP. Si l'attaquant attend plus de 1800s entre deux tentatives, son score repart à zéro.
* ''WHITELIST_FILE'' : Chemin vers le fichier contenant les IP (ou réseaux) à ne jamais bloquer.
Par défaut, dans cette config, sshguard utilise les commandes nftables pour bloquer les ips, voici comment voir ce qui a été bloqué
nft list table sshguard
table ip sshguard {
set attackers {
type ipv4_addr
flags interval
elements = { 45.78.219.211, 45.78.219.217,
45.78.227.4, 79.104.0.82,
103.86.180.10, 115.112.66.197,
154.91.170.52, 193.46.255.33,
193.46.255.99 }
}
chain blacklist {
type filter hook input priority filter - 10; policy accept;
ip saddr @attackers drop
}
}
Pour compter le nombre d'ip bloqué
nft list table sshguard | tr ',' '\n' | grep -cE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'
Alternative en utilisant ''jq'' et la sortie json
nft -j list table sshguard | jq '[.nftables[] | .set?.elem[]?] | length'
Sinon dans le journal
journalctl -xeu sshguard
Jan 09 14:45:20 my-vps sshguard[972514]: Attack from "115.112.66.197" on service SSH with danger 10.
Jan 09 14:45:22 my-vps sshguard[972514]: Attack from "115.112.66.197" on service SSH with danger 2.
Jan 09 14:45:22 my-vps sshguard[972514]: Attack from "115.112.66.197" on service SSH with danger 10.
Jan 09 14:45:23 my-vps sshguard[972514]: Attack from "45.78.227.4" on service SSH with danger 10.
Jan 09 14:45:24 my-vps sshguard[972514]: Attack from "115.112.66.197" on service SSH with danger 10.
Jan 09 14:45:24 my-vps sshguard[972514]: Blocking "115.112.66.197/32" for 604800 secs (4 attacks in 4 secs, after 1 abuses over 4 secs.)
Jan 09 14:45:24 my-vps sshguard[972514]: Attack from "45.78.227.4" on service SSH with danger 10.
Jan 09 14:45:26 my-vps sshguard[972514]: Attack from "45.78.227.4" on service SSH with danger 10.
Jan 09 14:45:26 my-vps sshguard[972514]: Blocking "45.78.227.4/32" for 604800 secs (3 attacks in 3 secs, after 1 abuses over 3 secs.)
Jan 09 14:46:06 my-vps sshguard[972514]: Attack from "45.78.219.217" on service SSH with danger 10.
Jan 09 14:46:08 my-vps sshguard[972514]: Attack from "45.78.219.217" on service SSH with danger 10.
Jan 09 14:46:09 my-vps sshguard[972514]: Attack from "45.78.219.217" on service SSH with danger 10.
==== Conseils d'optimisation ====
Pour renforcer la sécurité face aux bots modernes, voici les réglages recommandés :
Réduire le THRESHOLD : Passer à ''10'' ou ''20'' pour bannir dès la 1ère ou 2ème erreur. THRESHOLD=10
Augmenter le BLOCK_TIME : 120 secondes est insuffisant. Utilisez au moins 15 minutes pour décourager les tentatives répétées. BLOCK_TIME=900
Allonger le DETECTION_TIME : Augmenter la fenêtre de surveillance à 1 heure pour contrer les attaques lentes (low and slow). DETECTION_TIME=3600
Gestion de la Whitelist : Ajoutez impérativement vos IP de confiance dans ''/etc/sshguard/whitelist'' pour éviter l'auto-bannissement.
Vérification Backend : Assurez-vous que le service ''nftables'' est actif, car c'est lui qui exécute les règles de blocage.