Skip to content

Japanese keyword hack on WordPress: prove the cloaking, close the re-infection vector

Your WordPress site serves Japanese spam pages to Googlebot and clean pages to you. Prove the cloaking with curl, inventory the injected content, and close all six persistence vectors so the spam does not come back.

If Search Console has just told you that your WordPress site is indexed under hundreds of Japanese-language pages you never wrote, and the site looks completely normal when you load it in your browser, you are looking at the Japanese keyword hack. The reason you see nothing wrong is cloaking: injected PHP inspects each request and serves the spam only when the caller looks like a search engine crawler. You can prove that in one command with curl -A 'Googlebot/2.1'. After that, treat the spam pages as the symptom, not the problem. The problem is the persistence mechanism — a backdoor file, a rogue administrator account, an autoloaded wp_options row, a scheduled wp-cron event, an .htaccess rule, or a cPanel cron job — and if you delete the spam while leaving any one of those in place, it is back within days.

What you’ll accomplish

  • Prove the site is cloaking, with a reproducible comparison you can attach to an incident record.
  • Inventory what was injected: which files changed, which no longer match their published checksums, and which database rows carry the payload.
  • Check all six re-infection vectors explicitly, rather than assuming a file cleanup was enough.
  • Restore from clean sources, rotate every credential the attacker could have read, and close the entry point.
  • Get the Japanese results out of Google’s index by fixing what Googlebot sees, then requesting a review.

Prerequisites

  • Shell on the cPanel account. SSH as the account user, or cPanel’s Terminal if jailed shell is enabled. Everything below is possible through File Manager and phpMyAdmin, but you will be doing by hand what one find does in a second, and that is how vectors get missed.
  • WP-CLI 2.x. Most WHM servers ship wp on the path already; check with wp --info. If it is absent, the phar install documented at wp-cli.org works fine inside a home directory. WP-CLI needs PHP 7.4 or newer against a current WordPress.
  • GNU coreutils. The find -newermt and find -printf flags below are GNU extensions, present on AlmaLinux, CloudLinux and CentOS and therefore on essentially every cPanel server. They do not exist on macOS or BSD, so run these on the server, not on your laptop.
  • A verified Search Console property with owner access. Without it you cannot see the Security Issues report or request a review.
  • Off-server storage for the forensic copy — anything that is not the compromised account.
  • Know your docroot. The primary domain lives in /home/<cpaneluser>/public_html; addon and subdomain docroots are often /home/<cpaneluser>/<addondomain> or a subdirectory of public_html. One account frequently hosts several WordPress installs, and the hack rarely stays in one of them.

How it works

The injected code is a request-time decision, not a set of files sitting on disk waiting to be found by browsing the site. Somewhere early in the WordPress bootstrap — a must-use plugin, an active theme’s functions.php, a hijacked core file, or a php_value auto_prepend_file directive in .htaccess — a snippet runs on every request and looks at three things: the User-Agent header, the Referer header, and sometimes a reverse DNS lookup of the client IP. If the request looks like Googlebot, or looks like a click from a Google search results page, the code emits a doorway page: Japanese product spam with keyword-stuffed titles, pointing at the attacker’s storefronts. If it looks like anything else, the code stays out of the way and WordPress renders your real page.

Request path showing the same URL returning real content to a browser and Japanese spam to a Googlebot user-agent, with the injected cloaking code making the decision

That single decision explains the shape of the whole incident. It explains why the owner “sees nothing wrong” — you are on the browser branch of that diagram every time you check. It explains why the first alert comes from Search Console, or from a customer who searched your brand name and saw Japanese text under your domain. And it explains why a plugin scanner that crawls the site as a normal visitor reports it as clean.

The spam pages themselves usually do not exist as files. They are generated on the fly from a template plus a keyword list, mapped onto URL paths the attacker also publishes in an injected sitemap so Google finds them fast. That is why a site with 40 real pages can end up with 30,000 indexed URLs in a fortnight, and why “deleting the spam pages” is not a meaningful action — there is nothing to delete except the generator. Separately, the attacker almost always plants a way back in, which is the part that turns a bad afternoon into a bad month.

Step-by-step

1. Prove the cloaking before you change anything. Fetch the same URL twice, once with an ordinary browser user-agent and once with a crawler user-agent, and compare the bodies. Do this from the server or from your workstation — either is fine, as long as both requests come from the same IP.

curl -sS -A 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)' \
  -o /tmp/as-browser.html https://example.com/
curl -sS -A 'Googlebot/2.1 (+http://www.google.com/bot.html)' \
  -o /tmp/as-googlebot.html https://example.com/
wc -c /tmp/as-browser.html /tmp/as-googlebot.html
diff /tmp/as-browser.html /tmp/as-googlebot.html | head -40

On a clean site the two byte counts land within a few hundred bytes of each other and diff shows only nonces and timestamps. On a cloaked site the crawler response is often several times larger, and diff immediately shows Japanese characters, unfamiliar outbound links, and hidden markup. Some variants key on the referrer instead, so if the user-agent test comes back clean, try curl -sS -e 'https://www.google.com/' -o /tmp/as-search.html https://example.com/ and diff that against the browser fetch. Keep all three files, dated — this is your evidence.

2. Take the forensic copy before you touch the account. Even though it is infected, copy the whole account off the server now: files and database. You will need it to find the entry point, to reconstruct a timeline, and to recover anything you delete by mistake at 2 a.m.

cd /home/<cpaneluser>
wp db export /home/<cpaneluser>/forensic-$(date +%F).sql --path=public_html
tar -czf /home/<cpaneluser>/forensic-$(date +%F).tar.gz public_html forensic-$(date +%F).sql

Then move both artefacts off the server and delete them from the account — a .tar.gz of your whole site sitting under the docroot is itself a data leak.

3. Contain, so the attacker is not re-injecting while you clean. Restrict the docroot to your own address for the duration. In /home/<cpaneluser>/public_html/.htaccess, above the WordPress block:

# Allow only the operator address while the cleanup runs
Require ip 203.0.113.7

Reload the site from a different network and confirm you get a 403. Visitors will see an error page while you work; say so to the site owner up front rather than discovering it in a support ticket. If the account has an inactive FTP user or an unused SSH key, remove it now.

4. Inventory what changed. Start with modification times. Pick a date a few days before the first Search Console alert.

find /home/<cpaneluser>/public_html -type f -name '*.php' \
  -newermt '2026-08-20' -printf '%TY-%Tm-%Td %TH:%TM  %p\n' | sort

Expect a legitimate cluster from any plugin update, and one or two files that belong to nothing — a PHP file in wp-content/uploads/, a randomly named file at the docroot, an extra file in wp-content/mu-plugins/. Then look for the obfuscation signatures the payload almost always carries:

grep -rIl --include='*.php' -E \
  'eval\(|base64_decode\(|gzinflate\(|str_rot13\(|preg_replace\(.*/e|\$_REQUEST\[' \
  /home/<cpaneluser>/public_html | head -40

Then let WordPress itself tell you which of its own files were altered:

cd /home/<cpaneluser>/public_html
wp core verify-checksums
wp plugin verify-checksums --all

A clean core prints Success: WordPress installs verify against checksums. Anything else lists the modified or added file paths outright. Note the honest limit: wp plugin verify-checksums only works for plugins distributed through the wordpress.org directory. Premium and custom plugins are not covered, and that is exactly where an attacker prefers to hide.

5. Walk every persistence vector, not just the files. This is the step that decides whether the cleanup holds.

Map of six re-infection vectors — backdoor file, rogue admin, autoloaded option, wp-cron event, .htaccess rule, cPanel cron job — each restoring the payload, with the check command for each

wp user list --role=administrator --fields=ID,user_login,user_email,user_registered
wp option list --autoload=on --fields=option_name,size_bytes --orderby=size_bytes --order=desc | head -20
wp cron event list --fields=hook,next_run_relative,recurrence
grep -n -E 'auto_prepend_file|RewriteRule|RewriteCond|ErrorDocument' /home/<cpaneluser>/public_html/.htaccess
crontab -l

Read each result against what the site should have. An administrator registered at 03:14 with an address at a domain you do not recognise is the attacker. An autoloaded option a few hundred kilobytes in size, named something like wp_theme_cache_data, is the payload. A cron hook belonging to no installed plugin is the reinstaller. In cPanel the same cron list sits under Cron Jobs; the WHM-side and File Manager equivalents are documented at docs.cpanel.net. Write every finding down before you delete anything — the pattern across findings is what tells you how they got in.

6. Replace all code from clean sources. Do not hand-edit infected files. Re-download core at the version you are on, then force-reinstall every plugin and theme from its origin, and delete anything inactive.

wp core download --version=$(wp core version) --force --skip-content
wp plugin list --field=name | xargs -n1 -I{} wp plugin install {} --force
wp theme list --status=inactive --field=name | xargs -n1 wp theme delete
wp plugin list --status=inactive --field=name | xargs -n1 wp plugin uninstall

Premium plugins and custom code have to come from your own known-good copy or from the vendor. With no clean copy of a custom theme, you are reading it line by line — there is no shortcut. Delete the unexplained files from step 4 by hand, then re-run wp core verify-checksums until it comes back clean.

7. Clean the database and the schedule. Remove the rogue accounts, the injected options and the attacker’s cron hooks.

wp user delete 47 --reassign=1
wp option delete wp_theme_cache_data
wp cron event delete unknown_hook_name
wp transient delete --all
wp rewrite flush

Then check the content tables for injected posts, because some variants also write real posts as a fallback: wp post list --post_type=any --format=count compared against what the owner expects will usually settle it in one line.

8. Rotate everything the attacker could have read. The payload could read wp-config.php, so the database credentials are burned. Change the cPanel password, the database user password (update wp-config.php to match), every WordPress administrator password, all FTP accounts, and any API key stored in the site. Then run wp config shuffle-salts, which rewrites the authentication keys in wp-config.php and invalidates every existing login cookie in one move. Skipping this is how a site gets re-compromised with no new backdoor at all: the attacker simply logs in.

9. Fix what Google sees, then ask for a review. Remove the restriction from step 3, confirm the site serves clean content to crawlers, and only then go to Search Console. Delete any property owner you did not add — attackers verify themselves so they can submit spam sitemaps. Remove the injected sitemap, resubmit the real one, and run the URL Inspection live test on the homepage and on two spam URLs. If Security Issues shows a manual action, request a review and describe what you found and fixed; that reads very differently from a bare “please re-check”.

Validate it works

Re-run the proof from step 1 and expect the two responses to match:

curl -sS -A 'Mozilla/5.0' -o /tmp/after-browser.html https://example.com/
curl -sS -A 'Googlebot/2.1 (+http://www.google.com/bot.html)' -o /tmp/after-bot.html https://example.com/
diff /tmp/after-browser.html /tmp/after-bot.html

An empty diff — or a diff containing only nonces — is the pass condition. Then confirm the rest of the surface:

wp core verify-checksums
wp plugin verify-checksums --all
wp user list --role=administrator --fields=ID,user_login,user_registered
wp cron event list --fields=hook,next_run_relative
find /home/<cpaneluser>/public_html -type f -name '*.php' -mtime -1

Checksums clean, only the administrators you expect, only hooks you recognise, and no PHP files modified since your own cleanup finished. Run that find again 48 hours later. If a new PHP file has appeared and nobody deployed anything, a vector is still open and you are back at step 5.

Troubleshooting

The spam is back within a day or two. You missed a vector. In rough order of likelihood: a cPanel cron entry that re-downloads the payload; a second WordPress install under an addon domain on the same account; a backdoor outside the docroot in /home/<cpaneluser>/tmp or a mail directory; or a scheduled event that was simply re-registered on the next page load. Re-run step 4 with /home/<cpaneluser> as the search root instead of public_html.

WP-CLI dies with a fatal error before it prints anything. The injected code loads on the CLI too and often breaks when there is no HTTP request to inspect. Run diagnostics with the payload bypassed: wp --skip-plugins --skip-themes core verify-checksums. This is also a useful signal in itself — if --skip-plugins fixes the fatal, the payload is in a plugin or a must-use plugin, and ls -la wp-content/mu-plugins/ is your next command.

Checksums come back clean but the site is still cloaking. Core checksums only cover core. Check, in this order: wp-content/mu-plugins/, which loads unconditionally and which most people never look at; the active theme’s functions.php; any premium plugin, since those are outside checksum coverage; and .htaccess for auto_prepend_file, which injects a file into every PHP request without touching a single WordPress file.

Googlebot now gets clean HTML but Google still shows Japanese results. That is expected for a while. The index reflects the last crawl, and thousands of spam URLs will not be recrawled overnight. Make sure the spam URLs return 404 or 410 rather than a soft redirect to the homepage, make sure they are gone from every sitemap, and give it days to weeks. Do not submit removal requests for thousands of URLs one at a time; fix the responses and let the recrawl do the work.

ModSecurity blocks your curl test or your cleanup. On a WHM server with the OWASP rule set enabled, a Googlebot user-agent from a non-Google IP is a classic false-positive trigger. Check /usr/local/apache/logs/error_log for the rule ID, then disable that one rule for the account in WHM » Security Center » ModSecurity Tools. Turning ModSecurity off during an active compromise is the wrong direction.

When this is the wrong approach

If you cannot identify the entry vector and you have no backup from before the compromise, this is not a cleanup — it is a rebuild. Provision a clean account, install WordPress fresh, reinstall plugins from source, and migrate only content you have inspected: posts, pages, media files after checking them for PHP, and the user table minus every account you cannot account for. That is often faster than a fourth cleanup round, and it is the only option that gives you a defensible answer to “is it definitely gone”.

If the site handles payment data or personal information and the compromise is more than a few days old, stop treating this as an SEO problem. Preserve logs, overwrite nothing, and involve whoever owns your incident response and breach-notification obligations before you start deleting files. The Japanese spam may be the least interesting thing the attacker did with that access.

If the entry point turns out to be a nulled or pirated theme, cleanup is pointless while it stays installed — the backdoor is a feature of that distribution, not damage to it. And if the account has no shell access at all, be honest about what you cannot verify from File Manager; a partial cleanup that looks finished is worse than an unfinished one, because the owner stops watching.

Once the site is clean, spend the afternoon on the two changes that prevent the sequel: block PHP execution in the uploads directory, which removes the most common backdoor location outright, and audit the plugin fleet for abandoned code, which is where the initial vulnerability usually lived. Add file-change monitoring so the next injection arrives as an alert rather than a Search Console notice six weeks later.

Running WordPress that earns real money deserves an operator — Stack Harbor runs that stack under Enterprise WordPress; book an infrastructure consult.