WordPress File Integrity Monitoring and Backdoor Detection

File integrity checks for WordPress: find modified and injected files

After a hack, the first question is “which files were changed?”. Without a reference, nobody can say whether wp-includes/functions.php is original or has three extra lines at the end. WordPress publishes checksums for every core release, WP-CLI compares against them, and for plugins and themes a hash baseline of your own does the same job. Run regularly, the comparison also finds the next injection within hours instead of months.

Problem: A site sends spam and new administrator accounts appear. Malware scanners report nothing conclusive, and the admin cannot tell which of 9,000 PHP files are original.

Solution: Verify core with wp core verify-checksums and plugins from WordPress.org with wp plugin verify-checksums --all, search the uploads folder for PHP files, create a SHA-256 baseline of all PHP files after cleaning up, and compare against it daily from cron with an email when something differs.

Core and plugins against the official checksums

cd /var/www/example.com/public
wp core verify-checksums
# Warning: File doesn't verify against checksum: wp-includes/load.php
# Warning: File should not exist: wp-admin/css/colors/blue/shell.php
# Error: WordPress installation doesn't verify against checksums.

wp plugin verify-checksums --all

verify-checksums downloads the MD5 list for the installed version from WordPress.org and reports changed files and files that should not exist in core folders. For plugins, it works for plugins hosted on WordPress.org; premium and custom plugins have no published checksums and are reported as such. A changed core file is never legitimate: replace core with wp core download --force --skip-content --version=$(wp core version).

Places where PHP does not belong

find wp-content/uploads -type f \( -name '*.php' -o -name '*.phtml' -o -name '*.phar' \) -print
find . -name '*.php' -newer wp-includes/version.php -mtime -7 -not -path './wp-content/cache/*'
grep -rlE 'eval\(base64_decode|gzinflate\(base64_decode|\$_(POST|GET|REQUEST)\[[^]]+\]\(' --include='*.php' wp-content | head -50

The uploads folder should contain no PHP at all. The second command lists PHP files changed in the last week that are newer than the core version file, a quick view of recent changes. The grep catches the most common obfuscation patterns; attackers vary them, so an empty result proves nothing, but a hit is almost always malware. Disabling PHP in uploads prevents the first case: disable PHP execution in uploads.

A baseline of your own

For everything without official checksums (premium plugins, the theme, mu-plugins), create a list of hashes once the site is known to be clean:

#!/usr/bin/env bash
# /usr/local/bin/ha-integrity.sh
set -euo pipefail
SITE=/var/www/example.com/public
BASE=/var/lib/ha-integrity/baseline.sha256
NOW=$(mktemp)

cd "$SITE"
find . -type f \( -name '*.php' -o -name '.htaccess' -o -name '*.js' \) \
    -not -path './wp-content/cache/*' -not -path './wp-content/uploads/*.js' -print0 \
    | sort -z | xargs -0 sha256sum > "$NOW"

if [ ! -f "$BASE" ]; then
    mkdir -p "$(dirname "$BASE")"; mv "$NOW" "$BASE"; echo "baseline created"; exit 0
fi

if ! diff -q "$BASE" "$NOW" > /dev/null; then
    diff "$BASE" "$NOW" | mail -s "[integrity] files changed on $(hostname)" admin@example.com
fi
rm -f "$NOW"
# daily at 04:30, as root, so the files cannot be changed by the web server user
30 4 * * * /usr/local/bin/ha-integrity.sh

The baseline lives outside the web root and belongs to root; malware running as www-data cannot update it to hide its changes. After every legitimate update (core, plugins, theme), recreate it: delete the file and run the script once. A diff that appears without an update is the alarm.

After a finding

Restore changed files from a clean source (official ZIPs, the theme’s git repository), do not “clean” them line by line. Then look for the way in: outdated plugins, reused passwords, a writable wp-config.php. Change all passwords and the salts in wp-config.php (which logs out every session), and check the users table for unknown administrators. Hardening that reduces the next risk: WordPress security hardening.

NOTE: File integrity does not cover the database: injected scripts in posts, options like siteurl or rogue administrators are invisible to file hashes. Check those separately (for example wp user list --role=administrator and a search for <script in wp_posts and wp_options).