本文共 11985 字,大约阅读时间需要 39 分钟。
Over the course of the summer of 2013, the ModSecurity team participated in program through . We helped by mentoring 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 to hook into Java servers (Tomcat, Spring, Stuts, etc...) as a . We need BETA testers to help us ensure that the code is working properly. Please see the steps below and send us feedback.
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:
These native libraries are used by the ModSecurityFilter.
Download mod_security source code from , compile and install:
git clone https://github.com/mihaipitu/ModSecurity.git
cd ModSecurity/./autogen.sh./configure --enable-standalone-module --enable-java-modulemake
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.
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.
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.javaDefaultFileItem.java FileItemFactory.java FileUploadException.java package.htmlDeferredFileOutputStream.java FileItem.java FileUpload.java ThresholdingOutputStream.java./modsecurity:ModSecurityFilter.java MsHttpServletRequest.java MsHttpServletResponse.java MsOutputStream.javaModSecurity.java MSHttpServletRequestWrapper.java MsHttpTransaction.java MsWriter.java[root@localhost org]#
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.xmlextracted: WEB-INF/classes/org/apache/commons/fileupload/DefaultFileItem.classextracted: WEB-INF/classes/org/apache/commons/fileupload/DefaultFileItemFactory.classextracted: WEB-INF/classes/org/apache/commons/fileupload/DeferredFileOutputStream.classextracted: WEB-INF/classes/org/apache/commons/fileupload/DiskFileUpload.classextracted: WEB-INF/classes/org/apache/commons/fileupload/FileItem.classextracted: WEB-INF/classes/org/apache/commons/fileupload/FileItemFactory.classextracted: WEB-INF/classes/org/apache/commons/fileupload/FileUpload.classextracted: WEB-INF/classes/org/apache/commons/fileupload/FileUploadBase$InvalidContentTypeException.classextracted: WEB-INF/classes/org/apache/commons/fileupload/FileUploadBase$SizeLimitExceededException.classextracted: WEB-INF/classes/org/apache/commons/fileupload/FileUploadBase$UnknownSizeException.classextracted: WEB-INF/classes/org/apache/commons/fileupload/FileUploadBase.classextracted: WEB-INF/classes/org/apache/commons/fileupload/FileUploadException.classextracted: WEB-INF/classes/org/apache/commons/fileupload/MultipartStream$IllegalBoundaryException.classextracted: WEB-INF/classes/org/apache/commons/fileupload/MultipartStream$MalformedStreamException.classextracted: WEB-INF/classes/org/apache/commons/fileupload/MultipartStream.classextracted: WEB-INF/classes/org/apache/commons/fileupload/ThresholdingOutputStream.classextracted: WEB-INF/classes/org/apache/commons/fileupload/package.htmlextracted: WEB-INF/classes/org/modsecurity/ModSecurity.classextracted: WEB-INF/classes/org/modsecurity/ModSecurityFilter.classextracted: WEB-INF/classes/org/modsecurity/MsHttpServletRequest$1.classextracted: WEB-INF/classes/org/modsecurity/MsHttpServletRequest$BodyDiskServletInputStream.classextracted: WEB-INF/classes/org/modsecurity/MsHttpServletRequest$BodyMemoryServletInputStream.classextracted: WEB-INF/classes/org/modsecurity/MsHttpServletRequest$InterceptServletInputStream.classextracted: WEB-INF/classes/org/modsecurity/MsHttpServletRequest$Parameter.classextracted: WEB-INF/classes/org/modsecurity/MsHttpServletRequest.classextracted: WEB-INF/classes/org/modsecurity/MsHttpServletResponse.classextracted: WEB-INF/classes/org/modsecurity/MsHttpTransaction.classextracted: WEB-INF/classes/org/modsecurity/MsOutputStream.classextracted: WEB-INF/classes/org/modsecurity/MsWriter.classextracted: WEB-INF/web.xmlextracted: help.htmlextracted: 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
The next step add the following entry for the filter tag in your web.xml file:
ModSecurityFilter org.modsecurity.ModSecurityFilter conf /root/modsecurity-apache_2.7.5/modsecurity.conf libxml2 /usr/lib64/libxml2.so.2 libpcre /lib64/libpcre.so.0 libapr-1 /usr/lib64/libapr-1.so.0 libaprutil-1 /usr/lib64/libaprutil-1.so.0 libModSecurityJNI /usr/lib/libModSecurityJNI.so ModSecurityFilter /*
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
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.mappingInclude /root/owasp-modsecurity-crs/modsecurity_crs_10_setup.confInclude /root/owasp-modsecurity-crs/base_rules/*.conf
# /opt/apache-tomcat-7.0.42/bin/startup.shUsing CATALINA_BASE: /opt/apache-tomcat-7.0.42Using CATALINA_HOME: /opt/apache-tomcat-7.0.42Using CATALINA_TMPDIR: /opt/apache-tomcat-7.0.42/tempUsing JRE_HOME: /usr/lib/jvm/java-1.6.0-openjdkUsing 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*.logSep 27, 2013 6:35:18 PM org.apache.catalina.core.ApplicationContext logINFO: ModSecurity for Java (STABLE)/2.7.5 (http://www.modsecurity.org/) configured.Sep 27, 2013 6:35:18 PM org.apache.catalina.core.ApplicationContext logINFO: ModSecurity: APR compiled version="1.3.9"; loaded version="1.3.9"Sep 27, 2013 6:35:18 PM org.apache.catalina.core.ApplicationContext logINFO: 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 logINFO: ModSecurity: LUA compiled version="Lua 5.1"Sep 27, 2013 6:35:18 PM org.apache.catalina.core.ApplicationContext logINFO: ModSecurity: LIBXML compiled version="2.7.6"Sep 27, 2013 6:35:18 PM org.apache.catalina.core.ApplicationContext logINFO: ModSecurity started.
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:
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:
--CUT--
You can use scripts in SecRule ModSecurity directives using the action. First you need to put the bsh.jar file (which can be downloaded from ) 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");}
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:
转载地址:http://oglmb.baihongyu.com/