diff --git a/lib/firewall.sh b/lib/firewall.sh new file mode 100644 index 0000000..e280762 --- /dev/null +++ b/lib/firewall.sh @@ -0,0 +1,115 @@ +# unleash/lib/firewall.sh — pf firewall management for MDM blocking + +FIREWALL_ANCHOR="com.unleash/mdm" +FIREWALL_CONF="/etc/pf.conf" +FIREWALL_ANCHOR_DIR="/etc/pf.anchors" + +install_pf_mdm_block() { + local data_mount="$1" + local root="" + [ -n "$data_mount" ] && root="$data_mount" + + step "Installing pf anchor for MDM IP block..." + + local anchor_dir="${root}${FIREWALL_ANCHOR_DIR}" + local anchor_file="${anchor_dir}/${FIREWALL_ANCHOR}" + + mkdir -p "$anchor_dir" + + cat > "$anchor_file" << 'ANCHOR' +# Unleash MDM block — Apple MDM infrastructure IP ranges +# These ranges host deviceenrollment.apple.com, mdmenrollment.apple.com, etc. +# Blocking at pf level is immune to DNS-over-HTTPS bypass. +block drop out proto {tcp,udp} to {17.0.0.0/8} +block drop out proto {tcp,udp} to {17.128.0.0/10} +ANCHOR + chmod 644 "$anchor_file" + success "Anchor written: $anchor_file" + + local pf_conf="${root}${FIREWALL_CONF}" + local anchor_line="anchor \"${FIREWALL_ANCHOR}\"" + local load_line="load anchor \"${FIREWALL_ANCHOR}\" from \"${anchor_file}\"" + + if [ -f "$pf_conf" ]; then + if grep -q "com.unleash" "$pf_conf" 2>/dev/null; then + info "pf.conf already has Unleash anchor" + else + echo "" >> "$pf_conf" + echo "# Added by unleash — MDM block" >> "$pf_conf" + echo "$anchor_line" >> "$pf_conf" + echo "$load_line" >> "$pf_conf" + success "pf.conf updated" + fi + else + cat > "$pf_conf" <<- EOF + # pf.conf — restored by unleash + # + # Default macOS pf.conf + scrub-anchor "com.apple/*" + nat-anchor "com.apple/*" + rdr-anchor "com.apple/*" + dummynet-anchor "com.apple/*" + fwd-anchor "com.apple/*" + anchor "com.apple/*" + load anchor "com.apple" from "/etc/pf.anchors/com.apple" + + # Added by unleash — MDM block + ${anchor_line} + ${load_line} + EOF + success "pf.conf created with Unleash anchor" + fi + + step "Loading pf rules..." + if command -v pfctl &>/dev/null; then + pfctl -e -f "$pf_conf" 2>/dev/null && success "pf enabled and rules loaded" \ + || warn "pfctl failed — try: sudo pfctl -e -f $pf_conf" + else + warn "pfctl not available" + fi + + warn "This blocks ALL Apple services (iCloud, App Store, updates)." + warn "For selective blocking, use Little Snitch or LuLu instead." +} + +remove_pf_mdm_block() { + local data_mount="$1" + local root="" + [ -n "$data_mount" ] && root="$data_mount" + + step "Removing Unleash pf anchor..." + + local anchor_file="${root}${FIREWALL_ANCHOR_DIR}/${FIREWALL_ANCHOR}" + if [ -f "$anchor_file" ]; then + rm -f "$anchor_file" + success "Anchor file removed" + fi + + local pf_conf="${root}${FIREWALL_CONF}" + if [ -f "$pf_conf" ]; then + sed -i '' '/# Added by unleash/d' "$pf_conf" 2>/dev/null || true + sed -i '' '/com\.unleash/d' "$pf_conf" 2>/dev/null || true + success "pf.conf cleaned" + fi + + step "Flushing pf anchor..." + if command -v pfctl &>/dev/null; then + pfctl -a "$FIREWALL_ANCHOR" -F all 2>/dev/null || true + success "pf anchor flushed" + else + warn "pfctl not available" + fi +} + +pf_status() { + step "pf firewall status..." + if command -v pfctl &>/dev/null; then + pfctl -si 2>/dev/null | grep -E "Status|Enabled" || echo " pf not enabled" + echo "" + pfctl -a "$FIREWALL_ANCHOR" -s rules 2>/dev/null \ + && info "Unleash MDM anchor rules:" \ + || info "No Unleash MDM anchor loaded" + else + warn "pfctl not available" + fi +} diff --git a/lib/harden.sh b/lib/harden.sh new file mode 100644 index 0000000..cc81102 --- /dev/null +++ b/lib/harden.sh @@ -0,0 +1,101 @@ +# unleash/lib/harden.sh — Live-OS hardening post-bypass + +harden_live_os() { + step "Killing running MDM processes..." + for p in ManagedClient mdmclient activationd; do + if pkill -f "$p" 2>/dev/null; then + success "Killed $p" + else + info "$p not running" + fi + done + + step "Removing residual MDM profiles..." + if command -v profiles &>/dev/null; then + local installed + installed=$(profiles -C -output=xml 2>/dev/null | grep -c "ProfileDisplayName" || true) + if [ "$installed" -gt 0 ]; then + sudo profiles -D -F 2>/dev/null && success "Forced profile removal" \ + || warn "Profile removal failed" + else + info "No installed profiles to remove" + fi + else + warn "profiles command not available" + fi + + step "Cleaning user LaunchAgents..." + local home + for home in /Users/*/; do + [ -d "$home/Library/LaunchAgents" ] || continue + local cleaned=0 + for agent in "$home/Library/LaunchAgents/"*; do + [ -f "$agent" ] || continue + if grep -qiE "(mdm|enrollment|managedclient|depnotify)" "$agent" 2>/dev/null; then + rm -f "$agent" + cleaned=$((cleaned + 1)) + fi + done + [ "$cleaned" -gt 0 ] && success "$(basename "$home"): removed $cleaned agent(s)" \ + || info "$(basename "$home"): clean" + done + + step "Flushing DNS cache..." + if command -v dscacheutil &>/dev/null; then + sudo dscacheutil -flushcache && success "DNS cache flushed" + fi + if command -v killall &>/dev/null; then + sudo killall -HUP mDNSResponder 2>/dev/null && success "mDNSResponder restarted" || true + fi + + step "Checking for MDM keychain items..." + if command -v security &>/dev/null; then + local identities + identities=$(sudo security find-identity -p basic 2>/dev/null | grep -ci mdm || true) + if [ "$identities" -gt 0 ]; then + warn "$identities MDM-related identity(ies) found in keychain" + warn "Manual review recommended: security find-identity -p basic | grep -i mdm" + else + info "No MDM identities found in keychain" + fi + else + warn "security command not available" + fi + + step "Checking for JAMF/Intune/Workspace ONE agents..." + for agent_bin in /usr/local/bin/jamf /usr/local/bin/intune /opt/cisco/anyconnect/bin/*; do + if [ -f "$agent_bin" ]; then + warn "MDM agent binary found: $agent_bin" + fi + done + + step "Disabling iCloud Private Relay (DoH source)..." + if command -v defaults &>/dev/null; then + sudo defaults write /Library/Preferences/com.apple.networkextensions.plist PrivateRelayEnabled -bool false 2>/dev/null \ + && success "Private Relay disabled" \ + || info "Private Relay not configurable (expected on some configs)" + fi + + echo "" + echo -e "${GRN}============================================${NC}" + echo -e "${GRN} Live-OS Hardening Complete ${NC}" + echo -e "${GRN}============================================${NC}" + echo "" + echo -e "${YEL}Reboot recommended to verify all changes.${NC}" + echo -e "${YEL}For per-app blocking: install Little Snitch or LuLu.${NC}" +} + +harden_status() { + step "System extension status..." + if command -v systemextensionsctl &>/dev/null; then + systemextensionsctl list 2>/dev/null | head -20 || true + else + info "systemextensionsctl not available" + fi + + step "MDM-related LaunchDaemons loaded..." + launchctl list 2>/dev/null | grep -iE "mdm|managed|enrollment|activation" || info "None loaded" + + step "Running MDM processes..." + ps aux 2>/dev/null | grep -iE "mdm|managedclient|activation" | grep -v grep || info "None running" +} diff --git a/lib/status.sh b/lib/status.sh index f048c56..b30a41a 100644 --- a/lib/status.sh +++ b/lib/status.sh @@ -30,7 +30,7 @@ check_mdm_status() { step "Blocked domains in hosts" if [ -f "$hosts" ]; then local matches - matches=$(grep -iE 'iprofiles|enrollment|mdm|acmdm' "$hosts" 2>/dev/null) + 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}" @@ -38,6 +38,8 @@ check_mdm_status() { else echo -e " ${YEL}(none)${NC}" fi + local count; count=$(echo "$matches" | grep -c . 2>/dev/null || echo 0) + echo -e " ${CYAN}Total: $count of 13 expected domains blocked${NC}" else echo -e " ${YEL}(hosts not found)${NC}" fi @@ -77,3 +79,105 @@ check_mdm_status() { echo -e "${GRN}No active DEP record.${NC}" fi } + +deep_status() { + header "Deep MDM Audit" + + step "Installed Configuration Profiles" + if command -v profiles &>/dev/null; then + local profile_count + profile_count=$(sudo profiles -C -output=xml 2>/dev/null | grep -c "ProfileDisplayName" || echo 0) + if [ "$profile_count" -gt 0 ]; then + warn "$profile_count profile(s) installed:" + sudo profiles -C -output=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=$(ls "$home/Library/LaunchAgents/" 2>/dev/null | grep -iE "mdm|enrollment|managed" || true) + if [ -n "$user_agents" ]; then + echo -e " ${YEL}$(basename "$home"):${NC}" + echo "$user_agents" | sed 's/^/ /' + 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=$(ls /Library/LaunchDaemons/ 2>/dev/null | grep -iE "mdm|enrollment|managed" || true) + if [ -n "$sys_agents" ]; then + echo "$sys_agents" | sed 's/^/ /' + else + info "No MDM LaunchDaemons found" + fi + echo "" + + step "Running MDM Processes" + local procs + procs=$(ps aux 2>/dev/null | grep -iE "mdm|managedclient|activation" | grep -v grep || true) + if [ -n "$procs" ]; then + echo "$procs" | awk '{print " " $11 " (PID " $2 ")"}' + 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" + [ "$(sudo profiles -C -output=xml 2>/dev/null | grep -c "ProfileDisplayName" || echo 0)" -gt 0 ] && risk="MEDIUM" + ps aux 2>/dev/null | grep -qiE "mdm|managedclient" && 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 +} diff --git a/lib/whitelist.sh b/lib/whitelist.sh new file mode 100644 index 0000000..c94668b --- /dev/null +++ b/lib/whitelist.sh @@ -0,0 +1,118 @@ +# unleash/lib/whitelist.sh — Selective Apple services whitelist + +# Domains needed for iCloud, App Store, and system updates +readonly ICLOUD_DOMAINS=( + "apple.com" + "icloud.com" + "icloud.apple.com" + "setup.icloud.com" + "gsa.apple.com" + "appleid.apple.com" + "idmsa.apple.com" + "keyvalueservice.icloud.com" + "ckdatabase.icloud.com" + "ckdevices.icloud.com" +) + +readonly APPSTORE_DOMAINS=( + "itunes.apple.com" + "apps.apple.com" + "appstoreconnect.apple.com" + "buy.itunes.apple.com" + "sandbox.itunes.apple.com" +) + +readonly UPDATES_DOMAINS=( + "swscan.apple.com" + "swcdn.apple.com" + "swdist.apple.com" + "gdmf.apple.com" + "ns.itunes.apple.com" +) + +# MDM domains that MUST remain blocked +readonly MDM_BLOCKLIST=( + "mdmenrollment.apple.com" + "deviceenrollment.apple.com" + "iprofiles.apple.com" +) + +# Selective pf rules: block only MDM, allow Apple services +install_selective_block() { + local data_mount="$1" + local root="" + [ -n "$data_mount" ] && root="$data_mount" + + step "Installing selective pf rules (block MDM only, allow Apple services)..." + + local anchor_dir="${root}/etc/pf.anchors" + local anchor_file="${anchor_dir}/com.unleash.selective" + mkdir -p "$anchor_dir" + + cat > "$anchor_file" << 'ANCHOR' +# Unleash selective MDM block — blocks only MDM enrollment endpoints +# Allows iCloud, App Store, and system updates through. +ANCHOR + + local d + for d in "${MDM_BLOCKLIST[@]}"; do + echo "block drop out proto {tcp,udp} to {\$(host -t a \"$d\" 2>/dev/null | awk '/has address/{print \$NF}')}" >> "$anchor_file" + echo "block drop out proto {tcp,udp} to {\$(host -t aaaa \"$d\" 2>/dev/null | awk '/has IPv6 addr/{print \$NF}')}" >> "$anchor_file" + done + + chmod 644 "$anchor_file" + success "Selective anchor written: $anchor_file" + + local pf_conf="${root}/etc/pf.conf" + local anchor_line="anchor \"com.unleash.selective\"" + local load_line="load anchor \"com.unleash.selective\" from \"${anchor_file}\"" + + if [ -f "$pf_conf" ]; then + if grep -q "com.unleash.selective" "$pf_conf" 2>/dev/null; then + info "pf.conf already has selective anchor" + else + echo "" >> "$pf_conf" + echo "# Added by unleash — selective MDM block (iCloud-safe)" >> "$pf_conf" + echo "$anchor_line" >> "$pf_conf" + echo "$load_line" >> "$pf_conf" + success "pf.conf updated with selective rules" + fi + else + cat > "$pf_conf" <<- EOF +# pf.conf — restored by unleash (selective mode) +scrub-anchor "com.apple/*" +nat-anchor "com.apple/*" +rdr-anchor "com.apple/*" +dummynet-anchor "com.apple/*" +fwd-anchor "com.apple/*" +anchor "com.apple/*" +load anchor "com.apple" from "/etc/pf.anchors/com.apple" +# Added by unleash — selective MDM block (iCloud-safe) +${anchor_line} +${load_line} +EOF + success "pf.conf created" + fi + + if command -v pfctl &>/dev/null; then + pfctl -e -f "$pf_conf" 2>/dev/null && success "pf rules loaded (iCloud should work)" \ + || warn "pfctl failed" + fi +} + +restore_hosts_based_block() { + local data_mount="$1" + local root="" + [ -n "$data_mount" ] && root="$data_mount" + + local hosts="${root}/private/etc/hosts" + step "Restoring hosts-based block (leaving unleashes entries)..." + + if [ -f "$hosts" ]; then + if grep -q "Added by unleash" "$hosts" 2>/dev/null; then + info "unleash hosts entries intact" + else + info "No unleash hosts entries found" + fi + fi +} diff --git a/unleash b/unleash index f1a37a1..0629173 100755 --- a/unleash +++ b/unleash @@ -11,6 +11,11 @@ set -euo pipefail # ./unleash heal Check and re-apply suppression (auto-heal) # ./unleash persist Install LaunchDaemon for boot-time persistence # ./unleash unpersist Remove the persistence LaunchDaemon +# ./unleash firewall Block Apple MDM IP ranges via pf (kernel-level) +# ./unleash firewall-off Remove pf firewall MDM block +# ./unleash harden Live-OS hardening (kill MDM, remove profiles) +# ./unleash audit Deep MDM audit (profiles, certs, agents, processes) +# ./unleash whitelist Selective block (block MDM only, keep iCloud/App Store) # ./unleash backup Backup current state # ./unleash restore Restore from backup # ./unleash dualboot Target a different volume (needs sudo) @@ -27,7 +32,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 heal; do +for _lib in colors detect validate dscl suppress backup status heal firewall harden whitelist; do _lib_path="$LIB_DIR/$_lib.sh" if [ -f "$_lib_path" ]; then # shellcheck disable=SC1090 @@ -175,6 +180,69 @@ cmd_unpersist() { remove_persist_launchdaemon "$data_mount" } +cmd_firewall() { + header "unleash — pf Firewall MDM Block" + if ! is_root; then + error_exit "Firewall requires sudo: sudo ./unleash firewall" + fi + if is_recovery; then + local data_mount + data_mount=$(resolve_data_volume) + install_pf_mdm_block "$data_mount" + else + install_pf_mdm_block "" + fi +} + +cmd_firewall_off() { + header "unleash — Remove pf Firewall" + if ! is_root; then + error_exit "Requires sudo: sudo ./unleash firewall-off" + fi + if is_recovery; then + local data_mount + data_mount=$(resolve_data_volume) + remove_pf_mdm_block "$data_mount" + else + remove_pf_mdm_block "" + fi +} + +cmd_harden() { + header "unleash — Live-OS Hardening" + if ! is_root; then + error_exit "Hardening requires sudo: sudo ./unleash harden" + fi + if is_recovery; then + info "Hardening from Recovery will skip live-OS checks" + harden_live_os + else + harden_live_os + fi +} + +cmd_audit() { + header "unleash — Deep MDM Audit" + if ! is_root; then + error_exit "Audit requires sudo: sudo ./unleash audit" + fi + deep_status +} + +cmd_whitelist() { + header "unleash — Selective MDM Block" + if ! is_root; then + error_exit "Whitelist requires sudo: sudo ./unleash whitelist" + fi + if is_recovery; then + local data_mount + data_mount=$(resolve_data_volume) + install_selective_block "$data_mount" + else + install_selective_block "" + fi +} + cmd_status() { if is_recovery; then local data_mount @@ -197,6 +265,11 @@ cmd_interactive_recovery() { "Full bypass (create admin + suppress MDM)" "Suppress enrollment only (no user)" "Auto-heal (check and re-suppress)" + "Install pf firewall (blocks all Apple services)" + "Remove pf firewall" + "Install selective block (iCloud-safe)" + "Live-OS harden (kill MDM processes)" + "Deep MDM audit" "Backup current state" "Restore from backup" "Check MDM status" @@ -209,13 +282,28 @@ 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" - ;; - "Auto-heal (check and re-suppress)") - heal_suppress "$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" + ;; + "Install pf firewall (blocks all Apple services)") + install_pf_mdm_block "$data_mount" + ;; + "Remove pf firewall") + remove_pf_mdm_block "$data_mount" + ;; + "Install selective block (iCloud-safe)") + install_selective_block "$data_mount" + ;; + "Live-OS harden (kill MDM processes)") + harden_live_os + ;; + "Deep MDM audit") + deep_status + ;; + "Backup current state") backup_state "$data_mount" ;; "Restore from backup") @@ -289,16 +377,21 @@ 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 ;; + heal) cmd_heal ;; + persist) cmd_persist ;; + unpersist) cmd_unpersist ;; + firewall) cmd_firewall ;; + firewall-off) cmd_firewall_off ;; + harden) cmd_harden ;; + audit) cmd_audit ;; + whitelist) cmd_whitelist ;; + backup) cmd_backup ;; + restore) cmd_restore ;; + dualboot) cmd_dualboot ;; + status) cmd_status ;; version|-v|--version) cmd_version ;; help|-h|--help) - head -40 "$0" | grep -E '^# |^# \$' + head -50 "$0" | grep -E '^# |^# \$' exit 0 ;; "") @@ -310,7 +403,7 @@ main() { ;; *) echo -e "${RED}Unknown command: $1${NC}" >&2 - echo "Usage: $0 {bypass|suppress|backup|restore|dualboot|status|version|help}" >&2 + echo "Usage: $0 {bypass|suppress|heal|persist|unpersist|firewall|firewall-off|harden|audit|whitelist|backup|restore|dualboot|status|version|help}" >&2 exit 1 ;; esac