316 lines
10 KiB
Bash
316 lines
10 KiB
Bash
#!/bin/bash
|
|
|
|
# Script d'audit des fichiers SUID et GUID
|
|
# Auteur: Zen6
|
|
# Date: $(date +%Y-%m-%d)
|
|
# Version: 1.0
|
|
|
|
# Configuration
|
|
LOG_DIR="/var/log/audit"
|
|
LOG_FILE="$LOG_DIR/suid_guid_audit_$(date +%Y%m%d_%H%M%S).log"
|
|
REPORT_FILE="$LOG_DIR/suid_guid_report_$(date +%Y%m%d_%H%M%S).txt"
|
|
ALERT_FILE="$LOG_DIR/suid_guid_alerts_$(date +%Y%m%d_%H%M%S).txt"
|
|
WHITELIST_FILE="/etc/audit/suid_whitelist.conf"
|
|
|
|
# Couleurs pour l'affichage
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Fonction de logging
|
|
log() {
|
|
echo -e "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
|
|
}
|
|
|
|
# Fonction d'affichage
|
|
print_header() {
|
|
echo -e "${BLUE}========================================${NC}"
|
|
echo -e "${BLUE}$1${NC}"
|
|
echo -e "${BLUE}========================================${NC}"
|
|
}
|
|
|
|
# Vérification des privilèges root
|
|
check_root() {
|
|
if [[ $EUID -ne 0 ]]; then
|
|
echo -e "${RED}Ce script doit être exécuté en tant que root${NC}"
|
|
echo "Utilisez: sudo $0"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Création des répertoires nécessaires
|
|
setup_directories() {
|
|
if [[ ! -d "$LOG_DIR" ]]; then
|
|
mkdir -p "$LOG_DIR"
|
|
chmod 750 "$LOG_DIR"
|
|
fi
|
|
}
|
|
|
|
# Création du fichier de whitelist par défaut
|
|
create_default_whitelist() {
|
|
if [[ ! -f "$WHITELIST_FILE" ]]; then
|
|
mkdir -p "$(dirname $WHITELIST_FILE)"
|
|
cat > "$WHITELIST_FILE" << EOF
|
|
# Liste blanche des binaires SUID/SGID légitimes
|
|
# Format: chemin/du/binaire [SUID|SGID|BOTH]
|
|
/bin/su SUID
|
|
/bin/ping SUID
|
|
/bin/mount SUID
|
|
/bin/umount SUID
|
|
/usr/bin/passwd SUID
|
|
/usr/bin/sudo SUID
|
|
/usr/bin/chsh SUID
|
|
/usr/bin/chfn SUID
|
|
/usr/bin/gpasswd SUID
|
|
/usr/bin/crontab SUID
|
|
/usr/bin/at SUID
|
|
/usr/bin/newgrp SUID
|
|
/usr/sbin/unix_chkpwd SUID
|
|
/usr/bin/wall SGID
|
|
/usr/bin/write SGID
|
|
/usr/bin/ssh-agent SGID
|
|
/usr/bin/locate SGID
|
|
/usr/bin/mlocate SGID
|
|
EOF
|
|
log "Fichier de whitelist créé: $WHITELIST_FILE"
|
|
fi
|
|
}
|
|
|
|
# Fonction pour charger la whitelist
|
|
load_whitelist() {
|
|
declare -gA WHITELIST_SUID
|
|
declare -gA WHITELIST_SGID
|
|
|
|
if [[ -f "$WHITELIST_FILE" ]]; then
|
|
while IFS= read -r line || [[ -n "$line" ]]; do
|
|
# Ignorer les commentaires et lignes vides
|
|
[[ "$line" =~ ^#.*$ ]] && continue
|
|
[[ -z "$line" ]] && continue
|
|
|
|
read -r path type <<< "$line"
|
|
if [[ -n "$path" ]]; then
|
|
case "$type" in
|
|
SUID) WHITELIST_SUID["$path"]=1 ;;
|
|
SGID) WHITELIST_SGID["$path"]=1 ;;
|
|
BOTH) WHITELIST_SUID["$path"]=1; WHITELIST_SGID["$path"]=1 ;;
|
|
esac
|
|
fi
|
|
done < "$WHITELIST_FILE"
|
|
fi
|
|
}
|
|
|
|
# Fonction principale d'audit
|
|
audit_files() {
|
|
local tmp_file="/tmp/suid_audit_$$.tmp"
|
|
|
|
print_header "AUDIT DES FICHIERS SUID ET GUID"
|
|
log "Début de l'audit des fichiers SUID/SGID"
|
|
|
|
# Recherche des fichiers SUID
|
|
log "Recherche des fichiers SUID..."
|
|
find / -type f -perm -4000 2>/dev/null | while read -r file; do
|
|
if [[ -f "$file" ]]; then
|
|
local perms=$(ls -l "$file" | awk '{print $1}')
|
|
local owner=$(ls -l "$file" | awk '{print $3}')
|
|
local size=$(ls -lh "$file" | awk '{print $5}')
|
|
local mtime=$(stat -c %y "$file" 2>/dev/null | cut -d. -f1)
|
|
|
|
# Vérification dans la whitelist
|
|
if [[ -z "${WHITELIST_SUID[$file]}" ]]; then
|
|
echo "$file|SUID|$owner|$perms|$size|$mtime" >> "$tmp_file"
|
|
echo -e "${RED}[ALERTE SUID]${NC} $file (propriétaire: $owner, permissions: $perms)"
|
|
log "ALERTE SUID: $file (propriétaire: $owner)"
|
|
else
|
|
echo -e "${GREEN}[OK SUID]${NC} $file (whitelisté)"
|
|
log "OK SUID: $file (whitelisté)"
|
|
fi
|
|
fi
|
|
done
|
|
|
|
# Recherche des fichiers SGID
|
|
log "Recherche des fichiers SGID..."
|
|
find / -type f -perm -2000 2>/dev/null | while read -r file; do
|
|
if [[ -f "$file" ]]; then
|
|
local perms=$(ls -l "$file" | awk '{print $1}')
|
|
local group=$(ls -l "$file" | awk '{print $4}')
|
|
local size=$(ls -lh "$file" | awk '{print $5}')
|
|
local mtime=$(stat -c %y "$file" 2>/dev/null | cut -d. -f1)
|
|
|
|
# Vérification dans la whitelist
|
|
if [[ -z "${WHITELIST_SGID[$file]}" ]]; then
|
|
echo "$file|SGID|$group|$perms|$size|$mtime" >> "$tmp_file"
|
|
echo -e "${RED}[ALERTE SGID]${NC} $file (groupe: $group, permissions: $perms)"
|
|
log "ALERTE SGID: $file (groupe: $group)"
|
|
else
|
|
echo -e "${GREEN}[OK SGID]${NC} $file (whitelisté)"
|
|
log "OK SGID: $file (whitelisté)"
|
|
fi
|
|
fi
|
|
done
|
|
|
|
# Génération du rapport
|
|
if [[ -f "$tmp_file" ]]; then
|
|
generate_report "$tmp_file"
|
|
rm -f "$tmp_file"
|
|
else
|
|
log "Aucune anomalie SUID/SGID détectée"
|
|
echo -e "${GREEN}Aucune anomalie SUID/SGID détectée${NC}"
|
|
fi
|
|
}
|
|
|
|
# Génération du rapport détaillé
|
|
generate_report() {
|
|
local tmp_file="$1"
|
|
|
|
{
|
|
echo "=========================================="
|
|
echo "RAPPORT D'AUDIT SUID/SGID"
|
|
echo "Date: $(date '+%Y-%m-%d %H:%M:%S')"
|
|
echo "Serveur: $(hostname)"
|
|
echo "=========================================="
|
|
echo ""
|
|
echo "ANOMALIES DÉTECTÉES:"
|
|
echo "--------------------"
|
|
|
|
local suid_count=0
|
|
local sgid_count=0
|
|
|
|
while IFS='|' read -r file type owner perms size mtime; do
|
|
if [[ "$type" == "SUID" ]]; then
|
|
((suid_count++))
|
|
echo ""
|
|
echo "[$suid_count] FICHIER SUID SUSPECT: $file"
|
|
echo " Propriétaire: $owner"
|
|
echo " Permissions: $perms"
|
|
echo " Taille: $size"
|
|
echo " Dernière modification: $mtime"
|
|
echo " Recommandation: Vérifier la légitimité de ce binaire"
|
|
elif [[ "$type" == "SGID" ]]; then
|
|
((sgid_count++))
|
|
echo ""
|
|
echo "[$sgid_count] FICHIER SGID SUSPECT: $file"
|
|
echo " Groupe: $owner"
|
|
echo " Permissions: $perms"
|
|
echo " Taille: $size"
|
|
echo " Dernière modification: $mtime"
|
|
echo " Recommandation: Vérifier la légitimité de ce binaire"
|
|
fi
|
|
done < "$tmp_file"
|
|
|
|
echo ""
|
|
echo "STATISTIQUES:"
|
|
echo "-------------"
|
|
echo "Total SUID suspects: $suid_count"
|
|
echo "Total SGID suspects: $sgid_count"
|
|
echo ""
|
|
echo "RECOMMANDATIONS:"
|
|
echo "----------------"
|
|
echo "1. Examinez chaque binaire suspect manuellement"
|
|
echo "2. Supprimez les bits SUID/SGID si non nécessaires: chmod u-s <fichier>"
|
|
echo "3. Ajoutez les binaires légitimes à la whitelist: $WHITELIST_FILE"
|
|
echo "4. Vérifiez l'intégrité des binaires système"
|
|
echo "5. Surveillez les changements régulièrement"
|
|
|
|
} > "$REPORT_FILE"
|
|
|
|
# Création du fichier d'alertes (uniquement les anomalies)
|
|
grep -E "^\[ALERTE" "$LOG_FILE" > "$ALERT_FILE" 2>/dev/null || true
|
|
|
|
log "Rapport généré: $REPORT_FILE"
|
|
log "Alertes sauvegardées: $ALERT_FILE"
|
|
}
|
|
|
|
# Fonction de vérification supplémentaire (fichiers dans /tmp, /var/tmp)
|
|
check_temp_directories() {
|
|
print_header "VÉRIFICATION DES RÉPERTOIRES TEMPORAIRES"
|
|
log "Vérification des répertoires /tmp et /var/tmp..."
|
|
|
|
for dir in /tmp /var/tmp /dev/shm; do
|
|
if [[ -d "$dir" ]]; then
|
|
echo -e "${YELLOW}Vérification de $dir:${NC}"
|
|
find "$dir" -type f \( -perm -4000 -o -perm -2000 \) 2>/dev/null | while read -r file; do
|
|
echo -e "${RED}[URGENT] Fichier suspect dans $dir: $file${NC}"
|
|
log "URGENT: Fichier SUID/SGID dans $dir: $file"
|
|
done
|
|
fi
|
|
done
|
|
}
|
|
|
|
# Fonction de vérification des fichiers sans propriétaire
|
|
check_orphan_files() {
|
|
print_header "FICHIERS SUID/SGID SANS PROPRIÉTAIRE"
|
|
log "Recherche des fichiers SUID/SGID sans propriétaire valide..."
|
|
|
|
find / -type f \( -perm -4000 -o -perm -2000 \) -nouser -o -nogroup 2>/dev/null | while read -r file; do
|
|
echo -e "${RED}[ORPHELIN] $file${NC}"
|
|
log "ORPHELIN: $file"
|
|
echo "$file" >> "$ALERT_FILE"
|
|
done
|
|
}
|
|
|
|
# Fonction de vérification des hash MD5 pour les binaires système
|
|
check_system_binaries() {
|
|
print_header "VÉRIFICATION DES BINAIRES SYSTÈME"
|
|
log "Vérification des binaires système critiques..."
|
|
|
|
local system_bins=("/bin/su" "/bin/ping" "/usr/bin/sudo" "/usr/bin/passwd")
|
|
|
|
for bin in "${system_bins[@]}"; do
|
|
if [[ -f "$bin" ]]; then
|
|
local md5=$(md5sum "$bin" 2>/dev/null | awk '{print $1}')
|
|
log "Hash MD5 de $bin: $md5"
|
|
echo "$bin: $md5" >> "$LOG_DIR/system_hashes_$(date +%Y%m%d).txt"
|
|
fi
|
|
done
|
|
}
|
|
|
|
# Fonction principale
|
|
main() {
|
|
echo -e "${BLUE}"
|
|
cat << "EOF"
|
|
╔═══════════════════════════════════════╗
|
|
║ SUID/SGID Security Audit Tool ║
|
|
║ Linux Privilege Scanner ║
|
|
╚═══════════════════════════════════════╝
|
|
EOF
|
|
echo -e "${NC}"
|
|
|
|
check_root
|
|
setup_directories
|
|
create_default_whitelist
|
|
load_whitelist
|
|
|
|
echo ""
|
|
audit_files
|
|
echo ""
|
|
check_temp_directories
|
|
echo ""
|
|
check_orphan_files
|
|
echo ""
|
|
check_system_binaries
|
|
|
|
print_header "RÉSUMÉ DE L'AUDIT"
|
|
echo -e "Log complet: ${GREEN}$LOG_FILE${NC}"
|
|
echo -e "Rapport détaillé: ${GREEN}$REPORT_FILE${NC}"
|
|
echo -e "Alertes: ${RED}$ALERT_FILE${NC}"
|
|
echo -e "Whitelist: ${YELLOW}$WHITELIST_FILE${NC}"
|
|
|
|
if [[ -s "$ALERT_FILE" ]]; then
|
|
echo -e "\n${RED}⚠️ ATTENTION: Des anomalies ont été détectées !${NC}"
|
|
echo "Consultez $ALERT_FILE pour plus de détails"
|
|
exit 1
|
|
else
|
|
echo -e "\n${GREEN}✓ Aucune anomalie détectée${NC}"
|
|
exit 0
|
|
fi
|
|
}
|
|
|
|
# Gestion des signaux
|
|
trap 'echo -e "\n${RED}Script interrompu par utilisateur${NC}"; exit 1' INT TERM
|
|
|
|
# Exécution
|
|
main "$@"
|
|
|