Back to Resources
Level
Script
Security
This script addresses the difficulty of maintaining accurate control over administrative privileges on macOS systems by cross-referencing a list of detected admins against an authorized list. It ensures that you can quickly spot and remediate any unapproved accounts before they pose a security risk.
It retrieves a comma-separated list of admin accounts that Level has detected on a macOS endpoint and compares each username to the organization’s sanctioned list of authorized admins. If any user is found to be unauthorized, the script returns a flagged result, allowing you to take immediate corrective action. By exiting with a non-zero code when unauthorized admins are detected, it seamlessly integrates with Level’s alerting and monitoring features, enabling on-demand checks or scheduled audits without manual intervention.
#!/bin/bash
# This resource is provided as a convenience for Level users. We cannot
# guarantee it will work in all environments. Please test before deploying
# to your production environment. We welcome contributions to our community
# library
# Level Library
# https://level.io/library/script-macos-unauthorized-admins
# Define authorized admins
AUTHORIZED_ADMINS="{{cf_authorized_admins}}"
# Get all local admin users on macOS
admins=$(dscl . -read /Groups/admin GroupMembership | awk '{$1=""; print $0}' | xargs)
active_admins=()
for admin in $admins; do
if [[ -n "$admin" ]]; then
# Check if the account is expired
exp_date=$(dscl . -read /Users/"$admin" accountPolicyData 2>/dev/null | grep -A1 "accountExpires" | tail -1 | grep -oE '[0-9]{4}-[0-9]{2}-[0-9]{2}')
if [[ -z "$exp_date" || "$exp_date" > "$(date +%Y-%m-%d)" ]]; then
# Check if the account is locked
status=$(pwpolicy -u "$admin" -getpolicy 2>/dev/null | grep "isDisabled=1")
if [[ -z "$status" ]]; then
active_admins+=("$admin")
fi
fi
fi
done
# Convert array to comma-separated string
DETECTED_ADMINS=$(IFS=','; echo "${active_admins[*]}")
# Convert lists to arrays (lowercase for case-insensitive comparison)
IFS=',' read -r -a detectedArray <<< "$(echo "$DETECTED_ADMINS" | tr '[:upper:]' '[:lower:]' | sed 's/, */,/g')"
IFS=',' read -r -a authorizedArray <<< "$(echo "$AUTHORIZED_ADMINS" | tr '[:upper:]' '[:lower:]' | sed 's/, */,/g')"
# Find unauthorized admins
unauthorizedAdmins=()
for detected in "${detectedArray[@]}"; do
found=false
for authorized in "${authorizedArray[@]}"; do
if [[ "$detected" == "$authorized" ]]; then
found=true
break
fi
done
if [[ "$found" == false ]]; then
unauthorizedAdmins+=("$detected")
fi
done
# Join unauthorized admins into a comma-separated string
unauthorizedString=$(IFS=,; echo "${unauthorizedAdmins[*]}")
# Output unauthorized admins or success message
if [[ ${#unauthorizedAdmins[@]} -gt 0 ]]; then
echo "$unauthorizedString"
exit 1
else
echo "No unauthorized admins detected."
exit 0
fi
macOS - Unauthorized Admins
This Bash script compares detected administrator accounts against a predefined list of authorized administrators using Level's script variables and custom fields on macOS systems. It processes both lists into arrays with case-insensitive values, then iterates through the detected administrators to identify any that aren't present in the authorized list. The script outputs either a comma-separated list of unauthorized administrators and exits with an error code, or confirms no unauthorized admins were found and exits successfully.
Bash
100
Local system
Explore more automations, scripts, and policies to further enhance your IT operations.