WordPress malicious code removalIt means finding and deleting the unauthorized code added to your site from the files and database, and then closing the backdoors left by the attacker to return. Deleting the apparent redirect or spam link is often only the surface of the problem. All three layers, file, database and user, are scanned for permanent cleaning.

Where is malicious code stored?

Attackers put the code in unnoticeable places. The locations we encounter most frequently are:

  • PHP files with the image name in the uploads folder.
  • A single line of code added to the top or bottom of the theme's functions.php, header.php or footer.php file.
  • Include lines added to wp-config.php and index.php files.
  • Script tags embedded in widget or theme settings, other than the siteurl in the wp_options table.
  • Hidden links added under posts in wp_posts.

Finding malicious code in files

If you have SSH access, the following commands will speed up scanning:

find wp-content/uploads -name "*.php"
grep -rl "eval(base64_decode" wp-content
grep -rl "gzinflate(" wp-content
wp core verify-checksums

The first command lists the PHP files in uploads; There should not be PHP in this folder. The second and third command look for patterns used in executing encrypted code. The last command with WP-CLI compares the core files with the official hashes and shows the modified files. Every resulting file is opened and examined because some plugins use these functions for legitimate purposes.

Cleaning order

  1. Take a full backup and keep it separate; This copy is for review.
  2. Replace the wp-admin and wp-includes folders with the official WordPress package.
  3. Replace plugins and themes with a clean copy from the official source, delete unused ones.
  4. Delete the PHP files in uploads and disable PHP from running in this folder with the server rule.
  5. Search and clear records containing scripts, iframes and unknown domains in the database.
  6. Delete any administrator accounts you don't recognize and reset all passwords and security keys.

Symptom and possible site of contamination

symptompossible location
Only mobile visitors are redirected.htaccess or theme header file
Foreign language pages in GooglePHP files generating fake pages created in the root directory
Hidden links under postswp_posts content or theme footer file
Code comes back after cleaningBackdoor in uploads or mu-plugins

prevent reinfection

Make sure the gap is closed after cleaning. If it was accessed through an old plugin, update or remove that plugin. To turn off file editing from the panel, go to wp-config.php file define( 'DISALLOW_FILE_EDIT', true ); add the line. A security tool that monitors file changes will allow you to detect a new infection within hours, not days. For more comprehensive measures WordPress security optimisation See our guide.

How do you verify that the cleanup was successful?

After cleaning, test the site in different conditions. From the mobile device, click on the search result Google and log in from the incognito window, because some malicious codes only work in these conditions. in Google site:alanadiniz.com Search to see if there are any pages you don't recognize. Search Console Check the Security Issues report and request a review if there are any warnings.

It is important to monitor access logs and file changes for the next two weeks. For example, if a new PHP file appears in the uploads folder three days after the cleanup, the backdoor is still in place and the analysis will restart from requests at the time that file was created.

Common mistakes

  • Deleting the only visible code: As long as the backdoor remains in place, the code returns.
  • Forgetting to edit and update the theme file: The vulnerability in the old theme version remains open.
  • Bypassing the database: Even if the files are clean, the script in the database continues to redirect.

For the first hours of cleaning WordPress site was hacked Read our guide. To remove malicious code by expert hands and strengthen security WordPress security optimization service Check out our page.

Does the security plugin completely remove malicious code?

It finds known patterns and clears some of them. Hidden or newly written backdoors require manual inspection.

Why does malicious code come back after cleaning?

The attacker's exploit was not closed or a backdoor file was overlooked. Access logs help find this file.

Should the site remain closed during cleaning?

Yes, if the visitor is redirected maliciously. Otherwise, cleaning can be done more safely by opening the maintenance page.