feat: add firewall, harden, audit, whitelist commands
- firewall: pf kernel-level block of Apple MDM IP ranges (DoH-proof) - firewall-off: remove pf MDM block and restore pf.conf - harden: live-OS hardening (kill MDM processes, remove profiles, flush DNS) - audit: deep MDM audit (profiles, certs, launch agents, processes, risk scoring) - whitelist: selective block (block MDM only, preserve iCloud/App Store) - status: upgraded with deep audit mode and risk assessment - Interactive menus updated for all new commands
This commit is contained in:
+115
@@ -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
|
||||
}
|
||||
+101
@@ -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"
|
||||
}
|
||||
+105
-1
@@ -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 "<string>" | sed 's/.*<string>\(.*\)<\/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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user