Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
| linuxaddict:administrer:securite [24/04/2018, 18:49] – Claude Clerc | linuxaddict:administrer:securite [10/02/2024, 19:09] (Version actuelle) – Claude Clerc | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | {{tag> | + | {{tag> |
| ====== Sécuriser un serveur Linux ====== | ====== Sécuriser un serveur Linux ====== | ||
| + | |||
| + | ===== Commandes (très) utiles ===== | ||
| + | |||
| + | ==== Quels sont les ports ouverts ? ==== | ||
| + | nmap -v localhost | ||
| + | Cette liste de ports ouverts sera nécessaire pour concevoir le firewall. | ||
| + | |||
| + | ==== Quel programme utilise un port donné ? ==== | ||
| + | lsof -i tcp: | ||
| + | Une liste de commandes, avec n° PID, utilisateur, | ||
| + | |||
| + | ==== Que fait le programme de PID donné ? ==== | ||
| + | lsof -p NUMERO_PID | ||
| + | Les dernières lignes de cette longue liste permettent de déterminer précisément ce que fait le programme. | ||
| + | |||
| + | Pour obtenir le PID (//Process IDentifier// | ||
| + | ps auxwww | ||
| + | |||
| + | Par exemple, pour trouver le PID de geany (qui est affiché à l' | ||
| + | ps auxwww | grep geany | ||
| + | retourne : | ||
| + | <nom d' | ||
| + | Le PID de geany est donc actuellement de 4967. Il changera à chaque lancement de geany. | ||
| + | |||
| + | ==== Quels sont les paquets concernés par ce programme ? ==== | ||
| + | dpkg -S UNE_PARTIE_DU_NOM_DU_PROGRAMME | ||
| + | |||
| + | ===== Firewall ===== | ||
| + | <sxh bash> | ||
| + | #!/bin/sh | ||
| + | ### Firewall ### | ||
| + | |||
| + | case " | ||
| + | |||
| + | start|restart|reload|force-reload) | ||
| + | # Réinitialise les règles | ||
| + | iptables -t filter -F | ||
| + | iptables -t filter -X | ||
| + | |||
| + | # Bloque tout le trafic | ||
| + | iptables -t filter -P INPUT DROP | ||
| + | iptables -t filter -P FORWARD DROP | ||
| + | iptables -t filter -P OUTPUT DROP | ||
| + | |||
| + | # Autorise toute connexion avec l'IP VOTRE_IP_PUBLIQUE_PERSO | ||
| + | iptables -t filter -A INPUT -s VOTRE_IP_PUBLIQUE_PERSO -j ACCEPT | ||
| + | iptables -t filter -A OUTPUT -d VOTRE_IP_PUBLIQUE_PERSO -j ACCEPT | ||
| + | |||
| + | # Limiter les risques de flood (limite à 1 connexion par seconde) | ||
| + | iptables -A FORWARD -p tcp --syn -m limit --limit 1/second -j ACCEPT | ||
| + | iptables -A FORWARD -p udp -m limit --limit 1/second -j ACCEPT | ||
| + | iptables -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 1/second -j ACCEPT | ||
| + | |||
| + | # Autorise les connexions déjà établies et localhost | ||
| + | iptables -A INPUT -m state --state RELATED, | ||
| + | iptables -A OUTPUT -m state --state RELATED, | ||
| + | iptables -t filter -A INPUT -i lo -j ACCEPT | ||
| + | iptables -t filter -A OUTPUT -o lo -j ACCEPT | ||
| + | |||
| + | # ICMP (Ping) On autorise le ping | ||
| + | iptables -t filter -A INPUT -p icmp -j ACCEPT | ||
| + | iptables -t filter -A OUTPUT -p icmp -j ACCEPT | ||
| + | |||
| + | # SSH (port 22) | ||
| + | iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT | ||
| + | iptables -t filter -A OUTPUT -p tcp --dport 22 -j ACCEPT | ||
| + | |||
| + | # DNS sortant | ||
| + | iptables -t filter -A OUTPUT -p tcp --dport 53 -j ACCEPT | ||
| + | iptables -t filter -A OUTPUT -p udp --dport 53 -j ACCEPT | ||
| + | # DNS entrant | ||
| + | #iptables -t filter -A INPUT -p tcp --dport 53 -j ACCEPT | ||
| + | #iptables -t filter -A INPUT -p udp --dport 53 -j ACCEPT | ||
| + | |||
| + | # HTTP | ||
| + | iptables -t filter -A OUTPUT -p tcp --dport 80 -j ACCEPT | ||
| + | iptables -t filter -A INPUT -p tcp --dport 80 -j ACCEPT | ||
| + | |||
| + | # HTTPS | ||
| + | iptables -t filter -A OUTPUT -p tcp --dport 443 -j ACCEPT | ||
| + | iptables -t filter -A INPUT -p tcp --dport 443 -j ACCEPT | ||
| + | |||
| + | # MySQL | ||
| + | iptables -t filter -A OUTPUT -p tcp --dport 3306 -j ACCEPT | ||
| + | #iptables -t filter -A INPUT -p tcp --dport 3306 -j ACCEPT | ||
| + | iptables -t filter -A OUTPUT -p udp --dport 3306 -j ACCEPT | ||
| + | #iptables -t filter -A INPUT -p udp --dport 3306 -j ACCEPT | ||
| + | |||
| + | # MySQL-proxy | ||
| + | #iptables -t filter -A OUTPUT -p tcp --dport 6446 -j ACCEPT | ||
| + | #iptables -t filter -A INPUT -p tcp --dport 6446 -j ACCEPT | ||
| + | #iptables -t filter -A OUTPUT -p udp --dport 6446 -j ACCEPT | ||
| + | #iptables -t filter -A INPUT -p udp --dport 6446 -j ACCEPT | ||
| + | |||
| + | # FTP | ||
| + | #iptables -t filter -A OUTPUT -p tcp --dport 20:21 -j ACCEPT | ||
| + | #iptables -t filter -A INPUT -p tcp --dport 20:21 -j ACCEPT | ||
| + | |||
| + | # Mail SMTP | ||
| + | #iptables -t filter -A INPUT -p tcp --dport 25 -j ACCEPT | ||
| + | iptables -t filter -A OUTPUT -p tcp --dport 25 -j ACCEPT | ||
| + | |||
| + | # Mail sSMTP | ||
| + | #iptables -t filter -A INPUT -p tcp --dport 465 -j ACCEPT | ||
| + | iptables -t filter -A OUTPUT -p tcp --dport 465 -j ACCEPT | ||
| + | #iptables -t filter -A INPUT -p tcp --dport 587 -j ACCEPT | ||
| + | iptables -t filter -A OUTPUT -p tcp --dport 587 -j ACCEPT | ||
| + | |||
| + | # Mail POP3 | ||
| + | #iptables -t filter -A INPUT -p tcp --dport 110 -j ACCEPT | ||
| + | #iptables -t filter -A OUTPUT -p tcp --dport 110 -j ACCEPT | ||
| + | |||
| + | # Mail sPOP3 | ||
| + | #iptables -t filter -A INPUT -p tcp --dport 995 -j ACCEPT | ||
| + | #iptables -t filter -A OUTPUT -p tcp --dport 995 -j ACCEPT | ||
| + | |||
| + | # Mail IMAP | ||
| + | #iptables -t filter -A INPUT -p tcp --dport 143 -j ACCEPT | ||
| + | #iptables -t filter -A OUTPUT -p tcp --dport 143 -j ACCEPT | ||
| + | |||
| + | # Mail sIMAP | ||
| + | #iptables -t filter -A INPUT -p tcp --dport 993 -j ACCEPT | ||
| + | #iptables -t filter -A OUTPUT -p tcp --dport 993 -j ACCEPT | ||
| + | |||
| + | # NTP (horloge du serveur) | ||
| + | iptables -t filter -A OUTPUT -p udp --dport 123 -j ACCEPT | ||
| + | ;; | ||
| + | |||
| + | stop) | ||
| + | # Réinitialise les règles | ||
| + | iptables -t filter -F | ||
| + | iptables -t filter -X | ||
| + | |||
| + | # Autorise tout le trafic | ||
| + | iptables -t filter -P INPUT ACCEPT | ||
| + | iptables -t filter -P FORWARD ACCEPT | ||
| + | iptables -t filter -P OUTPUT ACCEPT | ||
| + | ;; | ||
| + | |||
| + | status) | ||
| + | RES=$(iptables -L | grep INPUT | awk ' | ||
| + | [ " | ||
| + | ;; | ||
| + | |||
| + | *) echo " | ||
| + | ;; | ||
| + | esac | ||
| + | exit 0 | ||
| + | |||
| + | </ | ||
| ===== Fail2ban ===== | ===== Fail2ban ===== | ||
| * [[https:// | * [[https:// | ||
| + | ==== Fignolage ==== | ||
| + | |||
| + | Après installation de fail2ban et paramétrage pour sshd : | ||
| + | |||
| + | Se connecter en root. | ||
| + | |||
| + | Installer, si ce n'est déjà fait le paquet '' | ||
| + | apt install util-linux | ||
| + | |||
| + | Créer le fichier ''/ | ||
| + | <sxh bash> | ||
| + | #!/bin/bash | ||
| + | |||
| + | for ip in $(lastb -i -s -5min | awk ' | ||
| + | if [[ $ip =~ ^[0-9] ]]; then | ||
| + | #echo $ip | ||
| + | / | ||
| + | fi | ||
| + | }; done | ||
| + | exit 0 | ||
| + | </ | ||
| + | Le rendre exécutable : | ||
| + | chmod +x / | ||
| + | |||
| + | Éditer la crontab : | ||
| + | crontab -e | ||
| + | | ||
| + | Ajouter les lignes : | ||
| + | # Bye bye importuns ! | ||
| + | */5 * * * * / | ||
| + | |||
| + | Quitter. La crontab se met en place. | ||
| + | |||
| + | Lister les IP bloquées (et leur nombre) avec : | ||
| + | fail2ban-client status sshd | ||
| ===== Liens ===== | ===== Liens ===== | ||
| * [[https:// | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| + | ---- | ||
| + | |||
| + | {{counter|today| personne a visité cette page aujourd' | ||