0

Relatively new to Apache modsec. Trying to do something simple and hitting a wall. I want to set an environment variable based on the User-Agent, and test it by setting a header. I have this:

SecRule HTTP_User_Agent "Gecko" "phase:2,nolog,setenv:MOZTEST=1,id:100000025"
Header always set MozTest "1" env=MOZTEST

Figured that in Firefox, the MozTest header should be set, but it's not. I also tried REQUEST_HEADERS:User-Agent in place of HTTP_User-Agent, but still no effect. Also tried increasing the phase to 5, even though it should have the user agent at phase 2. There is no error; the rule just doesn't seem to fire.

My guess is that the user-agent variable isn't set, but I don't know why. (Or even how to test that, as I've never had to do any custom logging.)

3
  • How/where do you go on to test/use the environment variable ? Regarding the user-agent being logged, there is extensive documentation for apache2 that you should look at in conjunction with your apache httpd configuration file. Try it, be amazed and educated.
    – user9517
    Feb 19, 2014 at 7:25
  • For now, as you see in the second line there, I'm just trying to set a header using the environment variable to test that the SecRule is firing (which it's not). The end goal is to use this secrule to skip another rule based on user agent, so it's not ultimately about setting environment variables at all; they're just a convenient way to test. Feb 19, 2014 at 7:48
  • I am aware that there is extensive Apache documentation... (I tend to spend significant time reading docs before I resort to ServerFault.) I'm saying I am not yet specifically familiar with how to output custom log entries from mod_security rules, so that I could check what it's seeing in those variables. If there's a specific piece of documentation you feel would help with that, please share it. Feb 19, 2014 at 7:49

1 Answer 1

0

Came across my own question while researching another issue years later.

It appears the main issue here is that I was trying to use a mod_rewrite variable (HTTP_USER_AGENT) in a mod_security context. Instead I could have either used the correct ModSecurity variable (REQUEST_HEADERS:User-Agent) or passed the mod rewrite variable as an environment variable (not that there's a good reason to do that when a ModSecurity alternative is available.)

So my example above should look like this:

SecRule REQUEST_HEADERS:User-Agent "Gecko" "phase:2,nolog,setenv:MOZTEST=1,id:100000025"
Header always set MozTest "1" env=MOZTEST

A full list of variables available to ModSecurity is here: https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual-(v2.x)#Variables

Alternatively—not useful in this case, but perhaps valuable to note for other circumstances—I could have used mod rewrite to dump the user agent into an environment variable, then matched on that with mod security, like this:

RewriteRule .* - [E=user_agent:%{HTTP_USER_AGENT}]
SecRule ENV:user_agent "Gecko" "phase:2,nolog,setenv:MOZTEST=1,id:100000025"
Header always set MozTest "1" env=MOZTEST

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .