diff --git a/ansible/install_fail2ban.sh b/ansible/install_fail2ban.sh index dc18b375b..ffe871e01 100755 --- a/ansible/install_fail2ban.sh +++ b/ansible/install_fail2ban.sh @@ -1,43 +1,32 @@ #!/bin/bash -# Demander les informations avec Zenity -USER_INPUT=$(zenity --forms --title="Configuration Fail2ban" \ - --text="Remplissez les informations pour l'installation" \ - --add-entry="Adresse IP du serveur" \ - --add-entry="Utilisateur SSH" \ - --add-entry="Mot de passe SSH" \ - --add-entry="Port SSH" \ - --add-entry="Webhook Mattermost" \ - --separator=",") - -# Vérifier si l'utilisateur a annulé -if [ $? -ne 0 ]; then - echo "Opération annulée." - exit 1 +# Vérifie si Zenity est installé, sinon l'installe +if ! command -v zenity &> /dev/null; then + echo "Zenity non trouvé, installation..." + sudo apt update && sudo apt install -y zenity fi -# Extraire les valeurs -IP_SERVER=$(echo "$USER_INPUT" | cut -d',' -f1) -SSH_USER=$(echo "$USER_INPUT" | cut -d',' -f2) -SSH_PASS=$(echo "$USER_INPUT" | cut -d',' -f3) -SSH_PORT=$(echo "$USER_INPUT" | cut -d',' -f4) -MATTERMOST_WEBHOOK=$(echo "$USER_INPUT" | cut -d',' -f5) +# Demande les infos via Zenity +IP_SERVER=$(zenity --entry --title "Configuration Serveur" --text "Entrez l'adresse IP du serveur :") +SSH_USER=$(zenity --entry --title "Configuration Serveur" --text "Entrez l'utilisateur SSH :") +SSH_PASS=$(zenity --password --title "Configuration Serveur" --text "Entrez le mot de passe SSH :") +SSH_PORT=$(zenity --entry --title "Configuration Serveur" --text "Entrez le port SSH (ex: 22) :" --entry-text "22") +MATTERMOST_WEBHOOK=$(zenity --entry --title "Mattermost" --text "Entrez l'URL du webhook Mattermost :") -# Vérifier que toutes les valeurs sont renseignées +# Vérifie si les variables sont vides if [[ -z "$IP_SERVER" || -z "$SSH_USER" || -z "$SSH_PASS" || -z "$SSH_PORT" || -z "$MATTERMOST_WEBHOOK" ]]; then - zenity --error --text="Toutes les informations doivent être remplies !" --width=300 + zenity --error --title "Erreur" --text "Toutes les informations sont requises !" exit 1 fi -# Générer un fichier d'inventaire dynamique +# Crée le fichier d'inventaire dynamique cat > inventory.ini < inventory.ini < après trop d'échecs SSH 🚨"}" {{ mattermost_webhook }} + actionunban = + notify: Restart Fail2ban handlers: - name: Restart Fail2ban systemd: name: fail2ban state: restarted + diff --git a/ansible/playbooks/fail2ban.yml.BACKUP b/ansible/playbooks/fail2ban.yml.BACKUP new file mode 100644 index 000000000..d457d52af --- /dev/null +++ b/ansible/playbooks/fail2ban.yml.BACKUP @@ -0,0 +1,49 @@ +--- +- name: Install and configure Fail2ban with Mattermost notifications + hosts: servers + become: yes + vars: + ssh_port: "{{ ssh_port }}" + ban_time: 1800 + max_retry: 3 + find_time: 600 + mattermost_webhook: "{{ mattermost_webhook }}" + + tasks: + - name: Install Fail2ban + apt: + name: fail2ban + state: present + update_cache: yes + + - name: Configure Fail2ban jail.local + copy: + dest: /etc/fail2ban/jail.local + content: | + [sshd] + enabled = true + port = {{ ssh_port }} + logpath = /var/log/auth.log + maxretry = 3 + bantime = 1800 + action = iptables-multiport[name=SSH, port={{ ssh_port }}, protocol=tcp] + mattermost + notify: + - Restart Fail2ban + + - name: Create Mattermost action file + copy: + dest: /etc/fail2ban/action.d/mattermost.conf + content: | + [Definition] + actionstart = + actionstop = + actionban = curl -X POST -H 'Content-Type: application/json' --data '{"text": "🚨 Fail2ban a banni après trop d'échecs SSH 🚨"}' {{ mattermost_webhook }} + actionunban = + notify: Restart Fail2ban + + handlers: + - name: Restart Fail2ban + systemd: + name: fail2ban + state: restarted