wp-hack-fix/wp-fix-hacked.sh

62 lines
1.9 KiB
Bash
Raw Permalink Normal View History

2025-07-07 11:28:55 +05:30
#!/usr/bin/env bash
set -euo pipefail
# ----------------------
# wp-fix-hacked.sh
# ----------------------
2025-07-07 11:35:49 +05:30
# Usage: cd /path/to/wp-install && bash wp-fix-hacked.sh
# Or: bash wp-fix-hacked.sh /path/to/wp-install
2025-07-07 11:34:36 +05:30
# Default: current directory
2025-07-07 11:28:55 +05:30
# ----------------------
2025-07-07 11:34:36 +05:30
# Determine target directory
if [ $# -gt 0 ]; then
ROOT_DIR="$1"
else
ROOT_DIR="$(pwd)"
fi
2025-07-07 11:28:55 +05:30
2025-07-07 11:34:36 +05:30
# Ensure were in a WP install
2025-07-07 11:35:49 +05:30
test -f "$ROOT_DIR/wp-config.php" || {
2025-07-07 11:34:36 +05:30
echo "⚠️ No wp-config.php found in $ROOT_DIR. Please run this from a WordPress install directory."
exit 1
2025-07-07 11:35:49 +05:30
}
2025-07-07 11:34:36 +05:30
USER="$(whoami)"
2025-07-07 11:35:49 +05:30
echo "🛑 Stopping most processes for user '$USER' (excluding this script)..."
# Kill all user processes except this script
for pid in $(pgrep -u "$USER"); do
if [ "$pid" != "$$" ]; then
kill "$pid" 2>/dev/null || true
fi
done
2025-07-07 11:28:55 +05:30
2025-07-07 11:34:36 +05:30
echo "📂 Cleaning WordPress install at: $ROOT_DIR"
2025-07-07 11:28:55 +05:30
2025-07-07 11:59:23 +05:30
# todo: not safe need more work
# # 1. Delete everything except wp-config.php & wp-content/
# find "$ROOT_DIR" -mindepth 1 \
# ! -path "$ROOT_DIR/wp-config.php" \
# ! -path "$ROOT_DIR/wp-content/*" \
# -exec rm -rf {} +
2025-07-07 11:34:36 +05:30
# 2. Remove ELF binaries
echo " • Removing ELF binaries..."
find "$ROOT_DIR" -type f -exec sh -c \
'file "$1" | grep -q ELF && echo " ↳ Deleting $1" && rm -f "$1"' sh {} \;
# 3. Flag suspicious PHP code
echo " • Checking for eval() injections:"
grep -iR --include="*.php" "eval(" "$ROOT_DIR" || echo " (none found)"
echo " • Checking for base64_decode() use:"
grep -iR --include="*.php" "base64_decode(" "$ROOT_DIR" || echo " (none found)"
echo " → Manually inspect any hits and remove malicious code."
# 4. Reinstall WP core
echo " • Re-downloading WordPress core..."
2025-07-07 11:35:49 +05:30
wp core download --path="$ROOT_DIR" --skip-content --force && \
echo " ✔ Core reinstalled successfully."
2025-07-07 11:28:55 +05:30
2025-07-07 11:35:49 +05:30
echo -e "
✅ Done! Review grep hits above, then secure your site (update credentials, plugins, themes)."