Ajouter README.md
This commit is contained in:
533
README.md
Normal file
533
README.md
Normal file
@@ -0,0 +1,533 @@
|
||||
# 🛡️ Gestionnaire de Bannissements Fail2ban
|
||||
|
||||
Script Bash complet pour gérer facilement les bannissements Fail2ban avec interface interactive et mode ligne de commande.
|
||||
|
||||
## 📋 Table des matières
|
||||
|
||||
- [Fonctionnalités](#fonctionnalités)
|
||||
- [Prérequis](#prérequis)
|
||||
- [Installation](#installation)
|
||||
- [Utilisation](#utilisation)
|
||||
- [Mode Interactif](#mode-interactif)
|
||||
- [Mode Ligne de Commande](#mode-ligne-de-commande)
|
||||
- [Exemples](#exemples)
|
||||
- [Captures d'écran](#captures-décran)
|
||||
- [Dépannage](#dépannage)
|
||||
- [Licence](#licence)
|
||||
|
||||
## ✨ Fonctionnalités
|
||||
|
||||
### Mode Interactif
|
||||
- 🎨 Interface colorée et intuitive
|
||||
- 📊 Affichage en temps réel des IPs bannies
|
||||
- 📈 Statistiques détaillées par jail
|
||||
- 🔄 Actualisation automatique
|
||||
- 🎯 Menu facile à naviguer
|
||||
|
||||
### Mode Ligne de Commande
|
||||
- ⚡ Actions rapides via arguments
|
||||
- 🤖 Compatible avec scripts et cron
|
||||
- 📝 Parfait pour l'automatisation
|
||||
- 🔧 Intégration CI/CD
|
||||
|
||||
### Opérations supportées
|
||||
- ✅ Lister toutes les IPs bannies
|
||||
- ✅ Lister les jails actives
|
||||
- ✅ Débannir une IP spécifique
|
||||
- ✅ Débannir toutes les IPs d'une jail
|
||||
- ✅ Débannir toutes les IPs de toutes les jails
|
||||
- ✅ Afficher les statistiques détaillées
|
||||
- ✅ Consulter les logs récents
|
||||
|
||||
## 🔧 Prérequis
|
||||
|
||||
- **OS**: Debian, Ubuntu ou dérivés
|
||||
- **Privilèges**: Root (sudo)
|
||||
- **Logiciels requis**:
|
||||
- `fail2ban` installé et actif
|
||||
- `bash` 4.0+
|
||||
- `systemctl`
|
||||
|
||||
### Vérification des prérequis
|
||||
|
||||
```bash
|
||||
# Vérifier Fail2ban
|
||||
sudo systemctl status fail2ban
|
||||
|
||||
# Vérifier la version de Bash
|
||||
bash --version
|
||||
```
|
||||
|
||||
## 📥 Installation
|
||||
|
||||
### Méthode 1: Téléchargement direct
|
||||
|
||||
```bash
|
||||
# Télécharger le script
|
||||
wget https://raw.githubusercontent.com/votre-repo/fail2ban-manager.sh
|
||||
|
||||
# Rendre exécutable
|
||||
chmod +x fail2ban-manager.sh
|
||||
|
||||
# Déplacer dans un répertoire du PATH (optionnel)
|
||||
sudo mv fail2ban-manager.sh /usr/local/bin/fail2ban-manager
|
||||
```
|
||||
|
||||
### Méthode 2: Copie manuelle
|
||||
|
||||
```bash
|
||||
# Créer le fichier
|
||||
sudo nano /usr/local/bin/fail2ban-manager
|
||||
|
||||
# Coller le contenu du script
|
||||
# Sauvegarder (Ctrl+O puis Ctrl+X)
|
||||
|
||||
# Rendre exécutable
|
||||
sudo chmod +x /usr/local/bin/fail2ban-manager
|
||||
```
|
||||
|
||||
### Méthode 3: Git
|
||||
|
||||
```bash
|
||||
# Cloner le dépôt
|
||||
git clone https://github.com/votre-repo/fail2ban-manager.git
|
||||
cd fail2ban-manager
|
||||
|
||||
# Installer
|
||||
sudo cp fail2ban-manager.sh /usr/local/bin/fail2ban-manager
|
||||
sudo chmod +x /usr/local/bin/fail2ban-manager
|
||||
```
|
||||
|
||||
## 🚀 Utilisation
|
||||
|
||||
### Mode Interactif
|
||||
|
||||
Lancez simplement le script sans arguments pour accéder au menu interactif :
|
||||
|
||||
```bash
|
||||
sudo fail2ban-manager
|
||||
```
|
||||
|
||||
**Menu disponible:**
|
||||
|
||||
```
|
||||
1. Lister toutes les IPs bannies
|
||||
2. Lister les jails actives
|
||||
3. Débannir une IP spécifique
|
||||
4. Débannir toutes les IPs d'une jail
|
||||
5. Débannir TOUTES les IPs
|
||||
6. Afficher les statistiques
|
||||
7. Voir les logs récents
|
||||
8. Actualiser
|
||||
0. Quitter
|
||||
```
|
||||
|
||||
### Mode Ligne de Commande
|
||||
|
||||
#### Afficher l'aide
|
||||
|
||||
```bash
|
||||
sudo fail2ban-manager --help
|
||||
```
|
||||
|
||||
#### Options disponibles
|
||||
|
||||
| Option | Alias | Description | Exemple |
|
||||
|--------|-------|-------------|---------|
|
||||
| `--list` | `-l` | Liste toutes les IPs bannies | `fail2ban-manager -l` |
|
||||
| `--jails` | `-j` | Liste les jails actives | `fail2ban-manager -j` |
|
||||
| `--unban <IP>` | `-u` | Débannit une IP spécifique | `fail2ban-manager -u 192.168.1.100` |
|
||||
| `--unban-jail <jail>` | `-U` | Débannit toutes les IPs d'une jail | `fail2ban-manager -U sshd` |
|
||||
| `--unban-all` | `-a` | Débannit TOUTES les IPs | `fail2ban-manager -a` |
|
||||
| `--stats` | `-s` | Affiche les statistiques | `fail2ban-manager -s` |
|
||||
| `--logs [jail] [lignes]` | `-L` | Affiche les logs | `fail2ban-manager -L sshd 50` |
|
||||
| `--interactive` | `-i` | Mode interactif | `fail2ban-manager -i` |
|
||||
| `--help` | `-h` | Affiche l'aide | `fail2ban-manager -h` |
|
||||
|
||||
## 📚 Exemples
|
||||
|
||||
### Exemple 1: Consultation rapide
|
||||
|
||||
```bash
|
||||
# Voir toutes les IPs bannies
|
||||
sudo fail2ban-manager --list
|
||||
|
||||
# Sortie:
|
||||
# [IPs actuellement bannies]
|
||||
#
|
||||
# Jail: sshd
|
||||
# • 192.168.1.100
|
||||
# • 203.0.113.50
|
||||
#
|
||||
# Jail: nginx-http-auth
|
||||
# • 198.51.100.25
|
||||
#
|
||||
# Total: 3 IP(s) bannie(s)
|
||||
```
|
||||
|
||||
### Exemple 2: Débannir une IP d'un faux positif
|
||||
|
||||
```bash
|
||||
# Un collègue s'est trompé de mot de passe plusieurs fois
|
||||
sudo fail2ban-manager --unban 192.168.1.150
|
||||
|
||||
# Sortie:
|
||||
# [Débannissement de 192.168.1.150]
|
||||
#
|
||||
# → Débannissement de 192.168.1.150 dans la jail sshd...
|
||||
# ✓ IP débannie de sshd
|
||||
#
|
||||
# ✓ IP 192.168.1.150 complètement débannie
|
||||
```
|
||||
|
||||
### Exemple 3: Maintenance après une attaque
|
||||
|
||||
```bash
|
||||
# Après avoir bloqué l'attaque, nettoyer toutes les IPs bannies
|
||||
sudo fail2ban-manager --unban-all
|
||||
|
||||
# Sortie:
|
||||
# [Débannissement de TOUTES les IPs]
|
||||
#
|
||||
# Êtes-vous sûr? (oui/non): oui
|
||||
#
|
||||
# Jail: sshd
|
||||
# → Débannissement de 192.168.1.100...
|
||||
# → Débannissement de 203.0.113.50...
|
||||
#
|
||||
# ✓ Total: 15 IP(s) débannie(s)
|
||||
```
|
||||
|
||||
### Exemple 4: Surveillance des tentatives
|
||||
|
||||
```bash
|
||||
# Afficher les statistiques
|
||||
sudo fail2ban-manager --stats
|
||||
|
||||
# Sortie:
|
||||
# [Statistiques Fail2ban]
|
||||
#
|
||||
# Jail Actuellement Total Tentatives
|
||||
# ──────────────────── ───────────── ──────── ───────────
|
||||
# sshd 5 127 23
|
||||
# nginx-http-auth 2 45 8
|
||||
# postfix-sasl 0 12 0
|
||||
```
|
||||
|
||||
### Exemple 5: Analyser les logs
|
||||
|
||||
```bash
|
||||
# Voir les 30 dernières lignes des logs SSH
|
||||
sudo fail2ban-manager --logs sshd 30
|
||||
|
||||
# Voir les logs d'une autre jail
|
||||
sudo fail2ban-manager --logs nginx-http-auth 50
|
||||
```
|
||||
|
||||
### Exemple 6: Automatisation avec cron
|
||||
|
||||
```bash
|
||||
# Débannir automatiquement toutes les IPs chaque nuit à 3h
|
||||
# Ajouter dans crontab -e:
|
||||
0 3 * * * /usr/local/bin/fail2ban-manager --unban-all <<< "oui" > /var/log/fail2ban-unban.log 2>&1
|
||||
|
||||
# Recevoir un rapport quotidien par email
|
||||
0 8 * * * /usr/local/bin/fail2ban-manager --stats | mail -s "Rapport Fail2ban" admin@example.com
|
||||
```
|
||||
|
||||
### Exemple 7: Script de monitoring
|
||||
|
||||
```bash
|
||||
#!/bin/bash
|
||||
# Script de monitoring des bannissements
|
||||
|
||||
# Compter les IPs bannies
|
||||
banned_count=$(sudo fail2ban-manager --list | grep "Total:" | awk '{print $2}')
|
||||
|
||||
# Alerter si plus de 10 IPs bannies
|
||||
if [ "$banned_count" -gt 10 ]; then
|
||||
echo "ALERTE: $banned_count IPs bannies!" | mail -s "Alerte Fail2ban" admin@example.com
|
||||
fi
|
||||
```
|
||||
|
||||
## 📸 Captures d'écran
|
||||
|
||||
### Vue principale (mode interactif)
|
||||
|
||||
```
|
||||
════════════════════════════════════════════════════════════════
|
||||
Gestionnaire de Bannissements Fail2ban
|
||||
════════════════════════════════════════════════════════════════
|
||||
|
||||
[Jails actives]
|
||||
|
||||
1. sshd - 3 IP(s) bannies (Total: 127)
|
||||
2. nginx-http-auth - 2 IP(s) bannies (Total: 45)
|
||||
3. postfix-sasl - 0 IP bannie (Total: 12)
|
||||
|
||||
[IPs actuellement bannies]
|
||||
|
||||
Jail: sshd
|
||||
• 192.168.1.100
|
||||
• 203.0.113.50
|
||||
• 198.51.100.25
|
||||
|
||||
Jail: nginx-http-auth
|
||||
• 192.0.2.10
|
||||
• 192.0.2.11
|
||||
|
||||
Total: 5 IP(s) bannie(s)
|
||||
|
||||
[Menu]
|
||||
|
||||
1. Lister toutes les IPs bannies
|
||||
2. Lister les jails actives
|
||||
3. Débannir une IP spécifique
|
||||
4. Débannir toutes les IPs d'une jail
|
||||
5. Débannir TOUTES les IPs
|
||||
6. Afficher les statistiques
|
||||
7. Voir les logs récents
|
||||
8. Actualiser
|
||||
0. Quitter
|
||||
|
||||
Votre choix:
|
||||
```
|
||||
|
||||
## 🔍 Dépannage
|
||||
|
||||
### Problème: "Fail2ban n'est pas installé"
|
||||
|
||||
**Solution:**
|
||||
```bash
|
||||
# Debian/Ubuntu
|
||||
sudo apt update
|
||||
sudo apt install fail2ban
|
||||
|
||||
# Vérifier l'installation
|
||||
sudo systemctl status fail2ban
|
||||
```
|
||||
|
||||
### Problème: "Fail2ban n'est pas actif"
|
||||
|
||||
**Solution:**
|
||||
```bash
|
||||
# Démarrer Fail2ban
|
||||
sudo systemctl start fail2ban
|
||||
|
||||
# Activer au démarrage
|
||||
sudo systemctl enable fail2ban
|
||||
```
|
||||
|
||||
### Problème: "Permission denied"
|
||||
|
||||
**Solution:**
|
||||
```bash
|
||||
# Le script nécessite les privilèges root
|
||||
sudo fail2ban-manager --list
|
||||
```
|
||||
|
||||
### Problème: Les couleurs ne s'affichent pas
|
||||
|
||||
**Solution:**
|
||||
```bash
|
||||
# Vérifier que votre terminal supporte les couleurs
|
||||
echo $TERM
|
||||
|
||||
# Si nécessaire, définir TERM
|
||||
export TERM=xterm-256color
|
||||
```
|
||||
|
||||
### Problème: Jail non trouvée
|
||||
|
||||
**Solution:**
|
||||
```bash
|
||||
# Lister les jails disponibles
|
||||
sudo fail2ban-manager --jails
|
||||
|
||||
# Vérifier la configuration Fail2ban
|
||||
sudo fail2ban-client status
|
||||
```
|
||||
|
||||
### Problème: Impossible de débannir une IP
|
||||
|
||||
**Solution:**
|
||||
```bash
|
||||
# Vérifier que l'IP est bien bannie
|
||||
sudo fail2ban-manager --list
|
||||
|
||||
# Débannir manuellement
|
||||
sudo fail2ban-client set sshd unbanip 192.168.1.100
|
||||
|
||||
# Vérifier les logs
|
||||
sudo tail -f /var/log/fail2ban.log
|
||||
```
|
||||
|
||||
## 🔐 Sécurité
|
||||
|
||||
### Bonnes pratiques
|
||||
|
||||
1. **Toujours vérifier avant de débannir en masse**
|
||||
```bash
|
||||
# D'abord lister
|
||||
sudo fail2ban-manager --list
|
||||
|
||||
# Puis décider
|
||||
sudo fail2ban-manager --unban-all
|
||||
```
|
||||
|
||||
2. **Conserver des logs**
|
||||
```bash
|
||||
# Logger toutes les actions
|
||||
sudo fail2ban-manager --unban-all 2>&1 | tee -a /var/log/fail2ban-actions.log
|
||||
```
|
||||
|
||||
3. **Limiter l'accès au script**
|
||||
```bash
|
||||
# Permissions restrictives
|
||||
sudo chmod 700 /usr/local/bin/fail2ban-manager
|
||||
sudo chown root:root /usr/local/bin/fail2ban-manager
|
||||
```
|
||||
|
||||
4. **Sauvegarder avant actions de masse**
|
||||
```bash
|
||||
# Exporter la liste avant débannissement
|
||||
sudo fail2ban-manager --list > /root/banned-ips-backup-$(date +%Y%m%d).txt
|
||||
```
|
||||
|
||||
## 📊 Intégration avec d'autres outils
|
||||
|
||||
### Monitoring avec Prometheus
|
||||
|
||||
```bash
|
||||
# Exporter les métriques
|
||||
banned_total=$(sudo fail2ban-manager --list | grep "Total:" | awk '{print $2}')
|
||||
echo "fail2ban_banned_ips $banned_total" > /var/lib/node_exporter/textfile/fail2ban.prom
|
||||
```
|
||||
|
||||
### Alertes avec Slack/Discord
|
||||
|
||||
```bash
|
||||
#!/bin/bash
|
||||
# Script d'alerte
|
||||
|
||||
WEBHOOK_URL="https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
|
||||
banned=$(sudo fail2ban-manager --list)
|
||||
|
||||
curl -X POST -H 'Content-type: application/json' \
|
||||
--data "{\"text\":\"Rapport Fail2ban:\n\`\`\`$banned\`\`\`\"}" \
|
||||
$WEBHOOK_URL
|
||||
```
|
||||
|
||||
### Dashboard avec Grafana
|
||||
|
||||
Combinez avec Elasticsearch et créez des visualisations des bannissements.
|
||||
|
||||
## 🛠️ Configuration avancée
|
||||
|
||||
### Alias pratiques
|
||||
|
||||
Ajoutez dans `~/.bashrc` ou `~/.zshrc`:
|
||||
|
||||
```bash
|
||||
# Alias Fail2ban
|
||||
alias f2b='sudo fail2ban-manager'
|
||||
alias f2b-list='sudo fail2ban-manager --list'
|
||||
alias f2b-stats='sudo fail2ban-manager --stats'
|
||||
alias f2b-unban='sudo fail2ban-manager --unban'
|
||||
```
|
||||
|
||||
### Fonction shell personnalisée
|
||||
|
||||
```bash
|
||||
# Débannir rapidement une IP
|
||||
unban() {
|
||||
if [ -z "$1" ]; then
|
||||
echo "Usage: unban <IP>"
|
||||
return 1
|
||||
fi
|
||||
sudo fail2ban-manager --unban "$1"
|
||||
}
|
||||
|
||||
# Utilisation: unban 192.168.1.100
|
||||
```
|
||||
|
||||
## 🤝 Contribution
|
||||
|
||||
Les contributions sont les bienvenues !
|
||||
|
||||
### Comment contribuer
|
||||
|
||||
1. Fork le projet
|
||||
2. Créer une branche (`git checkout -b feature/amelioration`)
|
||||
3. Commit les changements (`git commit -am 'Ajout fonctionnalité'`)
|
||||
4. Push vers la branche (`git push origin feature/amelioration`)
|
||||
5. Créer une Pull Request
|
||||
|
||||
### Idées d'amélioration
|
||||
|
||||
- [ ] Export des données en JSON/CSV
|
||||
- [ ] Intégration avec des bases de données de réputation IP
|
||||
- [ ] Notifications par email/webhook
|
||||
- [ ] Interface web
|
||||
- [ ] Support d'autres systèmes de bannissement (iptables direct, nftables)
|
||||
- [ ] Whitelist automatique d'IPs
|
||||
|
||||
## 📝 Changelog
|
||||
|
||||
### v1.0 (2024-12-06)
|
||||
- ✨ Version initiale
|
||||
- ✅ Mode interactif
|
||||
- ✅ Mode ligne de commande
|
||||
- ✅ Support multi-jails
|
||||
- ✅ Statistiques détaillées
|
||||
- ✅ Consultation des logs
|
||||
|
||||
## 📄 Licence
|
||||
|
||||
Ce script est distribué sous licence MIT.
|
||||
|
||||
```
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
```
|
||||
|
||||
## 👤 Auteur
|
||||
|
||||
Script créé pour faciliter la gestion quotidienne de Fail2ban.
|
||||
|
||||
## 🔗 Liens utiles
|
||||
|
||||
- [Documentation Fail2ban](https://www.fail2ban.org/wiki/index.php/Main_Page)
|
||||
- [GitHub Fail2ban](https://github.com/fail2ban/fail2ban)
|
||||
- [Tutoriel configuration Fail2ban](https://www.digitalocean.com/community/tutorials/how-to-protect-ssh-with-fail2ban-on-ubuntu)
|
||||
|
||||
## ⭐ Support
|
||||
|
||||
Si ce script vous a été utile, n'hésitez pas à :
|
||||
- ⭐ Star le projet
|
||||
- 🐛 Signaler des bugs
|
||||
- 💡 Proposer des améliorations
|
||||
- 📖 Améliorer la documentation
|
||||
|
||||
---
|
||||
|
||||
**Made with ❤️ for sysadmins**
|
||||
Reference in New Issue
Block a user