Update ssh_key_checks
This commit is contained in:
parent
8b83495925
commit
76d1c91cf9
@ -1,20 +1,14 @@
|
||||
|
||||
#!/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_PATHS" ]]; then
|
||||
AUTH_KEYS_PATHS=".ssh/authorized_keys"
|
||||
fi
|
||||
|
||||
echo "🔍 Rutas configuradas en sshd_config: $AUTH_KEYS_PATHS"
|
||||
echo "🔍 Buscando archivos de claves SSH en todo el sistema..."
|
||||
echo "------------------------------------------------------------"
|
||||
printf "%-20s %-50s %-10s %-15s %-10s\n" "Usuario" "Ruta authorized_keys" "Permisos" "SSH Habilitado" "Llaves"
|
||||
printf "%-20s %-50s %-10s %-15s %-10s\n" "Usuario" "Ruta del archivo" "Permisos" "SSH Habilitado" "Llaves"
|
||||
echo "------------------------------------------------------------"
|
||||
|
||||
# Buscar archivos que puedan contener claves SSH (no solo authorized_keys)
|
||||
KEY_FILES=$(find / -type f \( -name "authorized_keys" -o -name "*.pub" -o -name "*keys*" -o -name "*id_rsa*" -o -name "*id_ed25519*" \) 2>/dev/null)
|
||||
|
||||
# Variables para almacenar usuarios con llaves
|
||||
USERS_WITH_KEYS=()
|
||||
|
||||
@ -26,37 +20,22 @@ 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 del sistema
|
||||
if [[ ! -d "$homedir" || "$homedir" =~ ^(/bin|/sbin|/usr|/var|/proc|/sys|/dev|/run|/nonexistent) ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
user_has_keys=0
|
||||
user_keys_paths=()
|
||||
|
||||
# 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}"
|
||||
# Buscar archivos que pertenezcan a este usuario y contengan llaves válidas
|
||||
for key_file in $KEY_FILES; do
|
||||
file_owner=$(stat -c "%U" "$key_file" 2>/dev/null)
|
||||
if [[ "$file_owner" == "$username" ]]; then
|
||||
permisos=$(stat -c "%a" "$key_file")
|
||||
|
||||
# Si la ruta es relativa, asumir que está dentro del home del usuario
|
||||
if [[ "$expanded_path" != /* ]]; then
|
||||
expanded_path="$homedir/$expanded_path"
|
||||
fi
|
||||
# Verificar si el archivo contiene claves SSH
|
||||
num_llaves=$(grep -E -c "^(ssh-rsa|ssh-ed25519)" "$key_file" 2>/dev/null)
|
||||
|
||||
# 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")
|
||||
user_keys_paths+=("$key_file")
|
||||
fi
|
||||
else
|
||||
permisos="No existe"
|
||||
num_llaves=0
|
||||
fi
|
||||
done
|
||||
|
||||
@ -89,11 +68,9 @@ while IFS=: read -r username _ _ _ _ homedir _; do
|
||||
done
|
||||
fi
|
||||
|
||||
# Mostrar en pantalla
|
||||
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
|
||||
# Mostrar en pantalla si tiene llaves válidas
|
||||
if [[ ${#user_keys_paths[@]} -gt 0 ]]; then
|
||||
printf "%-20s %-50s %-10s %-15s %-10s\n" "$username" "${user_keys_paths[*]}" "$permisos" "$ssh_habilitado" "$num_llaves"
|
||||
USERS_WITH_KEYS+=("$username:${user_keys_paths[*]}")
|
||||
fi
|
||||
|
||||
@ -104,7 +81,7 @@ echo "------------------------------------------------------------"
|
||||
# Mostrar las llaves de cada usuario que tenga llaves autorizadas
|
||||
if [[ ${#USERS_WITH_KEYS[@]} -gt 0 ]]; then
|
||||
echo ""
|
||||
echo "🔑 Claves SSH autorizadas por usuario:"
|
||||
echo "🔑 Claves SSH autorizadas encontradas:"
|
||||
echo "============================================================"
|
||||
|
||||
for user_data in "${USERS_WITH_KEYS[@]}"; do
|
||||
@ -116,10 +93,11 @@ if [[ ${#USERS_WITH_KEYS[@]} -gt 0 ]]; then
|
||||
echo "------------------------------------------------------------"
|
||||
|
||||
for key_path in $key_paths; do
|
||||
[[ -f "$key_path" ]] && cat "$key_path"
|
||||
done
|
||||
echo "🔹 Contenido de $key_path:"
|
||||
grep -E "^(ssh-rsa|ssh-ed25519)" "$key_path" 2>/dev/null
|
||||
echo ""
|
||||
done
|
||||
done
|
||||
else
|
||||
echo "❌ No se encontraron usuarios con llaves SSH autorizadas."
|
||||
fi
|
||||
Loading…
Reference in New Issue
Block a user