- Joined
- Sep 23, 2014
- Messages
- 289
- Likes
- 450
- Degree
- 1
Unless you have the luxury of a dedicated sysadmin or devops person, you'll be wearing the sysadmin hat from time to time. A good working knowledge of Linux and WordPress security will go a long way to help prevent you from finding yourself in the potentially costly and embarrassing position of dealing with a compromised server.
We'll be focusing on a shared server scenario with WordPress installed. With this type of setup, the hosting provider (e.g. GoDaddy, HostGator) is managing the servers and taking care of updating and patching the server itself as well as making sure it doesn't crash and burn. The only security you have to worry about in this case is the security of the WordPress install itself and any content you upload to the server. This in itself can be a great thing because you don't have to worry about the whole server, just your sites.
Assumptions
- You're all set up and ready to go with a shared server as @Ryuzaki talks about in Day 4 - Setting Up Your Site
- WordPress is installed, you know your way around a bit and have even installed a plugin or two
- You know how to use the file manager on your cpanel or an SFTP program. We'll be using this to change file permissions
File permissions are the foundation of Linux security. In Linux, everything is a file, including directories. Each file has 3 permission groups and 3 permission types that, when used together, specify "who can do what".
The permission groups are:
- Owner/User : The owner of the file
- Group: The group assigned to the file
- World/Other: The rest of the world, such as website visitors
- Read: Can read file. Represented by an "r"
- Write: Can write to a file. Represented by a "w"
- Execute: Can run a file as an executable. In the case of directories, this allows files within the directory to be listed. Represented by an "x"
Read (r) = 4
Write (w) = 2
Execute (x) = 1
When you view a directory listing of files on a Linux server (or Mac) you'll see something like the following for each file
-rw-r--r-- 1 wp-user wp-group 418 Sep 25 2013 index.php
drwxr-xr-x 9 wp-user wp-group 4.0K Aug 6 2014 wp-admin
The file permissions are that string of characters that look like "-rw-r--r--" in the beginning of each file. The first character is reserved for advanced permissions. For example, if the file was a directory, this character would start with a "d". If the file was a regular file it would start with "-". There are other advanced permission types such as symbolic links, but we'll focus on regular files and directories for now.
So back to the first file in the directory listing example. We have:
-rw-r--r-- 1 wp-user wp-group 418 Sep 25 2013 index.php
Which is a regular file because it starts with a "-". Then we have "rw-r--r--" which means the following:
- Owner can read (4) and write (2) to the file but not execute (rw-)
- Group can read (4) the file but nothing else (r--)
- World can read (4) the file but nothing else (r--)
Owner (red)
Group (blue)
World (green)
-rw-r--r-- 1 wp-user wp-group 418 Sep 25 2013 index.php
- This is a regular file (starts with a "-") and it's named "index.php"
- The owner is "wp-user" and can read (4) and write (2) to the file. 4+2 is 6 which is the first number in the permission mode.
- The group is "wp-group" and can only read (4) the file and nothing else. 4 is the second number in the permission mode
- The world can only read (4) the file and nothing else. 4 is the third number in the permission mode
- The permission mode for this file is 644
Lets look at the 2nd file which is a directory. I'll color code again
drwxr-xr-x 9 wp-user wp-group 4.0K Aug 6 2014 wp-admin
- This file is a directory (notice the "d" as first character) and it's named "wp-admin"
- The owner can read (4), write (2) and execute (1) the file. Since this file is a directory, setting the "execute" mode will allow the owner to list files in the directory. 4 + 2 + 1 = 7 which is the first number in the permission mode
- The group can read (4) and execute (1) the file. 4+1 = 5 which is the second number in the permission mode.
- The world can read (4) and execute (1) the file. 4+1 = 5 which is the third number in the permission mode.
- The permission mode for this file is 755
Now it's time to take what you've learned and ensure that the file permissions are set correctly for WordPress. Using your SFTP program or file manager on your cpanel, verify the permission mode for each file and directory. The parent directory that WordPress is in on your server will vary depending on which hosting provider you chose. Once you've found it, the following are the file permissions you will assign to each file and directory:
- Each directory should have a permission mode of 755 (drwxr-xr-x).
- Each file should have a permission mode of 644 (-rw-r--r--)
- The wp-config.php file should have a permission mode of 600 (-rw-------). This helps to prevent others (the world) from seeing your database password
Also, please note that the correct file permissions might already be set for WordPress and if they are, great, that's one less thing to have to worry about.
If you'd like to do some further reading on Linux file permissions and more about how this works with WordPress, the following are great places to start:
- https://www.linux.com/learn/tutorials/309527-understanding-linux-file-permissions
- http://www.smashingmagazine.com/2014/05/proper-WordPress-filesystem-permissions-ownerships/
What's .htaccess and why does it begin with a dot? The .htaccess file is a way to make actual configuration changes to the web server (Apache in most cases) as well as giving you the ability to block IP addresses, user agents (bad bots) etc on a "per directory" basis. It starts with a "." because files in Linux that begin with a dot are typically used for configuration purposes and won't show with a normal list command. They are also referred to as "hidden files". Many SFTP/SCP programs will show these files by default, others you'll have to configure to see the files.
The most common usage of this file is to place .htaccess in your website root folder and set up some rules in it. By doing this, all files and all folders that are inside the parent folder will have the rules applied to it and you have a "poor mans firewall" of sorts.
Let's say you have an IP address you want to block and the IP address is 123.45.67.89 You would put the following into your .htaccess file:
Code:
Order Allow,Deny
Allow from ALL
Deny From 123.45.67.89
Here's where things get a bit confusing. The first line, "Order Allow,Deny" is telling the server to process the "Allow" directives first, followed by the "Deny" directives. This is what's happening:
- User/Bot visits your site
- The "Allow From ALL" rule is matched first. At this point the User/Bot is allowed, but the show is not over yet because the server still needs to process the Deny rules.
- The "Deny From 123.45.67.89" rule is evaluated. If the IP of the user/bot matches the IP in the rule, we deny access to the site with a 403 (Forbidden) error
- If the IP of the user/bot doesn't match any Deny rule, the user/bot is allowed access
Code:
Order Allow,Deny
Allow from ALL
Deny From 123.45.67
All we had to do was leave the last octet of the IP address off and we're now blocking the entire class C. This is not something you'll want to do that often, but you can if you like.
Blocking IP addresses is all find and good, but how about blocking crawlers by their user agent. Let's say you don't want the Ahrefs service from crawling your site. You can put the following in your .htaccess file
Code:
BrowserMatchNoCase "AhrefsBot" badbots
Order Allow,Deny
Allow from ALL
Deny from env=badbots
There are other ways to block bad user agents using Apache's "mod_rewrite". Here's an article that covers most of what I've described and a few more tricks you might be interested in:
http://www.inmotionhosting.com/supp...-unwanted-users-from-your-site-using-htaccess
Tip: A quick word of caution about .htaccess If you have a lot of entries in this file, it WILL have an impact on your page load time and overall server performance. At that point, you'll want to look into setting up a VPS so you can put the rules in the main server config file. The reason .htaccess is slow is mostly because Apache has to read/parse the file with each request. That's why you are able to make changes to the .htaccess file and the rules will be applied the very next request. Here's the word from Apache on .htaccess
Source: https://httpd.apache.org/docs/2.4/howto/htaccess.html
"You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance."
Principle of Least Privilege
The principal of least privilege is part of "computer security 101" (https://en.wikipedia.org/wiki/Principle_of_least_privilege). In a nutshell, it's all about only giving just enough access to a user or process to accomplish the task at hand and no more. Got a partner that only writes content? Give them "editor" access. How about people that do guest posts? Give them "contributor" access. Got a fly to swat? Use a flyswatter and save the nuke for warfare.
You should have only one admin account that you use to update WordPress, add/remove users, etc. All other users should have the "Editor" access level or below. This means your own personal account on the site should not have admin access. For more information on roles in WordPress, check out https://codex.WordPress.org/Roles_and_Capabilities
Strong Passwords
Ideally you want to go for passwords that
- Are a minimum of 12 characters in length
- Have a mixture of upper and lowercase letters
- Do not contain dictionary words
- Contain 1 or more symbols (e.g. !$%^#)
When it comes to plugins, I'm a "less is more" kind of guy. Plugins alter the way WordPress works and the end result can be either good or bad. As a developer, you can create a plugin to do pretty much anything you need and make WordPress do all kinds of things it wasn't intended to do.
Think about that for a second. Plugins can be created to help make marketers millions, or they can be created to compromise your site security. Sometimes unskilled developers or even coding mistakes can open your site up to attack.
Every plugin you install should
- Accomplish something you have a real need for. It's far too easy to install every plugin under the sun, thinking "I might need this". Wait till you need the functionality, THEN install
- Have a decent "reputation" (rating). When you install a plugin, take a look at a few of the user reviews and note whether the users have a positive or negative experience overall. You particularly want to pay attention to any security related issues.
Wordfence: https://WordPress.org/plugins/wordfence/
Themes
As with plugins, take caution when installing themes. Not only can themes open up a site to attack, they can be just plain poorly coded/implemented, leaving you with a slow site. The "less is more" mantra applies here as well.
A good theme to start with is BuSo Lightning, created by BuSo's own, @Ryuzaki. Very light theme that's coded well and won't put your site in harms way as far as security goes: https://www.buildersociety.com/threads/buso-lightning-the-fastest-wordpress-theme.763/
Keep WordPress Updated
Since version 3.7, WordPress will automatically apply security updates. It is up to you to keep plugins, themes, etc updated. You can configure WordPress to automatically update plugins and themes if you like but for now it's a good idea to get used to logging in and keeping things updated yourself. If you'd like to read more about different configuration options for automatic updates, this will point you in the right direction: https://codex.wordpress.org/Configuring_Automatic_Background_Updates
The Next Level
Working with a cpanel is great (not really), but there is no cpanel in the world that can give you anywhere near the power that the Linux Command Line will. Does having the power of every programming language you could ever need at your fingertips sound sexy to you? Yes? No?
Maybe it would be better if I showed you what you're missing out on. How about a primer on the command line that will make your cpanel sing "baby come back"? Something that goes beyond the basics? And how about I throw in some server hardening tips for good measure? I'm going to bring you all of that and more, just not right now. I want you to know and understand the limits of your cpanel before I lift the veil for you and show you why I call the command line "The last cpanel you'll ever need". Be on the lookout for that. It might be out already, might not be. You'll know it when you see it
Now, stay tuned for my ramblings on "Online Privacy".
Last edited: