check_mdm_status() { local data_mount="$1" local hosts="$data_mount/private/etc/hosts" local cfg="$data_mount/private/var/db/ConfigurationProfiles/Settings" local ldp="$data_mount/private/var/db/com.apple.xpc.launchd/disabled.plist" header "MDM Status Report" step "DEP markers ($cfg)" if [ -d "$cfg" ]; then for f in \ .cloudConfigHasActivationRecord \ .cloudConfigRecordFound \ .cloudConfigRecordNotFound \ .cloudConfigProfileInstalled \ .cloudConfigTimerCheck; do if [ -f "$cfg/$f" ]; then echo -e " ${GRN}$f${NC} — present" else echo -e " ${YEL}$f${NC} — absent" fi done else warn "Settings directory not found" fi echo "" step "Blocked domains in hosts" if [ -f "$hosts" ]; then local matches matches=$(grep -iE 'iprofiles|enrollment|mdm|acmdm|albert|gdmf|configuration|xp\.apple|gs\.apple|tb\.apple' "$hosts" 2>/dev/null) if [ -n "$matches" ]; then echo "$matches" | while IFS= read -r line; do echo -e " ${GRN}$line${NC}" done else echo -e " ${YEL}(none)${NC}" fi local count; count=$(echo "$matches" | grep -c . 2>/dev/null || true) count="${count:-0}" echo -e " ${CYAN}Total: $count of 13 expected domains blocked${NC}" else echo -e " ${YEL}(hosts not found)${NC}" fi echo "" step "Enrollment daemon status" if [ -f "$ldp" ]; then /usr/libexec/PlistBuddy -c "Print" "$ldp" 2>/dev/null || echo -e " ${YEL}(empty/corrupt)${NC}" else echo -e " ${YEL}(no override)${NC}" fi echo "" step "Profiles enrollment status" if command -v profiles &>/dev/null; then profiles status -type enrollment 2>/dev/null \ || echo -e " ${YEL}Cannot check (expected in Recovery)${NC}" else echo -e " ${YEL}'profiles' not available${NC}" fi echo "" step "Backup status" if has_backup; then echo -e " ${GRN}Backup exists:${NC} $(cat "$BACKUP_DIR/timestamp")" else echo -e " ${YEL}No backup${NC}" fi echo "" if [ -f "$cfg/.cloudConfigRecordFound" ]; then local org org=$(plutil -convert xml1 -o - "$cfg/.cloudConfigRecordFound" 2>/dev/null \ | grep -iA1 OrganizationName | tail -1 | sed -E 's/.*(.*)<\/string>.*/\1/') [ -n "$org" ] && echo -e "${YEL}Active DEP record found — device assigned to: $org${NC}" else echo -e "${GRN}No active DEP record.${NC}" fi } _mdm_agent_names() { local agent for agent in "$1/"*; do [ -f "$agent" ] || continue if printf '%s\n' "${agent##*/}" | grep -qiE 'mdm|enrollment|managed'; then printf '%s\n' "${agent##*/}" fi done } deep_status() { if [ "${1:-}" = "--json" ]; then deep_status_json return fi header "Deep MDM Audit" step "Installed Configuration Profiles" if command -v profiles &>/dev/null; then local profile_count profile_count=$(sudo profiles -C -output stdout-xml 2>/dev/null | grep -c "ProfileDisplayName" || true) profile_count="${profile_count:-0}" if [ "$profile_count" -gt 0 ]; then warn "$profile_count profile(s) installed:" sudo profiles -C -output stdout-xml 2>/dev/null | grep -A1 "ProfileDisplayName" | grep "" | sed 's/.*\(.*\)<\/string>.*/ - \1/' else info "No configuration profiles installed" fi else warn "profiles command not available" fi echo "" step "MDM Enrollment State" if command -v profiles &>/dev/null; then sudo profiles status -type enrollment 2>/dev/null || echo " Cannot determine" fi echo "" step "MDM Identity Certificates (Keychain)" if command -v security &>/dev/null; then local mdm_certs mdm_certs=$(sudo security find-identity -p basic 2>/dev/null | grep -ci "mdm\|MDM\|Apple.*Push" || true) if [ "$mdm_certs" -gt 0 ]; then warn "$mdm_certs MDM-related certificate(s) found" sudo security find-identity -p basic 2>/dev/null | grep -i "mdm\|Apple.*Push" else info "No MDM certificates found in keychain" fi else warn "security command not available" fi echo "" step "MDM LaunchAgents (User)" local found=0 for home in /Users/*/; do [ -d "$home/Library/LaunchAgents" ] || continue local user_agents user_agents=$(_mdm_agent_names "$home/Library/LaunchAgents") if [ -n "$user_agents" ]; then echo -e " ${YEL}$(basename "$home"):${NC}" while IFS= read -r agent; do printf ' %s\n' "$agent"; done <<< "$user_agents" found=$((found + 1)) fi done [ "$found" -eq 0 ] && info "No MDM LaunchAgents found in any user" echo "" step "MDM LaunchDaemons (System)" local sys_agents sys_agents=$(_mdm_agent_names /Library/LaunchDaemons) if [ -n "$sys_agents" ]; then while IFS= read -r agent; do printf ' %s\n' "$agent"; done <<< "$sys_agents" else info "No MDM LaunchDaemons found" fi echo "" step "Running MDM Processes" local procs procs=$(pgrep -il "mdm|managedclient|activation" 2>/dev/null || true) if [ -n "$procs" ]; then echo "$procs" | awk '{print " " $2 " (PID " $1 ")"}' else info "No MDM processes running" fi echo "" step "MDM Agent Binaries" for agent_bin in /usr/local/bin/jamf /usr/local/bin/jamfagent /opt/jamf/bin/jamf \ /usr/local/bin/intune /usr/local/bin/microsoft-intune \ /Applications/Jamf* /Applications/Microsoft\ Intune* /Applications/VMware\ Workspace*; do if [ -e "$agent_bin" ]; then warn "Agent binary found: $agent_bin" fi done info "Scan complete" echo "" step "Firewall Status" pf_status 2>/dev/null || info "pf firewall check skipped" step "Overall Assessment" local risk="LOW" local cfg="/private/var/db/ConfigurationProfiles/Settings" local installed installed=$(sudo profiles -C -output stdout-xml 2>/dev/null | grep -c "ProfileDisplayName" || true) installed="${installed:-0}" [ "$installed" -gt 0 ] && risk="MEDIUM" pgrep -i "mdm|managedclient" >/dev/null 2>&1 && risk="HIGH" [ -f "$cfg/.cloudConfigRecordFound" ] && risk="CRITICAL" case "$risk" in LOW) echo -e " ${GRN}Risk: $risk — Device appears clean${NC}" ;; MEDIUM) echo -e " ${YEL}Risk: $risk — Residual profiles detected${NC}" ;; HIGH) echo -e " ${RED}Risk: $risk — MDM processes still running${NC}" ;; CRITICAL) echo -e " ${RED}Risk: $risk — Active DEP record found${NC}" ;; esac } deep_status_json() { local json="" json="${json}{\n" json="${json} \"version\": \"$VERSION\",\n" json="${json} \"timestamp\": \"$(date -u '+%Y-%m-%dT%H:%M:%SZ')\",\n" local profile_count=0 if command -v profiles &>/dev/null; then profile_count=$(sudo profiles -C -output stdout-xml 2>/dev/null | grep -c "ProfileDisplayName" || true) profile_count="${profile_count:-0}" fi json="${json} \"profile_count\": $profile_count,\n" local enroll_state="unknown" if command -v profiles &>/dev/null; then enroll_state=$(sudo profiles status -type enrollment 2>/dev/null | head -1 | xargs || echo "unknown") fi enroll_state="${enroll_state//\"/\\\"}" json="${json} \"enrollment_state\": \"${enroll_state}\",\n" local mdm_certs=0 if command -v security &>/dev/null; then mdm_certs=$(sudo security find-identity -p basic 2>/dev/null | grep -ci "mdm\|MDM\|Apple.*Push" || true) fi json="${json} \"mdm_certificates\": $mdm_certs,\n" local running_procs=0 running_procs=$(pgrep -il "mdm|managedclient|activation" 2>/dev/null | awk 'END {print NR}' || true) json="${json} \"running_mdm_processes\": $running_procs,\n" local risk="LOW" [ "$profile_count" -gt 0 ] && risk="MEDIUM" [ "$running_procs" -gt 0 ] && risk="HIGH" [ -f "/private/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordFound" ] && risk="CRITICAL" json="${json} \"risk_score\": \"${risk}\"\n" json="${json}}" echo -e "$json" }