🔐 Audit sécurité Linux
Sudoers, SSH, ports ouverts, services, users récents
bashScript
Audit sécurité Linux
Script d'audit rapide pour vérifier les points de sécurité essentiels d'un serveur Linux (Debian/Ubuntu/RHEL).
Ce que le script vérifie
- Utilisateurs avec accès sudo
- Configuration SSH (PermitRootLogin, PasswordAuthentication)
- Ports ouverts en écoute
- Services actifs
- Comptes sans mot de passe
- Connexions récentes (auth.log)
- Mises à jour disponibles
- Règles firewall (ufw/iptables)
Script
#!/bin/bash
audit-securite-linux.sh — Audit sécurité rapide
Usage : sudo bash audit-securite-linux.sh
Testé sur : Debian 11/12, Ubuntu 20.04/22.04/24.04, RHEL 8/9
set -euo pipefail
RED='\033[0;31m'; YELLOW='\033[1;33m'; GREEN='\033[0;32m'; NC='\033[0m'; BOLD='\033[1m'
log_ok() { echo -e "${GREEN}[OK]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_err() { echo -e "${RED}[CRIT]${NC} $1"; }
log_info() { echo -e " $1"; }
section() { echo -e "\n${BOLD}═══ $1 ═══${NC}"; }
if [[ $EUID -ne 0 ]]; then echo "Lancer avec sudo"; exit 1; fi
echo -e "${BOLD}Audit sécurité Linux — $(hostname) — $(date '+%Y-%m-%d %H:%M')${NC}"
── OS & Kernel ──────────────────────────────────────────────────────────────
section "Système"
log_info "OS : $(cat /etc/os-release | grep PRETTY_NAME | cut -d'"' -f2)"
log_info "Kernel : $(uname -r)"
log_info "Uptime : $(uptime -p)"
── Utilisateurs sudo ────────────────────────────────────────────────────────
section "Utilisateurs sudo"
SUDO_USERS=$(grep -Po '^sudo.+:\K.*' /etc/group 2>/dev/null || getent group sudo wheel 2>/dev/null | cut -d: -f4)
if [[ -z "$SUDO_USERS" ]]; then
log_warn "Groupe sudo vide ou non trouvé"
else
log_info "Membres sudo/wheel : $SUDO_USERS"
fi
Comptes root (uid=0)
ROOT_ACCOUNTS=$(awk -F: '$3==0{print $1}' /etc/passwd)
for acc in $ROOT_ACCOUNTS; do
[[ "$acc" == "root" ]] && log_ok "root (uid=0)" || log_err "Compte avec uid=0 non-root : $acc"
done
Comptes sans mot de passe
NOPASS=$(awk -F: '($2=="" || $2=="!"){print $1}' /etc/shadow 2>/dev/null)
[[ -z "$NOPASS" ]] && log_ok "Aucun compte sans mot de passe" || log_err "Comptes sans mot de passe : $NOPASS"
── SSH ───────────────────────────────────────────────────────────────────────
section "Configuration SSH"
SSHD_CONF="/etc/ssh/sshd_config"
if [[ -f "$SSHD_CONF" ]]; then
check_ssh() {
local key=$1 expected=$2 label=$3
local val=$(grep -i "^$key" "$SSHD_CONF" 2>/dev/null | awk '{print $2}' | head -1)
val=${val:-"(défaut)"}
if [[ "$val" == "$expected" ]] || [[ -z "$val" && "$expected" == "no" ]]; then
log_ok "$label : $val"
else
log_err "$label : $val (attendu: $expected)"
fi
}
check_ssh "PermitRootLogin" "no" "PermitRootLogin"
check_ssh "PasswordAuthentication" "no" "PasswordAuthentication"
check_ssh "PermitEmptyPasswords" "no" "PermitEmptyPasswords"
check_ssh "X11Forwarding" "no" "X11Forwarding"
PORT=$(grep -i "^Port" "$SSHD_CONF" | awk '{print $2}' | head -1)
[[ "${PORT:-22}" == "22" ]] && log_warn "Port SSH standard 22 (envisager un changement)" || log_ok "Port SSH : $PORT"
else
log_warn "sshd_config non trouvé"
fi
── Ports en écoute ───────────────────────────────────────────────────────────
section "Ports en écoute"
ss -tlnp 2>/dev/null | tail -n +2 | while read line; do
log_info "$line"
done
── Firewall ──────────────────────────────────────────────────────────────────
section "Firewall"
if command -v ufw &>/dev/null; then
UFW_STATUS=$(ufw status 2>/dev/null | head -1)
[[ "$UFW_STATUS" == "active" ]] && log_ok "UFW actif" || log_warn "UFW inactif"
ufw status 2>/dev/null | grep -v "^Status" | grep -v "^$" | while read l; do log_info "$l"; done
elif command -v firewall-cmd &>/dev/null; then
firewall-cmd --state &>/dev/null && log_ok "firewalld actif" || log_warn "firewalld inactif"
else
IPTABLES=$(iptables -L INPUT --line-numbers 2>/dev/null | wc -l)
[[ $IPTABLES -gt 3 ]] && log_ok "iptables configuré ($IPTABLES règles INPUT)" || log_warn "Aucun firewall détecté (ufw/firewalld/iptables)"
fi
── Mises à jour ──────────────────────────────────────────────────────────────
section "Mises à jour"
if command -v apt &>/dev/null; then
apt-get update -qq 2>/dev/null
UPDATES=$(apt list --upgradable 2>/dev/null | grep -c upgradable || true)
SECURITY=$(apt list --upgradable 2>/dev/null | grep -c security || true)
[[ $UPDATES -eq 0 ]] && log_ok "Système à jour" || log_warn "$UPDATES mise(s) à jour disponible(s) dont $SECURITY sécurité"
elif command -v dnf &>/dev/null; then
UPDATES=$(dnf check-update 2>/dev/null | grep -c "^[a-zA-Z]" || true)
[[ $UPDATES -eq 0 ]] && log_ok "Système à jour" || log_warn "$UPDATES mise(s) à jour disponible(s)"
fi
── Connexions récentes ───────────────────────────────────────────────────────
section "Dernières connexions (10)"
last -n 10 2>/dev/null | while read l; do log_info "$l"; done
echo -e "\n${BOLD}Audit terminé — $(date '+%H:%M:%S')${NC}"
Utilisation
# Télécharger et exécuter
curl -O https://tools.rdr-it.com/scripts/audit-securite-linux.sh
chmod +x audit-securite-linux.sh
sudo ./audit-securite-linux.sh
Sauvegarder le rapport
sudo ./audit-securite-linux.sh 2>&1 | tee audit-$(hostname)-$(date +%Y%m%d).txt
🔧 Ouvrir tools.rdr-it.com — application complète →
Plus de 40 outils AdminSys gratuits · SSL · DNS · Docker · Nginx · SSH · Mermaid · et plus