ModSecurity icon indicating copy to clipboard operation
ModSecurity copied to clipboard

setvar scope when set inside Location block?

Open elops opened this issue 12 years ago • 2 comments

I can't find in documentation if this is a bug or not... but in essence when I setvar in Location/LocationMatch block it is gone when I try to check it outside Location/LocationMatch block

so following will not deny connection when SecRule in Location block was matched.

<Location ~ "^.*/xmlrpc.php">
  SecRule REQUEST_HEADERS:Content-Length "^0$" "id:100,pass,setvar:TX.foo=1"
</Location>

SecRule TX:FOO "@eq 1" "id:101,deny,status:406"

But if second rule is moved inside Location block; it will work. e.g.

<Location ~ "^.*/xmlrpc.php">
  SecRule REQUEST_HEADERS:Content-Length "^0$" "id:100,pass,setvar:TX.foo=1"
  SecRule TX:FOO "@eq 1" "id:101,deny,status:406"
</Location>

it will be blocked.

elops avatar Oct 31 '14 12:10 elops

This is not really a bug however this scenario needs to be documented better. The order in which rules are listed within conf files does influence rule processing order however there are two caveats:

  1. Rule Phases - Say you have these two rules -

SecRule REQUEST_HEADERS:Content-Length "^0$" "phase:2,id:100,pass,setvar:TX.foo=1" SecRule TX:FOO "@eq 1" "phase:1,id:101,deny,status:406"

In this case, the 2nd SecRule would actually run first as it is listed in "phase:1" processing bucket whereas the 1st SecRule has "phase:2" specified.

  1. Use of Apache Scope/Location Directive Containers If you choose to utilize Apache scope/location directives (such as ) - then these rules will be pushed to the end of the current processing phase specified within the SecRule. So, in your scenario, your 2nd SecRule (outside the scope) will be evaluated first and then the one inside would be evaluated.

While the use of Apache scope/location directives are useful from a rules readability perspective, they also can become too complex from a rule processing order (as you are seeing). This is why I normally opt to use SecRule-only rules. You could simply rewrite your rules like this -

SecRule REQUEST_FILENAME "@endsWith xmlrpc.php" "phase:1,id:100,pass,nolog" SecRule REQUEST_HEADERS:Content-Length "^0$" "setvar:TX.foo=1"

SecRule TX:FOO "@eq 1" "phase:1,id:101,deny,status:406"

rcbarnett-zz avatar Oct 31 '14 12:10 rcbarnett-zz

awesome, tyvm for detailed and insightful answer Ryan

elops avatar Oct 31 '14 13:10 elops