#!/bin/bash
# ==============================================================================
# CVE-2026-41940 Post-Compromise Cleanup and Persistence Check Script
# IMPORTANT: Run this as root ONLY after patching via /scripts/upcp --force
# ==============================================================================
# Ensure script is run as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root."
exit 1
fi
echo "=========================================================="
echo " Starting Post-Compromise Cleanup for CVE-2026-41940"
echo "=========================================================="
# ------------------------------------------------------------------------------
# Task 1: Purge All Affected Sessions
# ------------------------------------------------------------------------------
echo ""
echo "[*] Task 1: Purging all cPanel/WHM sessions..."
SESSIONS_DIR="/var/cpanel/sessions/raw"
if [ -d "$SESSIONS_DIR" ]; then
# Count files before deletion for reporting
SESSION_COUNT=$(ls -1q $SESSIONS_DIR | wc -l)
if [ "$SESSION_COUNT" -gt 0 ]; then
rm -rf $SESSIONS_DIR/*
echo "[+] Successfully purged $SESSION_COUNT session files."
else
echo "[+] Session directory is already empty."
fi
else
echo "[-] Warning: Session directory $SESSIONS_DIR not found."
fi
# ------------------------------------------------------------------------------
# Task 2: Check for Persistence Mechanisms
# ------------------------------------------------------------------------------
echo ""
echo "[*] Task 2: Checking for common persistence mechanisms..."
# A. Checking Cron Jobs for root and common users
echo "[+] Checking cron jobs..."
echo " - Root crontab:"
crontab -l 2>/dev/null | grep -v "^#" | grep -v "^\s*$" || echo " (No root cron jobs found or access denied)"
echo " - Checking /etc/cron.* directories for recently modified files (last 7 days):"
find /etc/cron.hourly /etc/cron.daily /etc/cron.weekly /etc/cron.monthly /etc/cron.d -type f -mtime -7 -exec ls -la {} \; | sed 's/^/ /'
# B. Checking SSH Authorized Keys
echo ""
echo "[+] Checking SSH authorized_keys files for recently added keys (last 7 days)..."
find /root/.ssh/authorized_keys /home/*/.ssh/authorized_keys -type f -mtime -7 -exec ls -la {} \; 2>/dev/null | sed 's/^/ /'
echo " (Please manually review the contents of any listed files for unauthorized keys)"
# C. Basic Rootkit Check using rkhunter
echo ""
echo "[+] Running Rootkit Hunter (rkhunter)..."
# Check if rkhunter is installed
if ! command -v rkhunter &> /dev/null; then
echo " rkhunter is not installed. Attempting to install..."
# Try yum (CentOS/CloudLinux/RHEL)
if command -v yum &> /dev/null; then
yum install epel-release -y > /dev/null 2>&1
yum install rkhunter -y > /dev/null 2>&1
# Try apt (Debian/Ubuntu - less common for cPanel, but just in case)
elif command -v apt-get &> /dev/null; then
apt-get update > /dev/null 2>&1
apt-get install rkhunter -y > /dev/null 2>&1
fi
if ! command -v rkhunter &> /dev/null; then
echo " [-] Failed to install rkhunter. Please install manually and run."
else
echo " [+] rkhunter installed successfully."
# Update properties database quietly
rkhunter --propupd > /dev/null 2>&1
fi
fi
if command -v rkhunter &> /dev/null; then
echo " Running quick rkhunter scan (warnings only)..."
# Run rkhunter, suppressing normal output, only showing warnings
rkhunter --check --rwo | sed 's/^/ ! /'
echo " (Full rkhunter log available at /var/log/rkhunter/rkhunter.log or /var/log/rkhunter.log)"
fi
# ------------------------------------------------------------------------------
# Final Advice
# ------------------------------------------------------------------------------
echo ""
echo "=========================================================="
echo " Cleanup and basic checks complete."
echo " IMPORTANT MANUAL ACTIONS REQUIRED:"
echo " 1. FORCE PASSWORD RESETS for root and all WHM user accounts immediately."
echo " 2. Review the cron and SSH key output above for anything suspicious."
echo " 3. Check /var/log/wtmp and /usr/local/cpanel/logs/access_log for unusual activity."
echo "=========================================================="