Renaming files with the word ‘old’…
Last week I got a call from a website owner who explained me to an issue that he was experiencing… Not hacked, not defaced, just a site that would periodically go down and then come back up on it’s own. He forwarded me an email that he received from his hosting provider… It was an automated email about the server running out of memory. I want to walk through what actually happened, because the root of it was one tiny, common, lazy habit that I am even guilty of was the sole reason for this issue.
The habit
You’re about to change something in wp-config.php. Database password, a debug flag, whatever. Before you do, you make a copy, just in case:
cp wp-config.php wp-config.php.old
Feels responsible. Feels like backups. And then you forget about it, and it sits in the web root for seven years.
Here’s the problem. Apache knows what to do with a .php file: run it through PHP and send the output. It has no idea what a .old file is. Or .bak, or .orig, or .txt, or wp-config.php~. So it does what it does with any file it doesn’t recognize. It serves it as plain text to whoever asks for it.
And people ask for it. Constantly. Well not people, really. But bots. Bots are scanning the web all day and all night looking for files like this that it can consume and take what it finds back to whoever sent the bot out in the first place.
What the bot did
Every site on the internet gets scanned around the clock by automated tools looking for exactly this kind of thing. They have a list of a few hundred URLs that tend to be left lying around: .env, .env.bak, phpinfo.php, adminer.php, wp-config.php.old, and so on. They walk through the list on every IP address they can find.
One of those scanners hit this site on a Sunday afternoon and then again Monday morning. I pulled the access log and it made about 2,200 requests in total, 378 of them in one ten-minute window. Almost every one was a URL that doesn’t exist.
Here’s the part that most people don’t think about. On a WordPress site, a request for a page that doesn’t exist is not cheap. Apache can’t find the file, so it hands the request to WordPress. WordPress boots up PHP, loads the theme, loads every plugin, queries the database, and renders a 404 page. That takes real memory, and it takes it for every single request.
Do that a few hundred times in a couple of minutes and PHP spawns worker after worker to keep up. The server had 5 GB of RAM. It ran out. The kernel started killing processes, PHP died mid-request, the host’s automation restarted everything, and the site was throwing 500 errors for a stretch. That was the “memory error” in the email.
So to be clear about cause and effect: the scanner didn’t crash the site because of the .old file. It crashed the site by sheer volume, and it would have tried anyway. But in the middle of all that noise, the .old file was the one request that came back with a 200 instead of a 404. Out of 2,200 tries, that was its one hit. It downloaded a copy of a config file containing a database username, a database password, and the site’s security keys.
How bad was it?
Not as bad as it could have been.
The database credentials in the old file were stale. The password had been changed since. I tested them myself from the server and got access denied. Even if they’d worked, the host blocks MySQL at the firewall, so nobody could have used them from outside anyway.
The security keys were a different story. Those are the eight long random strings in wp-config.php labeled AUTH_KEY, SECURE_AUTH_KEY, and so on. WordPress uses them to sign login cookies. They had never been changed, so the ones in the old file matched the live site exactly. With those, someone who knows what they’re doing can forge a logged-in admin session without ever guessing a password.
No sign anyone did. But “no sign” isn’t the same as “didn’t happen,” which is why you fix it instead of hoping.
What we did
In order:
Deleted the file. Obviously. Then I searched the whole web root for anything else with a backup-style extension: .old, .bak, .orig, .sql, .zip, .env, and so on. That was the only one. If you do nothing else after reading this, go run that search on your own sites.
Verified the old database password was dead. Tried it, got access denied, moved on.
Regenerated the security keys. WordPress.org has a generator at https://api.wordpress.org/secret-key/1.1/salt/ that spits out a fresh set every time you load it. Paste those eight lines over the old ones in wp-config.php. The only visible effect is that everybody gets logged out, which is exactly what you want when you suspect cookies could be forged.
Blocked the scanner’s IP at the host’s firewall. Worth doing but not a real fix, since the next scanner will come from somewhere else.
Made junk requests cheap. This is the actual fix. I added a block to the top of .htaccess, above the WordPress section, that tells Apache to answer the common scanner targets with an instant 404 before WordPress ever gets involved:
# BEGIN scanner shield
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule \.env - [NC,R=404,L]
RewriteRule ^env$ - [NC,R=404,L]
RewriteRule wp-config\.php - [NC,R=404,L]
RewriteRule (adminer|phpinfo|phpmyadmin)[^/]*\.php - [NC,R=404,L]
RewriteRule (^|/)\.(git|svn|htpasswd|DS_Store) - [NC,R=404,L]
RewriteRule \.(sql|sql\.gz|bak|old|orig|save|swp)$ - [NC,R=404,L]
</IfModule>
# END scanner shield
Every URL in that scanner’s list now gets a bare Apache 404 in a fraction of a millisecond, with no PHP and no database. Real visitors never request anything that matches these patterns, so it can’t break anything. You can test it with curl -sI https://yoursite.com/.env.bak | head -1 and you should get a 404 back before you’ve finished blinking.
Asked the host to sanity-check PHP-FPM. If pm.max_children times PHP’s memory_limit adds up to more than the server has, a big enough burst can still cause this. That’s a five-minute config change and it should have been done years ago.
The actual lesson
If you need a copy of a config file, put it somewhere the web server can’t reach. One directory up from the web root is fine. Your home directory is fine. Version control is better. Anywhere that isn’t inside public or docs or htdocs or www or whatever your host calls it. Ie. cp /home/domains/yoursite.com/docs/wp-config.php /home/domains/yoursite.com/wp-config.php.bak
Better yet, don’t keep copies of config files around at all. The values in them are the kind of thing that belong in a password manager, not in a file with a cute extension sitting next to index.php.
And regenerate your security keys once in a while. Nobody does this. It takes two minutes and it quietly invalidates every leaked cookie, every stale session on an old laptop, and every copy of the file you forgot you made.
This site is seven years old and had never been touched at this level. That’s not unusual. That’s most sites. The bot doesn’t care how old your site is or how small your business is. It’s just working through a list, and the only question is whether your site has something on it.