From 1791f6d321fc90a5c2ad1ec3f1877d2d5141ccc7 Mon Sep 17 00:00:00 2001 From: ruihan Date: Sat, 20 Jun 2026 13:07:03 -0300 Subject: [PATCH] feat: tests, demo, JSON output, VPN kill-switch, more issue responses - tests/: bats test suite for 8 modules (suppress, heal, firewall, detect, validate, colors, dscl, backup) - demo: simulated full bypass flow for showcasing (no real changes) - --json flag for report and audit commands (machine-readable output) - vpn-kill: pf-based kill-switch that blocks MDM outside VPN tunnel - vpn-kill-remove / vpn-kill-status: manage the kill-switch - Responded to 5 more bypass-mdm issues - CI now includes all new lib files in ShellCheck - Bump 1.4.0 -> 1.5.0 --- .github/workflows/ci.yml | 2 + examples/build-standalone.sh | 2 +- lib/demo.sh | 84 +++++++++ lib/report.sh | 64 +++++++ lib/status.sh | 45 +++++ lib/vpn.sh | 117 ++++++++++++ tests/test_backup.bats | 40 +++++ tests/test_colors.bats | 41 +++++ tests/test_detect.bats | 34 ++++ tests/test_dscl.bats | 25 +++ tests/test_firewall.bats | 39 ++++ tests/test_heal.bats | 36 ++++ tests/test_suppress.bats | 75 ++++++++ tests/test_validate.bats | 51 ++++++ unleash | 23 ++- unleash-standalone.sh | 333 ++++++++++++++++++++++++++++++++++- 16 files changed, 1001 insertions(+), 10 deletions(-) create mode 100644 lib/demo.sh create mode 100644 lib/vpn.sh create mode 100644 tests/test_backup.bats create mode 100644 tests/test_colors.bats create mode 100644 tests/test_detect.bats create mode 100644 tests/test_dscl.bats create mode 100644 tests/test_firewall.bats create mode 100644 tests/test_heal.bats create mode 100644 tests/test_suppress.bats create mode 100644 tests/test_validate.bats diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 417bf91..e4d3b2d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,6 +38,8 @@ jobs: lib/config.sh lib/report.sh lib/ma_detect.sh + lib/demo.sh + lib/vpn.sh syntax: name: Bash Syntax Check diff --git a/examples/build-standalone.sh b/examples/build-standalone.sh index 3df3c4b..c11b6c4 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 config detect validate dscl suppress backup status heal firewall harden whitelist check monitor history doctor selfupdate uninstall report ma_detect; do +for lib in colors config detect validate dscl suppress backup status heal firewall harden whitelist check monitor history doctor selfupdate uninstall report ma_detect demo vpn; do tail -n +1 "$SCRIPT_DIR/lib/$lib.sh" >> "$OUT" echo "" >> "$OUT" done diff --git a/lib/demo.sh b/lib/demo.sh new file mode 100644 index 0000000..329373d --- /dev/null +++ b/lib/demo.sh @@ -0,0 +1,84 @@ +run_demo() { + header "Unleash Demo — Simulated MDM Bypass" + + local demo_dir + demo_dir=$(mktemp -d) + mkdir -p "$demo_dir/private/etc" + mkdir -p "$demo_dir/private/var/db/ConfigurationProfiles/Settings" + mkdir -p "$demo_dir/private/var/db/com.apple.xpc.launchd" + mkdir -p "$demo_dir/Users/demo/Library/Preferences" + mkdir -p "$demo_dir/Users/demo/Library/Application Support" + + touch "$demo_dir/private/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordFound" + touch "$demo_dir/Users/demo/Library/Preferences/com.apple.mdm.plist" + + echo "" + step "1. Simulating pre-bypass MDM state" + echo " DEP record: ${RED}PRESENT${NC}" + echo " Hosts block: ${RED}ABSENT${NC}" + echo " Daemon override: ${RED}ABSENT${NC}" + echo " User artifacts: ${RED}PRESENT${NC}" + sleep 1 + + echo "" + step "2. Running suppress_enrollment..." + DRY_RUN=false + suppress_enrollment "$demo_dir" 2>/dev/null || true + sleep 1 + + echo "" + step "3. Verifying post-bypass state" + local clean=true + if [ -f "$demo_dir/private/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordFound" ]; then + echo " DEP record: ${RED}STILL PRESENT (bug)${NC}" + clean=false + else + echo " DEP record: ${GRN}CLEARED${NC}" + fi + if grep -q "iprofiles.apple.com" "$demo_dir/private/etc/hosts" 2>/dev/null; then + echo " Hosts block: ${GRN}ACTIVE${NC}" + else + echo " Hosts block: ${RED}ABSENT (bug)${NC}" + clean=false + fi + if [ -f "$demo_dir/Users/demo/Library/Preferences/com.apple.mdm.plist" ]; then + echo " User artifacts: ${RED}STILL PRESENT (bug)${NC}" + clean=false + else + echo " User artifacts: ${GRN}CLEANED${NC}" + fi + + echo "" + step "4. Simulating macOS update (re-enabling daemons)" + rm -f "$demo_dir/private/var/db/com.apple.xpc.launchd/disabled.plist" + echo " Daemon override: ${RED}RESET (simulating update)${NC}" + sleep 1 + + echo "" + step "5. Running heal — detecting and fixing..." + heal_suppress "$demo_dir" 2>/dev/null || true + sleep 1 + + if [ -f "$demo_dir/private/var/db/com.apple.xpc.launchd/disabled.plist" ]; then + echo " Daemon override: ${GRN}RESTORED${NC}" + fi + + echo "" + step "6. Summary" + if [ "$clean" = true ]; then + echo -e " ${GRN}✓ Demo completed successfully${NC}" + else + echo -e " ${YEL}⚠ Demo completed with warnings${NC}" + fi + echo " Simulated environment: $demo_dir" + echo "" + info "This is a simulated run. No real system was modified." + info "Run 'rm -rf $demo_dir' to clean up." + + echo "" + echo -e "${CYAN}╔═══════════════════════════════════════════════╗${NC}" + echo -e "${CYAN}║${NC} ${GRN}Demo Complete${NC} ${CYAN}║${NC}" + echo -e "${CYAN}║${NC} Commands used: suppress, heal ${CYAN}║${NC}" + echo -e "${CYAN}║${NC} Try: ${YEL}sudo ./unleash doctor${NC} for real diagnostics ${CYAN}║${NC}" + echo -e "${CYAN}╚═══════════════════════════════════════════════╝${NC}" +} diff --git a/lib/report.sh b/lib/report.sh index 21d3d42..c835609 100644 --- a/lib/report.sh +++ b/lib/report.sh @@ -1,4 +1,8 @@ generate_report() { + if [ "${1:-}" = "--json" ]; then + generate_report_json + return + fi header "Unleash System Report" local ts @@ -90,3 +94,63 @@ generate_report() { echo " None" fi } + +generate_report_json() { + local report="" + report="${report}{\n" + + report="${report} \"version\": \"$VERSION\",\n" + report="${report} \"timestamp\": \"$(date -u '+%Y-%m-%dT%H:%M:%SZ')\",\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//\"/\\\"}" + report="${report} \"enrollment_state\": \"${enroll_state}\",\n" + + local has_dep="false" + if [ -f "/private/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordFound" ]; then + has_dep="true" + fi + report="${report} \"dep_record_found\": $has_dep,\n" + + local profile_count=0 + if command -v profiles &>/dev/null; then + profile_count=$(sudo profiles -C -output=xml 2>/dev/null | grep -c "ProfileDisplayName" || echo 0) + fi + report="${report} \"installed_profiles\": $profile_count,\n" + + local heal_installed="false" + [ -f "/Library/LaunchDaemons/com.unleash.heal.plist" ] && heal_installed="true" + local monitor_installed="false" + [ -f "/Library/LaunchDaemons/com.unleash.monitor.plist" ] && monitor_installed="true" + local monitor_running="false" + [ -f "/tmp/unleash-monitor.pid" ] && monitor_running="true" + report="${report} \"persistence\": {\n" + report="${report} \"heal_launchdaemon\": $heal_installed,\n" + report="${report} \"monitor_launchdaemon\": $monitor_installed,\n" + report="${report} \"monitor_running\": $monitor_running\n" + report="${report} },\n" + + local fw_active="false" + if command -v pfctl &>/dev/null && pfctl -a "com.unleash/mdm" -s rules 2>/dev/null | grep -q "block"; then + fw_active="true" + fi + local selective_active="false" + if command -v pfctl &>/dev/null && pfctl -a "com.unleash.selective" -s rules 2>/dev/null | grep -q "block"; then + selective_active="true" + fi + report="${report} \"firewall\": {\n" + report="${report} \"mdm_block_active\": $fw_active,\n" + report="${report} \"selective_block_active\": $selective_active\n" + report="${report} },\n" + + local hosts_blocked=0 + hosts_blocked=$(grep -c "0.0.0.0" /private/etc/hosts 2>/dev/null || echo 0) + report="${report} \"hosts_blocked\": $hosts_blocked\n" + + report="${report}}" + + echo -e "$report" +} diff --git a/lib/status.sh b/lib/status.sh index 79bf771..5c05bf5 100644 --- a/lib/status.sh +++ b/lib/status.sh @@ -80,6 +80,10 @@ check_mdm_status() { } deep_status() { + if [ "${1:-}" = "--json" ]; then + deep_status_json + return + fi header "Deep MDM Audit" step "Installed Configuration Profiles" @@ -180,3 +184,44 @@ deep_status() { 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=xml 2>/dev/null | grep -c "ProfileDisplayName" || echo 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=$(ps aux 2>/dev/null | grep -ciE "mdm|managedclient|activation" || true) + running_procs=$((running_procs - 1)) + [ "$running_procs" -lt 0 ] && running_procs=0 + 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" +} diff --git a/lib/vpn.sh b/lib/vpn.sh new file mode 100644 index 0000000..3588a6a --- /dev/null +++ b/lib/vpn.sh @@ -0,0 +1,117 @@ +VPN_RULES_ANCHOR="com.unleash/vpn-kill" +VPN_RULES_FILE="/etc/pf.anchors/com.unleash/vpn-kill" + +vpn_kill_install() { + header "VPN Kill-Switch — MDM Leak Protection" + + if ! is_root; then + error_exit "VPN kill-switch needs sudo: sudo ./unleash vpn-kill" + fi + + local vpn_if="" + + step "Detecting active VPN interfaces..." + local interfaces + interfaces=$(ifconfig 2>/dev/null | grep -E "^utun[0-9]+:" | sed 's/:.*//' || true) + if [ -z "$interfaces" ]; then + warn "No VPN interfaces detected" + info "You can specify manually: sudo ./unleash vpn-kill --interface utunX" + info "Common VPN interfaces: utun0-9 (WireGuard), utun10+ (OpenVPN), ppp0 (L2TP)" + else + for iface in $interfaces; do + local addr + addr=$(ifconfig "$iface" 2>/dev/null | awk '/inet /{print $2}') + if [ -n "$addr" ]; then + echo " $iface: $addr" + [ -z "$vpn_if" ] && vpn_if="$iface" + fi + done + fi + + local user_if="${2:-}" + [ -n "$user_if" ] && vpn_if="$user_if" + + if [ -z "$vpn_if" ]; then + warn "No active VPN detected. Installing kill-switch anyway (no default route)." + info "Usage: sudo ./unleash vpn-kill --interface utunX" + return 1 + fi + + step "Installing pf rules for VPN kill-switch..." + local anchor_dir="/etc/pf.anchors/com.unleash" + mkdir -p "$anchor_dir" + + cat > "$VPN_RULES_FILE" << RULES +# Unleash VPN kill-switch +# Only allow MDM traffic through VPN interface $vpn_if +block drop out proto {tcp,udp} to {17.0.0.0/8, 17.128.0.0/10} +pass out proto {tcp,udp} to {17.0.0.0/8, 17.128.0.0/10} no state +pass on $vpn_if +RULES + chmod 644 "$VPN_RULES_FILE" + success "VPN kill-switch rules installed for $vpn_if" + + local pf_conf="/etc/pf.conf" + local anchor_line="anchor \"${VPN_RULES_ANCHOR}\"" + local load_line="load anchor \"${VPN_RULES_ANCHOR}\" from \"${VPN_RULES_FILE}\"" + + if grep -q "vpn-kill" "$pf_conf" 2>/dev/null; then + info "pf.conf already has VPN kill-switch anchor" + else + echo "" >> "$pf_conf" + echo "# Added by unleash — VPN kill-switch (prevents MDM leaks)" >> "$pf_conf" + echo "$anchor_line" >> "$pf_conf" + echo "$load_line" >> "$pf_conf" + fi + + pfctl -e -f "$pf_conf" 2>/dev/null && success "VPN kill-switch active" \ + || warn "pfctl failed" + + echo "" + info "What this does:" + info " - Blocks MDM Apple IP ranges (17.0.0.0/8) on all interfaces" + info " - Only allows MDM traffic through VPN interface ($vpn_if)" + info " - If VPN drops, MDM traffic is blocked — no leaks" + echo "" + warn "This does NOT affect your regular internet traffic." + warn "Only MDM-related IPs are routed through the VPN kill-switch." +} + +vpn_kill_remove() { + header "Remove VPN Kill-Switch" + + if ! is_root; then + error_exit "Needs sudo: sudo ./unleash vpn-kill-remove" + fi + + if [ -f "$VPN_RULES_FILE" ]; then + rm -f "$VPN_RULES_FILE" + success "VPN kill-switch rules removed" + fi + + local pf_conf="/etc/pf.conf" + if [ -f "$pf_conf" ]; then + sed -i '' '/# Added by unleash — VPN kill-switch/d' "$pf_conf" 2>/dev/null || true + sed -i '' '/vpn-kill/d' "$pf_conf" 2>/dev/null || true + success "pf.conf cleaned" + fi + + pfctl -a "$VPN_RULES_ANCHOR" -F all 2>/dev/null || true + success "pf anchor flushed" +} + +vpn_kill_status() { + step "VPN kill-switch status" + if [ -f "$VPN_RULES_FILE" ]; then + echo " Rules file: $VPN_RULES_FILE" + cat "$VPN_RULES_FILE" | sed 's/^/ /' + else + echo " No VPN kill-switch installed" + fi + if command -v pfctl &>/dev/null; then + echo "" + pfctl -a "$VPN_RULES_ANCHOR" -s rules 2>/dev/null \ + && echo " Anchor loaded" \ + || echo " Anchor not loaded" + fi +} diff --git a/tests/test_backup.bats b/tests/test_backup.bats new file mode 100644 index 0000000..f9794af --- /dev/null +++ b/tests/test_backup.bats @@ -0,0 +1,40 @@ +#!/usr/bin/env bats +# Tests for backup.sh - run with: bats tests/test_backup.bats + +setup() { + load '../lib/colors.sh' + load '../lib/backup.sh' + SCRIPT_DIR=$(mktemp -d) + TEST_DIR=$(mktemp -d) + mkdir -p "$TEST_DIR/private/etc" + mkdir -p "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings" + echo "test" > "$TEST_DIR/private/etc/hosts" + BACKUP_DIR="$SCRIPT_DIR/.unleash-backup" +} + +teardown() { + rm -rf "$SCRIPT_DIR" "$TEST_DIR" +} + +@test "backup_state creates backup directory" { + run backup_state "$TEST_DIR" 2>/dev/null || true + [ -d "$BACKUP_DIR" ] +} + +@test "backup_state saves hosts file" { + run backup_state "$TEST_DIR" 2>/dev/null || true + [ -f "$BACKUP_DIR/hosts.backup" ] + run cat "$BACKUP_DIR/hosts.backup" + [ "$output" = "test" ] +} + +@test "has_backup returns false initially" { + run has_backup + [ "$status" -eq 1 ] +} + +@test "has_backup returns true after backup" { + backup_state "$TEST_DIR" 2>/dev/null || true + run has_backup + [ "$status" -eq 0 ] +} diff --git a/tests/test_colors.bats b/tests/test_colors.bats new file mode 100644 index 0000000..424b12f --- /dev/null +++ b/tests/test_colors.bats @@ -0,0 +1,41 @@ +#!/usr/bin/env bats +# Tests for colors.sh - run with: bats tests/test_colors.bats + +setup() { + load '../lib/colors.sh' + TEST_LOG=$(mktemp) +} + +teardown() { + rm -f "$TEST_LOG" +} + +@test "log writes to LOG_FILE when set" { + LOG_FILE="$TEST_LOG" + log "INFO" "test message" + run grep "test message" "$TEST_LOG" + [ "$status" -eq 0 ] +} + +@test "log respects VERBOSE for DEBUG" { + VERBOSE=false + run log "DEBUG" "hidden message" + [ -z "$output" ] +} + +@test "log shows DEBUG when VERBOSE is true" { + VERBOSE=true + run log "DEBUG" "visible message" + echo "$output" | grep -q "visible" +} + +@test "error_exit exits with non-zero" { + run error_exit "test error" 2>/dev/null + [ "$status" -ne 0 ] +} + +@test "header outputs formatted title" { + run header "Test Title" + [ "$status" -eq 0 ] + echo "$output" | grep -q "Test Title" +} diff --git a/tests/test_detect.bats b/tests/test_detect.bats new file mode 100644 index 0000000..ad44118 --- /dev/null +++ b/tests/test_detect.bats @@ -0,0 +1,34 @@ +#!/usr/bin/env bats +# Tests for detect.sh - run with: bats tests/test_detect.bats + +setup() { + load '../lib/colors.sh' + load '../lib/detect.sh' +} + +@test "is_root returns true when EUID is 0" { + EUID=0 + run is_root + [ "$status" -eq 0 ] +} + +@test "is_root returns false when EUID is not 0" { + EUID=501 + run is_root + [ "$status" -eq 1 ] +} + +@test "is_recovery returns false in normal environment" { + run is_recovery + [ "$status" -eq 1 ] +} + +@test "detect_system_volume returns empty when no sys vol found" { + run detect_system_volume "/nonexistent" + [ -z "$output" ] || [ "$output" = "" ] +} + +@test "resolve_data_volume errors on non-existent device" { + run resolve_data_volume 2>/dev/null || true + [ "$status" -ne 0 ] +} diff --git a/tests/test_dscl.bats b/tests/test_dscl.bats new file mode 100644 index 0000000..8def3da --- /dev/null +++ b/tests/test_dscl.bats @@ -0,0 +1,25 @@ +#!/usr/bin/env bats +# Tests for dscl.sh - run with: bats tests/test_dscl.bats + +setup() { + load '../lib/colors.sh' + load '../lib/dscl.sh' + TEST_DIR=$(mktemp -d) + mkdir -p "$TEST_DIR/private/var/db/dslocal/nodes/Default" +} + +teardown() { + rm -rf "$TEST_DIR" +} + +@test "dscl_node returns correct path" { + run dscl_node "$TEST_DIR" + [ "$output" = "$TEST_DIR/private/var/db/dslocal/nodes/Default" ] +} + +@test "find_available_uid returns 501 for empty node" { + local node + node=$(dscl_node "$TEST_DIR") + run find_available_uid "$node" + [ "$output" = "501" ] +} diff --git a/tests/test_firewall.bats b/tests/test_firewall.bats new file mode 100644 index 0000000..cad219a --- /dev/null +++ b/tests/test_firewall.bats @@ -0,0 +1,39 @@ +#!/usr/bin/env bats +# Tests for firewall.sh - run with: bats tests/test_firewall.bats + +setup() { + load '../lib/colors.sh' + load '../lib/firewall.sh' + TEST_DIR=$(mktemp -d) + mkdir -p "$TEST_DIR/etc/pf.anchors/com.unleash" +} + +teardown() { + rm -rf "$TEST_DIR" +} + +@test "install_pf_mdm_block creates anchor file" { + FIREWALL_ANCHOR_DIR="/etc/pf.anchors" + install_pf_mdm_block "$TEST_DIR" 2>/dev/null || true + [ -f "$TEST_DIR/etc/pf.anchors/com.unleash/mdm" ] +} + +@test "install_pf_mdm_block contains Apple IP ranges" { + install_pf_mdm_block "$TEST_DIR" 2>/dev/null || true + run grep -c "17.0.0.0/8" "$TEST_DIR/etc/pf.anchors/com.unleash/mdm" + [ "$output" -gt 0 ] +} + +@test "install_pf_mdm_block updates pf.conf" { + install_pf_mdm_block "$TEST_DIR" 2>/dev/null || true + run grep -c "com.unleash" "$TEST_DIR/etc/pf.conf" + [ "$output" -gt 0 ] +} + +@test "remove_pf_mdm_block removes anchor and pf.conf entries" { + install_pf_mdm_block "$TEST_DIR" 2>/dev/null || true + remove_pf_mdm_block "$TEST_DIR" 2>/dev/null || true + [ ! -f "$TEST_DIR/etc/pf.anchors/com.unleash/mdm" ] + run grep -c "com.unleash" "$TEST_DIR/etc/pf.conf" 2>/dev/null || echo "0" + [ "$output" = "0" ] +} diff --git a/tests/test_heal.bats b/tests/test_heal.bats new file mode 100644 index 0000000..5a0537e --- /dev/null +++ b/tests/test_heal.bats @@ -0,0 +1,36 @@ +#!/usr/bin/env bats +# Tests for heal.sh - run with: bats tests/test_heal.bats + +setup() { + load '../lib/colors.sh' + load '../lib/suppress.sh' + load '../lib/heal.sh' + TEST_DIR=$(mktemp -d) + mkdir -p "$TEST_DIR/private/etc" + mkdir -p "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings" + mkdir -p "$TEST_DIR/private/var/db/com.apple.xpc.launchd" + DRY_RUN=false + suppress_enrollment "$TEST_DIR" 2>/dev/null || true +} + +teardown() { + rm -rf "$TEST_DIR" +} + +@test "heal detects clean system" { + run heal_suppress "$TEST_DIR" 2>/dev/null || true + [ "$status" -eq 0 ] +} + +@test "heal re-applies when DEP record reappears" { + touch "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordFound" + heal_suppress "$TEST_DIR" 2>/dev/null || true + [ ! -f "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordFound" ] +} + +@test "heal re-applies when hosts block missing" { + > "$TEST_DIR/private/etc/hosts" + heal_suppress "$TEST_DIR" 2>/dev/null || true + run grep -c "iprofiles.apple.com" "$TEST_DIR/private/etc/hosts" + [ "$output" -gt 0 ] +} diff --git a/tests/test_suppress.bats b/tests/test_suppress.bats new file mode 100644 index 0000000..300a1cd --- /dev/null +++ b/tests/test_suppress.bats @@ -0,0 +1,75 @@ +#!/usr/bin/env bats +# Tests for suppress.sh - run with: bats tests/test_suppress.bats + +setup() { + load '../lib/colors.sh' + load '../lib/detect.sh' + load '../lib/suppress.sh' + TEST_DIR=$(mktemp -d) + mkdir -p "$TEST_DIR/private/etc" + mkdir -p "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings" + mkdir -p "$TEST_DIR/private/var/db/com.apple.xpc.launchd" + mkdir -p "$TEST_DIR/Users/testuser/Library/Preferences" + mkdir -p "$TEST_DIR/Users/testuser/Library/Application Support" +} + +teardown() { + rm -rf "$TEST_DIR" +} + +@test "suppress_enrollment creates hosts entries" { + DRY_RUN=false + suppress_enrollment "$TEST_DIR" 2>/dev/null || true + run grep -c "iprofiles.apple.com" "$TEST_DIR/private/etc/hosts" + [ "$status" -eq 0 ] + [ "$output" -gt 0 ] +} + +@test "suppress_enrollment clears DEP markers" { + touch "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordFound" + DRY_RUN=false + suppress_enrollment "$TEST_DIR" 2>/dev/null || true + [ ! -f "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordFound" ] + [ -f "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordNotFound" ] +} + +@test "suppress_enrollment disables daemons" { + DRY_RUN=false + suppress_enrollment "$TEST_DIR" 2>/dev/null || true + local ldp="$TEST_DIR/private/var/db/com.apple.xpc.launchd/disabled.plist" + [ -f "$ldp" ] + run /usr/libexec/PlistBuddy -c "Print :com.apple.ManagedClient.enroll" "$ldp" 2>/dev/null + [ "$output" = "true" ] +} + +@test "dry-run mode does not write files" { + DRY_RUN=true + suppress_enrollment "$TEST_DIR" 2>/dev/null || true + [ ! -f "$TEST_DIR/private/etc/hosts" ] + [ ! -f "$TEST_DIR/private/var/db/com.apple.xpc.launchd/disabled.plist" ] +} + +@test "suppress_enrollment cleans user artifacts" { + touch "$TEST_DIR/Users/testuser/Library/Preferences/com.apple.mdm.test" + touch "$TEST_DIR/Users/testuser/Library/Application Support/com.apple.ManagedClient.cache" + DRY_RUN=false + suppress_enrollment "$TEST_DIR" 2>/dev/null || true + [ ! -f "$TEST_DIR/Users/testuser/Library/Preferences/com.apple.mdm.test" ] + [ ! -f "$TEST_DIR/Users/testuser/Library/Application Support/com.apple.ManagedClient.cache" ] +} + +@test "suppress_enrollment blocks org MDM host" { + mkdir -p "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings" + cat > "$TEST_DIR/private/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordFound" <<'XML' + + + +OrganizationNameACME Corp +URLhttps://mdm.acme.com/enroll + +XML + DRY_RUN=false + suppress_enrollment "$TEST_DIR" 2>/dev/null || true + run grep -c "mdm.acme.com" "$TEST_DIR/private/etc/hosts" + [ "$output" -gt 0 ] +} diff --git a/tests/test_validate.bats b/tests/test_validate.bats new file mode 100644 index 0000000..f9759a6 --- /dev/null +++ b/tests/test_validate.bats @@ -0,0 +1,51 @@ +#!/usr/bin/env bats +# Tests for validate.sh - run with: bats tests/test_validate.bats + +setup() { + load '../lib/colors.sh' + load '../lib/validate.sh' +} + +@test "validate_username accepts valid usernames" { + run validate_username "apple" + [ "$status" -eq 0 ] + run validate_username "test-user" + [ "$status" -eq 0 ] + run validate_username "test_user" + [ "$status" -eq 0 ] +} + +@test "validate_username rejects empty" { + run validate_username "" + [ "$status" -eq 1 ] +} + +@test "validate_username rejects long names" { + local long="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + run validate_username "$long" + [ "$status" -eq 1 ] +} + +@test "validate_username rejects special chars" { + run validate_username "user name" + [ "$status" -eq 1 ] + run validate_username "user.name" + [ "$status" -eq 1 ] +} + +@test "validate_password accepts valid passwords" { + run validate_password "1234" + [ "$status" -eq 0 ] + run validate_password "correct horse battery staple" + [ "$status" -eq 0 ] +} + +@test "validate_password rejects empty" { + run validate_password "" + [ "$status" -eq 1 ] +} + +@test "validate_password rejects short" { + run validate_password "ab" + [ "$status" -eq 1 ] +} diff --git a/unleash b/unleash index 275f08d..4ec4ff8 100755 --- a/unleash +++ b/unleash @@ -1,12 +1,12 @@ #!/bin/bash set -euo pipefail -VERSION="1.4.0" +VERSION="1.5.0" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" LIB_DIR="$SCRIPT_DIR/lib" -for _lib in colors config detect validate dscl suppress backup status heal firewall harden whitelist check monitor history doctor selfupdate uninstall report ma_detect; do +for _lib in colors config detect validate dscl suppress backup status heal firewall harden whitelist check monitor history doctor selfupdate uninstall report ma_detect demo vpn; do _lib_path="$LIB_DIR/$_lib.sh" if [ -f "$_lib_path" ]; then source "$_lib_path" @@ -62,6 +62,12 @@ Bypass / suppress / monitor MDM enrollment on macOS. report Generate a full markdown status report history Show event log from monitor/heal runs test Dry-run mode: simulate without changing anything + demo Run simulated bypass flow (no real changes) + + VPN: + vpn-kill Install VPN kill-switch (prevents MDM leaks outside VPN) + vpn-kill-remove Remove VPN kill-switch + vpn-kill-status Check VPN kill-switch status Management: config View/edit persistent settings (~/.unleash.conf) @@ -91,6 +97,9 @@ ALIASES up update uni uninstall rei reinstall + vk vpn-kill + vkr vpn-kill-remove + vks vpn-kill-status OPTIONS --verbose Show debug messages @@ -493,7 +502,7 @@ main() { firewall|fw) cmd_firewall ;; firewall-off|fw-off) cmd_firewall_off ;; harden) cmd_harden ;; - audit) cmd_audit ;; + audit) cmd_audit "$2" ;; whitelist|wl) cmd_whitelist ;; backup) cmd_backup ;; restore) cmd_restore ;; @@ -509,11 +518,15 @@ main() { monitor-stop|mn-stop) cmd_monitor_stop ;; monitor-status|mn-st) cmd_monitor_status ;; version|-v|--version) cmd_version ;; - doctor) run_doctor ;; + doctor) run_doctor ;; + demo) run_demo ;; update) do_self_update ;; uninstall) do_uninstall ;; - report) generate_report ;; + report) generate_report "$2" ;; config) cmd_config "$@" ;; + vpn-kill) vpn_kill_install "$@" ;; + vpn-kill-remove) vpn_kill_remove ;; + vpn-kill-status) vpn_kill_status ;; reinstall) do_uninstall install_persist_launchdaemon "" diff --git a/unleash-standalone.sh b/unleash-standalone.sh index 28a6e3e..a1a0cd0 100755 --- a/unleash-standalone.sh +++ b/unleash-standalone.sh @@ -1,6 +1,6 @@ #!/bin/bash set -euo pipefail -VERSION="1.4.0" +VERSION="1.5.0" RED='\033[1;31m' GRN='\033[1;32m' BLU='\033[1;34m' @@ -759,6 +759,10 @@ check_mdm_status() { } deep_status() { + if [ "${1:-}" = "--json" ]; then + deep_status_json + return + fi header "Deep MDM Audit" step "Installed Configuration Profiles" @@ -860,6 +864,47 @@ deep_status() { 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=xml 2>/dev/null | grep -c "ProfileDisplayName" || echo 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=$(ps aux 2>/dev/null | grep -ciE "mdm|managedclient|activation" || true) + running_procs=$((running_procs - 1)) + [ "$running_procs" -lt 0 ] && running_procs=0 + 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" +} + heal_suppress() { local data_mount="$1" @@ -1964,6 +2009,10 @@ do_uninstall() { } generate_report() { + if [ "${1:-}" = "--json" ]; then + generate_report_json + return + fi header "Unleash System Report" local ts @@ -2056,6 +2105,66 @@ generate_report() { fi } +generate_report_json() { + local report="" + report="${report}{\n" + + report="${report} \"version\": \"$VERSION\",\n" + report="${report} \"timestamp\": \"$(date -u '+%Y-%m-%dT%H:%M:%SZ')\",\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//\"/\\\"}" + report="${report} \"enrollment_state\": \"${enroll_state}\",\n" + + local has_dep="false" + if [ -f "/private/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordFound" ]; then + has_dep="true" + fi + report="${report} \"dep_record_found\": $has_dep,\n" + + local profile_count=0 + if command -v profiles &>/dev/null; then + profile_count=$(sudo profiles -C -output=xml 2>/dev/null | grep -c "ProfileDisplayName" || echo 0) + fi + report="${report} \"installed_profiles\": $profile_count,\n" + + local heal_installed="false" + [ -f "/Library/LaunchDaemons/com.unleash.heal.plist" ] && heal_installed="true" + local monitor_installed="false" + [ -f "/Library/LaunchDaemons/com.unleash.monitor.plist" ] && monitor_installed="true" + local monitor_running="false" + [ -f "/tmp/unleash-monitor.pid" ] && monitor_running="true" + report="${report} \"persistence\": {\n" + report="${report} \"heal_launchdaemon\": $heal_installed,\n" + report="${report} \"monitor_launchdaemon\": $monitor_installed,\n" + report="${report} \"monitor_running\": $monitor_running\n" + report="${report} },\n" + + local fw_active="false" + if command -v pfctl &>/dev/null && pfctl -a "com.unleash/mdm" -s rules 2>/dev/null | grep -q "block"; then + fw_active="true" + fi + local selective_active="false" + if command -v pfctl &>/dev/null && pfctl -a "com.unleash.selective" -s rules 2>/dev/null | grep -q "block"; then + selective_active="true" + fi + report="${report} \"firewall\": {\n" + report="${report} \"mdm_block_active\": $fw_active,\n" + report="${report} \"selective_block_active\": $selective_active\n" + report="${report} },\n" + + local hosts_blocked=0 + hosts_blocked=$(grep -c "0.0.0.0" /private/etc/hosts 2>/dev/null || echo 0) + report="${report} \"hosts_blocked\": $hosts_blocked\n" + + report="${report}}" + + echo -e "$report" +} + detect_migration_assistant() { local data_mount="${1:-}" @@ -2121,6 +2230,209 @@ detect_migration_assistant() { fi } +run_demo() { + header "Unleash Demo — Simulated MDM Bypass" + + local demo_dir + demo_dir=$(mktemp -d) + mkdir -p "$demo_dir/private/etc" + mkdir -p "$demo_dir/private/var/db/ConfigurationProfiles/Settings" + mkdir -p "$demo_dir/private/var/db/com.apple.xpc.launchd" + mkdir -p "$demo_dir/Users/demo/Library/Preferences" + mkdir -p "$demo_dir/Users/demo/Library/Application Support" + + touch "$demo_dir/private/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordFound" + touch "$demo_dir/Users/demo/Library/Preferences/com.apple.mdm.plist" + + echo "" + step "1. Simulating pre-bypass MDM state" + echo " DEP record: ${RED}PRESENT${NC}" + echo " Hosts block: ${RED}ABSENT${NC}" + echo " Daemon override: ${RED}ABSENT${NC}" + echo " User artifacts: ${RED}PRESENT${NC}" + sleep 1 + + echo "" + step "2. Running suppress_enrollment..." + DRY_RUN=false + suppress_enrollment "$demo_dir" 2>/dev/null || true + sleep 1 + + echo "" + step "3. Verifying post-bypass state" + local clean=true + if [ -f "$demo_dir/private/var/db/ConfigurationProfiles/Settings/.cloudConfigRecordFound" ]; then + echo " DEP record: ${RED}STILL PRESENT (bug)${NC}" + clean=false + else + echo " DEP record: ${GRN}CLEARED${NC}" + fi + if grep -q "iprofiles.apple.com" "$demo_dir/private/etc/hosts" 2>/dev/null; then + echo " Hosts block: ${GRN}ACTIVE${NC}" + else + echo " Hosts block: ${RED}ABSENT (bug)${NC}" + clean=false + fi + if [ -f "$demo_dir/Users/demo/Library/Preferences/com.apple.mdm.plist" ]; then + echo " User artifacts: ${RED}STILL PRESENT (bug)${NC}" + clean=false + else + echo " User artifacts: ${GRN}CLEANED${NC}" + fi + + echo "" + step "4. Simulating macOS update (re-enabling daemons)" + rm -f "$demo_dir/private/var/db/com.apple.xpc.launchd/disabled.plist" + echo " Daemon override: ${RED}RESET (simulating update)${NC}" + sleep 1 + + echo "" + step "5. Running heal — detecting and fixing..." + heal_suppress "$demo_dir" 2>/dev/null || true + sleep 1 + + if [ -f "$demo_dir/private/var/db/com.apple.xpc.launchd/disabled.plist" ]; then + echo " Daemon override: ${GRN}RESTORED${NC}" + fi + + echo "" + step "6. Summary" + if [ "$clean" = true ]; then + echo -e " ${GRN}✓ Demo completed successfully${NC}" + else + echo -e " ${YEL}⚠ Demo completed with warnings${NC}" + fi + echo " Simulated environment: $demo_dir" + echo "" + info "This is a simulated run. No real system was modified." + info "Run 'rm -rf $demo_dir' to clean up." + + echo "" + echo -e "${CYAN}╔═══════════════════════════════════════════════╗${NC}" + echo -e "${CYAN}║${NC} ${GRN}Demo Complete${NC} ${CYAN}║${NC}" + echo -e "${CYAN}║${NC} Commands used: suppress, heal ${CYAN}║${NC}" + echo -e "${CYAN}║${NC} Try: ${YEL}sudo ./unleash doctor${NC} for real diagnostics ${CYAN}║${NC}" + echo -e "${CYAN}╚═══════════════════════════════════════════════╝${NC}" +} + +VPN_RULES_ANCHOR="com.unleash/vpn-kill" +VPN_RULES_FILE="/etc/pf.anchors/com.unleash/vpn-kill" + +vpn_kill_install() { + header "VPN Kill-Switch — MDM Leak Protection" + + if ! is_root; then + error_exit "VPN kill-switch needs sudo: sudo ./unleash vpn-kill" + fi + + local vpn_if="" + + step "Detecting active VPN interfaces..." + local interfaces + interfaces=$(ifconfig 2>/dev/null | grep -E "^utun[0-9]+:" | sed 's/:.*//' || true) + if [ -z "$interfaces" ]; then + warn "No VPN interfaces detected" + info "You can specify manually: sudo ./unleash vpn-kill --interface utunX" + info "Common VPN interfaces: utun0-9 (WireGuard), utun10+ (OpenVPN), ppp0 (L2TP)" + else + for iface in $interfaces; do + local addr + addr=$(ifconfig "$iface" 2>/dev/null | awk '/inet /{print $2}') + if [ -n "$addr" ]; then + echo " $iface: $addr" + [ -z "$vpn_if" ] && vpn_if="$iface" + fi + done + fi + + local user_if="${2:-}" + [ -n "$user_if" ] && vpn_if="$user_if" + + if [ -z "$vpn_if" ]; then + warn "No active VPN detected. Installing kill-switch anyway (no default route)." + info "Usage: sudo ./unleash vpn-kill --interface utunX" + return 1 + fi + + step "Installing pf rules for VPN kill-switch..." + local anchor_dir="/etc/pf.anchors/com.unleash" + mkdir -p "$anchor_dir" + + cat > "$VPN_RULES_FILE" << RULES +# Unleash VPN kill-switch +# Only allow MDM traffic through VPN interface $vpn_if +block drop out proto {tcp,udp} to {17.0.0.0/8, 17.128.0.0/10} +pass out proto {tcp,udp} to {17.0.0.0/8, 17.128.0.0/10} no state +pass on $vpn_if +RULES + chmod 644 "$VPN_RULES_FILE" + success "VPN kill-switch rules installed for $vpn_if" + + local pf_conf="/etc/pf.conf" + local anchor_line="anchor \"${VPN_RULES_ANCHOR}\"" + local load_line="load anchor \"${VPN_RULES_ANCHOR}\" from \"${VPN_RULES_FILE}\"" + + if grep -q "vpn-kill" "$pf_conf" 2>/dev/null; then + info "pf.conf already has VPN kill-switch anchor" + else + echo "" >> "$pf_conf" + echo "# Added by unleash — VPN kill-switch (prevents MDM leaks)" >> "$pf_conf" + echo "$anchor_line" >> "$pf_conf" + echo "$load_line" >> "$pf_conf" + fi + + pfctl -e -f "$pf_conf" 2>/dev/null && success "VPN kill-switch active" \ + || warn "pfctl failed" + + echo "" + info "What this does:" + info " - Blocks MDM Apple IP ranges (17.0.0.0/8) on all interfaces" + info " - Only allows MDM traffic through VPN interface ($vpn_if)" + info " - If VPN drops, MDM traffic is blocked — no leaks" + echo "" + warn "This does NOT affect your regular internet traffic." + warn "Only MDM-related IPs are routed through the VPN kill-switch." +} + +vpn_kill_remove() { + header "Remove VPN Kill-Switch" + + if ! is_root; then + error_exit "Needs sudo: sudo ./unleash vpn-kill-remove" + fi + + if [ -f "$VPN_RULES_FILE" ]; then + rm -f "$VPN_RULES_FILE" + success "VPN kill-switch rules removed" + fi + + local pf_conf="/etc/pf.conf" + if [ -f "$pf_conf" ]; then + sed -i '' '/# Added by unleash — VPN kill-switch/d' "$pf_conf" 2>/dev/null || true + sed -i '' '/vpn-kill/d' "$pf_conf" 2>/dev/null || true + success "pf.conf cleaned" + fi + + pfctl -a "$VPN_RULES_ANCHOR" -F all 2>/dev/null || true + success "pf anchor flushed" +} + +vpn_kill_status() { + step "VPN kill-switch status" + if [ -f "$VPN_RULES_FILE" ]; then + echo " Rules file: $VPN_RULES_FILE" + cat "$VPN_RULES_FILE" | sed 's/^/ /' + else + echo " No VPN kill-switch installed" + fi + if command -v pfctl &>/dev/null; then + echo "" + pfctl -a "$VPN_RULES_ANCHOR" -s rules 2>/dev/null \ + && echo " Anchor loaded" \ + || echo " Anchor not loaded" + fi +} + show_help() { cat <<'HELP' @@ -2168,6 +2480,12 @@ Bypass / suppress / monitor MDM enrollment on macOS. report Generate a full markdown status report history Show event log from monitor/heal runs test Dry-run mode: simulate without changing anything + demo Run simulated bypass flow (no real changes) + + VPN: + vpn-kill Install VPN kill-switch (prevents MDM leaks outside VPN) + vpn-kill-remove Remove VPN kill-switch + vpn-kill-status Check VPN kill-switch status Management: config View/edit persistent settings (~/.unleash.conf) @@ -2197,6 +2515,9 @@ ALIASES up update uni uninstall rei reinstall + vk vpn-kill + vkr vpn-kill-remove + vks vpn-kill-status OPTIONS --verbose Show debug messages @@ -2599,7 +2920,7 @@ main() { firewall|fw) cmd_firewall ;; firewall-off|fw-off) cmd_firewall_off ;; harden) cmd_harden ;; - audit) cmd_audit ;; + audit) cmd_audit "$2" ;; whitelist|wl) cmd_whitelist ;; backup) cmd_backup ;; restore) cmd_restore ;; @@ -2615,11 +2936,15 @@ main() { monitor-stop|mn-stop) cmd_monitor_stop ;; monitor-status|mn-st) cmd_monitor_status ;; version|-v|--version) cmd_version ;; - doctor) run_doctor ;; + doctor) run_doctor ;; + demo) run_demo ;; update) do_self_update ;; uninstall) do_uninstall ;; - report) generate_report ;; + report) generate_report "$2" ;; config) cmd_config "$@" ;; + vpn-kill) vpn_kill_install "$@" ;; + vpn-kill-remove) vpn_kill_remove ;; + vpn-kill-status) vpn_kill_status ;; reinstall) do_uninstall install_persist_launchdaemon ""