Why the same file keeps changing
When a file is rewritten after every cleanup, there’s an active process with write access to it. In my experience it’s one of these:
- A backdoor elsewhere on the server that checks the file and re-injects its code, often on every page load.
- A scheduled task: a WordPress cron event or a hosting cron job that re-downloads the malware every few hours.
- Code stored in the database, such as an option or transient, that’s written back to disk when WordPress loads.
- A must-use plugin or a modified drop-in (
object-cache.php,advanced-cache.php) that runs before everything else. - A running PHP process that stays in the server’s memory and keeps rewriting files.
- Another site in the same hosting account that’s still infected.
- The attacker logging back in, with a password or session that’s still valid.
How to find the source
1. Note exactly when the file changes
ls -la --time-style=full-iso index.php wp-config.php wp-blog-header.php
The modification time is your best clue. A change at the same minute every hour points to a cron job. A change right after a visit to a specific URL points to a backdoor triggered by that request.
2. Match the time against your access logs
Look in your hosting access logs at the minute the file changed. A POST request to an unusual PHP file, or to a normal file with odd parameters, is very often the backdoor being used. That file is your target.
3. Check scheduled tasks
wp cron event list --skip-plugins --skip-themes
crontab -l
In your hosting control panel, also look under Cron Jobs. Anything that runs wget, curl or an unfamiliar PHP file is suspect.
4. Check the places that load first
wp-content/mu-plugins/(must-use plugins load before everything else)wp-content/object-cache.phpandadvanced-cache.php- the top of
wp-config.php, and any.user.iniorphp.iniwithauto_prepend_file
5. Search the database for the payload
Self-restoring malware often keeps a copy of itself in wp_options. My database malware page has safe queries to find large or encoded options.
6. Ask your host about running processes
On shared hosting, ask support to list and stop all PHP processes running under your account. On a VPS, check with ps aux for PHP processes that shouldn’t be there.
7. Check every site in the account
If other WordPress installs share the hosting account, including old staging copies, they can reinfect yours. Clean all of them at the same time.
Then fix it, in the right order
- Stop the process: remove the cron job, kill the processes, delete the backdoor.
- Remove every other piece of the infection together, so nothing restores anything else.
- Restore clean copies of the affected files.
- Change all passwords, revoke application passwords, and end all sessions.
- Close the entry point, which is usually an outdated plugin.
- Watch the file’s modification time for a few days.
From my cleanup work: a site’s index.php was rewritten every night at 3:00. The owner had restored it a dozen times. The cause was a hosting cron job, added through a hacked control panel account, that downloaded a fresh copy of the malware every night. No WordPress plugin could have seen it. Removing the cron job and changing the hosting password ended it.
If you’ve reached the point of restoring the same file every day, it’s usually faster to let someone find the source. See WordPress malware that keeps coming back.