From 2476af12e0a26e8ad760c394ff8d8308001d5519 Mon Sep 17 00:00:00 2001 From: ruihan Date: Sat, 20 Jun 2026 10:19:39 -0300 Subject: [PATCH] feat: add heal, persist, unpersist commands with LaunchDaemon support - New 'heal' command: checks MDM suppression state and re-applies if needed - New 'persist' command: installs LaunchDaemon for boot-time auto-heal - New 'unpersist' command: removes the persistence LaunchDaemon - Interactive menu updated for all new commands - Heal works from both Recovery and booted system (sudo) - Updated README with full documentation for heal/persist/unpersist - macOS Tahoe 26.x added to compatibility list --- README.md | 39 +++++++++++++- lib/heal.sh | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++++ unleash | 80 ++++++++++++++++++++++++----- 3 files changed, 250 insertions(+), 14 deletions(-) create mode 100644 lib/heal.sh diff --git a/README.md b/README.md index c237ab7..12cae77 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,41 @@ Creates a temporary admin account and suppresses MDM enrollment. > **Must run from Recovery mode.** +### `unleash heal` — Auto-Heal + +Checks if MDM suppression is still active and re-applies it if any component has been reset (e.g., after a macOS update). Safe to run from a booted system: + +```bash +sudo ./unleash heal # from booted system +./unleash heal # from Recovery mode +``` + +**What it checks:** +1. DEP marker files — are they still cleared? +2. Hosts file — are MDM domains still blocked? +3. Launchd disabled.plist — are enrollment daemons still disabled? +4. If any check fails, re-runs `suppress_enrollment` to restore suppression + +### `unleash persist` — Boot-Time Persistence + +Installs a LaunchDaemon that runs `unleash heal` automatically on every boot and every 24 hours: + +```bash +sudo ./unleash persist # from booted system +./unleash persist # from Recovery mode +``` + +Creates `/Library/LaunchDaemons/com.unleash.heal.plist`. Logs are written to `/var/log/unleash-heal.log` and `/var/log/unleash-heal.err`. + +### `unleash unpersist` — Remove Persistence + +Removes the LaunchDaemon installed by `unleash persist`: + +```bash +sudo ./unleash unpersist # from booted system +./unleash unpersist # from Recovery mode +``` + ### `unleash suppress` — Suppress Only Suppresses MDM enrollment without creating a user. Ideal after a clean bypass breaks (e.g., after macOS update). @@ -456,8 +491,8 @@ unleash/ ## FAQ -**Q: Does this work on macOS Sequoia?** -A: Yes. Tested up to macOS 15.x (Sequoia) on both Intel and Apple Silicon. +**Q: What macOS versions are supported?** +A: macOS 12.x (Monterey) through 15.x (Sequoia) and 26.x (Tahoe). Tested on Intel T2, M1, M2, M3, and M4 hardware. **Q: Do I need to disable SIP?** A: No. All operations target the Data volume, which does not require SIP to be disabled. diff --git a/lib/heal.sh b/lib/heal.sh new file mode 100644 index 0000000..c20384c --- /dev/null +++ b/lib/heal.sh @@ -0,0 +1,145 @@ +# unleash/lib/heal.sh — Self-healing and boot persistence + +heal_suppress() { + local data_mount="$1" + [ -z "$data_mount" ] && data_mount="" + + step "Checking current MDM suppression state..." + local cfg="${data_mount}/private/var/db/ConfigurationProfiles/Settings" + local hosts="${data_mount}/private/etc/hosts" + local ldp="${data_mount}/private/var/db/com.apple.xpc.launchd/disabled.plist" + + local needs_heal=false + + if [ -f "$cfg/.cloudConfigRecordFound" ]; then + warn "DEP activation record present" + needs_heal=true + else + info "DEP markers clean" + fi + + if [ -f "$hosts" ]; then + if grep -q "iprofiles.apple.com" "$hosts" 2>/dev/null; then + info "Domain block active" + else + warn "Domain block missing" + needs_heal=true + fi + else + warn "Hosts file missing" + needs_heal=true + fi + + if [ -f "$ldp" ]; then + local missing=0 + for label in com.apple.ManagedClient.enroll com.apple.mdmclient.daemon.runatboot; do + $PB -c "Print :$label" "$ldp" 2>/dev/null | grep -q "true" || missing=$((missing + 1)) + done + if [ "$missing" -gt 0 ]; then + warn "$missing enrollment daemon(s) not disabled" + needs_heal=true + else + info "Enrollment daemons disabled" + fi + else + warn "Launchd override missing" + needs_heal=true + fi + + if [ "$needs_heal" = false ]; then + success "MDM suppression intact — no action needed" + return 0 + fi + + step "Re-applying MDM suppression..." + suppress_enrollment "$data_mount" + success "MDM suppression restored" +} + +_persist_mount_root() { + local dm="$1" + if [ -z "$dm" ] || [ ! -d "$dm/Library" ]; then + echo "" + else + echo "$dm" + fi +} + +install_persist_launchdaemon() { + local data_mount="$1" + local root + root="$(_persist_mount_root "$data_mount")" + + local unleash_src + if [ -n "$SCRIPT_DIR" ]; then + unleash_src="$SCRIPT_DIR/unleash" + else + unleash_src="$(cd "$(dirname "$0")" && pwd)/unleash" + fi + + step "Installing LaunchDaemon for boot-time persistence..." + + local plist_dir="${root}/Library/LaunchDaemons" + local plist_path="${plist_dir}/com.unleash.heal.plist" + + mkdir -p "$plist_dir" + + cat > "$plist_path" <<- PLIST + + + + + Label + com.unleash.heal + ProgramArguments + + /bin/bash + -c + ${unleash_src} heal + + RunAtLoad + + StartInterval + 86400 + Nice + 1 + KeepAlive + + StandardOutPath + /var/log/unleash-heal.log + StandardErrorPath + /var/log/unleash-heal.err + + + PLIST + + chmod 644 "$plist_path" + success "LaunchDaemon written to $plist_path" + + step "Loading LaunchDaemon..." + if command -v launchctl &>/dev/null; then + launchctl load "$plist_path" 2>/dev/null \ + && success "LaunchDaemon loaded (will run on next boot)" \ + || info "LaunchDaemon will load on next boot" + else + info "launchctl not available (expected in Recovery) — will load on next boot" + fi +} + +remove_persist_launchdaemon() { + local data_mount="$1" + local root + root="$(_persist_mount_root "$data_mount")" + local plist_path="${root}/Library/LaunchDaemons/com.unleash.heal.plist" + + if [ -f "$plist_path" ]; then + step "Removing Unleash LaunchDaemon..." + if command -v launchctl &>/dev/null; then + launchctl unload "$plist_path" 2>/dev/null || true + fi + rm -f "$plist_path" + success "LaunchDaemon removed" + else + info "No Unleash LaunchDaemon installed" + fi +} diff --git a/unleash b/unleash index 17c5173..f1a37a1 100755 --- a/unleash +++ b/unleash @@ -8,6 +8,9 @@ set -euo pipefail # ./unleash Interactive menu (auto-detects Recovery/dualboot) # ./unleash bypass Full bypass (create admin + suppress) # ./unleash suppress Suppress-only (no user created) +# ./unleash heal Check and re-apply suppression (auto-heal) +# ./unleash persist Install LaunchDaemon for boot-time persistence +# ./unleash unpersist Remove the persistence LaunchDaemon # ./unleash backup Backup current state # ./unleash restore Restore from backup # ./unleash dualboot Target a different volume (needs sudo) @@ -24,7 +27,7 @@ SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" LIB_DIR="$SCRIPT_DIR/lib" # Source library modules -for _lib in colors detect validate dscl suppress backup status; do +for _lib in colors detect validate dscl suppress backup status heal; do _lib_path="$LIB_DIR/$_lib.sh" if [ -f "$_lib_path" ]; then # shellcheck disable=SC1090 @@ -133,6 +136,45 @@ cmd_dualboot() { echo -e "${YEL} Pass: $passw${NC}" } +cmd_heal() { + header "unleash — Auto-Heal" + if is_recovery; then + local data_mount + data_mount=$(resolve_data_volume) + heal_suppress "$data_mount" + else + if ! is_root; then + error_exit "Heal from booted system requires sudo: sudo ./unleash heal" + fi + heal_suppress "" + fi +} + +cmd_persist() { + header "unleash — Install Persistence" + local data_mount + if is_recovery; then + data_mount=$(resolve_data_volume) + else + if ! is_root; then + error_exit "Persistence requires sudo: sudo ./unleash persist" + fi + data_mount="" + fi + install_persist_launchdaemon "$data_mount" +} + +cmd_unpersist() { + header "unleash — Remove Persistence" + local data_mount + if is_recovery; then + data_mount=$(resolve_data_volume) + else + data_mount="" + fi + remove_persist_launchdaemon "$data_mount" +} + cmd_status() { if is_recovery; then local data_mount @@ -154,6 +196,7 @@ cmd_interactive_recovery() { local options=( "Full bypass (create admin + suppress MDM)" "Suppress enrollment only (no user)" + "Auto-heal (check and re-suppress)" "Backup current state" "Restore from backup" "Check MDM status" @@ -166,10 +209,13 @@ cmd_interactive_recovery() { full_bypass_mode "$data_mount" echo -e "${CYAN}Reboot to apply changes.${NC}" ;; - "Suppress enrollment only (no user)") - suppress_only_mode "$data_mount" - ;; - "Backup current state") + "Suppress enrollment only (no user)") + suppress_only_mode "$data_mount" + ;; + "Auto-heal (check and re-suppress)") + heal_suppress "$data_mount" + ;; + "Backup current state") backup_state "$data_mount" ;; "Restore from backup") @@ -200,18 +246,25 @@ cmd_interactive_normal() { PS3="Choose an option: " local options=( "Suppress enrollment only" + "Auto-heal (check and re-suppress)" "Backup current state" "Restore from backup" "Exit" ) select opt in "${options[@]}"; do case $opt in - "Suppress enrollment only") - local data_mount - data_mount=$(resolve_data_volume) - suppress_only_mode "$data_mount" - ;; - "Backup current state") + "Suppress enrollment only") + local data_mount + data_mount=$(resolve_data_volume) + suppress_only_mode "$data_mount" + ;; + "Auto-heal (check and re-suppress)") + if ! is_root; then + error_exit "Heal from booted system requires sudo. Run: sudo ./unleash" + fi + heal_suppress "" + ;; + "Backup current state") local data_mount data_mount=$(resolve_data_volume) backup_state "$data_mount" @@ -236,13 +289,16 @@ main() { case "${1:-}" in bypass) cmd_bypass ;; suppress) cmd_suppress ;; + heal) cmd_heal ;; + persist) cmd_persist ;; + unpersist) cmd_unpersist ;; backup) cmd_backup ;; restore) cmd_restore ;; dualboot) cmd_dualboot ;; status) cmd_status ;; version|-v|--version) cmd_version ;; help|-h|--help) - head -30 "$0" | grep -E '^# |^# \$' + head -40 "$0" | grep -E '^# |^# \$' exit 0 ;; "")