Pharma Hack Cleanup: Find Cloaked Viagra Spam That Only Google Sees, Remove It From Files and Database, Then Clear Search Results

By Information Security Asia · Updated November 30, 2026 · 6 min read

Pharma Hack Cleanup: Find Cloaked Viagra Spam That Only Google Sees, Remove It From Files and Database, Then Clear Search Results

Short answer: To remove pharma hack spam from a website, first prove it exists, because the spam is usually cloaked: visitors and logged-in admins see a normal site while Googlebot and search visitors see pill ads and links. Search Google for site:yourdomain.com viagra, use Search Console’s URL Inspection to view the crawled page, and fetch pages with a Googlebot user agent. Then back up the site, compare core and plugin files against official checksums, search themes, mu-plugins, .htaccess and the database for the injected code, and remove it. Change all passwords and keys, update everything, and confirm the cloaking is gone with the same tests. Finally, request a review if Search Console flagged the site, and let Google recrawl the cleaned pages.

How a pharma hack works

A pharma hack is search spam: attackers inject content advertising prescription drugs into a legitimate site so their links and pages benefit from its reputation. It’s closely related to the Japanese keyword hack, but the content is pharmacy spam in English and other languages. The defining trick is cloaking. The injected code checks who is asking for the page. If the request comes from a search engine crawler, or from a visitor who clicked a search result, it serves spam or redirects to a pharmacy site. If it comes from someone typing the URL directly or a logged-in administrator, it serves the normal page. That’s why owners often learn about it from a customer or a Search Console email rather than by looking at their own site.

Pharma Hack Cleanup: Find Cloaked Viagra Spam That Only Google Sees, Remove It From Files and Database, Then Clear Search Results

Injected code typically lives in several places at once so that removing one copy doesn’t end the infection. Common hiding spots on WordPress include the active theme’s functions.php, fake plugins, must-use plugins, wp-config.php, .htaccess rewrite rules, and serialized or base64-encoded values in the database’s options table.

Step 1: confirm the spam and how it’s served

  1. Search Google. Try site:yourdomain.com viagra, site:yourdomain.com cialis and site:yourdomain.com pharmacy. Spam titles or snippets in the results confirm Google has indexed injected content.
  2. Use URL Inspection in Search Console. Inspect an affected URL, then test the live URL and view the tested page’s HTML. This shows what Googlebot receives.
  3. Fetch as a crawler and as a search visitor from a terminal and compare with a normal fetch.
curl -s -A "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" https://example.com/ | grep -iE "viagra|cialis|pharmacy|pills"
curl -s -e "https://www.google.com/" https://example.com/ | grep -iE "viagra|cialis|pharmacy|pills"
curl -s https://example.com/ | grep -iE "viagra|cialis|pharmacy|pills"

If the first two commands return matches and the third doesn’t, you’re looking at user-agent or referrer cloaking. Some infections also check the visitor’s IP against Google’s ranges, in which case only Search Console’s live test will show the spam reliably. Note which URLs are affected; a hack that only touches the homepage behaves differently from one generating thousands of fake pages.

Where the spam appears Likely injection point What to check first
Spam text inside real pages for crawlers only Theme or plugin PHP with user-agent check functions.php, header.php, mu-plugins
Thousands of new spam URLs on your domain Rewrite rules or a doorway script .htaccess, unknown PHP files in uploads or root
Search visitors redirected to a pharmacy Referrer-based redirect .htaccess, wp_options, JavaScript in theme
Spam in page titles and meta descriptions only Database values or SEO plugin settings wp_options, wp_postmeta

Step 2: back up and freeze changes

Before cleaning, take a full backup of files and database, even though it’s infected. You’ll want it if something breaks, and it preserves evidence of how the attacker got in. Put the site in maintenance mode if possible, and don’t install new plugins until you’re done.

Step 3: compare files against known-good copies

On WordPress with SSH access, WP-CLI can verify core and plugin files against WordPress.org checksums:

wp core verify-checksums
wp plugin verify-checksums --all
wp plugin list --status=must-use
find . -name "*.php" -newer wp-config.php -mtime -30 -print

Any modified core file should be replaced with a fresh copy; the simplest approach is reinstalling core with wp core download --force --skip-content. Plugins that fail checksum checks should be deleted and reinstalled from WordPress.org. Premium plugins and themes don’t have public checksums, so reinstall them from the vendor. Look closely at must-use plugins, which load automatically and don’t appear in the normal plugin list; our guide to checking mu-plugins for malware covers that folder in detail.

Then search for the patterns that cloaking code relies on:

Pharma Hack Cleanup: Find Cloaked Viagra Spam That Only Google Sees, Remove It From Files and Database, Then Clear Search Results
grep -rniE "googlebot|HTTP_USER_AGENT|HTTP_REFERER" wp-content --include=*.php | grep -viE "/vendor/"
grep -rnE "base64_decode|gzinflate|str_rot13|eval\(" wp-content --include=*.php

Legitimate plugins do use some of these functions, so read the surrounding code before deleting. Code that checks for Googlebot or a Google referrer and then prints links, includes a remote file or redirects is what you’re looking for. Check .htaccess in the root and in subfolders for RewriteCond lines that match search-engine user agents or referrers.

Step 4: clean the database

Pharma spam is often stored in the database so it survives file cleanups. Search for spam keywords across all tables:

wp db search "viagra" --all-tables
wp db search "cialis" --all-tables
wp db query "SELECT option_name, LENGTH(option_value) FROM wp_options WHERE autoload='yes' ORDER BY LENGTH(option_value) DESC LIMIT 20;"

Large autoloaded options with unfamiliar names, especially containing base64 strings, deserve inspection. Remove spam from posts, postmeta and options carefully; if a value is serialized PHP, editing its length incorrectly breaks it, so delete obviously malicious options rather than editing them by hand. Also review the users table for administrators you didn’t create.

Step 5: close the way in

Cleaning without fixing the entry point means reinfection within days. Update WordPress, every plugin and theme; remove anything abandoned or nulled; change passwords for all admin users, hosting, SFTP and the database; and rotate the salts in wp-config.php so stolen sessions die. If you found a vulnerable plugin version in your logs or backup, note it. Our WordPress malware removal checklist lists each phase with a test for when it’s complete, and 12 places attackers hide backdoors helps make sure no access point survives.

Step 6: get the spam out of Google

Rerun the curl tests and Search Console’s live URL test. When both show clean pages, open Search Console’s Security Issues report. If Google flagged the site for hacked content, request a review explaining what you removed and how you closed the hole; see our guide to the Search Console security review. Spam URLs that no longer exist should return 404 or 410, which tells Google to drop them over time. For a handful of especially visible URLs, the Removals tool can hide them temporarily while Google recrawls. Submit an updated sitemap so the real pages are recrawled quickly. Spam titles in search results can linger for days or weeks after a clean recrawl.

Frequently asked questions

Why can’t I see the pharma spam on my own site?

Because the code hides it from direct visitors and logged-in users. Only crawlers and people arriving from search see it. Test with a crawler user agent or Search Console.

Will a security plugin remove a pharma hack?

Scanners help find modified files, but database spam and cleverly hidden backdoors are often missed. Combine a scan with checksum verification and a manual database search.

How long until Google stops showing spam results?

It depends on how quickly Google recrawls your pages. Clean pages, a review request if a warning was issued, and a fresh sitemap speed things up, but expect some delay.

Should I restore from a backup instead?

Only if you know the backup predates the infection, and you still need to close the vulnerability afterwards or the site will be reinfected.

Sources

Keep reading