Incident Response for 50 WordPress Sites
About 30 hacked sites cleaned, locked, and watched daily
Read case studyCleaning a compromised server is one job. Keeping it clean is another, especially when it runs old WordPress sites with premium plugins that nobody can update or check against an official copy. After the wp2shell cleanup, I wanted a setup where even a working webshell could not change or add a single line of PHP, and where I would hear about anything odd the next morning.
This note covers the lock I put on every site's code, the NGINX rule that goes with it, what the lock broke, two protections it turned out had never run, and the daily report that watches all of it.
The first round of hardening blocked the REST batch endpoint, patched Core, and locked a handful of entry files per site. That closes the door the attacker used, but it leaves three gaps:
So the goal changed from “find every bad file” to “make the code unwritable by anything that runs on the website”.
On Linux, chattr +i makes a file or folder immutable. Nobody can change, delete, or rename it, root included, until root removes the flag. On a folder, it also stops new files from being created inside it. A small script, wp-lock.sh, sets the flag per site on:
wp-config.php, .htaccess, index.php, and robots.txt.wp-admin/ and wp-includes/.wp-content/plugins/, themes/, and mu-plugins/.wp-content drop-ins: index.php, advanced-cache.php, object-cache.php, and db.php.These stay writable, because WordPress needs them at runtime: uploads/, cache/, languages/, and the WP Rocket config folder.
wp-lock.sh lock <app|all>
wp-lock.sh unlock <app>
wp-lock.sh status all
On the second server, this replaced a lock on ten files per site. Those ten files, watched with auditd, had already done their job: the watches showed hundreds of blocked write attempts per minute from the site’s own PHP user, which is how I found the code that kept reinfecting index.php.
The folders that stay writable are where an attacker would drop a new file, so NGINX refuses to run PHP from any of them. One rule per site covers everything in wp-content except plugins and themes, which are locked instead:
location ~* /wp-content/(?!plugins/|themes/)[^?]*\.(php[0-9]?|phtml|phar|pht|phps)(/|$) {
deny all;
}
One site had its own location ^~ /wp-content/uploads/ block. A ^~ prefix match skips every regex location, so the rule above never applied there, and that block needed its own deny inside it. The install script probes a PHP path under uploads on every site afterward, and each must answer 403.
Rejecting missing files before they reach WordPress addresses another problem: scanner requests can exhaust PHP capacity even when no code is compromised. The later scanner incident covers that change, which retained the upload and private-log protections.
The lock is meant to stop changes, and it does, including mine. Every update, plugin install, and deploy now needs a person in the loop:
wp-lock.sh unlock <app>
# update, install or deploy
wp-lock.sh lock <app>
Unlocking leaves a marker file, and the daily report alerts when a site has stayed unlocked for more than 24 hours, so a forgotten unlock gets noticed within a day.
Tools that assume they can write broke in instructive ways:
mu-plugins/ was immutable. Every site rolled back with no leftovers.wp plugin delete. With plugins/ immutable, WordPress fell back to asking for FTP credentials and waited for an answer that never came. The tool now lifts and restores the lock itself, runs every WP-CLI call under a timeout, moves plugins out instead of deleting them, and runs the same restore on Ctrl+C as on a failure.ctime, so the first report after a lock listed thousands of “PHP files written” on every site. The content was unchanged, and it happens once after every unlock and lock.On the first server, every site also has DISALLOW_FILE_MODS, so the dashboard no longer offers plugin installs and updates that would fail against the lock anyway.
Locking everything meant checking every must-use plugin on every site, and two of them had been doing nothing.
Must-use plugins that never loaded
After the first cleanup, the mu-plugins folders were owned by root. RunCloud’s file ACLs keep the site’s PHP user from reading root-owned files, so WordPress skipped them without an error. On one server the batch-endpoint block had not loaded on 14 sites since the cleanup. On the other, the plugin that routes mail through Amazon SES had not loaded on seven sites, and their mail went out without it. Changing the owner back to the site user fixed both, and the folders stay locked.
A block that another plugin undid
The must-use plugin from the wp2shell note returned 401 for anonymous batch requests from a rest_pre_dispatch filter at priority 0. ACF Pro hooks the same filter at priority 10 and returns null, which threw the 401 away. On every site with ACF Pro, the block did nothing. The fix was to register it last:
add_filter('rest_pre_dispatch', function ($result, $server, $request) {
if (strpos($request->get_route(), '/batch/v1') !== false && !is_user_logged_in()) {
return new WP_Error('rest_forbidden', 'Authentication required for batch API.', ['status' => 401]);
}
return $result;
}, PHP_INT_MAX, 3);
Afterward all 20 sites on that server answered 401 to an anonymous POST to /wp-json/batch/v1. The NGINX rule from that note only blocks the rest_route= form, so on those sites the /wp-json/batch/v1 path had no working block at all.
The weekly checksum email from the first cleanup only wrote when something failed. I replaced it with one script, shared by both servers, that runs from root cron every morning and always sends a Telegram message, either “ALL OK” or “ATTENTION” with the full report attached. A silent morning then means the check itself is broken.
Per site, it checks:
index.php hash.Across the server, it checks:
.htaccess allow-lists, against a baseline.--strict, which also reports added files.uploads still answers 403.The bot token lives in a root-only file, never in the script. --no-send runs the check without Telegram, and --init accepts the current state as the new baseline after I have verified a change. Its first real run found an obfuscated loader in a languages/ folder that every earlier sweep had missed.
A report that cries wolf gets ignored, so I traced every alert in the first week to its cause and fixed either the site or the check:
| Alert | Cause | Change |
|---|---|---|
| Spam word on two sites | “speCIALISt” contains “cialis” | Spam words match whole words only |
| Japanese text served to Googlebot | A Japanese language school’s real name | That site skips only the Japanese test; the spam-word test still runs |
| Wordfence firewall files changed | Wordfence rewrites them on every sync. They start with exit() and __halt_compiler(), so nothing after that line runs |
Checked for that first line instead of a hash. The real rules file is still hashed |
A changed .htaccess in wp-content |
An image plugin rewrote its own WebP rules during a routine self-check | Re-baselined with --init, not excluded |
| “opcache ok” on the first run | The script had raised the limit in an ini file PHP did not load | Verified in the running PHP, not in the file |
The .htaccess case was the one I thought about longest. Excluding that path would have silenced the alert, but that watch is exactly what catches an attacker’s .htaccess. Accepting the verified file as the new baseline keeps the check, and the next change to it alerts again.
uploads, and the report reads admins from the database, but injected content in a post is outside both.Lock the whole code base
With a few files locked, a working webshell can still add a new one. With the whole code base immutable, it can’t add or change any PHP.
Test that each protection runs
Two of mine had been doing nothing, one because of file ownership and one because of another plugin’s hook. Probe each layer from outside, separately.
Register security filters last
On a shared hook, the last callback wins. PHP_INT_MAX keeps another plugin from undoing the result.
Build tools that expect a locked server
Anything that touches code has to lift and restore the lock itself, and every WP-CLI call needs a timeout.
Always send the report
A message every day proves the check ran. Re-baseline a verified change instead of excluding the path, so the check that matters keeps working.
With the lock and the morning report in place, I no longer recheck these servers by hand. The unlicensed plugins still need to be replaced or licensed.
About 30 hacked sites cleaned, locked, and watched daily
Read case studyNext step
See how I work and what it costs, or email me. I usually reply within one working day.