How to Check wp-config.php and .htaccess for Malicious Code

Clean up: how to check wp-config.php and .htaccess

Two small files control a lot of how a WordPress site behaves: wp-config.php and .htaccess. Attackers know this. A single extra line in either one can load malicious code on every page, or send your Google visitors to a spam site while you see nothing wrong.

In this guide I show what these files normally look like, the warning signs I check for, and how to search for them quickly.

Before you start

  • Download a copy of both files first. In File Manager, right-click each file and choose Download. If you change something by mistake, you can put the original back.
  • Turn on hidden files. .htaccess starts with a dot, so File Manager hides it unless you tick Show Hidden Files (dotfiles) under Settings.

Both files sit in your website folder, usually public_html. To read one, right-click it and choose View, which is safer than Edit because it can’t change anything.

What a normal wp-config.php contains

A normal wp-config.php is short and predictable. In order, you will usually see:

  1. Database settings: DB_NAME, DB_USER, DB_PASSWORD, DB_HOST, DB_CHARSET and DB_COLLATE.
  2. Eight security keys and salts, such as AUTH_KEY and NONCE_SALT, each a long random string.
  3. $table_prefix, usually wp_ or a short variation.
  4. WP_DEBUG, normally false on a live site.
  5. Possibly a few extra settings added by your host or a plugin, such as WP_MEMORY_LIMIT or WP_CACHE.
  6. At the very end: require_once ABSPATH . 'wp-settings.php';

Nothing should come after that last line.

Warning signs in wp-config.php

  • Code after require_once ABSPATH . 'wp-settings.php';
  • Functions that unpack hidden code: eval(, base64_decode(, gzinflate(, str_rot13( or assert(. A normal wp-config.php doesn’t need any of them.
  • An include or require of a strange file, especially one disguised as an image or icon, like @include "/home/user/public_html/wp-content/uploads/favicon_8a3f.ico";
  • One extremely long line of random-looking characters.
  • A lot of empty space pushing code far to the right or far down, where you won’t notice it.

What a normal .htaccess contains

On most WordPress sites, .htaccess contains this block:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

You may also see blocks added by your host, such as one marked cPanel-generated handler that sets the PHP version, or blocks added by caching or security plugins, which usually name the plugin in a comment. Those are normal.

Warning signs in .htaccess

  • Redirects that depend on where the visitor came from, for example rules containing HTTP_REFERER with google, bing or yahoo. This sends search visitors to spam while you, typing the address directly, see the real site.
  • Rules that check for search engine bots, such as HTTP_USER_AGENT with googlebot. The attacker shows Google a different page from the one people see.
  • php_value auto_prepend_file or auto_append_file. This forces a file to run before or after every page. Check the same setting in .user.ini and php.ini files too.
  • AddHandler or AddType lines that make other file types run as PHP, for example letting a .jpg or .txt file run as code.
  • A new .htaccess in many folders at once that blocks everything except one oddly named PHP file. Attackers use this to protect their own files.

Search for the warning signs in one go

If you can use the Terminal (see my guide on opening the Terminal in cPanel and running WP-CLI), these commands do the checking for you. They only read; they change nothing.

cd ~/public_html
grep -nE "eval\(|base64_decode|gzinflate|str_rot13|assert\(" wp-config.php
grep -nE "auto_prepend_file|auto_append_file|HTTP_REFERER|HTTP_USER_AGENT|AddHandler|AddType" .htaccess

No output is a good sign. Any line that appears needs reading.

To check every .htaccess, .user.ini and php.ini file on the site for the auto-prepend trick:

find ~/public_html \( -name .htaccess -o -name .user.ini -o -name php.ini \) -exec grep -lE "auto_prepend_file|auto_append_file" {} +

If you find something

  1. Keep a copy of the infected file as evidence, outside public_html.
  2. Remove only the bad lines, or replace .htaccess with the standard WordPress block above.
  3. Change the security keys. This logs everyone out, including the attacker:
wp config shuffle-salts --skip-plugins --skip-themes
  1. Change the database password in cPanel → MySQL Databases, then update DB_PASSWORD in wp-config.php to match. Anyone who could edit this file could also read it.
  2. Find out how the attacker got in. An altered wp-config.php means someone could write files on your account. Cleaning the file without closing that door means it will happen again. My security series covers how I traced exactly that.

Ready to start your project?

Share what you need, and the next steps follow.

Planning a project?

Your details are used only to answer your request. Privacy notice