diff --git a/ssh_key_checks b/ssh_key_checks index 10c474c..c1802cc 100644 --- a/ssh_key_checks +++ b/ssh_key_checks @@ -1,23 +1,24 @@ -#!/bin/bash -# Se debe ejecutar con usuario root por el tema de permisos del archivo authorized_keys -# Obtener configuración de authorized_keys desde sshd_config -AUTH_KEYS_PATH=$(grep -E "^\s*AuthorizedKeysFile" /etc/ssh/sshd_config | awk '{print $2}') +#!/bin/bash +# Se debe ejecutar como root para poder leer archivos protegidos + +# Obtener rutas de `AuthorizedKeysFile` desde sshd_config +AUTH_KEYS_PATHS=$(grep -E "^\s*AuthorizedKeysFile" /etc/ssh/sshd_config | awk '{$1=""; print $0}') # Si no está definido, usar el valor por defecto -if [[ -z "$AUTH_KEYS_PATH" ]]; then - AUTH_KEYS_PATH=".ssh/authorized_keys" +if [[ -z "$AUTH_KEYS_PATHS" ]]; then + AUTH_KEYS_PATHS=".ssh/authorized_keys" fi -echo "Ruta configurada para authorized_keys: $AUTH_KEYS_PATH" +echo "🔍 Rutas configuradas en sshd_config: $AUTH_KEYS_PATHS" echo "------------------------------------------------------------" -printf "%-20s %-40s %-10s %-15s %-10s\n" "Usuario" "Ruta authorized_keys" "Permisos" "SSH Habilitado" "Llaves" +printf "%-20s %-50s %-10s %-15s %-10s\n" "Usuario" "Ruta authorized_keys" "Permisos" "SSH Habilitado" "Llaves" echo "------------------------------------------------------------" # Variables para almacenar usuarios con llaves USERS_WITH_KEYS=() -# Obtener usuarios permitidos y denegados en sshd_config +# Obtener listas de usuarios permitidos/denegados en sshd_config ALLOW_USERS=$(grep -E "^\s*AllowUsers" /etc/ssh/sshd_config | awk '{$1=""; print $0}') DENY_USERS=$(grep -E "^\s*DenyUsers" /etc/ssh/sshd_config | awk '{$1=""; print $0}') ALLOW_GROUPS=$(grep -E "^\s*AllowGroups" /etc/ssh/sshd_config | awk '{$1=""; print $0}') @@ -25,27 +26,39 @@ DENY_GROUPS=$(grep -E "^\s*DenyGroups" /etc/ssh/sshd_config | awk '{$1=""; print # Leer usuarios del sistema while IFS=: read -r username _ _ _ _ homedir _; do - # Omitir usuarios sin home válido o con home en lugares extraños + # Omitir usuarios sin home válido o del sistema if [[ ! -d "$homedir" || "$homedir" =~ ^(/bin|/sbin|/usr|/var|/proc|/sys|/dev|/run|/nonexistent) ]]; then continue fi - # Determinar la ruta real de authorized_keys - user_keys_path="$homedir/$AUTH_KEYS_PATH" + user_has_keys=0 + user_keys_paths=() - # Verificar si el archivo existe - if [[ -f "$user_keys_path" ]]; then - permisos=$(stat -c "%a" "$user_keys_path") - num_llaves=$(grep -c "ssh-" "$user_keys_path") + # Revisar todas las rutas configuradas + for path in $AUTH_KEYS_PATHS; do + # Reemplazar %u con el nombre de usuario en las rutas personalizadas + expanded_path="${path//%u/$username}" - # Guardar usuario en lista si tiene llaves - if [[ $num_llaves -gt 0 ]]; then - USERS_WITH_KEYS+=("$username:$user_keys_path") + # Si la ruta es relativa, asumir que está dentro del home del usuario + if [[ "$expanded_path" != /* ]]; then + expanded_path="$homedir/$expanded_path" fi - else - permisos="No existe" - num_llaves=0 - fi + + # Verificar si el archivo existe + if [[ -f "$expanded_path" ]]; then + permisos=$(stat -c "%a" "$expanded_path") + num_llaves=$(grep -c "ssh-" "$expanded_path") + + # Si tiene llaves, agregar a la lista + if [[ $num_llaves -gt 0 ]]; then + user_has_keys=1 + user_keys_paths+=("$expanded_path") + fi + else + permisos="No existe" + num_llaves=0 + fi + done # Determinar si el usuario puede hacer SSH ssh_habilitado="Sí" @@ -77,7 +90,12 @@ while IFS=: read -r username _ _ _ _ homedir _; do fi # Mostrar en pantalla - printf "%-20s %-40s %-10s %-15s %-10s\n" "$username" "$user_keys_path" "$permisos" "$ssh_habilitado" "$num_llaves" + printf "%-20s %-50s %-10s %-15s %-10s\n" "$username" "${user_keys_paths[*]:-No existe}" "$permisos" "$ssh_habilitado" "$num_llaves" + + # Guardar usuario con llaves + if [[ $user_has_keys -eq 1 ]]; then + USERS_WITH_KEYS+=("$username:${user_keys_paths[*]}") + fi done < /etc/passwd @@ -91,12 +109,15 @@ if [[ ${#USERS_WITH_KEYS[@]} -gt 0 ]]; then for user_data in "${USERS_WITH_KEYS[@]}"; do username="${user_data%%:*}" - key_path="${user_data##*:}" + key_paths="${user_data##*:}" echo "👤 Usuario: $username" - echo "📁 Archivo: $key_path" + echo "📁 Archivos: $key_paths" echo "------------------------------------------------------------" - cat "$key_path" + + for key_path in $key_paths; do + [[ -f "$key_path" ]] && cat "$key_path" + done echo "" done else