#!/bin/bash
# ── Providence Shield · CURE — check a Mac or Linux PC for known malware ────────────────────────────────────────
# curl -fsSL https://api.providenceai.pro/shield/cure.sh | bash
# … | bash -s -- --restore put back what this tool moved to quarantine (asks about each item)
#
# Free. Runs on YOUR computer. This script uploads nothing.
#
# What it does, exactly:
# Mac : asks Apple's own XProtect removers to run (needs your password), reviews the programs that start
# themselves, lists configuration profiles, and — only if ClamAV is already installed — scans your
# downloads, desktop, apps and start-up folders with it.
# Linux : scans with ClamAV if it is installed (on Debian/Ubuntu it offers to install it, and asks first),
# shows rkhunter warnings if rkhunter is already installed, and reviews scheduled jobs.
#
# Rules this script keeps:
# · It never says a computer is clean. It says which scanners ran, what they found, and which did not run.
# · Nothing is moved without a question about that one item (default answer: No). Nothing is ever deleted.
# · The only things it can move: a file ClamAV flagged twice, or a launch file (.plist) you chose to switch off.
# Those go to a quarantine folder and --restore puts them back. Configuration profiles are never removed.
# · A pattern match of ours is reported as "looks suspicious — review", never as "virus".
#
# Unattended use (tests): SHIELD_ANSWER=y|n answers every question, SHIELD_NO_SUDO=1 never asks for admin rights,
# SHIELD_CURE_PATHS=/a:/b limits the ClamAV scan to those folders, SHIELD_HOME=
moves the report/quarantine folder.
#
# The whole script is one function called on the last line, so a download that stops half-way runs nothing.
main() {
set -u
PATH=/usr/bin:/bin:/usr/sbin:/sbin; export PATH
umask 077
OS="$(uname)"
SH="${SHIELD_HOME:-$HOME/.providence-shield}"
STAMP="$(date +%Y-%m-%d_%H%M%S)"
UQ="$SH/quarantine" # things moved WITHOUT admin rights; restored without them
RQ="/private/var/db/providence-shield" # Mac only: root-owned 0700, for system launch files moved with admin rights
REPORT="$SH/cure_$STAMP.txt"
SELF_URL="https://api.providenceai.pro/shield/cure.sh"
ESC=$'\033'; TAB=$'\t'
mkdir -p "$SH" 2>/dev/null || { echo "Cannot create $SH — nothing was checked."; return 1; }
if [ -L "$SH" ] || [ ! -O "$SH" ]; then
echo "The folder $SH does not belong to the account running this (were you using sudo?)."
echo "Run it as yourself, without sudo — it asks for the password when it needs it. Nothing was checked."
return 1
fi
if [ -t 1 ]; then G="${ESC}[38;5;178m"; R="${ESC}[31m"; GR="${ESC}[32m"; N="${ESC}[0m"; TTYOUT=1; else G=""; R=""; GR=""; N=""; TTYOUT=0; fi
TTY=0; if { : < /dev/tty; } 2>/dev/null && { : > /dev/tty; } 2>/dev/null; then TTY=1; fi
plain() { sed "s/${ESC}\\[[0-9;]*[A-Za-z]//g"; } # the report file gets no colour codes
say() { printf '%s\n' "$*"; printf '%s\n' "$*" | plain >> "$REPORT"; }
hdr() { say ""; say "${G}== $*${N}"; }
clean() { printf '%s' "$1" | LC_ALL=C tr -d '\000-\037\177'; } # file names can carry terminal escape codes
b64e() { printf '%s' "$1" | base64 | tr -d '\n'; }
b64d() { printf '%s' "$1" | base64 --decode 2>/dev/null; }
fmode() { if [ "$OS" = "Darwin" ]; then stat -f %Lp "$1" 2>/dev/null; else stat -c %a "$1" 2>/dev/null; fi; }
addlist() { if [ -n "$1" ]; then printf '%s, %s' "$1" "$2"; else printf '%s' "$2"; fi; }
# Questions read the keyboard (/dev/tty) — never stdin, which is the script itself under curl | bash.
ask() {
local a=""
if [ -n "${SHIELD_ANSWER:-}" ]; then
say "$1 [y/N] $SHIELD_ANSWER (AUTOMATIC ANSWER)"
case "$SHIELD_ANSWER" in [Yy]*) return 0;; esac; return 1
fi
if [ "$TTY" = 1 ]; then
printf '%s [y/N] ' "$1" > /dev/tty; IFS= read -r a < /dev/tty || a=""
printf '%s [y/N] %s\n' "$1" "$(clean "$a")" | plain >> "$REPORT"
else
say "$1"; say " (no keyboard to ask — left alone)"
fi
case "$a" in [Yy]|[Yy][Ee][Ss]) return 0;; esac; return 1
}
# ── admin rights: one helper, so SHIELD_NO_SUDO stops every use ──
ROOT=0; KEEP="" # 0 none · 1 already root · 2 through sudo
asroot() {
if [ "$ROOT" = 1 ]; then "$@"; elif [ "$ROOT" = 2 ]; then /usr/bin/sudo -n "$@"; else return 1; fi
}
cleanup() {
if [ -n "$KEEP" ]; then kill "$KEEP" 2>/dev/null; wait "$KEEP" 2>/dev/null; fi
rm -f "$SH/.xp_$STAMP" "$SH/.hits_$STAMP" "$SH/.errs_$STAMP" "$SH/.cnt_$STAMP" "$SH/.one_$STAMP" 2>/dev/null
}
trap cleanup EXIT
get_root() {
if [ "$(id -u)" = 0 ]; then ROOT=1; return 0; fi
[ "${SHIELD_NO_SUDO:-}" = 1 ] && return 1
[ -x /usr/bin/sudo ] || return 1
if /usr/bin/sudo -n true 2>/dev/null; then ROOT=2
elif [ "$TTY" = 1 ] && [ -z "${SHIELD_ANSWER:-}" ]; then
say " $1"
if /usr/bin/sudo -v < /dev/tty; then ROOT=2; fi
fi
[ "$ROOT" = 2 ] || return 1
# keep the admin ticket alive; the loop only ends when this script does, so the trap never kills a reused PID
( while kill -0 "$$" 2>/dev/null; do /usr/bin/sudo -n -v 2>/dev/null; sleep 50; done ) /dev/null 2>&1 &
KEEP=$!
return 0
}
# ── quarantine: every entry is its own folder // holding "item" (the file) and "meta" ──
# meta lines: v1 / file / base64(original path) / octal mode / base64(reason). Names never go into a path, so they
# cannot be too long, collide, or point somewhere else.
NQ=0; MOVED=0
write_meta() { printf 'v1\nfile\n%s\n%s\n%s\n' "$(b64e "$1")" "$2" "$(b64e "$3")"; }
quarantine_user() { # — only a regular file this account can move by itself
local p="$1" why="$2" d mode
[ -f "$p" ] && [ ! -L "$p" ] || return 1
mode="$(fmode "$p")"; case "$mode" in [0-7][0-7][0-7]|[0-7][0-7][0-7][0-7]) ;; *) mode=600;; esac
NQ=$((NQ+1)); d="$UQ/$STAMP/$NQ"
mkdir -p "$d" 2>/dev/null || return 1
write_meta "$p" "$mode" "$why" > "$d/meta" || { rmdir "$d" 2>/dev/null; return 1; }
if mv -- "$p" "$d/item" 2>/dev/null && [ ! -e "$p" ] && [ -f "$d/item" ]; then
chmod a-x "$d/item" 2>/dev/null; MOVED=$((MOVED+1)); return 0
fi
rm -f "$d/meta"; rmdir "$d" 2>/dev/null; return 1
}
rq_ready() { # the root-only quarantine: must be a real root-owned directory
[ "$OS" = "Darwin" ] || return 1
asroot /bin/mkdir -p "$RQ/quarantine" 2>/dev/null || return 1
[ -d "$RQ" ] && [ ! -L "$RQ" ] || return 1
[ "$(ls -ldn "$RQ" | awk '{print $3}')" = 0 ] || return 1
asroot /bin/chmod 700 "$RQ" 2>/dev/null || return 1
}
quarantine_root() { #
local p="$1" why="$2" d mode
[ -f "$p" ] && [ ! -L "$p" ] || return 1
rq_ready || return 1
mode="$(fmode "$p")"; case "$mode" in [0-7][0-7][0-7]|[0-7][0-7][0-7][0-7]) ;; *) mode=644;; esac
NQ=$((NQ+1)); d="$RQ/quarantine/$STAMP/$NQ"
asroot /bin/mkdir -p "$d" 2>/dev/null || return 1
write_meta "$p" "$mode" "$why" | asroot /usr/bin/tee "$d/meta" >/dev/null || return 1
if asroot /bin/mv -- "$p" "$d/item" 2>/dev/null && [ ! -e "$p" ]; then MOVED=$((MOVED+1)); return 0; fi
asroot /bin/rm -f "$d/meta" 2>/dev/null; return 1
}
# ── restore ──
restore_one() { #
local d="$1" how="$2" meta ver kind o64 mode w64 orig why
if [ "$how" = root ]; then
meta="$(asroot /bin/cat "$d/meta" 2>/dev/null)" || return 0
asroot /bin/test -f "$d/item" && ! asroot /bin/test -L "$d/item" || return 0
else
[ -f "$d/meta" ] && [ ! -L "$d/meta" ] && [ -O "$d/meta" ] && [ -f "$d/item" ] && [ ! -L "$d/item" ] || return 0
meta="$(cat "$d/meta")"
fi
{ IFS= read -r ver; IFS= read -r kind; IFS= read -r o64; IFS= read -r mode; IFS= read -r w64; } </dev/null; asroot /bin/rm -f "$d/meta"; asroot /bin/rmdir "$d" 2>/dev/null
RDONE=$((RDONE+1)); say " ${GR}put back.${N} It will start again after the next restart."
else say " ${R}could not put it back${N} — it is still in quarantine."; fi
else
ask " Move it back there?" || return 0
if [ ! -d "$(dirname "$orig")" ]; then mkdir -p "$(dirname "$orig")" 2>/dev/null; fi
if mv -- "$d/item" "$orig" 2>/dev/null && [ -e "$orig" ]; then
chmod "$mode" "$orig" 2>/dev/null; rm -f "$d/meta"; rmdir "$d" 2>/dev/null; rmdir "$(dirname "$d")" 2>/dev/null
RDONE=$((RDONE+1)); say " ${GR}put back.${N}"
else say " ${R}could not put it back${N} — it is still in quarantine ($(clean "$d/item"))."; fi
fi
}
do_restore() {
local d
RSEEN=0; RDONE=0
say "${G}Providence Shield · restore${N} $(date)"
for d in "$UQ"/*/*; do [ -d "$d" ] && [ ! -L "$d" ] || continue; restore_one "$d" user; done
if [ "$OS" = "Darwin" ] && [ -d "$RQ" ]; then
if get_root "Some items were moved with admin rights. Your Mac password is needed to put them back."; then
while IFS= read -r d; do
case "$d" in "$RQ/quarantine/"[0-9]*/[0-9]*) restore_one "$d" root;; esac
done </dev/null)
EOF
else say " · items moved with admin rights (if any) were not looked at — no admin rights this time."; fi
fi
if [ "$RSEEN" = 0 ]; then say " Nothing is in quarantine."; else say "Put back $RDONE of $RSEEN item(s). Report: $REPORT"; fi
}
case "${1:-}" in
--restore) do_restore; return 0;;
"") ;;
*) echo "Usage: cure.sh [--restore]"; return 2;;
esac
FOUND=0 # a scanner (ClamAV, Apple's removers) named a threat
SUSP=0 # one of OUR patterns matched — "looks suspicious", not proof
REVIEW=0 # things listed for you to look at; not counted as problems
DISABLED=0 # launch files switched off at your choice
OKBY=""; NOTRUN=""; RANFOUND=""
say "${G}Providence Shield · CURE${N} $(date) ($OS)"
say " Report: $REPORT"
if [ -n "${SHIELD_ANSWER:-}" ]; then
say "${R} ############################################################################${N}"
say "${R} # UNATTENDED MODE: every question is answered \"$(clean "$SHIELD_ANSWER")\" automatically.${N}"
say "${R} # (SHIELD_ANSWER is set. Unset it to be asked about each item.)${N}"
say "${R} ############################################################################${N}"
fi
if [ "$OS" = "Darwin" ]; then
get_root "Your Mac password is asked once: Apple's removers and the computer-wide start-up folders need administrator rights." || say " (no administrator rights — the parts that need them are skipped and listed at the end)"
else
get_root "Your password may be asked once: installing or updating the scanner needs administrator rights." || say " (no administrator rights — the parts that need them are skipped and listed at the end)"
fi
FDA=1
if [ "$OS" = "Darwin" ] && ! ls "/Library/Application Support/com.apple.TCC" >/dev/null 2>&1; then
FDA=0
say " ${R}! This Terminal does not have Full Disk Access, so this is a PARTIAL check:${N} macOS hides parts of"
say " your Library (Mail, Safari, Messages, other apps' data) from it. To check everything: System Settings ›"
say " Privacy & Security › Full Disk Access › switch on Terminal, quit Terminal, then run this again."
fi
# ── 1. Apple's removers ──
xp_status() { grep -o '"status_message"[[:space:]]*:[[:space:]]*"[^"]*"' | tail -1 | sed 's/.*:[[:space:]]*"//; s/"$//'; }
if [ "$OS" = "Darwin" ]; then
hdr "1 · Apple's malware removers (XProtect Remediator)"
XP="/Library/Apple/System/Library/CoreServices/XProtect.app/Contents/MacOS"
XT="$SH/.xp_$STAMP"
if [ "$ROOT" = 0 ]; then
say " · not run — needs your Mac password"; NOTRUN="$(addlist "$NOTRUN" "Apple's XProtect removers (need the Mac password)")"
elif [ ! -d "$XP" ]; then
say " · not run — this macOS has no XProtect Remediator"; NOTRUN="$(addlist "$NOTRUN" "Apple's XProtect removers (not on this macOS)")"
else
if [ -x /usr/bin/xprotect ]; then
if asroot /usr/bin/xprotect update /dev/null 2>&1; then say " ✓ Apple's malware list is up to date"; else say " · could not update Apple's malware list right now — using the one already on this Mac"; fi
fi
T0="$(date '+%Y-%m-%d %H:%M:%S')"
xn=0; xclean=0; xunk=0; xfail=0; XUNK=""; XDET=""
for r in "$XP"/XProtectRemediator*; do
[ -f "$r" ] && [ -x "$r" ] || continue
name="${r##*/XProtectRemediator}"; xn=$((xn+1))
if [ "$TTYOUT" = 1 ]; then printf ' running Apple remover %-24s\r' "$name"; fi
asroot "$r" "$XT" 2>&1; rc=$?
st="$(xp_status < "$XT")"
if [ -z "$st" ]; then XUNK="$XUNK $name"; [ "$rc" = 0 ] || xfail=$((xfail+1)); XRC="$rc"
else XDET="$XDET$name=$st "; fi
done
if [ "$TTYOUT" = 1 ]; then printf '%60s\r' ""; fi
# removers that printed nothing: their result may be in the system log of the last few minutes
if [ -n "$XUNK" ]; then
/usr/bin/log show --start "$T0" --info --style compact --predicate 'subsystem BEGINSWITH "com.apple.XProtectFramework" AND eventMessage CONTAINS "status_message"' >"$XT" 2>/dev/null
for name in $XUNK; do
st="$(grep "XProtectRemediator$name\\[" "$XT" | xp_status)"
if [ -n "$st" ]; then XDET="$XDET$name=$st "; else xunk=$((xunk+1)); fi
done
fi
for e in $XDET; do
name="${e%%=*}"; st="${e#*=}"
case "$st" in
NoThreatDetected) xclean=$((xclean+1));;
ThreatRemediated|Remediated)
FOUND=$((FOUND+1)); say " ${R}! Apple's remover \"$name\" reports it removed a threat${N} (status: $(clean "$st"))";;
*Threat*|*Detect*|*Remediat*|*Infect*)
FOUND=$((FOUND+1)); say " ${R}! Apple's remover \"$name\" reports: $(clean "$st")${N} — it did not say the threat was removed";;
*) xunk=$((xunk+1)); say " · Apple's remover \"$name\" ended with a status we do not know: $(clean "$st")";;
esac
done
if [ "$xn" = 0 ]; then
say " · no removers found on this Mac"; NOTRUN="$(addlist "$NOTRUN" "Apple's XProtect removers (none found)")"
elif [ "$xunk" = 0 ]; then
say " $xclean of $xn of Apple's removers reported \"no threat detected\"."
if [ "$xclean" = "$xn" ]; then OKBY="$(addlist "$OKBY" "Apple's XProtect removers ($xn)")"; fi
elif [ "$xfail" = "$xn" ]; then
# every remover refused to start (seen on macOS 26 and 27: exit code 27, "launched incorrectly") — nothing was scanned
say " Apple's removers did not run: this macOS version only lets the system itself start them (exit code ${XRC:-?})."
say " macOS runs them on its own in the background; this script cannot tell you what they found."
NOTRUN="$(addlist "$NOTRUN" "Apple's XProtect removers (this macOS version only lets the system start them)")"
else
say " Apple's remover ran; its result could not be read on this macOS version ($xunk of $xn gave no readable result)."
NOTRUN="$(addlist "$NOTRUN" "Apple's XProtect removers (result could not be read on this macOS version)")"
fi
if [ -x /usr/bin/xprotect ]; then /usr/bin/xprotect status 2>/dev/null | head -4 | while IFS= read -r l; do say " $(clean "$l")"; done; fi
[ "$FDA" = 1 ] || say " · without Full Disk Access Apple's removers could not look everywhere either."
fi
fi
# ── 2. ClamAV ──
hdr "2 · ClamAV virus scan"
CLAM=""; for c in /usr/bin/clamscan /usr/local/bin/clamscan /opt/homebrew/bin/clamscan; do if [ -x "$c" ]; then CLAM="$c"; break; fi; done
if [ -z "$CLAM" ] && [ "$OS" = "Linux" ]; then
if [ -x /usr/bin/apt-get ] && [ "$ROOT" != 0 ]; then
say " ClamAV (a free, open-source virus scanner) is not installed."
if ask " Install it now with apt? (adds the clamav packages, roughly 300 MB with its virus list)"; then
say " installing… (this can take a few minutes)"
asroot /usr/bin/env DEBIAN_FRONTEND=noninteractive /usr/bin/apt-get update -q /dev/null 2>&1
if asroot /usr/bin/env DEBIAN_FRONTEND=noninteractive /usr/bin/apt-get install -y -q --no-install-recommends clamav /dev/null 2>&1 && [ -x /usr/bin/clamscan ]; then
CLAM=/usr/bin/clamscan; say " ✓ ClamAV installed"
else say " ${R}✕ the install did not work${N} — try yourself: sudo apt-get install clamav"; NOTRUN="$(addlist "$NOTRUN" "ClamAV (install failed)")"; fi
else NOTRUN="$(addlist "$NOTRUN" "ClamAV (not installed; you chose not to install it)")"; fi
elif [ -x /usr/bin/apt-get ]; then
say " · ClamAV is not installed and installing it needs administrator rights."; NOTRUN="$(addlist "$NOTRUN" "ClamAV (not installed; no administrator rights to install it)")"
else
say " · ClamAV is not installed. Install the package \"clamav\" with your system's package manager, then run this again."
NOTRUN="$(addlist "$NOTRUN" "ClamAV (not installed; this system has no apt to install it with)")"
fi
elif [ -z "$CLAM" ]; then
say " · ClamAV is not installed, so no file scan was done. To add one: install Homebrew, run"
say " brew install clamav and then run this again."
NOTRUN="$(addlist "$NOTRUN" "ClamAV (not installed)")"
fi
case "$CLAM" in /usr/local/*|/opt/homebrew/*)
if [ "$(id -u)" = 0 ]; then
say " · ClamAV here comes from Homebrew; it is never run as root. Run this as your normal user for the file scan."
NOTRUN="$(addlist "$NOTRUN" "ClamAV (Homebrew program, not run as root)")"; CLAM=""
fi;;
esac
if [ -n "$CLAM" ]; then
FRESH="${CLAM%clamscan}freshclam"; PREFIX="${CLAM%/bin/clamscan}"
DBS="/var/lib/clamav $PREFIX/var/lib/clamav /usr/local/share/clamav"
dbok() { local d f; for d in $DBS; do for f in main.cvd main.cld daily.cvd daily.cld; do [ -s "$d/$f" ] && return 0; done; done; return 1; }
# update the virus list. A Homebrew freshclam runs as YOU, never as root; the system one in /usr/bin may use admin rights.
upd=1
if [ -x "$FRESH" ]; then
case "$FRESH" in
/usr/bin/*) if asroot "$FRESH" --quiet /dev/null 2>&1 || "$FRESH" --quiet /dev/null 2>&1; then upd=0; fi;;
*) if "$FRESH" --quiet /dev/null 2>&1; then upd=0
else # a fresh Homebrew install has no freshclam.conf — use a minimal one of our own
mkdir -p "$PREFIX/var/lib/clamav" 2>/dev/null
printf 'DatabaseMirror database.clamav.net\n' > "$SH/freshclam.conf"
if "$FRESH" --quiet --config-file="$SH/freshclam.conf" /dev/null 2>&1; then upd=0; fi
fi;;
esac
fi
if [ "$upd" = 0 ]; then say " ✓ virus list updated"
elif dbok; then
say " · could not update the virus list now — scanning with the one already installed"
for d in $DBS; do if [ -n "$(find "$d" -maxdepth 1 -name 'daily.c?d' -mtime +30 2>/dev/null)" ]; then say " ${R}that list is more than 30 days old${N} — new malware will be missed"; fi; done
elif [ "$OS" = "Linux" ]; then
say " · the virus list is not there yet — waiting up to 3 minutes for the system's updater to download it"
w=0; while [ "$w" -lt 36 ] && ! dbok; do sleep 5; w=$((w+1)); done
fi
STDDB=1; dbok || STDDB=0
if [ -n "${SHIELD_CURE_PATHS:-}" ]; then IFS=: read -r -a TARGETS <<< "$SHIELD_CURE_PATHS"
elif [ "$OS" = "Darwin" ]; then TARGETS=("$HOME/Downloads" "$HOME/Desktop" "$HOME/Library/LaunchAgents" "$HOME/Library/Application Support" "/Applications" "/Library/LaunchAgents" "/Library/LaunchDaemons" "/private/tmp" "/private/var/tmp")
else TARGETS=("$HOME/Downloads" "$HOME/Desktop" "$HOME/.local" "$HOME/.config/autostart" "$HOME/.config/systemd" "/tmp" "/var/tmp" "/etc/cron.d" "/etc/systemd/system"); fi
EX=(); for t in "${TARGETS[@]}"; do case "$t" in /*) if [ -e "$t" ]; then EX+=("$t"); fi;; esac; done
if [ "${#EX[@]}" -eq 0 ]; then
say " · none of the folders to scan exist — nothing scanned"; NOTRUN="$(addlist "$NOTRUN" "ClamAV (no folder to scan)")"
else
say " Scanning these folders (large ones can take a long time — it is working while the counter moves):"
for t in "${EX[@]}"; do say " $(clean "$t")"; done
HITS="$SH/.hits_$STAMP"; ERRS="$SH/.errs_$STAMP"; CNT="$SH/.cnt_$STAMP"; : > "$HITS"; : > "$CNT"
"$CLAM" -r --no-summary -- "${EX[@]}" 2>"$ERRS" > "$HITS";; esac
if [ "$TTYOUT" = 1 ] && [ $((n % 50)) = 0 ]; then
cur=""; for t in "${EX[@]}"; do case "$line" in "$t"/*) cur="$t";; esac; done
printf '\r %d files checked — now in %s%s' "$n" "$(clean "$cur")" "${ESC}[K"
fi
done
printf '%s\n' "$n" > "$CNT"
}
crc="${PIPESTATUS[0]}"
if [ "$TTYOUT" = 1 ]; then printf '\r%s' "${ESC}[K"; fi
nfiles="$(cat "$CNT" 2>/dev/null)"; case "$nfiles" in ''|*[!0-9]*) nfiles=0;; esac
nerr="$(grep -c . "$ERRS" 2>/dev/null)"; case "$nerr" in ''|*[!0-9]*) nerr=0;; esac
if [ "$crc" -ge 2 ] && [ "$nfiles" = 0 ]; then
# exit 2 with nothing scanned = the scanner itself could not start (most often: no virus list downloaded yet)
say " ${R}✕ ClamAV could not scan (exit code $crc).${N} Nothing was checked by it."
head -3 "$ERRS" | while IFS= read -r l; do say " $(clean "$l")"; done
if [ "$STDDB" = 0 ]; then say " Its virus list is missing. Download it with: freshclam (Linux: sudo freshclam) and run this again."; fi
NOTRUN="$(addlist "$NOTRUN" "ClamAV (could not start: exit code $crc$( [ "$STDDB" = 0 ] && printf ', virus list missing'))")"
else
chits=0; unconf=0
# Strict parse: only lines ending in " FOUND"; the path is everything before the LAST ": "; it must be a regular,
# non-symlink file (never a folder); and ClamAV must flag that one file again (exit code 1) before it counts.
while IFS= read -r line; do
case "$line" in *" FOUND") ;; *) continue;; esac
f="${line%: *}"; sig="${line##*: }"; sig="${sig% FOUND}"
if [ ! -f "$f" ] || [ -L "$f" ]; then unconf=$((unconf+1)); say " · ClamAV printed a hit we could not match to one file: $(clean "$line")"; continue; fi
"$CLAM" --no-summary -- "$f" "$SH/.one_$STAMP" 2>/dev/null; one=$?
if [ "$one" != 1 ]; then unconf=$((unconf+1)); say " · ClamAV flagged, then did not confirm (not counted): $(clean "$f")"; continue; fi
chits=$((chits+1)); FOUND=$((FOUND+1))
say " ${R}! ClamAV: $(clean "$sig")${N}"
say " file: $(clean "$f")"
case "$f" in
*.app/*)
say " It is inside an app. Not moved — taking one piece out of an app breaks it. If you do not trust the app, drag the whole app to the Trash.";;
*/Library/Application\ Support/MobileSync/*)
say " It is inside an iPhone/iPad backup. Not moved — that would damage the backup. The file cannot run on this Mac.";;
*.sqlite|*.sqlite3|*.sqlite-wal|*.sqlite-shm|*.sqlitedb|*.db|*.db-wal|*.db-shm|*.realm|*.ldb|*.mbox|*.mbox/*|*.emlx|*.pst|*.ost|*/Library/Mail/*|*/.thunderbird/*|*/Thunderbird/Profiles/*)
say " It is a database or mailbox an app is using. Not moved — that could lose your data. Usually this is an infected attachment or download record: delete that message or item in the app.";;
*)
if [ ! -w "$(dirname "$f")" ]; then
say " Not moved — this account cannot move it (it belongs to the system or another user). Remove it yourself if you do not need it."
elif ask " Move this one file to quarantine? (it can be put back with --restore)"; then
if quarantine_user "$f" "ClamAV: $sig"; then say " ${GR}moved to quarantine${N}"; else say " ${R}could not move it${N} — it is still where it was"; fi
else say " left where it is"; fi;;
esac
done < "$HITS"
scope="$nfiles files"; [ "$crc" -ge 2 ] && scope="$scope; PARTIAL — $nerr files or folders could not be read"
if [ "$crc" -ge 2 ]; then say " · ClamAV could not read $nerr files or folders (no permission, or macOS privacy protection) — those were NOT checked."; fi
if [ "$STDDB" = 0 ]; then
say " ${R}· ClamAV's standard virus list is missing${N} — it scanned with almost no signatures. Run freshclam and scan again."
NOTRUN="$(addlist "$NOTRUN" "ClamAV (standard virus list missing — scan not meaningful)")"
elif [ "$chits" = 0 ] && [ "$unconf" = 0 ]; then
say " ClamAV found no known malware in the $nfiles files it could read."
OKBY="$(addlist "$OKBY" "ClamAV ($scope)")"
elif [ "$chits" -gt 0 ]; then
RANFOUND="$(addlist "$RANFOUND" "ClamAV ($scope)")"
else
REVIEW=$((REVIEW+unconf)); NOTRUN="$(addlist "$NOTRUN" "ClamAV (ran, but $unconf hit(s) could not be confirmed — see above)")"
fi
fi
fi
fi
# ── 3. rkhunter — only if it is already installed; its warnings are for review, not counted as found ──
if [ "$OS" = "Linux" ] && [ -x /usr/bin/rkhunter ]; then
hdr "3 · rootkit check (rkhunter)"
if [ "$ROOT" = 0 ]; then say " · not run — needs administrator rights"; NOTRUN="$(addlist "$NOTRUN" "rkhunter (needs administrator rights)")"
else
w="$(asroot /usr/bin/rkhunter --check --sk --rwo --nocolors /dev/null | head -40)"
if [ -n "$w" ]; then
nw="$(printf '%s\n' "$w" | grep -c .)"; REVIEW=$((REVIEW+nw))
say " rkhunter printed $nw warning line(s). Review these — most rkhunter warnings on a normal system are changed"
say " system files after updates, not rootkits:"
printf '%s\n' "$w" | while IFS= read -r l; do say " $(clean "$l")"; done
rk="$(printf '%s\n' "$w" | grep -i 'possible rootkit:' | head -3)"
if [ -n "$rk" ]; then
FOUND=$((FOUND+1))
say " ${R}! rkhunter names a specific rootkit above.${N} rkhunter cannot remove rootkits. If that is confirmed, the safe"
say " fix is: back up your own files, reinstall the system, change your passwords from another device."
fi
else say " rkhunter printed no warnings."; OKBY="$(addlist "$OKBY" "rkhunter")"; fi
fi
fi
# download-and-run shapes: curl|wget … | sh, base64 -d | sh, /dev/tcp/, nc -e, bash -i >& …
DLX='(curl|wget)[^|;&]*\|[[:space:]]*(sudo[[:space:]]+)?(/bin/|/usr/bin/)?(ba|z|da)?sh([^[:alnum:]_]|$)|base64[[:space:]]+(-d|-D|--decode)[^|]*\|[[:space:]]*(/bin/|/usr/bin/)?(ba|z|da)?sh([^[:alnum:]_]|$)|/dev/tcp/|(^|[^[:alnum:]_])(nc|ncat|netcat)[[:space:]]([^|;&]*[[:space:]])?-[a-z]*e[[:space:]]|bash[[:space:]]+-i[[:space:]]*>&|do shell script[^|]*(curl|wget)'
dlexec() { printf '%s' "$1" | grep -qiE "$DLX"; }
# ── 4. Mac: what starts by itself ──
if [ "$OS" = "Darwin" ]; then
hdr "4 · programs that start themselves"
PB=/usr/libexec/PlistBuddy; s4=0
for d in "$HOME/Library/LaunchAgents" /Library/LaunchAgents /Library/LaunchDaemons; do
for p in "$d"/*.plist; do
[ -f "$p" ] && [ ! -L "$p" ] || continue
prog="$("$PB" -c 'Print :Program' "$p" 2>/dev/null)" || prog=""
a0="$("$PB" -c 'Print :ProgramArguments:0' "$p" 2>/dev/null)" || a0=""
bin="${prog:-$a0}"; [ -n "$bin" ] || continue
allargs=""; script=""
for i in 1 2 3 4 5 6 7 8; do
a="$("$PB" -c "Print :ProgramArguments:$i" "$p" 2>/dev/null)" || break
allargs="$allargs $a"
if [ -z "$script" ]; then case "$a" in /*) if [ -f "$a" ]; then script="$a"; fi;; esac; fi
done
case "$bin" in
/*) ;;
*) r="$(PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/homebrew/bin"; command -v -- "$bin" 2>/dev/null)"
case "$r" in /*) bin="$r";; *) continue;; esac;; # a bare name launchd resolves itself — not "missing"
esac
why=""; level=""; tgt="$bin"; isscript=0
case "${bin##*/}" in sh|bash|zsh|dash|ksh|env|osascript|python|python[0-9]*|perl|ruby|node)
if [ -n "$script" ]; then tgt="$script"; isscript=1; fi
if dlexec "$allargs"; then level=susp; why="it runs a command that downloads code from the internet and runs it"; fi;;
esac
if [ -z "$why" ]; then
if [ ! -e "$tgt" ]; then
if [ "$FDA" = 1 ]; then level=review; why="its program is no longer there — usually a leftover from something you uninstalled; harmless"; fi
else
unsigned=$isscript
if [ "$isscript" = 0 ]; then
sig="$(/usr/bin/codesign -dv --verbose=2 "$tgt" 2>&1)"
# on Apple silicon everything carries at least an "ad hoc" signature, which proves nothing about who made it
if printf '%s' "$sig" | grep -q 'not signed at all'; then unsigned=1
elif printf '%s' "$sig" | grep -q '^Signature=adhoc'; then unsigned=1
elif printf '%s' "$sig" | grep -q '^TeamIdentifier=not set' && ! printf '%s' "$sig" | grep -q '^Authority=Software Signing'; then unsigned=1; fi
fi
loc=""
case "$tgt" in
/tmp/*|/private/tmp/*|/var/tmp/*|/private/var/tmp/*|/Users/Shared/*) loc=temp;;
"$HOME"/.*)
top="${tgt#"$HOME"/}"; top="${top%%/*}"
case "$top" in .local|.config|.docker|.providence*|.cargo|.rustup|.nvm|.pyenv|.rbenv|.nodenv|.goenv|.jenv|.asdf|.sdkman|.npm|.bun|.deno|.volta|.go|.gem|.nix-profile|.dotfiles|.homesick|.yadm|.chezmoi|.oh-my-zsh|.orbstack|.rd|.colima|.lima|.ollama|.vscode*|.cursor|.claude|.bin|.scripts) ;; *) loc=hidden;; esac;;
"$HOME/Library/Application Support"/*) loc=appsupport;;
/opt/homebrew/*|/usr/local/Cellar/*|/usr/local/opt/*) ;;
*/.*/*) loc=hidden;;
esac
if [ "$unsigned" = 1 ]; then
case "$loc" in
temp) level=susp; why="it starts an unsigned program or script from a temporary/shared folder";;
hidden) level=susp; why="it starts an unsigned program or script from a hidden folder";;
appsupport) level=review; why="it starts an unsigned helper from Application Support — common for small apps, also used by adware; check that you know the name";;
esac
fi
fi
fi
[ -n "$why" ] || continue
s4=$((s4+1))
if [ "$level" = review ]; then
REVIEW=$((REVIEW+1)); say " · review: $(clean "${p##*/}") — $why"; say " starts: $(clean "$tgt")"; continue
fi
SUSP=$((SUSP+1))
say " ${R}? looks suspicious:${N} $(clean "${p##*/}") — $why"
say " starts: $(clean "$tgt")$( [ -n "$allargs" ] && printf ' (arguments:%s)' "$(clean "$allargs" | cut -c1-300)")"
say " This is a pattern match, not proof. Developers' own tools can look like this."
case "$d" in
"$HOME"/*)
if ask " Stop it from starting? (moves only this launch file to quarantine; the program is left alone; --restore puts it back)"; then
/bin/launchctl bootout "gui/$(id -u)" "$p" 2>/dev/null
if quarantine_user "$p" "launch file switched off: $why"; then DISABLED=$((DISABLED+1)); say " ${GR}switched off${N} — launch file moved to quarantine"; else say " ${R}could not move the launch file${N} — nothing changed"; fi
else say " left as it is"; fi;;
*)
if [ "$ROOT" = 0 ]; then say " Not changed — switching off a computer-wide launch file needs administrator rights."
elif ask " Stop it from starting? (moves only this launch file to a root-only quarantine; the program is left alone; --restore puts it back)"; then
asroot /bin/launchctl bootout system "$p" 2>/dev/null
if quarantine_root "$p" "launch file switched off: $why"; then DISABLED=$((DISABLED+1)); say " ${GR}switched off${N} — launch file moved to quarantine"; else say " ${R}could not move the launch file${N} — nothing changed"; fi
else say " left as it is"; fi;;
esac
done
done
if [ "$s4" = 0 ]; then say " Nothing obviously suspicious in the places we checked (your and the computer's LaunchAgents and LaunchDaemons)."; fi
[ "$FDA" = 1 ] || say " · without Full Disk Access some start-up programs could not be inspected."
say " Not checked here: login items and browser extensions — see System Settings › General › Login Items & Extensions."
hdr "5 · configuration profiles"
PSRC="all"
pl="$(asroot /usr/bin/profiles list -all /dev/null)" || { pl="$(/usr/bin/profiles list /dev/null)"; PSRC="yours"; }
ids="$(printf '%s\n' "$pl" | grep 'profileIdentifier' | sed 's/.*profileIdentifier: //')"
if /usr/bin/profiles status -type enrollment 2>/dev/null | grep -q 'MDM enrollment: Yes'; then
say " ${R}This Mac is managed by an organisation (work or school).${N} Its profiles are expected. Do NOT remove them —"
say " removing a management profile can lock you out of work or school services. Ask your IT department instead."
fi
if [ -n "$ids" ]; then
say " Profiles installed on this Mac (a profile can set your browser's homepage, search engine, proxy and certificates):"
while IFS= read -r pid; do
[ -n "$pid" ] || continue; REVIEW=$((REVIEW+1))
case "$pid" in
com.apple.*) say " · $(clean "$pid") (the name says Apple — e.g. Screen Time; this script cannot verify who installed it)";;
*) say " · $(clean "$pid")";;
esac
done <