ModSecurity is an open-source, cross-platform web application firewall (WAF) module. Known as the "Swiss Army Knife" of WAFs, it enables web application defenders to gain visibility into HTTP(S) traffic and provides a power rules language and API to implement advanced protections. ModSecurity is a toolkit for real-time web application monitoring, logging, and access control.
Real-time application security monitoring and access control
At its core, ModSecurity gives you access to the HTTP traffic stream, in real-time, along with the ability to inspect it. This is enough for real-time security monitoring. There's an added dimension of what's possible through ModSecurity's persistent storage mechanism, which enables you to track system elements over time and perform event correlation. You are able to reliably block if you so wish because ModSecurity uses full request and response buffering.
Web servers traditionally do very little when it comes to logging for security purposes. They log very little by default, and even with a lot of tweaking you are not able to get everything that you need. I have yet to encounter a web server that is able to log full transaction data. ModSecurity gives you the ability to log anything you need, including raw transaction data, which is essential for forensics. In addition, you get to choose which transactions are logged, which parts of a transaction are logged, and which parts are sanitized.
A security assessment is largely seen as an active scheduled event, in which an independent team is sourced to try to perform a simulated attack. The continuous passive security assessment is a variation of real-time monitoring, where, instead of focusing on the behavior of the external parties, you focus on the behavior of the system itself. It's an early warning system of sorts that can detect traces of many abnormalities and security weaknesses before they are exploited.
One of my favorite uses for ModSecurity is attack surface reduction, in which you selectively narrow down the HTTP features you are willing to accept (e.g., request methods, request headers, content types, etc.). ModSecurity can assist you in enforcing many similar restrictions, either directly, or through collaboration with other Apache modules. They all fall under web application hardening. For example, it is possible to fix many session management issues, as well as cross-site request forgery vulnerabilities.
Prerequisites
You will need one (physical or virtual) machine installed with Ubuntu or Debian having a non-root user sudo privileges. This guide assumes that you have already set up Apache on your Ubuntu or Debian server.
Install ModSecurity
Type below command to install ModSecurity on Ubuntu:Restart apache service to take mod-security module into account:
Type below command to install ModSecurity on Debian:
Type below command to install ModSecurity on CentOS/RHEL 7
Type below command to install ModSecurity on CentOS/RHEL 8
Configure ModSecurity
The following steps are for Ubuntu or Debian based distributions. If you are on CentOS/RHEL, file paths and commands will differ slightly.Move and change the name of the default ModSecurity file:
Download the OWASP ModSecurity CRS from Github:
Move and rename crs-setup.conf.example to crs-setup.conf. Then move rules/ directory as well.
The configuration file should match the path above as defined in the IncludeOptional directive.
Add another Include directive pointing to the ruleset:
Save and close the editor.
Restart Apache to take changes into effect:
Test ModSecurity
The OWASP CRS builds on top of ModSecurity so that existing rules can be extended.Edit the default Apache configuration file and add two additional directives, using the default configuration as an example:
Save and close the editor.
Restart Apache to take changes into effect.
Curl the index page to intentionally trigger the alarms:
The response code should be 403. There should be a message in the logs that shows the defined ModSecurity rule worked.
You can check using: sudo tail -f /var/log/apache2/error.log
Verify the OWASP CRS is in effect:
Check the error logs again: the rule has caught the attempted execution of an arbitrary bash script.
Wrapping up
Review the configuration files located in /etc/modsecurity/*.conf. Most of the files are commented with definitions of the available options. ModSecurity uses an Anomaly Scoring Level where the highest number (5) is most severe. Review the wiki for additional directives to update the rules when encountering false positives. You may wish to go through the following official resources for additional information on this topic.
after including the directive in
ReplyDeleteTrying to restart apache fails to error :
Jun 12 18:52:23 vodacom apachectl[14315]: AH00526: Syntax error on line 773 of /etc/modsecurity/crs/crs-setup.conf:
Jun 12 18:52:23 vodacom apachectl[14315]: Error parsing actions: Unknown action: "
Jun 12 18:52:23 vodacom apachectl[14315]: Action 'start' failed.
Jun 12 18:52:23 vodacom apachectl[14315]: The Apache error log may have more information.
Jun 12 18:52:23 vodacom systemd[1]: apache2.service: Control process exited, code=exited status=1
Jun 12 18:52:23 vodacom systemd[1]: apache2.service: Failed with result 'exit-code'.
Jun 12 18:52:23 vodacom systemd[1]: Failed to start The Apache HTTP Server.
-- Subject: Unit apache2.service has failed
Pay attention to this and fix it first, then reload the apache service:
DeleteSyntax error on line 773 of /etc/modsecurity/crs/crs-setup.conf
Most likely some actions are loaded twice.
ReplyDelete(for debian)
Make sure /etc/apache2/mods-available/security2.conf has
#IncludeOptional /usr/share/modsecurity-crs/*.load commented out!
If not, you're including /etc/modsecurity/crs/crs-setup.conf AND /etc/modsecurity/crs-setup.conf !!