SecAction "id:240001,phase:1,nolog,pass,t:none,setvar:tx.alloweddomain=dmzfw.local"…
- User
SecAction "id:240001,phase:1,nolog,pass,t:none,setvar:tx.allowed_domain=dmzfw.local"
SecRule REQUEST_HEADERS:Referer "!@contains %{tx.allowed_domain}"
"id:240004,phase:2,deny,status:403,log,msg:'bad referer'"SecRule REQUEST_HEADERS:Origin "!@contains %{tx.allowed_domain}"
"id:240005,phase:2,deny,status:403,log,msg:'bad origin'"Modify these 2 last rules to work only with /DVWA/security.php page
- Assistant
To make the last two rules work only when the requested URI is
/DVWA/security.php, you need to add a condition that checks the request URI.You can do this by adding a condition on
REQUEST_URIto eachSecRule, and use thechainaction so both conditions must be met.Here is the modified version:
SecRule REQUEST_URI "^/DVWA/security\.php$" "phase:2,nolog,pass,chain" SecRule REQUEST_HEADERS:Referer "!@contains %{tx.allowed_domain}" \ "id:240004,deny,status:403,log,msg:'bad referer'" SecRule REQUEST_URI "^/DVWA/security\.php$" "phase:2,nolog,pass,chain" SecRule REQUEST_HEADERS:Origin "!@contains %{tx.allowed_domain}" \ "id:240005,deny,status:403,log,msg:'bad origin'"Explanation:
- The first
SecRulechecksREQUEST_URIto match exactly/DVWA/security.php. - It uses
chainto combine with the next rule. - The second
SecRulein the chain performs the original referer/origin check. - Both conditions must be true for the rule to fire (and deny the request).
- Removed redundant
phase:2from the second rule in the chain to avoid confusion. Only the first rule in the chain requires the phase declaration. nologis added to the URI check rule to avoid logging intermediate matches; logs will only be generated if the deny rule triggers.
If you want the rules to work at any time the URI starts with
/DVWA/security.php(e.g.,/DVWA/security.php?foo=bar), the regex would be:SecRule REQUEST_URI "^/DVWA/security\.php" "phase:2,nolog,pass,chain" ...But generally, the exact match above is sufficient and usually preferred for precise control.
- The first
- User
directive Rules must have an ID
- Assistant
Correct, ModSecurity rules must have a unique
idin everySecRuledirective (the first rule in a chain also needs an ID, the chained rules don’t have one).The previous example missed the
idon the URI matching rule.Here's the corrected modification with
idspecified in both the URI check rules (which are the first rule in each chain). The chained second rules do not have anid:SecRule REQUEST_URI "^/DVWA/security\.php$" "id:240006,phase:2,nolog,pass,chain" SecRule REQUEST_HEADERS:Referer "!@contains %{tx.allowed_domain}" \ "deny,status:403,log,msg:'bad referer'" SecRule REQUEST_URI "^/DVWA/security\.php$" "id:240007,phase:2,nolog,pass,chain" SecRule REQUEST_HEADERS:Origin "!@contains %{tx.allowed_domain}" \ "deny,status:403,log,msg:'bad origin'"Summary:
id:240006for the URI + Referer rule.id:240007for the URI + Origin rule.- Only the first rule of the chain requires the ID.
- The chained rule continues with the check and deny actions.
- The
nologon URI rule avoids generating multiple logs unnecessarily.
This will ensure your rules apply only on
/DVWA/security.phpand enforce the referer/origin domain check as intended. - User
Will it work with GET and POST requests?
- Assistant
Yes, the modified rules will work with both GET and POST requests.
Why?
- These rules operate on HTTP request headers (
RefererandOrigin) and on theREQUEST_URI. REQUEST_URIis available for all HTTP methods (GET, POST, PUT, DELETE, etc.).REQUEST_HEADERS:RefererandREQUEST_HEADERS:Originare present on requests regardless of method when the client sends them (most browsers send these headers on POST and GET requests depending on context).- Since you placed these in
phase:2(request headers processing), ModSecurity evaluates them after request headers are read but before the request body is processed (which means before payload inspection for POST, etc.).
Summary:
- GET or POST method → the request URI matches
/DVWA/security.php→ then check Referer and Origin headers → if they don't contain the allowed domain, deny with 403.
Thus, both GET and POST requests to
/DVWA/security.phpwill be controlled by these rules.
A side note:
- If for some reason you wanted to restrict only POST (or only GET), you could add a condition on
REQUEST_METHOD. For example:
SecRule REQUEST_METHOD "POST" "chain" SecRule REQUEST_URI "^/DVWA/security\.php$" "chain" SecRule REQUEST_HEADERS:Referer "!@contains %{tx.allowed_domain}" "deny,status:403,log,msg:'bad referer'"But that’s unnecessary if you want both methods monitored.
- These rules operate on HTTP request headers (