Linux – Mod_security exclusion not fully working, still blocking CSS and images

apache-2.4httpdlinuxmod-security

I have put either of the following in my rules:

SecRule REQUEST_URI "@beginsWith /directory" "phase:1,id:12345,allow"
SecRule REQUEST_URI "@beginsWith /directory" "phase:1,id:12345,ctl:ruleEngine=off"

When I browse /directory/javascript.php?sqlinjection it will allow the HTML within /directory to be displayed, but the CSS and images which reside in the root directory /css/ and /images/, respectively are not displayed. It seems like they are being blocked because there's SQL injection on the page but no exclusions for those directories.

How can I configure it to fully shut off mod_security within /directory and display all images and CSS?

Best Answer

In ModSecurity each request is treated as an independent request and it has no idea that you are loading the CSS and images for a previous request.

The only way it might know this is what's in the Referer HTTP Header. In fact I suspect this is what is causing you the problem, since the URL of the referring page contains what looks like a SQL Injection so each resource it loads, also gets blocked.

So you've a number of options:

  1. You could write similar rules for /css and /images. Please note that your two rules both do the same thing and both are not necessary - choose one or the other. Also each rule needs to have an independent id but you have repeated 12345 in both rules.

  2. You could shut off ModSecurity for ALL requests. It seems to be causing you more problems and it's solving. Obviously this means you lose the security protections it gives but if you are just going to end up allowing nearly every request to shut it off, then that is one option.

  3. You could write a new rule to bypass ModSecurity based on that referer:

    #Allow any requests for resources loaded by pages in /directory SecRule REQUEST_HEADERS:Referer "@beginsWith https://www.example.com/directory" "phase:1,id:12347,allow"

  4. You could override the rule affected for this field (I'm assuming that rule 942100 is the one that's firing in this example):

    #Exclude Referer HTTP Header from being checked by rule id 949110 SecRuleUpdateTargetById 942100 !REQUEST_HEADERS:Referer

Option 4) would be the recommended way so you are only disabling the rule which is giving a false positive, for that particular field. Note that multiple rules may be firing so you may need multiple exceptions. You also stated on a previous question that rule 949110 was firing, but this is the final checking rule, so you need to know the rules that fired before this, and caused 949110 to step into action, and filter them out as per above.

This is also how you should really prevent your original PHP script from being blocked btw, rather than blanket turning off ModSecurity for the whole /directory location.

To be honest these are all fairly standard things you need to do to configure ModSecurity so suggest you read up on it more. The ModSecurity Handbook is the best way of learning this IMHO.

Related Topic