WordPress Security ServicesMalware Removal
Recurring Infections

WordPress index.php (or wp-config.php) Keeps Getting Hacked: How to Find What's Rewriting It

You replace index.php with a clean copy. The next morning it's been changed again. Or wp-config.php gets new lines at the top, or wp-blog-header.php keeps disappearing. When the same file keeps getting hacked, something on your server is rewriting it, and that's what you need to find.

Sergey By Sergey, WordPress security specialist Updated 3 min read
In this guide
  1. Why the same file keeps changing
  2. How to find the source
  3. Then fix it, in the right order

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:

  1. A backdoor elsewhere on the server that checks the file and re-injects its code, often on every page load.
  2. A scheduled task: a WordPress cron event or a hosting cron job that re-downloads the malware every few hours.
  3. Code stored in the database, such as an option or transient, that’s written back to disk when WordPress loads.
  4. A must-use plugin or a modified drop-in (object-cache.php, advanced-cache.php) that runs before everything else.
  5. A running PHP process that stays in the server’s memory and keeps rewriting files.
  6. Another site in the same hosting account that’s still infected.
  7. 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.php and advanced-cache.php
  • the top of wp-config.php, and any .user.ini or php.ini with auto_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

  1. Stop the process: remove the cron job, kill the processes, delete the backdoor.
  2. Remove every other piece of the infection together, so nothing restores anything else.
  3. Restore clean copies of the affected files.
  4. Change all passwords, revoke application passwords, and end all sessions.
  5. Close the entry point, which is usually an outdated plugin.
  6. 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.

Sergey, WordPress Security Specialist
About the author
Sergey, WordPress Security Specialist

10 years in WordPress security and incident investigation, and more than 200 websites cleaned of malware. Top Rated on Upwork with a 100% Job Success score. More about me

Frequently Asked Questions

Can I just make index.php read-only?

It can buy time, but it doesn't fix anything. Malware running as the same user can usually change permissions back, and the backdoor that rewrites the file is still on the server, free to do anything else. Find and remove the process first.

Why is it always index.php?

Because index.php runs on almost every request. Code added there executes for every visitor, which makes it the most valuable file for an attacker. wp-config.php and wp-blog-header.php are popular for the same reason.

Could my host be the cause?

Rarely directly, but a shared hosting account where another site is infected is a very common cause. Sites under the same hosting user can write to each other's files.

Need a hand?

Let me clean your WordPress site for you

Send me your site URL and what you're seeing. I'll reply with what I think is going on and the next step.

$95 per infected site · 1-month guarantee · direct contact with the specialist

Get My Site Cleaned →