v1.7.0+v1.8.0: mocks, MA deep scan, Configurator detect, pf backup, disk check
This commit is contained in:
@@ -1,9 +1,23 @@
|
|||||||
|
|
||||||
BACKUP_DIR="$(dirname "$(dirname "$0")")/.unleash-backup"
|
BACKUP_DIR="$(dirname "$(dirname "$0")")/.unleash-backup"
|
||||||
|
|
||||||
|
check_disk_space() {
|
||||||
|
local data_mount="$1"
|
||||||
|
local needed_kb=10240
|
||||||
|
local available_kb
|
||||||
|
available_kb=$(df -k "$data_mount" 2>/dev/null | tail -1 | awk '{print $4}')
|
||||||
|
if [ -n "$available_kb" ] && [ "$available_kb" -lt "$needed_kb" ]; then
|
||||||
|
warn "Low disk space: $(echo "$available_kb" | awk '{printf "%.0f MB", $1/1024}') available"
|
||||||
|
echo -n "Continue anyway? [y/N] "
|
||||||
|
read -r answer
|
||||||
|
[ "$answer" != "y" ] && [ "$answer" != "Y" ] && error_exit "Aborted by user"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
backup_state() {
|
backup_state() {
|
||||||
local data_mount="$1"
|
local data_mount="$1"
|
||||||
mkdir -p "$BACKUP_DIR"
|
mkdir -p "$BACKUP_DIR"
|
||||||
|
check_disk_space "$data_mount"
|
||||||
step "Saving backup to $BACKUP_DIR"
|
step "Saving backup to $BACKUP_DIR"
|
||||||
|
|
||||||
if [ -f "$data_mount/private/etc/hosts" ]; then
|
if [ -f "$data_mount/private/etc/hosts" ]; then
|
||||||
|
|||||||
@@ -3,16 +3,34 @@ FIREWALL_ANCHOR="com.unleash/mdm"
|
|||||||
FIREWALL_CONF="/etc/pf.conf"
|
FIREWALL_CONF="/etc/pf.conf"
|
||||||
FIREWALL_ANCHOR_DIR="/etc/pf.anchors"
|
FIREWALL_ANCHOR_DIR="/etc/pf.anchors"
|
||||||
|
|
||||||
|
pf_backup_anchor() {
|
||||||
|
local root="$1"
|
||||||
|
local anchor_file="${root}${FIREWALL_ANCHOR_DIR}/${FIREWALL_ANCHOR}"
|
||||||
|
[ ! -f "$anchor_file" ] && return 0
|
||||||
|
local backup="${anchor_file}.backup.$(date +%s)"
|
||||||
|
cp "$anchor_file" "$backup" && info "Backed up pf anchor: $backup"
|
||||||
|
}
|
||||||
|
|
||||||
|
pf_backup_conf() {
|
||||||
|
local root="$1"
|
||||||
|
local pf_conf="${root}${FIREWALL_CONF}"
|
||||||
|
[ ! -f "$pf_conf" ] && return 0
|
||||||
|
local backup="${pf_conf}.backup.$(date +%s)"
|
||||||
|
cp "$pf_conf" "$backup" && info "Backed up pf.conf: $backup"
|
||||||
|
}
|
||||||
|
|
||||||
install_pf_mdm_block() {
|
install_pf_mdm_block() {
|
||||||
local data_mount="$1"
|
local data_mount="$1"
|
||||||
local root=""
|
local root=""
|
||||||
[ -n "$data_mount" ] && root="$data_mount"
|
[ -n "$data_mount" ] && root="$data_mount"
|
||||||
|
|
||||||
step "Installing pf anchor for MDM IP block..."
|
step "Installing pf anchor for MDM IP block..."
|
||||||
|
pf_backup_conf "$root"
|
||||||
|
|
||||||
local anchor_dir="${root}${FIREWALL_ANCHOR_DIR}"
|
local anchor_dir="${root}${FIREWALL_ANCHOR_DIR}"
|
||||||
local anchor_file="${anchor_dir}/${FIREWALL_ANCHOR}"
|
local anchor_file="${anchor_dir}/${FIREWALL_ANCHOR}"
|
||||||
|
|
||||||
|
pf_backup_anchor "$root"
|
||||||
mkdir -p "$anchor_dir"
|
mkdir -p "$anchor_dir"
|
||||||
|
|
||||||
cat > "$anchor_file" << 'ANCHOR'
|
cat > "$anchor_file" << 'ANCHOR'
|
||||||
@@ -80,6 +98,7 @@ remove_pf_mdm_block() {
|
|||||||
|
|
||||||
local anchor_file="${root}${FIREWALL_ANCHOR_DIR}/${FIREWALL_ANCHOR}"
|
local anchor_file="${root}${FIREWALL_ANCHOR_DIR}/${FIREWALL_ANCHOR}"
|
||||||
if [ -f "$anchor_file" ]; then
|
if [ -f "$anchor_file" ]; then
|
||||||
|
pf_backup_anchor "$root"
|
||||||
rm -f "$anchor_file"
|
rm -f "$anchor_file"
|
||||||
success "Anchor file removed"
|
success "Anchor file removed"
|
||||||
fi
|
fi
|
||||||
|
|||||||
+83
-1
@@ -1,3 +1,58 @@
|
|||||||
|
detect_configurator_enrollment() {
|
||||||
|
local data_mount="${1:-}"
|
||||||
|
|
||||||
|
header "Apple Configurator / ASM Enrollment Check"
|
||||||
|
|
||||||
|
local sys_dir
|
||||||
|
if [ -n "$data_mount" ] && [ -d "$data_mount/private/var/db" ]; then
|
||||||
|
sys_dir="$data_mount/private/var/db"
|
||||||
|
elif [ -d "/private/var/db" ]; then
|
||||||
|
sys_dir="/private/var/db"
|
||||||
|
else
|
||||||
|
warn "Cannot locate system database directory"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
local indicators=0
|
||||||
|
|
||||||
|
# Configurator enrollment flag
|
||||||
|
if [ -f "$sys_dir/ConfigurationProflements/Setup/.configuratorEnrollment" ]; then
|
||||||
|
echo -e "${YEL}⚠ Apple Configurator enrollment detected${NC}"
|
||||||
|
indicators=$((indicators + 1))
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check for ASM/ABM cloud config records
|
||||||
|
for f in "$sys_dir"/ConfigurationProfiles/Settings/.cloudConfig*; do
|
||||||
|
[ -f "$f" ] || continue
|
||||||
|
local name
|
||||||
|
name=$(basename "$f")
|
||||||
|
echo -e "${YEL} DEP marker present: $name${NC}"
|
||||||
|
indicators=$((indicators + 1))
|
||||||
|
done
|
||||||
|
|
||||||
|
# Enrollment challenge tokens
|
||||||
|
local challenge_dir="$sys_dir/ConfigurationProflements/Setup"
|
||||||
|
if [ -f "$challenge_dir/.configuratorEnrollment" ]; then
|
||||||
|
echo -e "${YEL} Configurator challenge present${NC}"
|
||||||
|
indicators=$((indicators + 1))
|
||||||
|
fi
|
||||||
|
|
||||||
|
# DEP enrollment receipt
|
||||||
|
if [ -f "$sys_dir/com.apple.DEPReceipt" ]; then
|
||||||
|
echo -e "${YEL} DEP receipt found${NC}"
|
||||||
|
indicators=$((indicators + 1))
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$indicators" -eq 0 ]; then
|
||||||
|
echo -e "${GRN}✓ No Configurator/ASM enrollment detected${NC}"
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
echo ""
|
||||||
|
echo -e "${YEL}Fix: re-run bypass from Recovery, then run 'sudo ./unleash harden'${NC}"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
detect_migration_assistant() {
|
detect_migration_assistant() {
|
||||||
local data_mount="${1:-}"
|
local data_mount="${1:-}"
|
||||||
|
|
||||||
@@ -48,10 +103,37 @@ detect_migration_assistant() {
|
|||||||
ma_indicators=$((ma_indicators + 1))
|
ma_indicators=$((ma_indicators + 1))
|
||||||
details="$details $user: ManagedClient Application Support\n"
|
details="$details $user: ManagedClient Application Support\n"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
local caches="$home/Library/Caches"
|
||||||
|
if [ -d "$caches" ]; then
|
||||||
|
local enrollment_cache
|
||||||
|
enrollment_cache=$(ls "$caches"/com.apple.enrollment* 2>/dev/null | head -3 || true)
|
||||||
|
if [ -n "$enrollment_cache" ]; then
|
||||||
|
ma_indicators=$((ma_indicators + 1))
|
||||||
|
details="$details $user: enrollment cache files\n"
|
||||||
|
fi
|
||||||
|
|
||||||
|
local mdmd_cache
|
||||||
|
mdmd_cache=$(ls "$caches"/com.apple.mdm* 2>/dev/null | head -3 || true)
|
||||||
|
if [ -n "$mdmd_cache" ]; then
|
||||||
|
ma_indicators=$((ma_indicators + 1))
|
||||||
|
details="$details $user: MDM cache files\n"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
local keychains="$home/Library/Keychains"
|
||||||
|
if [ -f "$keychains/OCSPCache.plist" ]; then
|
||||||
|
local ocsp_mdm
|
||||||
|
ocsp_mdm=$(grep -l "mdm" "$keychains/OCSPCache.plist" 2>/dev/null || true)
|
||||||
|
if [ -n "$ocsp_mdm" ]; then
|
||||||
|
ma_indicators=$((ma_indicators + 1))
|
||||||
|
details="$details $user: MDM OCSP cache\n"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ "$ma_indicators" -gt 0 ]; then
|
if [ "$ma_indicators" -gt 0 ]; then
|
||||||
echo -e "${YEL}⚠ Migration Assistant artifacts detected:${NC}"
|
echo -e "${YEL}⚠ Migration Assistant artifacts detected (severity: $ma_indicators):${NC}"
|
||||||
echo -e "$details" | sed '/^$/d'
|
echo -e "$details" | sed '/^$/d'
|
||||||
echo ""
|
echo ""
|
||||||
echo -e "${YEL}These user-level artifacts can re-enable MDM on every login.${NC}"
|
echo -e "${YEL}These user-level artifacts can re-enable MDM on every login.${NC}"
|
||||||
|
|||||||
+45
-14
@@ -1,39 +1,70 @@
|
|||||||
#!/usr/bin/env bats
|
#!/usr/bin/env bats
|
||||||
# Tests for firewall.sh - run with: bats tests/test_firewall.bats
|
|
||||||
|
|
||||||
setup() {
|
setup() {
|
||||||
load '../lib/colors.sh'
|
load '../lib/colors.sh'
|
||||||
load '../lib/firewall.sh'
|
load '../lib/firewall.sh'
|
||||||
TEST_DIR=$(mktemp -d)
|
TEST_DIR=$(mktemp -d)
|
||||||
mkdir -p "$TEST_DIR/etc/pf.anchors/com.unleash"
|
mkdir -p "$TEST_DIR/etc/pf.anchors"
|
||||||
|
mkdir -p "$TEST_DIR/private/etc"
|
||||||
}
|
}
|
||||||
|
|
||||||
teardown() {
|
teardown() {
|
||||||
rm -rf "$TEST_DIR"
|
rm -rf "$TEST_DIR"
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "install_pf_mdm_block creates anchor file" {
|
@test "install_pf_mdm_block creates anchor" {
|
||||||
FIREWALL_ANCHOR_DIR="/etc/pf.anchors"
|
run install_pf_mdm_block "$TEST_DIR" 2>/dev/null || true
|
||||||
install_pf_mdm_block "$TEST_DIR" 2>/dev/null || true
|
|
||||||
[ -f "$TEST_DIR/etc/pf.anchors/com.unleash/mdm" ]
|
[ -f "$TEST_DIR/etc/pf.anchors/com.unleash/mdm" ]
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "install_pf_mdm_block contains Apple IP ranges" {
|
@test "install_pf_mdm_block includes MDM ranges" {
|
||||||
install_pf_mdm_block "$TEST_DIR" 2>/dev/null || true
|
run 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"
|
run grep -c "17.0.0.0/8" "$TEST_DIR/etc/pf.anchors/com.unleash/mdm"
|
||||||
[ "$output" -gt 0 ]
|
[ "$output" -gt 0 ]
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "install_pf_mdm_block updates pf.conf" {
|
@test "install_pf_mdm_block creates pf.conf" {
|
||||||
install_pf_mdm_block "$TEST_DIR" 2>/dev/null || true
|
run install_pf_mdm_block "$TEST_DIR" 2>/dev/null || true
|
||||||
|
[ -f "$TEST_DIR/etc/pf.conf" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "install_pf_mdm_block adds anchor to pf.conf" {
|
||||||
|
run install_pf_mdm_block "$TEST_DIR" 2>/dev/null || true
|
||||||
run grep -c "com.unleash" "$TEST_DIR/etc/pf.conf"
|
run grep -c "com.unleash" "$TEST_DIR/etc/pf.conf"
|
||||||
[ "$output" -gt 0 ]
|
[ "$output" -gt 0 ]
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "remove_pf_mdm_block removes anchor and pf.conf entries" {
|
@test "install_pf_mdm_block is idempotent on pf.conf" {
|
||||||
install_pf_mdm_block "$TEST_DIR" 2>/dev/null || true
|
run install_pf_mdm_block "$TEST_DIR" 2>/dev/null || true
|
||||||
remove_pf_mdm_block "$TEST_DIR" 2>/dev/null || true
|
run install_pf_mdm_block "$TEST_DIR" 2>/dev/null || true
|
||||||
|
run grep -c "com.unleash" "$TEST_DIR/etc/pf.conf"
|
||||||
|
[ "$output" -eq 2 ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "remove_pf_mdm_block cleans anchor file" {
|
||||||
|
run install_pf_mdm_block "$TEST_DIR" 2>/dev/null || true
|
||||||
|
run remove_pf_mdm_block "$TEST_DIR" 2>/dev/null || true
|
||||||
[ ! -f "$TEST_DIR/etc/pf.anchors/com.unleash/mdm" ]
|
[ ! -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" ]
|
|
||||||
|
@test "remove_pf_mdm_block cleans pf.conf" {
|
||||||
|
run install_pf_mdm_block "$TEST_DIR" 2>/dev/null || true
|
||||||
|
run remove_pf_mdm_block "$TEST_DIR" 2>/dev/null || true
|
||||||
|
run grep -c "com.unleash" "$TEST_DIR/etc/pf.conf" || true
|
||||||
|
[ "$output" -eq 0 ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "pf_backup_anchor creates backup file" {
|
||||||
|
mkdir -p "$TEST_DIR/etc/pf.anchors"
|
||||||
|
echo "test" > "$TEST_DIR/etc/pf.anchors/com.unleash/mdm"
|
||||||
|
run pf_backup_anchor "$TEST_DIR" 2>/dev/null || true
|
||||||
|
run ls "$TEST_DIR/etc/pf.anchors/com.unleash/mdm.backup."* 2>/dev/null
|
||||||
|
[ -n "$output" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "pf_backup_conf creates backup file" {
|
||||||
|
echo "test" > "$TEST_DIR/etc/pf.conf"
|
||||||
|
run pf_backup_conf "$TEST_DIR" 2>/dev/null || true
|
||||||
|
run ls "$TEST_DIR/etc/pf.conf.backup."* 2>/dev/null
|
||||||
|
[ -n "$output" ]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,11 @@
|
|||||||
setup() {
|
setup() {
|
||||||
load '../lib/colors.sh'
|
load '../lib/colors.sh'
|
||||||
load '../lib/monitor.sh'
|
load '../lib/monitor.sh'
|
||||||
|
TEST_DIR=$(mktemp -d)
|
||||||
|
}
|
||||||
|
|
||||||
|
teardown() {
|
||||||
|
rm -rf "$TEST_DIR"
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "monitor_status exits cleanly when not running" {
|
@test "monitor_status exits cleanly when not running" {
|
||||||
@@ -14,3 +19,41 @@ setup() {
|
|||||||
run stop_monitor 2>/dev/null || true
|
run stop_monitor 2>/dev/null || true
|
||||||
[ "$status" -eq 0 ]
|
[ "$status" -eq 0 ]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "install_monitor_launchdaemon creates plist file" {
|
||||||
|
SCRIPT_DIR="$TEST_DIR"
|
||||||
|
mkdir -p "$TEST_DIR/unleash"
|
||||||
|
run install_monitor_launchdaemon "$TEST_DIR" 2>/dev/null || true
|
||||||
|
[ -f "$TEST_DIR/Library/LaunchDaemons/com.unleash.monitor.plist" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "install_monitor_launchdaemon plist is valid xml" {
|
||||||
|
SCRIPT_DIR="$TEST_DIR"
|
||||||
|
mkdir -p "$TEST_DIR/unleash"
|
||||||
|
run install_monitor_launchdaemon "$TEST_DIR" 2>/dev/null || true
|
||||||
|
run grep -c "plist" "$TEST_DIR/Library/LaunchDaemons/com.unleash.monitor.plist"
|
||||||
|
[ "$output" -gt 0 ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "install_monitor_launchdaemon references unleash binary" {
|
||||||
|
SCRIPT_DIR="$TEST_DIR"
|
||||||
|
mkdir -p "$TEST_DIR/unleash"
|
||||||
|
touch "$TEST_DIR/unleash/unleash"
|
||||||
|
run install_monitor_launchdaemon "$TEST_DIR" 2>/dev/null || true
|
||||||
|
run grep -c "unleash" "$TEST_DIR/Library/LaunchDaemons/com.unleash.monitor.plist"
|
||||||
|
[ "$output" -gt 0 ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "uninstall_monitor_launchdaemon removes plist" {
|
||||||
|
SCRIPT_DIR="$TEST_DIR"
|
||||||
|
mkdir -p "$TEST_DIR/Library/LaunchDaemons"
|
||||||
|
touch "$TEST_DIR/Library/LaunchDaemons/com.unleash.monitor.plist"
|
||||||
|
run uninstall_monitor_launchdaemon "$TEST_DIR" 2>/dev/null || true
|
||||||
|
[ ! -f "$TEST_DIR/Library/LaunchDaemons/com.unleash.monitor.plist" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "monitor_mdm requires root" {
|
||||||
|
EUID=501
|
||||||
|
run monitor_mdm 2>/dev/null || true
|
||||||
|
[ "$status" -ne 0 ]
|
||||||
|
}
|
||||||
|
|||||||
+26
-7
@@ -3,19 +3,38 @@
|
|||||||
setup() {
|
setup() {
|
||||||
load '../lib/colors.sh'
|
load '../lib/colors.sh'
|
||||||
load '../lib/vpn.sh'
|
load '../lib/vpn.sh'
|
||||||
|
TEST_DIR=$(mktemp -d)
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "install_vpn_killswitch exits cleanly" {
|
teardown() {
|
||||||
run install_vpn_killswitch 2>/dev/null || true
|
rm -rf "$TEST_DIR"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "vpn_kill_install requires root" {
|
||||||
|
EUID=501
|
||||||
|
run vpn_kill_install 2>/dev/null || true
|
||||||
|
[ "$status" -ne 0 ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "vpn_kill_remove exits cleanly when not installed" {
|
||||||
|
run vpn_kill_remove 2>/dev/null || true
|
||||||
[ "$status" -eq 0 ]
|
[ "$status" -eq 0 ]
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "remove_vpn_killswitch exits cleanly" {
|
@test "vpn_kill_status exits cleanly" {
|
||||||
run remove_vpn_killswitch 2>/dev/null || true
|
run vpn_kill_status 2>/dev/null || true
|
||||||
[ "$status" -eq 0 ]
|
[ "$status" -eq 0 ]
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "vpn_killswitch_status exits cleanly" {
|
@test "vpn_kill_remove cleans pf.conf" {
|
||||||
run vpn_killswitch_status 2>/dev/null || true
|
mkdir -p "$TEST_DIR/etc"
|
||||||
[ "$status" -eq 0 ]
|
echo '# Added by unleash — VPN kill-switch' > "$TEST_DIR/etc/pf.conf"
|
||||||
|
echo 'anchor "com.unleash/vpn-kill"' >> "$TEST_DIR/etc/pf.conf"
|
||||||
|
VPN_RULES_FILE="$TEST_DIR/etc/pf.anchors/com.unleash/vpn-kill"
|
||||||
|
mkdir -p "$(dirname "$VPN_RULES_FILE")"
|
||||||
|
touch "$VPN_RULES_FILE"
|
||||||
|
|
||||||
|
PF_CONF="$TEST_DIR/etc/pf.conf"
|
||||||
|
run vpn_kill_remove 2>/dev/null || true
|
||||||
|
[ ! -f "$VPN_RULES_FILE" ]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user