ModSecurity: Open Source Web Application Firewall

ModSecurity for Java - Help Page

ModSecurity is an open source intrusion detection and prevention engine for web applications. It can also be called an web application firewall. It operates embedded into the web server, acting as a powerful umbrella, shielding applications from attacks.

ModSecurity for Java is designed as a Java Filter which makes use of ModSecurity's native code using the JNI technology.


Installation

First you need to choose whether to download and compile ModSecurity from the project's version control web-site: github.com/SpiderLabs/ModSecurity or using pre-compiled binaries from modsecurity.org. The native libraries (.so, .dll, etc.) needed for ModSecurity for Java are:

  1. zlib1 (Windows only)
  2. libxml2
  3. pcre
  4. libapr-1
  5. libapriconv-1 (Windows only)
  6. libaprutil-1
  7. ModSecurityJNI (JNI wrapper for mod_security code)

These native libraries are used by the ModSecurityFilter.


Compile ModSecurity native library

Install required packages for compilation. For example, on Debian/Ubuntu like systems:

sudo apt-get install g++ make automake autoconf libtool
                            

Install required dependent packages:

sudo apt-get install libxml2 libxml2-dev libxml2-utils libaprutil1 libaprutil1-dev apache2-prefork-dev
                            

Download mod_security source code from GitHub, compile and install:

cd mod_security/
./autogen.sh
./configure --enable-standalone-module --enable-java-module
make
                            

Copy compiled library in a convenient folder:

sudo cp ./java/.libs/libModSecurityJNI.so /usr/lib/
                            

Java Web Applications with ModSecurity Filter

ModSecurity for Java uses Java Filters in order to intercept Http requests and responses. ModsecurityTestApp is an example of Java EE Web application using the ModSecurity Filter. To use ModSecurity Filter in your Web application, copy the source files from mod_security/java/ModSecurityTestApp/src/ in your application and add the following entry for the filter tag in your web.xml file:


<filter>
        <filter-name>ModSecurityFilter</filter-name>
	    <filter-class>org.modsecurity.ModSecurityFilter</filter-class>
	    <init-param>
                <param-name>conf</param-name>
                <param-value>c:\inetpub\wwwroot\owasp-crs\modsecurity.conf</param-value> 
                <!-- Path to the main configuration file of ModSecurity. You can activate the core 
                     rules by including in modsecurity.conf file:
                        Include modsecurity_crs_10_setup.conf
                        Include activated_rules\*.conf
                -->
            </init-param>
    </filter>

    <filter-mapping>
	    <filter-name>ModSecurityFilter</filter-name>
	    <url-pattern>/*</url-pattern>
    </filter-mapping>
</filter>
                            
                            

The ModSecurity Filter makes use of the native libraries written in C/C++ using the JNI technology. There are two ways of loading native libraries by Java Web Applications:

  1. Loading native libraries directly in the ModSecurityFilter

    Although this is easier, it is not recommended because the JVM will raise UnsatisfiedLinkError if the ModSecurity Filter is used in multiple applications within the same server or the application is redeployed while the server is running. The libraries are loaded in the ModSecurity class using System.loadLibrary(). In this case the server has to be started with the following VM options:

    -Djava.library.path=/path/to/libraries/folder/
                                        

    You can specify multiple folders for the java.library.path variable by using : (colon) or ; (semi-colon), depending on your environment. Also, the libraries can be loaded using their absolute path using System.load().

  2. Loading native libraries when the Web Server starts

    ModSecurityLoader.jar should be placed in the Java server library loader folder (for example, in Tomcat 7: $CATALINA_HOME/lib). You can build or modify the load directory of ModSecurityLoader from /mod_security/java/ModSecurityLoader/src/.