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:
-
zlib1 (Windows only)
-
libxml2
-
pcre
-
libapr-1
-
libapriconv-1 (Windows only)
-
libaprutil-1
-
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>
<!--
<init-param>
<param-name>zlib1</param-name>
<param-value>c:\work\mod_security\java\libs\zlib1.dll</param-value>
</init-param>
<init-param>
<param-name>libxml2</param-name>
<param-value>c:\work\mod_security\java\libs\libxml2.dll</param-value>
</init-param>
<init-param>
<param-name>libpcre</param-name>
<param-value>c:\work\mod_security\java\libs\pcre.dll</param-value>
</init-param>
<init-param>
<param-name>libapr-1</param-name>
<param-value>c:\work\mod_security\java\libs\libapr-1.dll</param-value>
</init-param>
<init-param>
<param-name>libapriconv-1</param-name>
<param-value>c:\work\mod_security\java\libs\libapriconv-1.dll</param-value>
</init-param>
<init-param>
<param-name>libaprutil-1</param-name>
<param-value>c:\work\mod_security\java\libs\libaprutil-1.dll</param-value>
</init-param>
<init-param>
<param-name>libModSecurityJNI</param-name>
<param-value>c:\work\mod_security\java\libs\ModSecurityJNI.dll</param-value>
</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:
-
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 by uncommenting the init-param elements in the above
filter example.
-
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).
The server has to be started with the VM options:
-Djava.library.path=/path/to/libraries/folder/
or alternatively by specifying init-param elements with absolute paths
in the ModSecurityLoaderConfig.xml file.
BeanShell scripting with ModSecurity
You can use BeanShell scripts in SecRule
ModSecurity directives using the exec action. First you need to put the
bsh.jar file (which can be downloaded from beanshell.org)
into the current directory of your server (for example $CATALINA_HOME/bin in Tomcat).
An example of an exec can be the following:
SecAction "setenv:msg=%{rule.msg},exec:/usr/local/apache/conf/beanshell_script.bsh"
The environment variable set in the SecAction can be accessed in BeanShell scripts
using:
System.getenv("msg");
|