-
Notifications
You must be signed in to change notification settings - Fork 37
feat: support custom regex for rule id lookup #628
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
152d505
682de7b
224fcb7
8154fdf
d791f62
544b7a7
daec834
56e0226
cad3ec0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -504,3 +504,34 @@ func (s *readTestSuite) TestFalsePositiveIds() { | |||||
| s.Len(foundRuleIds, 1) | ||||||
| s.Contains(foundRuleIds, uint(942370)) | ||||||
| } | ||||||
|
|
||||||
| func (s *readTestSuite) TestFTWLogLines_CustomLogIdRegex() { | ||||||
| cfg, err := config.NewConfigFromEnv() | ||||||
| s.Require().NoError(err) | ||||||
| s.NotNil(cfg) | ||||||
|
|
||||||
| startMarker, endMarker := generateLogMarkers(100000, 1) | ||||||
| startMarkerLine := "X-cRs-TeSt: " + startMarker | ||||||
| endMarkerLine := "X-cRs-TeSt: " + endMarker | ||||||
| logLinesOnly := | ||||||
| `[Tue Jan 05 02:21:09.637165 2021] [:error] [pid 76:tid 139683434571520] [client 172.23.0.1:58998] [client 172.23.0.1] ModSecurity: Warning. Pattern match "\\\\b(?:keep-alive|close),\\\\s?(?:keep-alive|close)\\\\b" at REQUEST_HEADERS:Connection. [file "/etc/modsecurity.d/owasp-crs/rules/REQUEST-920-PROTOCOL-ENFORCEMENT.conf"] [line "339"] [id "920210"] [msg "Multiple/Conflicting Connection Header Data Found"] [data "close,close"] [severity "WARNING"] [ver "OWASP_CRS/3.3.0"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-protocol"] [tag "paranoia-level/1"] [tag "OWASP_CRS"] [tag "capec/1000/210/272"] [hostname "localhost"] [uri "/"] [unique_id "X-PNFSe1VwjCgYRI9FsbHgAAAIY"] | ||||||
| [Tue Jan 05 02:21:09.637731 2021] [:error] [pid 76:tid 139683434571520] [client 172.23.0.1:58998] [client 172.23.0.1] ModSecurity: Warning. Match of "pm AppleWebKit Android" against "REQUEST_HEADERS:User-Agent" required. [file "/etc/modsecurity.d/owasp-crs/rules/REQUEST-920-PROTOCOL-ENFORCEMENT.conf"] [line "1230"] [id="920300"] [msg "Request Missing an Accept Header"] [severity "NOTICE"] [ver "OWASP_CRS/3.3.0"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-protocol"] [tag "OWASP_CRS"] [tag "capec/1000/210/272"] [tag "PCI/6.5.10"] [tag "paranoia-level/2"] [hostname "localhost"] [uri "/"] [unique_id "X-PNFSe1VwjCgYRI9FsbHgAAAIY"] | ||||||
| [Tue Jan 05 02:21:09.638572 2021] [:error] [pid 76:tid 139683434571520] [client 172.23.0.1:58998] [client 172.23.0.1] ModSecurity: Warning. Operator GE matched 5 at TX:anomaly_score. [file "/etc/modsecurity.d/owasp-crs/rules/REQUEST-949-BLOCKING-EVALUATION.conf"] [line "91"] [id "949110"] [msg "Inbound Anomaly Score Exceeded (Total Score: 5)"] [severity "CRITICAL"] [ver "OWASP_CRS/3.3.0"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-generic"] [hostname "localhost"] [uri "/"] [unique_id "X-PNFSe1VwjCgYRI9FsbHgAAAIY"]` | ||||||
| logLines := fmt.Sprintf("%s\n%s\n%s", startMarkerLine, logLinesOnly, endMarkerLine) | ||||||
| s.filename, err = utils.CreateTempFileWithContent("", logLines, "test-errorlog-") | ||||||
| s.Require().NoError(err) | ||||||
|
|
||||||
| cfg.LogFile = s.filename | ||||||
| runnerConfig := config.NewRunnerConfiguration(cfg) | ||||||
|
|
||||||
| ll, err := NewFTWLogLines(runnerConfig) | ||||||
| s.Require().NoError(err) | ||||||
| ll.WithStartMarker(bytes.ToLower([]byte(startMarkerLine))) | ||||||
| ll.WithEndMarker(bytes.ToLower([]byte(endMarkerLine))) | ||||||
| err = ll.WithStdLogIdRegex(`\[id="(\d+)"\]`) | ||||||
|
||||||
| err = ll.WithStdLogIdRegex(`\[id="(\d+)"\]`) | |
| err = ll.WithStdLogIdRegex(`\[id="(\d+)"\]`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Copilot This seems to be an issue with .editorconfig. I changed it in my latest commit.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,16 +8,27 @@ import ( | |||||||||||||||||||||
| "errors" | ||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||
| "os" | ||||||||||||||||||||||
| "regexp" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| "github.com/coreruleset/go-ftw/v2/config" | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // NewFTWLogLines is the base struct for reading the log file | ||||||||||||||||||||||
| func NewFTWLogLines(cfg *config.RunnerConfig) (*FTWLogLines, error) { | ||||||||||||||||||||||
| stdLogIdRegex, err := regexp.Compile(cfg.StdLogIdRegex) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| return nil, fmt.Errorf("could not compile stdLogIdRegex: %s", err) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| jsonLogIdRegex, err := regexp.Compile(cfg.JsonLogIdRegex) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| return nil, fmt.Errorf("could not compile jsonLogIdRegex: %s", err) | ||||||||||||||||||||||
|
blinxen marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||
| } | ||||||||||||||||||||||
| ll := &FTWLogLines{ | ||||||||||||||||||||||
| logFilePath: cfg.LogFilePath, | ||||||||||||||||||||||
| runMode: cfg.RunMode, | ||||||||||||||||||||||
| LogMarkerHeaderName: bytes.ToLower([]byte(cfg.LogMarkerHeaderName)), | ||||||||||||||||||||||
| stdLogIdRegex: stdLogIdRegex, | ||||||||||||||||||||||
| jsonLogIdRegex: jsonLogIdRegex, | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
blinxen marked this conversation as resolved.
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if err := ll.openLogFile(); err != nil { | ||||||||||||||||||||||
|
|
@@ -42,6 +53,15 @@ func (ll *FTWLogLines) WithEndMarker(marker []byte) { | |||||||||||||||||||||
| ll.endMarker = bytes.ToLower(marker) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func (ll *FTWLogLines) WithStdLogIdRegex(regex string) error { | ||||||||||||||||||||||
| compiledRegex, err := regexp.Compile(regex) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| return err | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| ll.stdLogIdRegex = compiledRegex | ||||||||||||||||||||||
| return nil | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
||||||||||||||||||||||
| func (ll *FTWLogLines) WithJsonLogIdRegex(regex string) error { | |
| compiledRegex, err := regexp.Compile(regex) | |
| if err != nil { | |
| return err | |
| } | |
| ll.jsonLogIdRegex = compiledRegex | |
| return nil | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Copilot
What do you mean with
keep these overrides internal to tests via config
Uh oh!
There was an error while loading. Please reload this page.