Here’s an easy way to protect your WordPress site from hackers and brute force attacks by adding server-side protection to your wp-login.php page in the form of an .htaccess login.
As WordPress has become more popular, it’s also become a bigger target for hackers. In the last few years, there have been several automated attacks on WordPress sites where malicious visitors try to break into WordPress sites by running dictionary attacks against WordPress login pages.
By making a few quick additions to your site, we can thwart several of these automated attacks, while preserving resources on the server for legitimate site visitors.
What are Dictionary Attacks?
A dictionary attack uses a targeted technique of successively trying all the words in an exhaustive list called a dictionary . . . a dictionary attack tries only those possibilities which are most likely to succeed, typically derived from a list of words for example a dictionary (hence the phrase dictionary attack). http://en.wikipedia.org/wiki/Dictionary_attack
What does this mean? Well, the hacker uses a program or script to automatically try to login to your site using a username that they either know, or guess, along with a password generated from a list. When they fail to login, the script tries again with the next password on the list.
Can’t the Server Block Dictionary Attacks?
On IvyCat servers, we have Brute Force Protection that watches for behavior like this and blocks these malicious visitors by IP address. But, alas, these folks aren’t dumb and, when they’re blocked, often change their IP address and continue the attack from a different location.
I’m using Strong Passwords, so I’m safe, right?
If you’re using good, strong passwords, this is often more of an annoyance than anything, but one side-effect of these attacks is increased load on the server.
Your site and the server it’s hosted on have to work every time someone tries to login and, if your site is the subject of an automated attack where they’re trying to login multiple times a minute, or even second, it can cause the server to work extra hard just to handle the enormous amount of login requests.
So, we’re going to use .htaccess to evaluate login attempts in a way that doesn’t cause so much server load.
How WordPress Login Requests Work
Technically, when a request comes in for any page on your WordPress site, the request comes to the web server and the web server passes the request through to WordPress for handling. This happens for any page, including your WordPress login.
When a visitor is trying to login to your WordPress site, here’s what happens:
- The request for your WordPress login comes to the server, either using
- The server sees this request and passes it through to WordPress, which loads your site’s login page,
- The visitor enters a username and password and clicks Login.
- WordPress has to parse the entered login to see if it’s legitimate.
- If the credentials are good, the visitor is taken to their WordPress Admin, but if they’re bad, WordPress has to load a page to return an error to the visitor.
While the above steps aren’t super-taxing on a server by themselves, imagine if you have 100 people trying to login to your site at once, or even a login attempt every second – the server can get overwhelmed and spend a bunch of time and energy fielding bogus login requests when it could be using resources to serve up websites.
So, we’re going to stop visitors using a server-generated login before they get to the WordPress to make sure they’re legit. If they can’t pass the server’s login page, they’ll never get to the WordPress login page, making it easier on the server since WordPress isn’t required to load to evaluate passwords.
Protect wp-login.php and wp-admin with .htaccess
So, we’ve put together a simple set of instructions to allow you to protect your WordPress login using .htaccess.
There are three basic steps:
- Create a hidden password file.
- Generate a login and password and add it to the hidden password file.
- Modify your site’s .htaccess file to require your hidden password when logging into WordPress.
Step 1 – Create a hidden password file
In the root of your site, create a file called
.wp-admin, which will create your new, hidden credentials. The file can be named something else too, but we recommend that you put a period at the beginning of the filename, so that it’s hidden by default.
Step 2 – Generate a login and password and add it to the hidden password file
- Go to http://www.htaccesstools.com/htpasswd-generator/
- Enter a username and password that you’ll use to protect the WordPress login.
- Click Create
.htpasswdfile to encrypt your password.
- .htaccesstools.com will return a string of text that looks something like this:
- Open the
.wp-adminfile that you created in Step 1 above and paste in the .htpasswd entry you just created.
Step 3 – Modify your site’s .htaccess file
By now, you’ve generated a login and password that you’ll use to protect your WordPress login, but the last step is to make sure that the web server, Apache, knows that it needs to require this login every time someone tries to login to your WordPress site.
- Using File Manager or FTP, login to your hosting account and open the
.htaccessfile at the root of your site and paste in the following code:
ErrorDocument 401 "Unauthorized Access" ErrorDocument 403 "Forbidden" <FilesMatch "wp-login.php"> AuthName "Authorized Only" AuthType Basic AuthUserFile /home/username/.wp-admin require valid-user <FilesMatch>
- Save your changes.
- Open a browser and go to your site’s login page (ex. http://yoursite.com/wp-login.php).
You should now see a pop-up generated by the server that asks for your username and password. Once you enter the username and password that you just created and submit, you’ll be taken to the WordPress login screen and can proceed like normal.