From 5cfcf6f748b94ef4986c9b906a6aa3f19dbdfa87 Mon Sep 17 00:00:00 2001 From: ruihan Date: Sat, 20 Jun 2026 12:19:55 -0300 Subject: [PATCH] feat: history, dry-run, Discord webhook, bump 1.3.0 - history: read event logs from monitor/heal - test/dry-run: simulate bypass, suppress, harden without side effects - --dry-run: global flag for any command - monitor --webhook : Discord notification on MDM detection - Update CHANGELOG, CI, standalone for all new features - Bump 1.2.0 -> 1.3.0 --- .github/workflows/ci.yml | 1 + examples/build-standalone.sh | 2 +- lib/colors.sh | 1 + lib/history.sh | 44 ++++++++++++ lib/monitor.sh | 11 +++ lib/suppress.sh | 20 +++++- unleash | 52 +++++++++++++- unleash-standalone.sh | 127 ++++++++++++++++++++++++++++++++++- 8 files changed, 250 insertions(+), 8 deletions(-) create mode 100644 lib/history.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 730f7b0..cdfff07 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,6 +31,7 @@ jobs: lib/whitelist.sh lib/check.sh lib/monitor.sh + lib/history.sh syntax: name: Bash Syntax Check diff --git a/examples/build-standalone.sh b/examples/build-standalone.sh index 4d149b0..25d0654 100755 --- a/examples/build-standalone.sh +++ b/examples/build-standalone.sh @@ -11,7 +11,7 @@ set -euo pipefail VERSION="$VERSION" HEADER -for lib in colors detect validate dscl suppress backup status heal firewall harden whitelist check monitor; do +for lib in colors detect validate dscl suppress backup status heal firewall harden whitelist check monitor history; do tail -n +1 "$SCRIPT_DIR/lib/$lib.sh" >> "$OUT" echo "" >> "$OUT" done diff --git a/lib/colors.sh b/lib/colors.sh index 8584304..5b56343 100644 --- a/lib/colors.sh +++ b/lib/colors.sh @@ -8,6 +8,7 @@ NC='\033[0m' LOG_FILE="" VERBOSE=false +DRY_RUN=false log() { local level="$1" diff --git a/lib/history.sh b/lib/history.sh new file mode 100644 index 0000000..309c7fe --- /dev/null +++ b/lib/history.sh @@ -0,0 +1,44 @@ +show_history() { + header "Unleash Event History" + + local logs=("/var/log/unleash-monitor.log" "/var/log/unleash-heal.log" "/var/log/unleash-monitor.err") + local found=false + + for logfile in "${logs[@]}"; do + if [ -f "$logfile" ] && [ -s "$logfile" ]; then + found=true + local name + name=$(basename "$logfile") + step "$name" + while IFS= read -r line; do + echo " $line" + done < "$logfile" + echo "" + fi + done + + if [ "$found" = false ]; then + info "No event logs found." + info "Run 'sudo ./unleash monitor' or 'sudo ./unleash persist' first." + fi + + step "Backup records" + local backup_dir=".unleash-backup" + if [ -d "$backup_dir" ] && [ -f "$backup_dir/timestamp" ]; then + local ts + ts=$(cat "$backup_dir/timestamp") + echo " Last backup: $ts" + else + echo " No backup found" + fi +} + +clear_history() { + local logs=("/var/log/unleash-monitor.log" "/var/log/unleash-heal.log" "/var/log/unleash-monitor.err") + for logfile in "${logs[@]}"; do + if [ -f "$logfile" ]; then + : > "$logfile" + success "Cleared: $logfile" + fi + done +} diff --git a/lib/monitor.sh b/lib/monitor.sh index 983d3fb..f5eec98 100644 --- a/lib/monitor.sh +++ b/lib/monitor.sh @@ -80,6 +80,7 @@ monitor_mdm() { local logfile="/var/log/unleash-monitor.log" local pidfile="/tmp/unleash-monitor.pid" + local webhook="${DISCORD_WEBHOOK:-}" if [ -f "$pidfile" ] && kill -0 "$(cat "$pidfile")" 2>/dev/null; then echo -e "${YEL}Monitor already running (PID $(cat "$pidfile"))${NC}" @@ -135,6 +136,11 @@ monitor_mdm() { if command -v osascript &>/dev/null; then osascript -e "display notification \"$reason\" with title \"Unleash MDM Alert\" subtitle \"MDM enrollment detected\"" 2>/dev/null || true fi + if [ -n "$webhook" ]; then + local payload + payload=$(printf '{"content":"🚨 **Unleash Alert** — MDM enrollment detected: %s","username":"Unleash Monitor"}' "$reason") + curl -s -H "Content-Type: application/json" -d "$payload" "$webhook" 2>/dev/null || warn "Webhook failed" + fi heal_suppress "" fi fi @@ -150,6 +156,11 @@ monitor_mdm() { if command -v osascript &>/dev/null; then osascript -e "display dialog \"MDM keeps coming back after 12 attempts. Something is persistently re-enrolling.\" with title \"Unleash\" buttons {\"OK\"} default button \"OK\"" 2>/dev/null || true fi + if [ -n "$webhook" ]; then + curl -s -H "Content-Type: application/json" \ + -d '{"content":"🚨 **Unleash CRITICAL** — 12 consecutive MDM detections. Persistent re-enrollment detected.","username":"Unleash Monitor"}' \ + "$webhook" 2>/dev/null || true + fi consecutive_failures=0 fi diff --git a/lib/suppress.sh b/lib/suppress.sh index 4da71d9..2c513f5 100644 --- a/lib/suppress.sh +++ b/lib/suppress.sh @@ -4,6 +4,16 @@ PB=/usr/libexec/PlistBuddy suppress_enrollment() { local data_mount="$1" + + if [ "$DRY_RUN" = true ]; then + info "[DRY RUN] Would suppress MDM enrollment on $data_mount" + info "[DRY RUN] - Clear DEP markers in ConfigurationProfiles/Settings" + info "[DRY RUN] - Block 13+ Apple MDM domains in /etc/hosts" + info "[DRY RUN] - Disable 4 enrollment daemons in launchd disabled.plist" + info "[DRY RUN] - Clean MDM artifacts from /Users/*/Library" + return 0 + fi + 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" @@ -25,7 +35,6 @@ suppress_enrollment() { step "Blocking enrollment domains (Data volume hosts)..." [ -f "$hosts" ] || { mkdir -p "$(dirname "$hosts")"; touch "$hosts"; } - grep -q "Added by unleash" "$hosts" 2>/dev/null || { echo "" >>"$hosts" echo "# Added by unleash — DEP enrollment block" >>"$hosts" @@ -123,6 +132,15 @@ suppress_only_mode() { full_bypass_mode() { local data_mount="$1" + + if [ "$DRY_RUN" = true ]; then + info "[DRY RUN] Would perform full MDM bypass on $data_mount" + info "[DRY RUN] - Create admin user" + info "[DRY RUN] - Skip Setup Assistant (.AppleSetupDone)" + info "[DRY RUN] - Then run suppress_enrollment" + return 0 + fi + local node node=$(dscl_node "$data_mount") diff --git a/unleash b/unleash index d657ccd..a04be65 100755 --- a/unleash +++ b/unleash @@ -1,12 +1,12 @@ #!/bin/bash set -euo pipefail -VERSION="1.2.0" +VERSION="1.3.0" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" LIB_DIR="$SCRIPT_DIR/lib" -for _lib in colors detect validate dscl suppress backup status heal firewall harden whitelist check monitor; do +for _lib in colors detect validate dscl suppress backup status heal firewall harden whitelist check monitor history; do _lib_path="$LIB_DIR/$_lib.sh" if [ -f "$_lib_path" ]; then source "$_lib_path" @@ -44,6 +44,8 @@ Bypass / suppress / monitor MDM enrollment on macOS. Monitoring: check Pre-format / pre-upgrade safety report + history Show event log from monitor/heal runs + test Dry-run mode: simulate without changing anything monitor Background daemon (watches MDM every 5 min) monitor-install Install monitor as LaunchDaemon (boot persistent) monitor-uninstall Remove monitor LaunchDaemon @@ -76,6 +78,7 @@ ALIASES OPTIONS --verbose Show debug messages + --dry-run Simulate without making changes --log-file Write logs to file (appended) Run without arguments for interactive menu. @@ -283,14 +286,53 @@ cmd_check() { } cmd_monitor() { - case "${2:-}" in + local webhook="" + local args=() + while [ $# -gt 0 ]; do + case "$1" in + install|uninstall) args+=("$1"); shift ;; + --webhook) webhook="$2"; shift 2 ;; + *) args+=("$1"); shift ;; + esac + done + set -- "${args[@]}" + if [ -n "$webhook" ]; then + DISCORD_WEBHOOK="$webhook" + fi + case "${1:-}" in install) install_monitor_launchdaemon "" ;; uninstall) uninstall_monitor_launchdaemon "" ;; *) monitor_mdm ;; esac } + cmd_monitor_stop() { stop_monitor; } cmd_monitor_status() { monitor_status; } +cmd_history() { show_history; } +cmd_history_clear() { clear_history; } +cmd_test() { + header "Dry Run Mode" + DRY_RUN=true + local cmd="${2:-suppress}" + case "$cmd" in + bypass) info "Simulating: bypass"; cmd_bypass ;; + suppress) info "Simulating: suppress"; cmd_suppress ;; + heal) info "Simulating: heal"; cmd_heal ;; + harden) info "Simulating: harden"; cmd_harden ;; + firewall) info "Simulating: firewall"; cmd_firewall ;; + whitelist) info "Simulating: whitelist"; cmd_whitelist ;; + all) + info "Simulating all available operations..." + info "" + DRY_RUN=true suppress_enrollment "/" + info "" + DRY_RUN=true full_bypass_mode "/" + info "" + info "Dry-run complete. No changes were made." + ;; + *) info "Usage: ./unleash test {bypass|suppress|heal|harden|firewall|whitelist|all}" ;; + esac +} cmd_interactive_recovery() { header "unleash" @@ -413,6 +455,7 @@ parse_global_opts() { while [ $# -gt 0 ]; do case "$1" in --verbose) VERBOSE=true; shift ;; + --dry-run) DRY_RUN=true; shift ;; --log-file) LOG_FILE="$2"; shift 2 ;; *) args+=("$1"); shift ;; esac @@ -440,6 +483,9 @@ main() { dualboot) cmd_dualboot ;; status|st|ls) cmd_status ;; check) cmd_check ;; + history) cmd_history ;; + history-clear) cmd_history_clear ;; + test|dry-run) cmd_test "$@" ;; monitor|mn) cmd_monitor "$@" ;; monitor-install|mn-install) install_monitor_launchdaemon "" ;; monitor-uninstall|mn-uninstall) uninstall_monitor_launchdaemon "" ;; diff --git a/unleash-standalone.sh b/unleash-standalone.sh index c57ed2a..a897f25 100755 --- a/unleash-standalone.sh +++ b/unleash-standalone.sh @@ -1,6 +1,6 @@ #!/bin/bash set -euo pipefail -VERSION="1.2.0" +VERSION="1.3.0" RED='\033[1;31m' GRN='\033[1;32m' BLU='\033[1;34m' @@ -11,6 +11,7 @@ NC='\033[0m' LOG_FILE="" VERBOSE=false +DRY_RUN=false log() { local level="$1" @@ -331,6 +332,16 @@ PB=/usr/libexec/PlistBuddy suppress_enrollment() { local data_mount="$1" + + if [ "$DRY_RUN" = true ]; then + info "[DRY RUN] Would suppress MDM enrollment on $data_mount" + info "[DRY RUN] - Clear DEP markers in ConfigurationProfiles/Settings" + info "[DRY RUN] - Block 13+ Apple MDM domains in /etc/hosts" + info "[DRY RUN] - Disable 4 enrollment daemons in launchd disabled.plist" + info "[DRY RUN] - Clean MDM artifacts from /Users/*/Library" + return 0 + fi + 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" @@ -352,7 +363,6 @@ suppress_enrollment() { step "Blocking enrollment domains (Data volume hosts)..." [ -f "$hosts" ] || { mkdir -p "$(dirname "$hosts")"; touch "$hosts"; } - grep -q "Added by unleash" "$hosts" 2>/dev/null || { echo "" >>"$hosts" echo "# Added by unleash — DEP enrollment block" >>"$hosts" @@ -450,6 +460,15 @@ suppress_only_mode() { full_bypass_mode() { local data_mount="$1" + + if [ "$DRY_RUN" = true ]; then + info "[DRY RUN] Would perform full MDM bypass on $data_mount" + info "[DRY RUN] - Create admin user" + info "[DRY RUN] - Skip Setup Assistant (.AppleSetupDone)" + info "[DRY RUN] - Then run suppress_enrollment" + return 0 + fi + local node node=$(dscl_node "$data_mount") @@ -1403,6 +1422,7 @@ monitor_mdm() { local logfile="/var/log/unleash-monitor.log" local pidfile="/tmp/unleash-monitor.pid" + local webhook="${DISCORD_WEBHOOK:-}" if [ -f "$pidfile" ] && kill -0 "$(cat "$pidfile")" 2>/dev/null; then echo -e "${YEL}Monitor already running (PID $(cat "$pidfile"))${NC}" @@ -1458,6 +1478,11 @@ monitor_mdm() { if command -v osascript &>/dev/null; then osascript -e "display notification \"$reason\" with title \"Unleash MDM Alert\" subtitle \"MDM enrollment detected\"" 2>/dev/null || true fi + if [ -n "$webhook" ]; then + local payload + payload=$(printf '{"content":"🚨 **Unleash Alert** — MDM enrollment detected: %s","username":"Unleash Monitor"}' "$reason") + curl -s -H "Content-Type: application/json" -d "$payload" "$webhook" 2>/dev/null || warn "Webhook failed" + fi heal_suppress "" fi fi @@ -1473,6 +1498,11 @@ monitor_mdm() { if command -v osascript &>/dev/null; then osascript -e "display dialog \"MDM keeps coming back after 12 attempts. Something is persistently re-enrolling.\" with title \"Unleash\" buttons {\"OK\"} default button \"OK\"" 2>/dev/null || true fi + if [ -n "$webhook" ]; then + curl -s -H "Content-Type: application/json" \ + -d '{"content":"🚨 **Unleash CRITICAL** — 12 consecutive MDM detections. Persistent re-enrollment detected.","username":"Unleash Monitor"}' \ + "$webhook" 2>/dev/null || true + fi consecutive_failures=0 fi @@ -1508,6 +1538,51 @@ monitor_status() { fi } +show_history() { + header "Unleash Event History" + + local logs=("/var/log/unleash-monitor.log" "/var/log/unleash-heal.log" "/var/log/unleash-monitor.err") + local found=false + + for logfile in "${logs[@]}"; do + if [ -f "$logfile" ] && [ -s "$logfile" ]; then + found=true + local name + name=$(basename "$logfile") + step "$name" + while IFS= read -r line; do + echo " $line" + done < "$logfile" + echo "" + fi + done + + if [ "$found" = false ]; then + info "No event logs found." + info "Run 'sudo ./unleash monitor' or 'sudo ./unleash persist' first." + fi + + step "Backup records" + local backup_dir=".unleash-backup" + if [ -d "$backup_dir" ] && [ -f "$backup_dir/timestamp" ]; then + local ts + ts=$(cat "$backup_dir/timestamp") + echo " Last backup: $ts" + else + echo " No backup found" + fi +} + +clear_history() { + local logs=("/var/log/unleash-monitor.log" "/var/log/unleash-heal.log" "/var/log/unleash-monitor.err") + for logfile in "${logs[@]}"; do + if [ -f "$logfile" ]; then + : > "$logfile" + success "Cleared: $logfile" + fi + done +} + show_help() { cat <<'HELP' @@ -1537,6 +1612,8 @@ Bypass / suppress / monitor MDM enrollment on macOS. Monitoring: check Pre-format / pre-upgrade safety report + history Show event log from monitor/heal runs + test Dry-run mode: simulate without changing anything monitor Background daemon (watches MDM every 5 min) monitor-install Install monitor as LaunchDaemon (boot persistent) monitor-uninstall Remove monitor LaunchDaemon @@ -1569,6 +1646,7 @@ ALIASES OPTIONS --verbose Show debug messages + --dry-run Simulate without making changes --log-file Write logs to file (appended) Run without arguments for interactive menu. @@ -1776,14 +1854,53 @@ cmd_check() { } cmd_monitor() { - case "${2:-}" in + local webhook="" + local args=() + while [ $# -gt 0 ]; do + case "$1" in + install|uninstall) args+=("$1"); shift ;; + --webhook) webhook="$2"; shift 2 ;; + *) args+=("$1"); shift ;; + esac + done + set -- "${args[@]}" + if [ -n "$webhook" ]; then + DISCORD_WEBHOOK="$webhook" + fi + case "${1:-}" in install) install_monitor_launchdaemon "" ;; uninstall) uninstall_monitor_launchdaemon "" ;; *) monitor_mdm ;; esac } + cmd_monitor_stop() { stop_monitor; } cmd_monitor_status() { monitor_status; } +cmd_history() { show_history; } +cmd_history_clear() { clear_history; } +cmd_test() { + header "Dry Run Mode" + DRY_RUN=true + local cmd="${2:-suppress}" + case "$cmd" in + bypass) info "Simulating: bypass"; cmd_bypass ;; + suppress) info "Simulating: suppress"; cmd_suppress ;; + heal) info "Simulating: heal"; cmd_heal ;; + harden) info "Simulating: harden"; cmd_harden ;; + firewall) info "Simulating: firewall"; cmd_firewall ;; + whitelist) info "Simulating: whitelist"; cmd_whitelist ;; + all) + info "Simulating all available operations..." + info "" + DRY_RUN=true suppress_enrollment "/" + info "" + DRY_RUN=true full_bypass_mode "/" + info "" + info "Dry-run complete. No changes were made." + ;; + *) info "Usage: ./unleash test {bypass|suppress|heal|harden|firewall|whitelist|all}" ;; + esac +} cmd_interactive_recovery() { header "unleash" @@ -1906,6 +2023,7 @@ parse_global_opts() { while [ $# -gt 0 ]; do case "$1" in --verbose) VERBOSE=true; shift ;; + --dry-run) DRY_RUN=true; shift ;; --log-file) LOG_FILE="$2"; shift 2 ;; *) args+=("$1"); shift ;; esac @@ -1933,6 +2051,9 @@ main() { dualboot) cmd_dualboot ;; status|st|ls) cmd_status ;; check) cmd_check ;; + history) cmd_history ;; + history-clear) cmd_history_clear ;; + test|dry-run) cmd_test "$@" ;; monitor|mn) cmd_monitor "$@" ;; monitor-install|mn-install) install_monitor_launchdaemon "" ;; monitor-uninstall|mn-uninstall) uninstall_monitor_launchdaemon "" ;;