How To Setup & Configure ModSecurity On Apache (Debian, Ubuntu, CentOS)
How To Setup & Configure ModSecurity On Apache (Debian, Ubuntu, CentOS)
Apache
March 11, 2019 APACHE SERVER SECURITY
Home » Web Servers » How to Set up & Con gure ModSecurity on Apache
Introduction
ModSecurity is a plug-in module for Apache that works like a rewall. It functions through rule sets, which allow
you to customize and con gure your server security.
ModSecurity can also monitor web tra c in real time and help you detect and respond to intrusions. It can be
used with Apache, Nginx, and IIF and is compatible with Debian, Ubuntu, and CentOS.
This tutorial explains how to install and con gure ModSecurity on Apache web servers.
Prerequisites
The LAMP stack (Linux, Apache, MySQL, PHP) installed and con gured
Access to a user account with sudo or root privileges
A package manager (APT or YUM), included by default
A command line/terminal window (Ctrl-Alt-T, Ctrl-Alt-F1)
A text editor, like nano
On Debian / Ubuntu
On CentOS
Note: Ubuntu has a slightly different syntax for the ModSecurity package.
2. Next, change the ModSecurity detection mode. First, move into the /etc/modsecurity folder:
sudo cd /etc/modsecurity
3. Open the con guration le in a text editor (we will be using nano):
SecRuleEngine DetectionOnly
SecRuleEngine On
4. Use CTRL+X to exit, then press y then Enter to save the changes.
cd
6. Restart Apache:
On Debian/Ubuntu
On CentOS
This will turn on ModSecurity using the basic default rules. In some versions of Linux, this includes the OWASP
Core Rule Set. However, this might differ from the latest version maintained by the developers.
This places a copy of the directory as a subdirectory of your current working location.
cd owasp-modsecurity-crs
6. Next, check your security2.conf le to verify it’s set to load the ModSecurity rules:
IncludeOptional /etc/modsecurity/*.conf
Include /etc/modsecurity/rules/*.conf
If they are not there, add them. Do not duplicate them, or you risk disabling your Apache service.
On Debian/Ubuntu
On CentOS
2. Locate the </VirtualHost> tag at the bottom and add the following lines:
SecRuleEngine On
SecRule ARGS:testparam "@contains test" "id:1234,deny,status:403,msg:'phoenixNAP tes
t rule was triggered'"
On Debian/Ubuntu
On CentOS
curl localhost/index.html?testparam=test
The system responds by attempting to display the default webpage. Instead of the content, it generates error
codes and messages inside the tags:
5. You can con rm that ModSecurity worked by looking for code 403 at the Apache error logs with the
command:
One of the entries towards the bottom should be the ModSecurity error code:
curl localhost/index.html?exec=/bin/bash
The output shows the same error messages as the last time.
2. View the Apache error.log le again and you will nd that the the rule kicked in:
<html>
<body>
<?php
if(isset($_POST['data']))
echo $_POST['data'];
else
{
?>
<form method="post" action="">
Enter text here:<textarea name="data"></textarea>
<input type="submit"/>
</form>
<?php
}
?>
</body>
</html>
Of course, change the keywords in the last line to anything you want.
On Debian/Ubuntu
On CentOS
localhost/test.php
6. Type one of the keywords from the rule into the form. In this example: enlarge, Nigerian, or gold.
Note: We don’t need to add this custom_rules le to the security2.conf le, because we
speci ed a wildcard (IncludeOptional /etc/modsecurity/*.conf). If we had speci ed an
individual .conf le, we would need to add this custom_rules le to the security2.conf le.
Conclusion
You should now have a solid understanding of how to install, set up, and con gure ModSecurity on Apache.
Make sure you installed the LAMP stack properly before following the steps in this guide.
Goran Jevtic
Goran combines his leadership skills and passion for research, writing, and technology as a
Technical Writing Team Lead at phoenixNAP. Working with multiple departments and on various
projects, he has developed an extraordinary understanding of cloud and virtualization
technology trends and best practices.
Enable Automatic DoS & DDoS on Find All Open How to Set up &
Security Updates Apache With Ports with Nmap Use NGINX as a
on Ubuntu mod_evasive February 16, 2019 Reverse Proxy
January 8, 2019
May 2, 2019 March 5, 2019
The Nmap hosted
If you do not keep your The mod_evasive tool security tool can help Nginx is a reverse
system up-to-date, you services module that well your rewall and standard proxy server
run the risk of helps your server stay security con guration works on behalf of ...
READ MORE
system... an...
READ MORE READ MORE
SysAdmin
Europe NEEDS
Big Memory Disaster Recovery
Virtualization
Infrastructure Solutions
DevOps and
High Availability
Development
BUY NOW Solutions
Security
Cloud Evaluation
Backup and
Recovery
Web Servers
Networking
Databases
Contact Leg Privacy Terms of DMC GDP Sitem © 2022 Copyright phoenixNAP | Global IT Services. All Rights
Us al Policy Use A R ap Reserved.