This blog has moved! Please update your
bookmarks to http://www.blog.modsecurity.org.

« November 2006 | Main | January 2007 »

Using ModSecurity 2 Collections in Rules

A recent posting on the ModSecurity mailing list by K.C. Li is a very good excuse to discuss some major changes between ModSecurity version 1 and 2 and how to it influence rule writing. K. C. used the following rule in ModSecurity v1:

SecFilterSelective ARGS "(^|[^_])(comments?|story)=.*(href|http)"

This rule searched for the values "href" or "http" in a bunch of different parameters: story, _story, comment, comments, _comment and _comments. The rule replaces 6 rules, each one specific to a parameter. While the rule is very effective, as K.C. writes, it suffers from the following shortcomings:

The rule can be corrected like this:

SecFilterSelective ARGS "(?:^|\&)_?(?:comments?|story)=[^\&]*\b(?:href|http)\b"

By adding checks for a "&" prior to the parameter name and ensuring "&" does not exists between the parameter name and the keyword, we make sure that we capture the parameters in any location in the request string and that the tokens are part of the value for this parameter only. The meta character "\b" is a regular expression meta character that matches a word boundary, ensuring that "href" and "http" are tokens. The construct "?:" at the beginning of each parentheses is a performance optimization which prevents the parentheses from capturing the value, a side effect that is not needed unless we use the capture action.

Well, but all this become very complex.

While in ModSecurity 1.x the ARGS location is simply a string that represented either QUERY_STRING or POST_PAYLOAD, in ModSecurity 2 ARGS is a collection that enables searching in individual parameters. Collections are fundamental in ModSecurity 2 and I suggest reading the relevant section in ModSecurity 2 reference guide.

You can still use the location QUERY_STRING|REQUEST_BODY to rewrite the rule for ModSecurity 2.0 but using the ARGS collection will make the rule much simpler. Using a regular expression to select the elements of the collection tested, the following rule will do the same in ModSecurity 2:

SecRule ARGS:'/(?:^|^_)(?:comments?|story)$/' "\b(?:href|http)\b"

The other rules that K.C. uses can also use collections and be converted to a single ModSecurity V2 rule. Instead of:

SecFilterSelective HTTP_x-aaaaaaaaa|HTTP_XAAAAAAAAA ".+"
SecFilterSelective HTTP_x-aaaaaaaaaaa|HTTP_XAAAAAAAAAAA ".+"
SecFilterSelective HTTP_x-aaaaaaaaaaaa|HTTP_X_AAAAAAAAAAAA ".+"

You can simply write:

SecRule "&REQUEST_HEADERS:'/^(?i)x[-_]a{9,12}$/'" "@gt 0"

This rule uses the "&" construct to count the number of elements in a collection, or a subset if a regular expression is used to select elements from the collection.

To complement the discussion, a word about actions in ModSecurity 2. Just as in ModSecurity 1, there is no need to explicitly state actions in each rule and the actions listed in SecDefaultAction will be used. However, due to the bigger role that actions now have in ModSecurity, it is advisable to add them to each rule. Especially important are:

So K.C. rules becomes:

SecRule "ARGS:'/(?:^|^_)(?:comments?|story)$/'" "\b(?:href|http)\b" \
    "deny,log,status:403,phase:2,t:lowercase,t:urlDecodeUni,id:90004,severity:2,msg:'Comment Spam'"
 
SecRule "&REQUEST_HEADERS:'/^(?i)x[-_]a{9,12}$/'" "@gt 0" \
    "deny,log,status:403,phase:2,t:lowercase,id:90005,severity:2,msg:'Comment Spam'"

Posted by ofer at 10:46 AM

ModSecurity v2.0 Webcast

In response to many of the common questions and issues posted to the mail-list, we at Breach Security decided to host a webcast to help provide answers and shed some light on the new v2.0 features – http://www.modsecurity.org/training/. This is the first of many training programs to support and enhance your use of ModSecurity and its dynamic web application security protection.

If you are interested in free training on:

The webcast is scheduled for Thursday, December 14th 2006 at 1:00pm EST. To register please click here.

Posted by rcbarnett at 01:40 PM

Ryan Barnett Is Now Bloggin'

Greetings everyone. My name is Ryan Barnett and I am the Director of Application Security Training for Breach Security. I am also a long time user of ModSecurity. I will be working closely with both Ivan and Ofer on ModSecurity development and rule updates, however my main area of focus is on effectively using ModSecurity. ModSecurity has so many cool new features, however many people aren't quite sure how to use them in their environment. That is where I come in. I will help to answer questions, update documentation and create specific use cases that highlight how to leverage ModSecurity to help protect your web apps.

So, check back here frequently for news, updates and tips and tricks.

Cheers.

Posted by rcbarnett at 12:59 PM