WordPress Security

Finding Malware Hidden in the WordPress Database

Finding Malware Hidden in the WordPress Database

A common frustration in WordPress cleanups goes like this. A site owner reinstalls WordPress core, replaces every plugin and theme with fresh copies, scans every file and finds nothing. Yet visitors are still being redirected, spam links still appear in the page source and Google still flags the site.

The reason is usually the database. File scanners look at files. File integrity monitoring watches files. But WordPress stores most of what it displays in the database: post content, widgets, theme settings, plugin options, user accounts and more. Attackers know this, and they hide code there precisely because many cleanups never look.

Here’s how database malware works and how I find and remove it.

Why attackers use the database

Storing malicious code in the database has advantages for attackers:

  • Survives file cleanups. Reinstalling core and plugins doesn’t touch database content.
  • Evades file scanners and integrity checks.
  • Looks like legitimate content. Scripts in settings that are designed to hold code, like header scripts, look normal at a glance.
  • Executes through legitimate features. WordPress and plugins output the stored content as part of normal page rendering.

Where database malware hides

Post and page content

Attackers inject scripts, iframes or hidden links directly into the content of posts and pages, often hundreds or thousands at once. The injected code may be at the end of the content, hidden with CSS so visitors don’t see it, or obfuscated.

wp_options

The options table holds site settings and plugin configuration. Common targets:

  • siteurl and home: changed to redirect the whole site.
  • Theme options and Customizer settings that output code in headers or footers.
  • Plugin settings for code snippet, header and footer, analytics or SEO plugins that accept scripts.
  • Widget settings, stored as serialised data, especially custom HTML and text widgets.
  • Active plugins list: occasionally modified to activate a malicious plugin.
  • Cron entries: scheduled tasks that recreate malware.

Users and user meta

  • Rogue administrator accounts, sometimes with names that look legitimate, like “wpsupport” or “admin2”.
  • Capability changes that give low-level users administrator powers.

Spam posts and pages

SEO spam hacks often create large numbers of posts, sometimes with unusual post types or statuses, hidden from normal admin views.

Comments

Comments can contain injected links or scripts, though WordPress filters most dangerous code from comments by default.

Step 1: Back up before you search

Take a complete database export before investigating:

You’ll want to compare and possibly restore parts later, and you shouldn’t make changes without a way back.

Step 2: Check the critical options

Start with options that affect the whole site:

siteurl and home should be your real domain. users_can_register should usually be 0, unless you need public registration. default_role should be subscriber or customer, never administrator. The active plugins list should match what you see on disk and in the dashboard.

Step 3: Search for scripts and suspicious patterns

WP-CLI’s db search command is the quickest tool. Search for script tags, iframes and common obfuscation patterns:

The stats option shows which tables and columns contain matches. Then look at the actual matches in each location.

Not every match is malicious. Analytics snippets in header settings, embedded videos in posts and legitimate plugin code will appear too. The skill is distinguishing them:

  • Legitimate code usually references known services, is readable and was added deliberately.
  • Malicious code is often obfuscated, loads from unfamiliar domains, uses long strings of encoded characters or appears in places that shouldn’t contain scripts.

Search for unfamiliar domains

Once you identify a suspicious domain in one place, search for it everywhere:

Attackers usually inject the same code in many places.

Search for base64 and hex patterns

Long encoded strings are common in malware:

Legitimate data, like images stored in settings or licence data, can also match, so investigate each result.

Step 4: Check users

List administrators and review each one:

Some malware hides users from the admin list but not from the database. Also check for users with administrator capabilities assigned directly in user meta:

Adjust the table prefix if yours isn’t wp_. Compare the user IDs with your known administrators.

Step 5: Look for spam content

Count posts by type and status, looking for anything unexpected:

A sudden thousands of posts of a type you don’t use, or many posts with odd statuses, indicates spam injection. Also check the newest posts:

Step 6: Check scheduled tasks

Malicious cron events can reinject database malware after cleanup:

Look for unfamiliar hook names, especially random strings. Then search the code for those hook names to see what they do.

Step 7: Clean carefully

When you’ve identified malicious content:

  • For injected code in many posts, use a careful search and replace. Because WordPress stores serialised data, use WP-CLI rather than raw SQL replacements:

Check the dry run results, then run it without dry-run. Obfuscated injections that vary between posts may need a regular expression with the –regex option, or manual cleanup.

  • For malicious options, restore the correct value or delete the option if it was created by malware.
  • For rogue users, delete them, reassigning any content to a legitimate user.
  • For spam posts, delete them in bulk once you’ve confirmed they’re all spam.
  • For malicious cron events, delete them and remove the code that registers them.

Step 8: Find the root cause

Database malware arrives through something: a vulnerable plugin, a stolen admin account, a malicious plugin or a compromised hosting account. Check access logs and activity logs for the time the injected content appeared. Fix the entry point, rotate credentials and regenerate security keys.

Step 9: Verify and monitor

After cleanup:

  • Repeat the searches to confirm nothing remains.
  • View pages as an anonymous visitor and check the source and network requests.
  • Use URL Inspection in Search Console to see what Google sees.
  • Set up monitoring for new administrator accounts, changes to critical options and new scripts on key pages.

Database malware in practice

One site I cleaned had been reinfected three times by previous cleanups. Each time, the files were replaced, the site looked clean for a few days, and then visitors arriving from Google were redirected to a scam page again. The reason was a single option in wp_options that a theme printed into every page’s footer. It contained an obfuscated script that only activated for visitors coming from search engines, and only once per visitor, which is why the site owner never saw it.

A database search for the obfuscation pattern found it in seconds. It had also been injected into the content of about two hundred older posts, and a rogue administrator account with a plausible name had been created at the same time. The entry point turned out to be a vulnerable form plugin that allowed the attacker to change options.

Once the option, the posts and the user were cleaned, the plugin was updated, all passwords were rotated and monitoring was added for changes to theme options, the reinfections stopped.

Make database checks part of every cleanup

Whenever you clean a hacked WordPress site, include the database in your checklist alongside files: critical options, script searches, users, post counts and cron events. It takes perhaps half an hour on a typical site, and it’s often the difference between a cleanup that lasts and one that has to be repeated.

If you manage several sites, keep a short script with these searches so the check takes minutes, not hours, and run it on every site after any security incident in your portfolio.

Frequently asked questions

Can malware really live only in the database?

Yes. Many infections store their code in posts, options or widgets and rely on WordPress to output it. File-only cleanups miss these entirely.

Is it safe to run search and replace on my database?

With a backup and a dry run first, using WP-CLI, which handles serialised data correctly, it’s a safe and effective cleanup method. Raw SQL replacements can corrupt serialised data.

How do I know if a script in my options is malicious?

Check where it loads from, whether it’s obfuscated and whether anyone deliberately added it. Unknown domains, encoded code and scripts in unexpected settings are strong warning signs.

Why does the malware come back after cleaning?

Usually because a backdoor file, a malicious cron task or the original vulnerability remains. Clean files and database together, and fix the entry point.

Final thoughts

The database is where many WordPress infections hide, because so many cleanups never look there. Back up first, check critical options, search systematically for scripts, iframes and obfuscation, review users and posts, inspect scheduled tasks and clean carefully with serialisation-safe tools. Then fix the entry point and monitor for changes. A cleanup that includes the database is far more likely to be the last one you need.