ModSecurity for Java - BETA Testers Needed

简介: Over the course of the summer of 2013, the ModSecurity team participated in Google's Summer of Code (GSoC) program through OWASP.
Over the course of the summer of 2013, the ModSecurity team participated in  Google's Summer of Code (GSoC)  program through OWASP.  We helped by mentoring  Mihai Pitu  who developed a port of ModSecurity for Java!  

  
The main problem this project solves is that you will no longer have to front-end your Java app servers with a reverse proxy in order to gain ModSecurity protections!  ModSecurity
standalone code uses JNI to hook into Java servers (Tomcat, Spring, Stuts, etc...) as a Servlet Filter
.  We need BETA testers to help us ensure that the code is working properly.  Please see the steps below and send us feedback.

Installation Steps

Step 1: 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
                            

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.

Download mod_security source code from GitHub, compile and install:

git clone https://github.com/mihaipitu/ModSecurity.git
cd ModSecurity/
./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/
                            

The libModSecurityJNI.so file is the connector that plugs the "standalone" ModSecurity code into the Java application as a Filter.

Step 2: Add ModSecurity Java/Class Files to the App

To use the ModSecurity filter in your Java web application, you will need to copy either the source .java files or the compiled .class files into your application.

Scenario 1: Add ModSecurity source java files to application and create WAR

If you would like to add ModSecurity into your application and create a WAR file, then you should copy the source files from the ModSecurity/java/ModSecurityTestApp/src/java/org directory into your app directory structure and then create the WAF file:

# pwd
/tmp/ModSecurity/java/ModSecurityTestApp/src/java/org
# ls -R
.:
apache  modsecurity

./apache:
commons

./apache/commons:
fileupload

./apache/commons/fileupload:
DefaultFileItemFactory.java    DiskFileUpload.java   FileUploadBase.java       MultipartStream.java
DefaultFileItem.java           FileItemFactory.java  FileUploadException.java  package.html
DeferredFileOutputStream.java  FileItem.java         FileUpload.java           ThresholdingOutputStream.java

./modsecurity:
ModSecurityFilter.java  MsHttpServletRequest.java         MsHttpServletResponse.java  MsOutputStream.java
ModSecurity.java        MSHttpServletRequestWrapper.java  MsHttpTransaction.java      MsWriter.java
[root@localhost org]# 

Scenario 2: Add ModSecurity compiled class files to an already compiled app

If you already have a compiled app and you would like to add in ModSecurity filtering without recompiling a new WAF file, you can instead use the compiled .class files.  Simple unpack the ModSecurityTestApp.war file and copy the files into the running app's directory structure.

# jar xvf ModSecurityTestApp.war
  created: META-INF/
extracted: META-INF/MANIFEST.MF
  created: WEB-INF/
  created: WEB-INF/classes/
  created: WEB-INF/classes/org/
  created: WEB-INF/classes/org/apache/
  created: WEB-INF/classes/org/apache/commons/
  created: WEB-INF/classes/org/apache/commons/fileupload/
  created: WEB-INF/classes/org/modsecurity/
extracted: META-INF/context.xml
extracted: WEB-INF/classes/org/apache/commons/fileupload/DefaultFileItem.class
extracted: WEB-INF/classes/org/apache/commons/fileupload/DefaultFileItemFactory.class
extracted: WEB-INF/classes/org/apache/commons/fileupload/DeferredFileOutputStream.class
extracted: WEB-INF/classes/org/apache/commons/fileupload/DiskFileUpload.class
extracted: WEB-INF/classes/org/apache/commons/fileupload/FileItem.class
extracted: WEB-INF/classes/org/apache/commons/fileupload/FileItemFactory.class
extracted: WEB-INF/classes/org/apache/commons/fileupload/FileUpload.class
extracted: WEB-INF/classes/org/apache/commons/fileupload/FileUploadBase$InvalidContentTypeException.class
extracted: WEB-INF/classes/org/apache/commons/fileupload/FileUploadBase$SizeLimitExceededException.class
extracted: WEB-INF/classes/org/apache/commons/fileupload/FileUploadBase$UnknownSizeException.class
extracted: WEB-INF/classes/org/apache/commons/fileupload/FileUploadBase.class
extracted: WEB-INF/classes/org/apache/commons/fileupload/FileUploadException.class
extracted: WEB-INF/classes/org/apache/commons/fileupload/MultipartStream$IllegalBoundaryException.class
extracted: WEB-INF/classes/org/apache/commons/fileupload/MultipartStream$MalformedStreamException.class
extracted: WEB-INF/classes/org/apache/commons/fileupload/MultipartStream.class
extracted: WEB-INF/classes/org/apache/commons/fileupload/ThresholdingOutputStream.class
extracted: WEB-INF/classes/org/apache/commons/fileupload/package.html
extracted: WEB-INF/classes/org/modsecurity/ModSecurity.class
extracted: WEB-INF/classes/org/modsecurity/ModSecurityFilter.class
extracted: WEB-INF/classes/org/modsecurity/MsHttpServletRequest$1.class
extracted: WEB-INF/classes/org/modsecurity/MsHttpServletRequest$BodyDiskServletInputStream.class
extracted: WEB-INF/classes/org/modsecurity/MsHttpServletRequest$BodyMemoryServletInputStream.class
extracted: WEB-INF/classes/org/modsecurity/MsHttpServletRequest$InterceptServletInputStream.class
extracted: WEB-INF/classes/org/modsecurity/MsHttpServletRequest$Parameter.class
extracted: WEB-INF/classes/org/modsecurity/MsHttpServletRequest.class
extracted: WEB-INF/classes/org/modsecurity/MsHttpServletResponse.class
extracted: WEB-INF/classes/org/modsecurity/MsHttpTransaction.class
extracted: WEB-INF/classes/org/modsecurity/MsOutputStream.class
extracted: WEB-INF/classes/org/modsecurity/MsWriter.class
extracted: WEB-INF/web.xml
extracted: help.html
extracted: index.jsp
# cp -R /tmp/ModSecurity/java/ModSecurityTestApp/dist/WEB-INF/classes/org/* /opt/apache-tomcat-7.0.42/webapps/WebGoat/WEB-INF/classes/org 

Step 3: Activate the ModSecurityFilter in web.xml 

The next step 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>/root/modsecurity-apache_2.7.5/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>
            <!--
            OPTIONAL parameters for loading native libraries from absolute paths. This is an alternitive to specifying 
            -Djava.library.path=/path/to/libs/ variable to the JVM which 

            zlib1 and libapriconv-1 are Windows only libraries
            -->
            <init-param>
                <param-name>libxml2</param-name>
                <param-value>/usr/lib64/libxml2.so.2</param-value> 
            </init-param>
            <init-param>
                <param-name>libpcre</param-name>
                <param-value>/lib64/libpcre.so.0</param-value> 
            </init-param>
            <init-param>
                <param-name>libapr-1</param-name>
                <param-value>/usr/lib64/libapr-1.so.0</param-value> 
            </init-param>
            <init-param>
                <param-name>libaprutil-1</param-name>
                <param-value>/usr/lib64/libaprutil-1.so.0</param-value> 
            </init-param>
            <init-param>
                <param-name>libModSecurityJNI</param-name>
                <param-value>/usr/lib/libModSecurityJNI.so</param-value> 
            </init-param>
    </filter>
    <filter-mapping>
	    <filter-name>ModSecurityFilter</filter-name>
	    <url-pattern>/*</url-pattern>
    </filter-mapping>

Step 4: Configure ModSecurity Settings

In the web.xml file, you added a path to the main modsecurity.conf file holding directives such as SecRuleEngine, SecAuditEngine, etc...  You should update this file as needed for your environment.

# head /root/modsecurity-apache_2.7.2/modsecurity.conf
# -- Rule engine initialization ----------------------------------------------

# Enable ModSecurity, attaching it to every transaction. Use detection
# only to start with, because that minimises the chances of post-installation
# disruption.
#
SecRuleEngine DetectionOnly 

Step 5: Add in Rule Files

The ModSecurity filter knows how to handle "Apache Include" directives in the "conf" param value.  This means that if you want to create your own rule files or utilize the OWASP ModSecurity CRS, you should add appropriate Include directives to the main modsecurity.conf file:

# tail /root/modsecurity-apache_2.7.2/modsecurity.conf
# Specify your Unicode Code Point.
# This mapping is used by the t:urlDecodeUni transformation function
# to properly map encoded data to your language. Properly setting
# these directives helps to reduce false positives and negatives.
#
#SecUnicodeCodePage 20127
#SecUnicodeMapFile unicode.mapping

Include /root/owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
Include /root/owasp-modsecurity-crs/base_rules/*.conf 

Step 6: Start Java Server and Confirm ModSecurity Initialization

# /opt/apache-tomcat-7.0.42/bin/startup.sh
Using CATALINA_BASE:   /opt/apache-tomcat-7.0.42
Using CATALINA_HOME:   /opt/apache-tomcat-7.0.42
Using CATALINA_TMPDIR: /opt/apache-tomcat-7.0.42/temp
Using JRE_HOME:        /usr/lib/jvm/java-1.6.0-openjdk
Using CLASSPATH:       /opt/apache-tomcat-7.0.42/bin/bootstrap.jar:/opt/apache-tomcat-7.0.42/bin/tomcat-juli.jar
# cat /opt/apache-tomcat-7.0.42/logs/localhost*.log
Sep 27, 2013 6:35:18 PM org.apache.catalina.core.ApplicationContext log
INFO: ModSecurity for Java (STABLE)/2.7.5 (http://www.modsecurity.org/) configured.
Sep 27, 2013 6:35:18 PM org.apache.catalina.core.ApplicationContext log
INFO: ModSecurity: APR compiled version="1.3.9"; loaded version="1.3.9"
Sep 27, 2013 6:35:18 PM org.apache.catalina.core.ApplicationContext log
INFO: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
Sep 27, 2013 6:35:18 PM org.apache.catalina.core.ApplicationContext log
INFO: ModSecurity: LUA compiled version="Lua 5.1"
Sep 27, 2013 6:35:18 PM org.apache.catalina.core.ApplicationContext log
INFO: ModSecurity: LIBXML compiled version="2.7.6"
Sep 27, 2013 6:35:18 PM org.apache.catalina.core.ApplicationContext log
INFO: ModSecurity started.

Step 7: Test Rules

The next step is to send some example attacks to your application to ensure that the it is working properly.  If you send some XSS attacks for instance, you should see logs similar to the following in the Tomcat logs directory:

Screen Shot 2013-09-27 at 1.52.09 PM

Step 8: Verify Audit Logging

In addition to the short, 1-line alert messages sent to the Tomcat logs, ModSecurity will also generate appropriate Audit log entries depending on your configuration.  You can review the corresponding Audit log entry for your test request(s) to see fully request/response payloads:

Screen Shot 2013-09-27 at 1.55.52 PM

--CUT--

Screen Shot 2013-09-27 at 1.58.51 PM

Bonus Testing: 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:


# Alert and Block based on Anomaly Scores
#
SecRule TX:ANOMALY_SCORE "@gt 0" \
    "chain,phase:2,id:'981176',t:none,deny,log,msg:'Inbound Anomaly Score Exceeded (Total Score: %{TX.ANOMALY_SCORE}, SQLi=%{TX.SQL_INJECTION_SCORE}, XSS=%{TX.XSS_SCORE}): Last Matched Message: %{tx.msg}',logdata:'Last Matched Data: %{matched_var}',setvar:tx.inbound_tx_msg=%{tx.msg},setvar:tx.inbound_anomaly_score=%{tx.anomaly_score}"
        SecRule TX:ANOMALY_SCORE "@ge %{tx.inbound_anomaly_score_level}" chain
                SecRule TX:ANOMALY_SCORE_BLOCKING "@streq on" chain
                        SecRule TX:/^\d+\-/ "(.*)" "setenv:block_session=1,exec:/usr/local/apache/conf/beanshell_script.bsh"

The environment variable set in the SecRule can be accessed in BeanShell scripts using some pseudo-code like this to instruct the app to block the current session:


import org.owasp.webgoat.session.WebSession;
System.getenv("block_session");
if (block_user != null) {
  session.setAttribute(BLOCKED, "true");
}

Conclusion

We encourage Java application server users to test out the new ModSecurity version and to give us feedback on any issues found.  If you find any bugs, please contact through one of the following:

目录
相关文章
|
6月前
|
Java
java实现Beta函数
java实现Beta函数
|
7天前
|
Java
Java并发编程:深入理解线程池
【4月更文挑战第30天】本文将深入探讨Java并发编程中的一个重要主题——线程池。我们将从线程池的基本概念入手,了解其工作原理和优势,然后详细介绍如何使用Java的Executor框架创建和管理线程池。最后,我们将讨论一些高级主题,如自定义线程工厂和拒绝策略。通过本文的学习,你将能够更好地理解和使用Java的线程池,提高你的并发编程能力。
|
1天前
|
安全 Java
Java中的并发编程:理解并发性与线程安全
Java作为一种广泛应用的编程语言,在并发编程方面具有显著的优势和特点。本文将探讨Java中的并发编程概念,重点关注并发性与线程安全,并提供一些实用的技巧和建议,帮助开发人员更好地理解和应用Java中的并发机制。
|
1天前
|
Java
Java中的多线程编程:基础知识与实战技巧
【5月更文挑战第6天】多线程编程是Java中的一个重要特性,它允许我们在一个程序中同时执行多个任务。本文将介绍Java多线程的基础知识,包括线程的创建、启动、同步和通信,以及如何在Java中实现多线程编程。通过实例代码和解析,帮助读者深入理解Java多线程编程的概念和应用。
|
2天前
|
Java
Java中的多线程编程:基础知识与实践
【5月更文挑战第5天】在现代软件开发中,多线程编程是一个重要的概念,尤其是在Java这样的多平台、高性能的编程语言中。通过多线程,我们可以实现并行处理,提高程序的运行效率。本文将介绍Java中多线程编程的基础知识,包括线程的概念、创建和控制方法,以及一些常见的多线程问题和解决方案。
|
5天前
|
存储 缓存 前端开发
Java串口通信技术探究3:RXTX库线程 优化系统性能的SerialPortEventListener类
Java串口通信技术探究3:RXTX库线程 优化系统性能的SerialPortEventListener类
20 3
|
5天前
|
Java
JAVA难点包括异常处理、多线程、泛型和反射,以及复杂的分布式系统知识
JAVA难点包括异常处理、多线程、泛型和反射,以及复杂的分布式系统知识。入坑JAVA因它的面向对象特性、平台无关性、强大的标准库和活跃的社区支持。
21 2
|
6天前
|
Java 调度 开发者
Java中的多线程编程:基础与实践
【5月更文挑战第2天】本文将深入探讨Java中的多线程编程,从基础概念到实际应用,为读者提供全面的理解和实践指导。我们将首先介绍线程的基本概念和重要性,然后详细解析Java中实现多线程的两种主要方式:继承Thread类和实现Runnable接口。接着,我们将探讨线程同步的问题,包括synchronized关键字和Lock接口的使用。最后,我们将通过一个实际的生产者-消费者模型来演示多线程编程的实践应用。
|
6天前
|
安全 Java 程序员
Java中的多线程编程:从理论到实践
【5月更文挑战第2天】 在计算机科学中,多线程编程是一项重要的技术,它允许多个任务在同一时间段内并发执行。在Java中,多线程编程是通过创建并管理线程来实现的。本文将深入探讨Java中的多线程编程,包括线程的概念、如何创建和管理线程、以及多线程编程的一些常见问题和解决方案。
17 1