In the last 24 hours, I have been the unfortunate recipient of a hacked WordPress site. Thankfully the site was non-critical and certainly not very popular. So rather than rush to restore the site, I’m taking the opportunity to understand firstly what was exploited to make this hack possible and secondly the remedy to stop this happening again.
eval("\n\$dgreusdi = intval(__LINE__) * 337;");
$a = str_replace($dgreusdi, "E", $a);
This is a crypto-mining script. It is not easy to understand what it is achieving by looking at the source code, however, the whole idea of this script is to earn a crypto-miner some crypto-currency: crypto-miners get rewarded with crypto-currency to be the first to ensure “the authenticity of information and updating the blockchain with the transaction” – so you can understand very quickly why they go around hacking peoples websites…they are incentivised by money!
Concerned about the potential impact to the WordPress community having only found information about vulnerabilities in Drupal being exploited to achieve this hack, I reached out on WordPress StackExchange, and the WordPress.org Support Forums. I was honestly surprised by the response! My understanding was an exploitation of WordPress, a plugin(s) or a theme, however nobody was interested in where the vulnerability was…just patch it up and add more layers of protection was the response I received…
From my point of view, the location of the vulnerability points towards the Contact Form 7 plugin, because one of the crypto-mining scripts was uploaded to the directory of this plugin. So, in the meantime, I’ll be investigating this plugin and any vulnerabilities that can be exploited to upload malware to the web server (I am much more interested in the “how” than the StackExchange or WordPress.org community).
Inevitably, I do need to restore this site, and on WordPress.org there are two great articles you can read on how to recover from a hacked WordPress site, and how to harden your WordPress site to reduce the chances of it being hacked again.
Step 1: Find the infection
The security scan is the most important part of restoring your hacked website, without it the vulnerability may still exist and your headache could happen all over again!
When choosing the scanning software, you are going to need something server-side so that it can see all of the website files. For that reason I can recommend either Cerber Security, Sucuri Security or Wordfence Security, all of which are plugins for WordPress that will help you discover vulnerabilities now and in the future, should they occur.
I also recommend that you check with your website hosting provider if they provide a security scanning service. I host with 1&1 and it was their automated security scanning service that first alerted me to the website hack and, crucially, they automatically neutralised it as well.
Your computer could be infected!
This is unfortunately true. It could be that your computer is infected with the same hack or it was the fact that your computer was hacked in the first place that allowed the hackers to gain access to your website (e.g. sniffing FTP login credentials or tracing keys on your keyboard). Consequently, you should run anti-virus and anti-malware scanning software on your computer immediately. I can recommend THIS AND THAT, which have free 30 day trials so you don’t even need to pay for it.
Don’t progress until your scanning is complete!
Imagine if the hackers know when you login to any web server via FTP and they can capture both your username and password! That it why you don’t move forward until the scanning is complete – there’s no point changing your passwords until you find the infection.
Step 2: Fix the infection
Your security scan will often suggest a resolution to the infection it finds, so obviously I recommend to follow this advice. However, in the case of the CoinHive crypto-jacking malware infection I experienced, I was simply able to remove the following 4 files that were found by the security scan:
Clearly, the names of these files are randomly generated and so I’m sure if you experience the same hack that the filename will be different.
Whatever kind of hack you experience, you are going to need to either clean the files that have been altered or clean the database (or both). To find our more about what to do in these situations you can read:
- To clean your database: “8. Go through the posts and repair any damage in the posts themselves” on Smackdown
- To clean your website files: “a few useful tools” on the Wordfence plugin website
Where you are at this point really depends how severe the infection was. You may well have cleaned the infection, and if you think you have done that, then you should run your security scans again. However, and unfortunately, it is still possible that remnants (and vulnerabilities) still exist in your website, which is why I recommend that you continue to Step 3.
Step 3: Change your usernames & passwords
How secure (i.e obscure) are both your usernames and passwords? You may not be able to change your usernames for your FTP or MySQL database accounts, however you can certainly for WordPress WP Admin access and your own computer!
- Change usernames where possible for FTP, MySQL databases, WordPress WP Admin & your own computer
- Change the passwords for these users as well (using long, complex & unique phrases) – use the LastPass password generator tool
Step 4: Maintenance mode
If your live website is still down, then I highly recommend placing it in maintenance mode so that visitors do not receive any kind of negative experience as a result of the hack and are instead informed that maintenance is going-on. You can do this manually by creating the “.maintenance” file in the website root and the “maintenance.php” file in the “wp-content” folder (read “How to manually enable and customize maintenance mode in WordPress” on Hostpapa for some help on this) or you can use a plugin such as Maintenance Mode, which is my favourite.
Step 5: Start again
I’m going to take a copy of my hacked site, so that I can further investigate how the hack might have occurred, and it is still a worthwhile backup for you to extract certain files when you need them.
However, then I’m going to create a fresh version of my website by:
- Creating a new database
- Installing the latest version of WordPress (from a fresh download)
- Installing the same theme & plugins that I had before (again, from fresh downloads)
- Then, most importantly, running a security scan of all the website files
Do not do this on your live web server!
It is possible that the vulnerability exists in a theme or plugin that you have installed and so you really do not want to be putting this back on your public web server – instead use a web server that is offline (your local server will do fine) and then when you are done re-creating your live site you can put it live.
Use version control!
It is in a situation like this especially when version control (such as Git and SVN) pay dividend. You should use version control for your theme (if it is bespoke or a child theme) at the very least – this is usually sufficient as everything else (WordPress core, plugins and parent themes) you can just download and install again.
Step 6: Harden WordPress
What can you do to stop this from happening again? It is the most important question – lesson’s have been learnt and it’s time to harden-up your website security. Here are my recommendations on how to do that:
- Security scanning & alerts: Install either Cerber Security, Sucuri Security or Wordfence Security
- Clean-up: Remove obsolete themes & plugins
- Limit access: Remove redundant user accounts
- Change usernames & passwords: For all accounts you decide to keep
- Schedule backups: Schedule backups of your database & website files on at least a daily basis to another location (i.e. not the same server that your website is on) using UpdraftPlus
- Update everything, manually: Manually update WordPress core, all themes & plugins and then check everything on your site to be sure it is working as you expect, then turn off automatic updates for WordPress core, and schedule a day, every month when you are going to login to the WP Admin and check for updates
- Set permissions: Set the recommended permissions for WordPress folders & files (see “Core Directories / Files” on WordPress.org)
- Secure wp-admin: Secure the “wp-admin” folder with password protection by following “How to Password Protect Your WordPress Admin (wp-admin) Directory” on wpbeginner – be sure to follow “Update: Here is how to fix the Admin Ajax Issue” as well
- Obfuscate wp-login: Use the Cerber Security plugin to change the login page URL from “wp-login.php” to some other random name
- Protect wp-includes: Protect the “wp-includes” folder by updating the .htaccess file in the website root as per “WP-Includes” on WordPress.org
- Protect uploads: Protect the “uploads” folder by creating an .htaccess file in the “uploads” folder as per “WP-Content/Uploads” on WordPress.org
- Protect wp-config: Protect the “wp-config.php” file by updating the .htaccess file in the website root as per “WP-Config.php” on WordPress.org
- Disable file editing: Disable the file editing from the WP Admin by adding
define('DISALLOW_FILE_EDIT', true);to “wp-config.php”
Step 7: ?
What else would you do? Do you have any recommendations? Let me know in the comments below…