ModSecurity
For providers of web applications, security is a permanent and highly important topic. This applies to their own applications as well as to the servers these run on. Where server configuration or outdated software used to be the cause for concern, today it is the applications or scripts that can create security gaps. The problem is that users now expect a wide range of functionality from web applications, such as access to databases or backend interfaces. The more such an application can do, however, the more room there is for flawed code that gives attackers an opening.
What sounds alarming at first can in fact be solved with a few expert steps. Web application firewalls (WAFs) can be placed between the users and the web application and thereby provide the desired data security. WAFs work particularly well together with network firewalls, because this secures different layers of the OSI model. While the WAF monitors layers 5 to 7, the network firewall takes care of layers 3 and 4. This way, all information is inspected, from the IP addresses and the ports to the data packets sent over the HTTP protocol, which makes attacks considerably harder.
ModSecurity, an open source WAF for Apache, is particularly interesting for securing web servers.
Small Project, Big Impact
Ivan Ristic wrote the first version of ModSecurity in 2002 to be able to trace traffic to and from an Apache HTTP Server. His company Thinking Stone was later acquired by Breach Security and after that by Trustwave Holdings. Ristic left the company in 2009, when it was still Breach Security, but remains involved in the development of ModSecurity. In 2017, he and a coauthor published a new edition of the ModSecurity handbook for all versions up to 2.9.2.
Principles of ModSecurity
Because Ivan Ristic wrote ModSecurity with very clear, practical requirements in mind, the current version also stays close to the actual needs of its users. Particular attention is paid to the many ways ModSecurity can be adapted, so that the application can be used in many different scenarios. To keep it that way, Ristic defined four basic principles that must always apply to ModSecurity.
- Flexibility
ModSecurity is written primarily for professional users who want to observe, analyze, and secure the HTTP traffic of their servers. For this purpose, the WAF comes without predefined settings and adapts to the system environment and the individual requirements at hand. This keeps ModSecurity flexible enough to be adapted to a wide range of scenarios. - Passivity
ModSecurity makes no decisions. Even though it would be easy to define rules for that, the tool is designed to observe and to give the user extensive information about the traffic. The resulting decisions are left entirely to the user. - Predictability
Users should always know what they can expect from ModSecurity. Ristic considers this especially important so that users are aware of the weak points of the web application firewall and can take appropriate countermeasures. - Quality over quantity
Ristic and the ModSecurity community hold their work to high quality standards. For this reason, the web application firewall does not have every feature it could have, but the features it does have are of high quality. Users can rely on ModSecurity to work dependably and do not have to deal with hastily implemented features that, in the worst case, open security gaps instead of closing them.
Installation Options
ModSecurity can be used both as a direct plugin for Apache servers and as a reverse proxy. Both variants have advantages and disadvantages.
- Embedded
ModSecurity was intended as an Apache module from the start, so it can be used with all current versions from 2.0.x onward, although use with 2.2.x is recommended. This form of deployment is particularly well suited to users who have already settled on their server architecture or when several web servers are to be protected at the same time. In this model, however, ModSecurity and the web server share the same resources. - Reverse proxy
When ModSecurity is used as a reverse proxy, the server and the web application firewall can operate separately from each other. In terms of resource allocation, the WAF is assigned a fixed amount in this model and can therefore process more rules, or more complex ones, than in the embedded setup. There are disadvantages here as well, however. In this type of installation, ModSecurity can become an additional security gap that can only be avoided by using two or more reverse proxies.
Use Cases
Even though ModSecurity is very flexible in how it is used and can be adapted to individual requirements, there are some typical scenarios in which the web application firewall is frequently deployed.
They are the following.
- Real-time monitoring and access control
This is one of the most fundamental functions of ModSecurity. The plugin gives users access to the server traffic and makes it possible to inspect it. - Logging
Where web servers normally store few log files, ModSecurity provides a wealth of data about the HTTP traffic. This makes it possible to trace and evaluate all kinds of server requests. - Continuous, passive security assessment
ModSecurity has an internal early warning system that runs continuously in the background and observes the performance of the plugin. If any errors or deviations occur, they show up in the security assessment and can be fixed by the admin before these security gaps are exploited. - Web application hardening
With ModSecurity, admins can clearly define which HTTP requests the server accepts. This way, they significantly reduce the attack surface.
Rule Sets
Like all web application firewalls, ModSecurity is only as good as the set of rules it acts on. Administrators must therefore either already have in-depth experience in creating rule sets or fall back on one of the many ready-made rule sets to cover as many different attack vectors as possible. ModSecurity uses rules for different phases of the client request.
- Request headers
The very first filtering of requests, before other steps such as access control or authorization have taken place. - Request body
ModSecurity gets full access to the content of the client request. - Response headers
ModSecurity filters the server responses at the earliest possible point. - Response body
The WAF gets complete access to the content of the server response. - Logging
ModSecurity accesses all other relevant data before a log file of the client request is created.
After the rule sets have been implemented successfully, it is very important to test them in practical use. In most cases, the first step is only to record which requests ModSecurity would block based on the rule set in use. No requests are actually blocked yet, and only log files are written. Only with knowledge of these files does the admin decide how these cases should actually be handled. As mentioned above, this corresponds to the principle of passivity in ModSecurity. In this way, ModSecurity avoids blocking users whose access would have been entirely legitimate.
Conclusion
ModSecurity is a very comprehensive and powerful plugin for Apache servers. Using this web application firewall does, however, require experience.
Without knowledge of the subject, the many options for defining your own rule sets in particular can quickly lead to blocking users whose access to the server is both permitted and wanted. Depending on the chosen form of installation, incorrect or improper handling of ModSecurity can also open security gaps instead of closing them.
It is therefore especially important that the admin learns about ModSecurity and the rule sets thoroughly in advance or already has extensive experience with this plugin.