Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIAi2 WildChat-4.8M (AI2)

SecAction "id:240001,phase:1,nolog,pass,t:none,setvar:tx.alloweddomain=dmzfw.local"…

  1. 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

  2. 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_URI to each SecRule, and use the chain action 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 SecRule checks REQUEST_URI to match exactly /DVWA/security.php.
    • It uses chain to combine with the next rule.
    • The second SecRule in 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:2 from the second rule in the chain to avoid confusion. Only the first rule in the chain requires the phase declaration.
    • nolog is 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.

  3. User

    directive Rules must have an ID

  4. Assistant

    Correct, ModSecurity rules must have a unique id in every SecRule directive (the first rule in a chain also needs an ID, the chained rules don’t have one).

    The previous example missed the id on the URI matching rule.

    Here's the corrected modification with id specified in both the URI check rules (which are the first rule in each chain). The chained second rules do not have an id:

    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:240006 for the URI + Referer rule.
    • id:240007 for 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 nolog on URI rule avoids generating multiple logs unnecessarily.

    This will ensure your rules apply only on /DVWA/security.php and enforce the referer/origin domain check as intended.

  5. User

    Will it work with GET and POST requests?

  6. Assistant

    Yes, the modified rules will work with both GET and POST requests.

    Why?

    • These rules operate on HTTP request headers (Referer and Origin) and on the REQUEST_URI.
    • REQUEST_URI is available for all HTTP methods (GET, POST, PUT, DELETE, etc.).
    • REQUEST_HEADERS:Referer and REQUEST_HEADERS:Origin are 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.php will 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.