WP Security Matters еBook EN
WP Security Matters еBook EN
WP Security Matters еBook EN
2
Introduction
Wondering if WordPress is secure? The answer is yes. WordPress
is built with the latest security technology and has
INTRO a regular release schedule that includes up-to-date vulnerability
patches. It’s also monitored and maintained by a large
community of developers who care deeply about security.
At SiteGround, we are aware of the many ways your site can fall
prey to attacks and we’re committed to helping site owners
protect themselves. Use this guide to increase your knowledge
of WordPress web security, implement new measures to secure
your website, and spread the word with those around you about
the need to protect their sites.
3
1
Protect Your Files
and Databases
4
1/ Before Installing WordPress
When setting up a new WordPress installation, you should
always choose the latest stable version. Before installing it,
follow these two simple web security steps in the
wp-config.php file:
By default, all WordPress installations use the prefix wp_ for their database.
This is consistent throughout WordPress, so it’s recommended to change
the prefix for each site to prevent possible attacks related to the database.
To alter the WordPress table prefix, change the following line in the
configuration file, wp-config.php, with the prefix that you would like to use:
$table_prefix = 'wp_';
For example:
$table_prefix = 'newsite_wp_';
This change will also allow you to have several WordPress installations on
the same database, as long as you do not repeat the prefix.
If your site is already installed and you didn’t change the default prefix
during the installation process, it’s not too late. Use a plugin such as
Change Table Prefix to make modifications. You can also do this
manually, but I don’t recommend it if you are not familiar with performing
database changes.
WordPress has secret keys, called Keys and Salt, that are stored in the
wp-config.php file. They protect open sessions by encrypting the session
data in the browser’s cookie. Before beginning the installation, you should
generate the secret keys.
5
Like with the database prefix, you can change the secret keys on an existing
site, at any time, a task that I recommend you perform routinely in order to
invalidate active sessions and force all users to log in again.
Although you can generate your own keys manually, I recommend using the
official WordPress service found at api.wordpress.org/secret-key/1.1/salt
and replace the keys with the ones in your wp-config.php file.
Before moving on to the next tip, I have one more piece of advice about
WordPress secret keys for live sites. In the unlikely scenario where you need to
deny any type of access to the admin panel, even with login credentials, you
can configure keys to invalidate every microsecond by replacing them in the
wp-config.php with the following:
define('AUTH_KEY', microtime());
define(''SECURE_AUTH_KEY', microtime());
define('LOGGED_IN_KEY', microtime());
define('NONCE_KEY', microtime());
define('AUTH_SALT', microtime());
define('SECURE_AUTH_SALT', microtime());
define('LOGGED_IN_SALT', microtime());
define('NONCE_SALT', microtime());
6
2/ After Installing WordPress
Once you’ve finished installing your new WordPress site,
you’ll need to undertake different actions to protect your
recently installed website.
One of the first things you should do to secure your new WordPress site is
to delete the admin profile used during installation and create a new user
with admin permissions, as well as any other necessary user accounts.
Protect files from attacks and intrusions by adding the following lines of
code in the htaccess file. Ideally, this should be done at the beginning of
the file located at the root directory of your site:
Options -Indexes
<files .htaccess>
Order allow,deny
</files>
<files wp-config.php>
Order allow,deny
</files>
7
You should block access to any unnecessary files by creating a new
.htaccess file in the /wp-admin directory and adding the following lines of
code:
<files install.php>
Order allow,deny
</files>
<files setup-config.php>
Order allow,deny
</files>
Some of these actions can easily be achieved by using a plugin such as the
SiteGround Security plugin. It’s a free plugin developed by SiteGround
and available to everyone to secure WordPress websites with just a few
clicks.
If you are using that plugin, you can choose an option that disables the
creation of common usernames. You need to go to SiteGround Security
plugin dashboard > Login Security > Disable Common Usernames.
What is more, if you already have one or more users with a weak
username, it’ll ask you to provide new ones: a pop-up window will appear
where you’ll be able to put a new username and automatically replace the
existing weak ones.
8
Disable Common Usernames
Using common usernames like ‘admin’ is a security threat that often results in unauthorised
access. By enabling this option we will disable the creation of common usernames and if you
already have one or more users with a weak username, we’ll ask you to provide a new one(s).
You can also go to SiteGround Security plugin dashboard > Site Securi-
ty > Lock and Protect System Folders. By enabling this option, an .htac-
cess file is placed in your WordPress’ system folders, preventing the
execution of unauthorized or suspicious scripts from being executed from
them.
SiteGround Security
We have created a list of tools to help you harden your WordPress site’s security and
keep it safe from malware, exploits and other malicious actions. The options tagged
as recommended are essential for your site security.
Site Security
9
You can change the permissions through an FTP client or through an
admin panel provided by your web host. With SiteGround, it’s easy to
change file and folder permissions in your Site Tools area.
Pro Tip: For more tips on WordPress user roles and permissions, check the
blog post about The Principle of Least Privilege on the SiteGround blog.
<Files *.php>
</Files>
Note: take into account that after every modification in a .htaccess file,
you should check it in your installation. Flush the cache to confirm that the
added rules are working properly.
10
5/ Disable File Editing in WordPress
This step focuses on adding a layer of security to the admin
panel to prevent unwanted intruders and limit mistakes
made by authorized users.
To disable the file edit option in the WordPress admin panel, use the
following line of code in the configuration file, wp-config.php:
You can add an additional layer of control for live sites if you don’t want
users to install themes and plugins on their own. To do this, add the
following code to the configuration file - wp-config.php:
11
Disable Themes & Plugins Editor
Disable the option to edit themes and plugins code directly from the WordPress admin to
prevent potential coding error or unauthorised access via the WordPress editor.
It hides the real IP of your server which prevents direct attacks against
your site by masking the real IP where you are hosted.
Although we hope you’ll never have to use this tip, it’s better
to be safe than sorry and have a full backup of your site.
You rarely need to restore a full site backup, but in case you
do, SiteGround has a tool for backups and easy restores
developed inhouse, independent of the web service
infrastructure. You can rest easy knowing we have copies of
your files in case of any incident, and you’ll be able to restore
your site easily and quickly.
I recommend you follow the 3-2-1 rule as a strategy for backups that
contain important data.
12
Keep 3 backups
In 2 different formats (minimum)
1 of the backups should be in a different physical location
In case disaster strikes, it’s useless to have all your backups in the same
format or location. With SiteGround’s shared hosting plans you have 30-
day backups, and they keep your files in different physical locations
than the one where your main website is located. For more information,
read the blog post Geographically Distributed Backups for Enhanced
Data Protection.
13
2
To use the HTTPS protocol on your site, install an SSL certificate on your
web server and change the URL in the admin panel.
With SiteGround, all hosting plans include free Let’s Encrypt SSL
certificates that can be installed and configured with an easy tool in the
control panel under Security section > Let’s Encrypt.
There are several WordPress plugins that force an HTTPS connection on all
your site resources, to avoid warnings or errors when serving both HTTP
and HTTPS content on the same page.
Finally, you must force any new session in the admin panel of your site to
be under SSL protocol by adding the following code to the wp-config.php:
define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);
Note: remember that you must have an active SSL in your installation,
for example, the one provided by Let’s Encrypt.
15
page, which appear by default if the username or the password are
incorrect.
function no_wordpress_login_errors(){
return ‘Thanks for trying but there were issues with the login.’;
There are several plugins in the WordPress repository that allow you to
change the path and location of your login page.
I recommend the SiteGround Security plugin. With it you have the option
to change the URL to a custom one and avoid attacks by going to
SiteGround Security plugin dashboard > Login Security > Custom
Login URL. If you have enabled user registration for your website, you can
change the default sign-up URL as well.
16
Custom Login URL RECOMMENDED
Attackers only try exploits on /wp-admin as a default login URL for WordPress. CONFIGURE
Change it to avoid these attacks and have an easily memorisable login URL.
However, other plugins exist and many security plugins also include this
functionality.
Once the login attempt limit is reached, the IP from which the attempts
have originated will be blocked from accessing your login page for 1 hour
and will be added in the Blocked tab of the Activity Log page. If the
attempts continue after the first hour, the block will then be set for 24
hours and after that for 7 days. You can manually remove the block at any
point in the Activity Log page.
17
SiteGround Security - Activity Log
The activity log can help you monitor your site and login page for unauthorised
visitors or brute force attempts. You can easily block and unbloc IPs or visitors
that look suspicious and prevent them from malicious actions.
Blocked
Other security plugins like Wordfence normally also include this feature, as
do the following plugins:
18
At SiteGround, customers have this service by default. They analyze the
types of connections and block attack attempts in a completely
transparent way for their customers. Some firewall plugins are:
Wordfence Security (be careful with the Live Traffic functionality, which
may leave you without service due to server overload)
All in one security and firewall
Start with the X-Frame-Options header, which prevents pages from being
opened in an external frame or iframe. This prevents clickjacking attacks
on your website: a technique that tricks internet users into revealing
confidential information on a seemingly normal website.
By adding the following line of code to your .htaccess file, you tell the
browser that frames can only be opened from the same domain or origin:
19
If your website includes services that can be embedded by third parties,
you can specify which domains are allowed and deny access to the rest.
For example:
To reduce the risk of XSS, build on the following tip by using the content-
security-policy header or browser content security policy, which specifies
what content from your site or third parties is allowed to dynamically load.
For example, if you want your site to only accept content from the same
domain, add the following line of code to your .htaccess file:
To modify the variables for your specific project, for example, to allow
scripts from third parties like Google Analytics, use this line of code:
20
If you have the SiteGround Security plugin, you can also use the
Advanced XSS protection option. You just need to go to SiteGround
Security plugin dashboard > Site Security > Advanced XSS protection.
This feature will protect your website against cross-site scripting attacks by
automatically inserting additional lines inside your website’s .htaccess file.
The fourth header you can use to boost your security is the X-con-
tent-type-options, which protects you from unwanted styles and scripts to
load when the expected MIME types do not match what was declared on
the page. To add this protection, add this line to your .htaccess file:
Another header you can use to secure your website better is the HSTS
(HTTP Strict-Transport-Security) response header. It allows the website to
tell browsers that it should only be accessed using HTTPS, instead of
using HTTP.
If you’re using the SiteGround Security plugin, you can simply enable
an option to automatically have this header. You can do this from
SiteGround Security plugin dashboard > Site Security > Force HTTP
Strict-Transport-Security (HSTS).
21
Force HTTP Strict-Transport-Security (HSTS)
Enabling this option protects your site from cookie hijacking and protocol downgrade
attacks. It is an additional rule forcing browsers to request HTTPS for your domain.
Disable XML-RPC
XML-RPC was designed as a protocol enabling WordPress to communicate with third-party
systems but recently it has been used in a number of exploits. Unless you specifically need
to use it, we recommend that XML-RPC is always disabled.
You can also completely deny access to the xmlrpc.php file by using rules
in the .htaccess file or by deleting it if you are sure that you don’t need it.
To deny access via .htaccess, add the following lines of code to the file:
<Files xmlrpc.php>
Order Deny,Allow
</Files>
22
You can also use plugins like Disable XML-RPC.
For those who absolutely need this API functionality, the best solution is to
enable it only from the IP where you need access and deny the rest.
In this specific case, add the following lines of code to the .htaccess file,
modifying the IP to the one requiring access:
<Files xmlrpc.php>
</Files>
23
3
Maintain a Secure
WordPress
Installation
24
15/ Choose Reputable Plugins and
Themes
Only download plugins and themes from the WordPress repository and
reputable sites. Before choosing your next plugin or theme, I recommend
you:
25
You can delete the information from the HTML header and from the static
files by adding the following code to the functions.php file of your theme
or in the utilities of your plugin:
/*
*/
global $wp_version;
return $src;
/*
*/
function SG_remove_wp_generator() {
return '';
add_filter('the_generator', 'SG_remove_wp_generator');
26
You can also hide information about the current WordPress version. If you’re
using the SiteGround Security plugin, you can go to SiteGround
Security plugin dashboard > Site Security > Hide WordPress Version. By
enabling this option, the information about your WordPress version will be
removed from your site’s HTML code automatically.
error_reporting( 0 );
ini_set( 'display_errors', 0 );
27
18/ Hide Apache and PHP Information
ServerSignature Off
There are two ways to hide the information about your site’s PHP version
that some servers send in the HTTP header. First, add the following code
to the .htaccess file:
expose_php = Off
Note: normally you can add this line of code to your active php.ini through
the server admin panel, but this may be different depending on your
hosting provider.
28
19/ Keep Your WordPress Updated
To protect your website against known security
vulnerabilities, you should use the latest version of the
WordPress core software, keep any installed plugins
updated, and update your themes.
Note: the automatic update won’t run if you have disabled the WordPress
cron.
You’ll receive an email to the address used by the platform admin account
after every update.
If you want to update plugins automatically, add the following line of code
to the functions.php file of your active theme or in your functionality
plugin:
29
Before adding this code, delete all the unused plugins on your site. Simply
deactivated them is not enough to remove potential vulnerabilities. Delete
them!
Lastly, remember that keeping your WordPress site secure is great, but the
computer you use should also be protected from malicious software and
viruses. Make sure you use a reputable antivirus and your operating
system is up to date.
Your hosting provider should offer you a secure platform and actively
maintain the security of their infrastructure. Be wary if your host uses
outdated software, unsecured access, and if their tech support has little
knowledge of WordPress.
Choosing the correct hosting provider will influence the success and
security of your WordPress project in a big way.
2530
21/ Bonus Tip: Take Post-hack Actions
If you believe that your website was compromised, you need
to take actions to identify and address the vulnerabilities.
These actions can easily be achieved using the
SiteGround Security plugin, and they include:
If you choose this option from SiteGround Security plugin dashboard >
Post-hack Actions > Reinstall All Free Plugins, the action will reinstall all
free plugins you have currently installed and use the same plugin versions.
This will remove any additional code added to them by an attacker.
31
Logging out all users
You can choose this option from SiteGround Security plugin dashboard
> Post-hack Actions > Log out all users, and it will log out all currently
logged in users without asking them to change their passwords. This will
prevent any users from performing any more actions on your website.
32
Conclusion
They say common sense isn’t so common, but in terms of
security, it’s your best ally. Use strong passwords, delete
RESUME
inactive users, assign the right roles to each user, do not save
active sessions on public computers, keep the server time
updated, only allow secure access, and actively monitor your
website.
33
4
About
the Author
34
Fernando Puente
fpuenteonline