-
Notifications
You must be signed in to change notification settings - Fork 311
Add modsecurity log vars #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
meirdev
wants to merge
7
commits into
owasp-modsecurity:master
Choose a base branch
from
meirdev:add-modsecurity-log-vars
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+330
−1
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8146728
Expose $modsecurity_intervention and $modsecurity_triggered_rules
meirdev 3311753
Add tests for $modsecurity_intervention and $modsecurity_triggered_rules
meirdev 2bcbfba
Update readme
meirdev 70fc3c6
Using the new interface
meirdev 7e44a0b
Fix sonarcloud issue
meirdev 875e610
Merge branch 'master' into add-modsecurity-log-vars
meirdev 69da45e
fix nginx cachable variables and possible overflow
meirdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| #!/usr/bin/perl | ||
|
|
||
| # Tests for $modsecurity_intervention and $modsecurity_triggered_rules. | ||
|
|
||
| ############################################################################### | ||
|
|
||
| use warnings; | ||
| use strict; | ||
|
|
||
| use Test::More; | ||
|
|
||
| BEGIN { use FindBin; chdir($FindBin::Bin); } | ||
|
|
||
| use lib 'lib'; | ||
| use Test::Nginx; | ||
|
|
||
| ############################################################################### | ||
|
|
||
| select STDERR; $| = 1; | ||
| select STDOUT; $| = 1; | ||
|
|
||
| my $t = Test::Nginx->new()->has(qw/http/); | ||
|
|
||
| $t->write_file_expand('nginx.conf', <<'EOF'); | ||
|
|
||
| %%TEST_GLOBALS%% | ||
|
|
||
| daemon off; | ||
|
|
||
| events { | ||
| } | ||
|
|
||
| http { | ||
| %%TEST_GLOBALS_HTTP%% | ||
|
|
||
| log_format modsec '$request_uri|i=$modsecurity_intervention|r=$modsecurity_triggered_rules'; | ||
| access_log %%TESTDIR%%/access.log modsec; | ||
|
|
||
| server { | ||
| listen 127.0.0.1:8080; | ||
| server_name localhost; | ||
|
|
||
| location /pass { | ||
| modsecurity on; | ||
| modsecurity_rules ' | ||
| SecRuleEngine On | ||
| SecRule ARGS "@streq never" "id:100,phase:2,log,pass" | ||
| '; | ||
| } | ||
|
|
||
| location /match-logonly { | ||
| modsecurity on; | ||
| modsecurity_rules ' | ||
| SecRuleEngine On | ||
| SecRule ARGS "@streq hit" "id:200,phase:2,log,pass" | ||
| '; | ||
| } | ||
|
|
||
| location /multi { | ||
| modsecurity on; | ||
| modsecurity_rules ' | ||
| SecRuleEngine On | ||
| SecRule ARGS "@streq hit" "id:301,phase:2,log,pass" | ||
| SecRule ARGS "@streq hit" "id:302,phase:2,log,pass" | ||
| SecRule ARGS "@streq hit" "id:303,phase:2,log,pass" | ||
| '; | ||
| } | ||
|
|
||
| location /mixed-logging { | ||
| modsecurity on; | ||
| modsecurity_rules ' | ||
| SecRuleEngine On | ||
| SecRule ARGS "@streq hit" "id:701,phase:2,nolog,pass" | ||
| SecRule ARGS "@streq hit" "id:702,phase:2,noauditlog,pass" | ||
| SecRule ARGS "@streq hit" "id:703,phase:2,log,pass" | ||
| '; | ||
| } | ||
|
|
||
| location /allow { | ||
| modsecurity on; | ||
| modsecurity_rules ' | ||
| SecRuleEngine On | ||
| SecRule ARGS "@streq skip" "id:800,phase:1,log,allow" | ||
| SecRule ARGS "@streq skip" "id:801,phase:1,log,deny,status:403" | ||
| '; | ||
| } | ||
|
|
||
| location /block { | ||
| modsecurity on; | ||
| modsecurity_rules ' | ||
| SecRuleEngine On | ||
| SecRule ARGS "@streq go" "id:400,phase:1,log,deny,status:403" | ||
| '; | ||
| } | ||
|
|
||
| location /redirect { | ||
| modsecurity on; | ||
| modsecurity_rules ' | ||
| SecRuleEngine On | ||
| SecRule ARGS "@streq go" "id:500,phase:1,log,status:302,redirect:http://example.com/" | ||
| '; | ||
| } | ||
|
|
||
| location /block-phase3 { | ||
| modsecurity on; | ||
| modsecurity_rules ' | ||
| SecRuleEngine On | ||
| SecRule ARGS "@streq go" "id:600,phase:3,log,deny,status:403" | ||
| '; | ||
| } | ||
| } | ||
| } | ||
| EOF | ||
|
|
||
| $t->write_file("/block-phase3", "body"); | ||
| $t->run(); | ||
| $t->plan(16); | ||
|
|
||
| ############################################################################### | ||
|
|
||
| # No rule matches: intervention=0, no rule list (nginx prints '-' for missing var). | ||
| http_get('/pass?arg=x'); | ||
| like(log_line($t, '/pass'), qr/\|i=0\|/, 'pass: intervention=0'); | ||
| like(log_line($t, '/pass'), qr/\|r=-$/, 'pass: no triggered rules'); | ||
|
|
||
| # Rule matches but non-disruptive: intervention=0, rule id listed. | ||
| http_get('/match-logonly?arg=hit'); | ||
| like(log_line($t, '/match-logonly'), qr/\|i=0\|/, 'log-only: intervention=0'); | ||
| like(log_line($t, '/match-logonly'), qr/\|r=200$/, 'log-only: rule id captured'); | ||
|
|
||
| # Multiple rules all matching: intervention=0, every id listed. | ||
| http_get('/multi?arg=hit'); | ||
| like(log_line($t, '/multi'), qr/\|i=0\|/, 'multi: intervention=0'); | ||
| like(log_line($t, '/multi'), qr/\|r=301,302,303$/, 'multi: all rule ids listed in order'); | ||
|
|
||
| # Three rules with different logging actions (nolog / noauditlog / log). | ||
| http_get('/mixed-logging?arg=hit'); | ||
| like(log_line($t, '/mixed-logging'), qr/\|i=0\|/, 'mixed-logging: intervention=0'); | ||
| like(log_line($t, '/mixed-logging'), qr/\|r=703$/, 'mixed-logging: only the log-action rule is captured (nolog/noauditlog both clear m_saveMessage)'); | ||
|
|
||
| # allow action: short-circuits rule evaluation but is NOT treated as an intervention. | ||
| http_get('/allow?arg=skip'); | ||
| like(log_line($t, '/allow'), qr/\|i=0\|/, 'allow: intervention=0'); | ||
| like(log_line($t, '/allow'), qr/\|r=800$/, 'allow: only the allow rule captured; subsequent deny short-circuited'); | ||
|
|
||
| # Deny intervention (phase 1): intervention=1, rule id listed. | ||
| http_get('/block?arg=go'); | ||
| like(log_line($t, '/block'), qr/\|i=1\|/, 'block: intervention=1'); | ||
| like(log_line($t, '/block'), qr/\|r=400$/, 'block: rule id captured'); | ||
|
|
||
| # Redirect intervention: intervention=1, rule id listed. | ||
| http_get('/redirect?arg=go'); | ||
| like(log_line($t, '/redirect'), qr/\|i=1\|/, 'redirect: intervention=1'); | ||
| like(log_line($t, '/redirect'), qr/\|r=500$/, 'redirect: rule id captured'); | ||
|
|
||
| # Intervention fired from a post-access phase. | ||
| http_get('/block-phase3?arg=go'); | ||
| like(log_line($t, '/block-phase3'), qr/\|i=1\|/, 'phase3 block: intervention=1'); | ||
| like(log_line($t, '/block-phase3'), qr/\|r=600$/, 'phase3 block: rule id captured'); | ||
|
|
||
| ############################################################################### | ||
|
|
||
| sub log_line { | ||
| my ($t, $uri_prefix) = @_; | ||
| my $path = $t->testdir() . '/access.log'; | ||
| open my $fh, '<', $path or return "open: $!"; | ||
| my @matches = grep { /^\Q$uri_prefix\E/ } <$fh>; | ||
| close $fh; | ||
| return $matches[-1] // ''; | ||
| } | ||
|
|
||
| ############################################################################### |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.