• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » How to Install and Configure ModSecurity for WordPress

By Abhishek Ghosh April 23, 2023 7:16 pm Updated on April 23, 2023

How to Install and Configure ModSecurity for WordPress

Advertisement

In an earlier article, we published an introduction on mod security. While there are many good reasons to use mod security, this module adds complexity to any WordPress installation.

This guide is based on the official directions for WordPress to create a window so that a WordPress user can edit any post, and upload media. Still, you need to monitor the log for unwanted errors (in /var/log/apache2/error.log file). If the errors can not be managed by best efforts, unfortunately, you will have to disable ModSecurity. Apart from the log file, keep an eye on Google Analytics.

 

How to Install ModSecurity on Ubuntu Server

 

We will use the ModSecurity module with ModSecurity Core Rule Set from the Ubuntu repo:

Advertisement

---

Vim
1
2
3
4
5
6
7
apt install gnupg2 software-properties-common curl wget git unzip -y
# add-apt-repository ppa:ondrej/apache2 -y
# apt update -y
# apt install apache2 -y
sudo apt-get install libapache2-mod-security2 -y
a2enmod security2
sudo systemctl restart apache2

Open /etc/apache2/mods-enabled/security2.conf file:

Vim
1
nano /etc/apache2/mods-enabled/security2.conf

Uncomment:

Vim
1
2
3
...
IncludeOptional /etc/modsecurity/*.conf
...

Next, move/rename this file:

Vim
1
mv /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf

Open the ModSecurity configuration file:

Vim
1
nano /etc/modsecurity/modsecurity.conf

Add these:

Vim
1
2
SecRuleEngine On
SecAuditLogParts ABCEFHJKZ

Restart Apache:

Vim
1
systemctl restart apache2

The rules can be found here:

Vim
1
/usr/share/modsecurity-crs

Open the default Apache configuration file for your site:

Vim
1
sudo nano /etc/apache2/sites-available/000-default.conf

Find the VirtualHost closing tag at the bottom and add the following lines:

Vim
1
2
SecRuleEngine On
SecRule ARGS:testparam "@contains test" "id:1234,deny,status:403,msg:'Security rule was triggered'"

Open these files one by one:

Vim
1
2
nano /etc/apache2/apache2.conf
nano /etc/apache2/conf-enabled/security.conf

… add or edit these two directives to the suggested values shown below:

Vim
1
2
ServerTokens Prod
ServerSignature Off

Run a configuration test and restart Apache:

Vim
1
2
apachectl configtest
systemctl restart apache2

You can change the msg to whatever you prefer. Now, test your site:

Vim
1
curl https://domain-name/?exec=/bin/bash

You should receive an error like the one below:

Vim
1
2
3
4
5
6
7
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access this resource.</p>
</body></html>

That completes the generic configuration of ModSecurity.

 

How to Configure ModSecurity on Ubuntu Server for WordPress

 

Open crs-setup.conf with a text editor:

Vim
1
nano /etc/modsecurity/crs/crs-setup.conf

Search the document with the phrase exclusions_wordpress, and you’ll find commented out settings as we have shown below:

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# SecRule RESecAction \
#SecAction \
# "id:900130,\
#  phase:1,\
#  nolog,\
#  pass,\
#  t:none,\
#  setvar:tx.crs_exclusions_cpanel=1,\
#  setvar:tx.crs_exclusions_dokuwiki=1,\
#  setvar:tx.crs_exclusions_drupal=1,\
#  setvar:tx.crs_exclusions_nextcloud=1,\
#  setvar:tx.crs_exclusions_phpbb=1,\
#  setvar:tx.crs_exclusions_phpmyadmin=1,\
#  setvar:tx.crs_exclusions_wordpress=1,\
#  setvar:tx.crs_exclusions_xenforo=1"

Edit it in a way so that it looks exactly like the below example:

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SecAction \
"id:900130,\
  phase:1,\
  nolog,\
  pass,\
  t:none,\
  setvar:tx.crs_exclusions_wordpress=1
#  setvar:tx.crs_exclusions_cpanel=1,\
#  setvar:tx.crs_exclusions_dokuwiki=1,\
#  setvar:tx.crs_exclusions_drupal=1,\
#  setvar:tx.crs_exclusions_nextcloud=1,\
#  setvar:tx.crs_exclusions_phpbb=1,\
#  setvar:tx.crs_exclusions_phpmyadmin=1,\
#  setvar:tx.crs_exclusions_wordpress=1,\
#  setvar:tx.crs_exclusions_xenforo=1"

Run a configuration test:

Vim
1
apachectl configtest

If you face errors, correct the faulty syntax. Restart Apache if you get the Syntax OK message:

Vim
1
systemctl restart apache2

Open modsecurity.conf file:

Vim
1
nano /etc/modsecurity/modsecurity.conf

Find the below keywords and set the limit like our example:

Vim
1
2
SecPcreMatchLimit 1000000
SecPcreMatchLimitRecursion 1000000

Find line number 86 in the modsecurity.conf :

Vim
1
nano +86 /etc/modsecurity/modsecurity.conf

Comment out the line(s) to make like this one:

Vim
1
2
3
4
5
6
...
#
# SecRule MULTIPART_UNMATCHED_BOUNDARY "!@eq 0" \
# "id:'200004',phase:2,t:none,log,deny,msg:'Multipart parser detected a possible un>
# PCRE Tuning
...

How to Install and Configure ModSecurity for WordPress

Create a whitelist rule:

Vim
1
nano /etc/apache2/conf-enabled/whitelist.conf

Paste the below content:

Vim
1
2
3
4
5
6
7
<LocationMatch "/wp-admin/theme-editor.php">
  SecRuleRemoveById 300015 300016 300017 950907 950005 950006 960008 960011 960904 959006 980130
</LocationMatch>
 
<LocationMatch "/wp-admin/admin-ajax.php">
  SecRuleRemoveById 300015 300016 300017 950907 950005 950006 960008 960011 960904 959006 981173 980130
</LocationMatch>

Run a configuration test and restart Apache:

Vim
1
2
apachectl configtest
systemctl restart apache2

With the above settings, you should be able to login to WordPress, edit posts and upload media. If you still face the Access Denied webpage from Apache, then open the default Apache configuration file for your site:

Vim
1
sudo nano /etc/apache2/sites-available/000-default.conf

Find the VirtualHost closing tag at the bottom and add comment out the following lines to make like below example:

Vim
1
2
# SecRuleEngine On
# SecRule ARGS:testparam "@contains test" "id:1234,deny,status:403,msg:'Security rule was triggered'"

Disable ModSecurity, run a configuration test, if the test passed then restart Apache:

Vim
1
2
3
a2dismod security2
apachectl configtest
sudo systemctl restart apache2

This will disable ModSecurity. Later find the specific issue on your setup, fix that and again enable ModSecurity.

Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to How to Install and Configure ModSecurity for WordPress

  • What is ModSecurity? Why Should We Use ModSecurity

    ModSecurity is an open-source web application firewall. It is available as a module for the Apache Server, and also Microsoft IIS and Nginx web server. It provides a rule configuration language (SecRules) for real-time monitoring, logging, and filtering of HTTP communications. ModSecurity is probably most commonly used to add protection against general vulnerabilities using the […]

  • Nginx WordPress Installation Guide (All Steps)

    This is a Full Nginx WordPress Installation Guide With All the Steps, Including Some Optimization and Setup Which is Compatible With WordPress DOT ORG Example Settings For Nginx.

  • How To Install Apache2 on Ubuntu 18.04 With Let’s Encrypt, HTTP/2, HSTS

    Here is Detailed Guide on How To Install Apache2 on Ubuntu 18.04 With Let’s Encrypt, HTTP/2, HSTS With Commands and Configurations For Most Secured Setup.

  • Set Up mod_security And fail2ban To Create Anti-Spam Filter

    Here is How To Set Up mod_security And fail2ban To Create Anti-Spam Filter For Web Software Auch as WordPress, Forum Software.

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Dynamic WebP Image Delivery in WordPress with Bunny CDNJune 3, 2023
  • Advantages and Disadvantages of Ubuntu Server DistributionJune 2, 2023
  • Typography on the WebJune 2, 2023
  • How to Use JuliaMono Font in Urvanov/Crayon Syntax HighlighterJune 1, 2023
  • What Is a Sales Funnel?June 1, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT