Article
Backup files: the .bak next to your config that hands over the keys
Published 2026-11-25
Backup files: the .bak next to your config that hands over the keys
There is a specific kind of bad afternoon that starts with a Google alert or a customer email and ends with you scrolling a log looking for the first time someone requested config.php.bak. Not the live file. The backup copy of it. The one someone made by hand three years ago while fixing something at 2am and forgot in the web root.
If you have never gone looking for backup files on your own server, the list is usually longer than you expect. This article is about why a copy with the wrong extension is worse than the original, how to find every copy on your server, and what to do when one of them turns out to have been reachable.
Why the extension is the whole problem
A PHP file named config.php gets executed by the server. The request comes in, PHP runs the code, the visitor gets whatever the code prints. The database password inside that file never leaves the server.
Now rename that file to config.php.bak. The extension is no longer .php, so the PHP handler does not match it. Apache and nginx both fall back to serving unknown extensions as static content: the raw bytes of the file go to whoever asked. The visitor does not get a rendered page, they get the source, including every credential, every API key, and every internal hostname in it.
The same happens with config.php~ (some editors leave these), config.php.old, config.php.orig, config.php.save, and on Windows servers, Copy of config.php, which is still close enough to guess. The pattern is always the same: a rename that moves the file outside the extension the script engine knows about. OWASP keeps a whole testing category for this, WSTG-CONF-04, because it keeps showing up on real web servers. MITRE tracks it as CWE-530, exposure of a backup file to an unauthorized sphere.
The dangerous part is not exotic. It is that a web server will happily serve a file that the language runtime refuses to look at.
What leaks, in order of how much it hurts
Not every backup is a crisis. Here is the honest ordering, based on what these files actually contain.
Database dumps (.sql, .sql.gz). A dump of the production database can hold user records, password hashes, email addresses, order history, session tokens, admin notes. This is the worst category, because it is not a lead for an attacker, it is the data itself. If a db.sql was reachable in your web root, you are handling a data exposure incident, not a hygiene problem.
Config backups (config.php.bak, .env.bak, settings.php.old). Source code with credentials inside. On WordPress, that means wp-config.php contents: database password plus the salts that sign admin login cookies. On Laravel, a .env copy holds the APP_KEY and every third-party API key in plain text. The .env copy is arguably the single worst small file to leave behind, because Laravel signs cookies with APP_KEY; anyone holding it can forge session cookies if they also control request content.
Site archives (.zip, .tar.gz, site-backup.zip). The whole application, config files included, in one downloadable package. Archives are bulk disclosure: source, includes, dependencies, and often a copy of the environment file or a database dump sitting next to the code it belongs to.
Old source copies (.php.old, ~ files, .swp). The old version contains the vulnerability that was already fixed in the live file. An attacker who reads viewdoc.php.old gets a version they can study offline, with a known flaw, while viewdoc.php looks patched from the outside. Old copies also show the site's internal structure, which makes every other attack cheaper.
Editor artifacts (.swp, filename~). Vim swap files and the tilde copies some editors write on every save. Small, forgettable, and they hold the previous version of whatever was being edited.
An old backup is not automatically harmless because it is old. Credentials may still work. Old code reveals architecture that is still in production. And a dump from two years ago still contains real customer records.
How they get there
Almost none of these files are written by the application. They are deploy-time and debugging-time artifacts, which is why code review never catches them: they are not in the diff, because they were never in the repo.
The usual paths:
- Someone edits a file directly on the server, saves a copy first, forgets it.
- A database export is run during setup or debugging, into the web root because that is the directory the panel had open.
- A host panel or plugin writes backups beneath the served directory; several WordPress backup plugins have shipped exactly this behavior, and some generated guessable filenames, which turned the file from "hidden" into "findable" (CVE-2024-7315 documents the chain for one plugin).
- A rollback or migration leaves
release-old.zipbehind. - A whole-directory copy (
cp -r site site.bak) puts a second complete site one guess away.
That last one deserves its own warning. cp -r under the web root creates a directory where every file is served. The PHP files inside still execute, but the .bak-suffixed ones do not, and the static ones all load. If the copied tree contains an .env or a dump, the copy has doubled your exposure.
How to find them on your server
Over SSH, from the web root (adjust depth and path for your layout):
# Editor and rename artifacts
find . -name '*.bak' -o -name '*.old' -o -name '*.orig' \
-o -name '*~' -o -name '*.swp' -o -name '*.save' -o -name '*.tmp'
# Database dumps and archives
find . -name '*.sql' -o -name '*.sql.gz' -o -name '*.dump' \
-o -name '*.zip' -o -name '*.tar.gz' -o -name '*.tgz'
# Configuration copies, any suffix
find . -name 'wp-config*' -o -name '.env*' -o -name 'config*.php*'
# Anything modified in the last 30 days that is not owned by git
find . -type f -mtime -30 -not -path './.git/*' | head -50
The last one catches the file that was created yesterday, which is the one that matters most when you are checking after an incident or before a launch. mtime -30 also has a way of finding log files and upload-folder junk you did not know was served, but those are worth knowing about too.
If you have no SSH access, most hosting panels (cPanel file manager, DirectAdmin) have a search function that filters by extension or partial name. It is slower than find but the same list applies.
Checking from the outside
You can also confirm reachability from the public side, which is the part that actually matters, because a file that exists but is not reachable is a much smaller problem than a file that is. From your own domain only:
for f in backup.sql db.sql dump.sql backup.zip site.tar.gz \
config.php.bak wp-config.php.bak .env.bak index.php~; do
code=$(curl -sk -o /dev/null -w '%{http_code}' "https://example.com/$f")
echo "$code $f"
done
A 200 means the file is being served and you should treat its contents as read. A 403 means the file is there but a rule blocks it: better, but it is still on the server, and the rule only covers the extensions you thought of. A 404 is the only answer that means the file is not there. Do not settle for a 403; a .bak saved as config.bak.txt during some future 2am fix will walk right past an extension deny rule.
The two-stage reality is worth naming: reachability is what you observe, and what the file contained is what determines the response. A 200 on index.html.bak is embarrassing. A 200 on db.sql is a breach assessment.
How to fix it
The durable fix is a location rule, not a deny rule. Backups live outside the web root, on storage the web server cannot serve. The extension never matters if the file is never in the served directory in the first place.
- Move all backups out of the web root. Database dumps, archives, config copies, everything. Backups belong in a directory the server cannot reach over HTTP, or a private storage bucket that is not publicly readable. If the backup tool writes into the served tree by default, that default is the defect; change where it writes.
- Delete the copies that are already there. Run the
findlists above, delete everything that is not production content, and do not keep a "just in case" copy in a served folder. Version control is the "just in case": that is what it is for, and it keeps history without keeping it next to the live files. - Deploy from a clean checkout, not a working folder. A CI build from a fresh checkout contains no stray dumps, because none exist in the repo. Deploy scripts that rsync a working directory copy everything, including the artifacts.
- Add a server deny rule as the second layer, not the first. If you run nginx:
Apache:location ~* \.(bak|old|orig|save|swp|sql|dump)$ { return 404; } location ~* /\.(env|git) { return 404; }
A deny rule helps, but it is a pattern list, and pattern lists lose to names you did not think of. The location rule (backups never in the web root) is the fix; the deny rule is the seatbelt.<FilesMatch "\.(bak|old|orig|save|swp|sql|dump)$"> Require all denied </FilesMatch> - If a backup was reachable, rotate what it held. Permission changes do not un-read a file. If a config backup sat at a guessable URL for any length of time, rotate the database password, rotate the API keys, and on WordPress, regenerate the salts (which logs everyone out; do it in a quiet window). If a database dump was reachable, treat it as a data exposure: follow your incident process, check what personal data it contained, and notify where your obligations say you must. And grep the access logs for that filename first, so you know the date range you are dealing with instead of assuming the worst forever.
WordPress specifics
WordPress sites collect backup files faster than most stacks, because the ecosystem of backup and migration plugins is large and their storage defaults vary. Some plugins keep backups under wp-content/backups/, others under wp-content/uploads/, and uploads is served by default in every standard nginx and Apache WordPress configuration. A backup plugin that writes inside uploads is writing to a public directory; that needs checking the first time you install it, not after an incident.
The migration tools are the other offender. Every migration plugin ships a "package" file: a zip of the site plus a database dump, written somewhere the receiving end can download it. While a migration is in progress there is a window where that package is reachable. The honest advice is to delete the package the moment the migration finishes, and to run migrations in quiet windows, not over a weekend when nobody will notice a stalled job.
The checklist
- Run the
findlists. Note every hit, even the "harmless" ones. - Delete or move everything outside the web root. Version control keeps the history.
- Add the extension deny rule as a second layer (nginx or Apache snippet above).
- Re-run the
findlists after cleanup, becausecp -rand panels have a way of recreating the problem. - Check the access log for any filename that turned out to be reachable, before deciding how bad it was.
- Make "no dumps in the web root" part of the launch checklist, the same line as "TLS checks green".
The pattern behind all of this is one sentence: the served directory should contain only what you intend to serve. The rest belongs somewhere the web server cannot reach.
CheckWebSecurity's external scan probes for exactly this class of problem: leftover backup and editor copies, readable configuration files, exposed dumps and archives, directory listing, and the standard header set. A backup file the scan can read from the outside is a finding you want delivered by a report, not by a stranger's HTTP request. Start a free scan and see what your web root is actually handing out.
Related: An exposed .git directory: why it gives away your whole repository · Directory listing: what an "Index of /" page leaks · wp-config.php permissions: the one file that should not be 644