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'"