# đŸ›Ąïž 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 ` | `-u` | DĂ©bannit une IP spĂ©cifique | `fail2ban-manager -u 192.168.1.100` | | `--unban-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 " 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**