testsAndMisc/linux_configuration/scripts/utils/android_guardian/service.sh

109 lines
3.0 KiB
Bash
Raw Normal View History

#!/system/bin/sh
# Android Guardian Service - runs at boot
# This service:
# 1. Monitors and protects the hosts file
# 2. Blocks installation of forbidden apps
2025-12-21 19:12:16 +01:00
# 3. Prevents module from being disabled via Magisk UI
# 4. Can only be stopped via ADB with the correct command
MODDIR=${0%/*}
GUARDIAN_DIR="/data/adb/android_guardian"
LOG_FILE="$GUARDIAN_DIR/guardian.log"
BLOCKED_APPS_FILE="$GUARDIAN_DIR/blocked_apps.txt"
CONTROL_FILE="$GUARDIAN_DIR/control"
HOSTS_BACKUP="$GUARDIAN_DIR/hosts.backup"
2025-12-21 19:12:16 +01:00
MODULE_DIR="/data/adb/modules/android_guardian"
DISABLE_FILE="$MODULE_DIR/disable"
REMOVE_FILE="$MODULE_DIR/remove"
# Ensure guardian directory exists
mkdir -p "$GUARDIAN_DIR"
log() {
2026-02-20 00:21:41 +01:00
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >>"$LOG_FILE"
}
# Initialize control file if not exists
2026-02-20 00:21:41 +01:00
[ ! -f "$CONTROL_FILE" ] && echo "ENABLED" >"$CONTROL_FILE"
log "=== Android Guardian starting ==="
# Enable wireless ADB on boot (persistent port 5555)
setprop service.adb.tcp.port 5555
stop adbd
start adbd
log "Wireless ADB enabled on port 5555"
2025-12-21 19:12:16 +01:00
# Function to check if guardian is enabled (via ADB control, not Magisk UI)
is_enabled() {
2026-02-20 00:21:41 +01:00
[ "$(cat "$CONTROL_FILE" 2>/dev/null)" = "ENABLED" ]
}
2025-12-21 19:12:16 +01:00
# Function to protect module from being disabled via Magisk UI
protect_module() {
2026-02-20 00:21:41 +01:00
# Remove disable file if someone tried to disable via Magisk
if [ -f "$DISABLE_FILE" ]; then
log "Module disable attempt detected via Magisk UI! Re-enabling..."
rm -f "$DISABLE_FILE"
log "Module re-enabled"
fi
# Remove remove file if someone tried to uninstall via Magisk
if [ -f "$REMOVE_FILE" ]; then
log "Module removal attempt detected via Magisk UI! Blocking..."
rm -f "$REMOVE_FILE"
log "Module removal blocked"
fi
2025-12-21 19:12:16 +01:00
}
# Function to restore hosts file if tampered
protect_hosts() {
2026-02-20 00:21:41 +01:00
if [ -f "$HOSTS_BACKUP" ]; then
current_hash=$(md5sum /system/etc/hosts 2>/dev/null | cut -d' ' -f1)
backup_hash=$(md5sum "$HOSTS_BACKUP" 2>/dev/null | cut -d' ' -f1)
if [ "$current_hash" != "$backup_hash" ]; then
log "Hosts file tampering detected! Restoring..."
cp "$HOSTS_BACKUP" "$MODDIR/system/etc/hosts"
log "Hosts file restored"
fi
fi
}
# Function to uninstall blocked apps
check_blocked_apps() {
2026-02-20 00:21:41 +01:00
if [ ! -f "$BLOCKED_APPS_FILE" ]; then
return
fi
while IFS= read -r package || [ -n "$package" ]; do
# Skip comments and empty lines
case "$package" in
\#* | "") continue ;;
esac
# Check if package is installed
if pm list packages 2>/dev/null | grep -q "package:$package"; then
log "Blocked app detected: $package - Uninstalling..."
pm uninstall "$package" 2>/dev/null && log "Uninstalled: $package" || log "Failed to uninstall: $package"
fi
done <"$BLOCKED_APPS_FILE"
}
2025-12-21 19:12:16 +01:00
# Main monitoring loop - runs every 5 seconds for faster protection
while true; do
2026-02-20 00:21:41 +01:00
# ALWAYS protect module from UI disabling (even if guardian is "disabled" via ADB)
# This ensures only ADB can control the guardian
protect_module
2025-12-21 19:12:16 +01:00
2026-02-20 00:21:41 +01:00
if is_enabled; then
protect_hosts
check_blocked_apps
fi
2026-02-20 00:21:41 +01:00
# Check every 5 seconds (faster response to disable attempts)
sleep 5
done &
log "Guardian service started (PID: $!)"