|
5 | 5 | "fmt"
|
6 | 6 | "net"
|
7 | 7 | "net/http"
|
| 8 | + "slices" |
8 | 9 | "strings"
|
9 | 10 | "time"
|
10 | 11 |
|
@@ -34,6 +35,18 @@ var excludedMatchCollections = []string{
|
34 | 35 | "TX", // Score has been exceeded
|
35 | 36 | }
|
36 | 37 |
|
| 38 | +var CRSAnomalyScores = []string{ |
| 39 | + "sql_injection_score", |
| 40 | + "xss_score", |
| 41 | + "rfi_score", |
| 42 | + "lfi_score", |
| 43 | + "rce_score", |
| 44 | + "php_injection_score", |
| 45 | + "http_violation_score", |
| 46 | + "session_fixation_score", |
| 47 | + "anomaly_score", |
| 48 | +} |
| 49 | + |
37 | 50 | func AppsecEventGenerationGeoIPEnrich(src *models.Source) error {
|
38 | 51 |
|
39 | 52 | if src == nil || src.Scope == nil || *src.Scope != types.Ip {
|
@@ -79,9 +92,9 @@ func formatCRSMatch(vars map[string]string, hasInBandMatches bool, hasOutBandMat
|
79 | 92 | case hasOutBandMatches:
|
80 | 93 | msg += "out-of-band: "
|
81 | 94 | }
|
82 |
| - for _, var_name := range appsec.CRSAnomalyScores { |
83 |
| - if val, ok := vars[var_name]; ok && val != "0" { |
84 |
| - msg += fmt.Sprintf("%s: %s, ", strings.Replace(strings.Replace(var_name, "TX.", "", 1), "_score", "", 1), val) |
| 95 | + for _, var_name := range CRSAnomalyScores { |
| 96 | + if val, ok := vars["TX."+var_name]; ok && val != "0" { |
| 97 | + msg += fmt.Sprintf("%s: %s, ", strings.Replace(var_name, "_score", "", 1), val) |
85 | 98 | }
|
86 | 99 | }
|
87 | 100 | return msg
|
@@ -349,31 +362,43 @@ func (r *AppsecRunner) AccumulateTxToEvent(evt *types.Event, req *appsec.ParsedR
|
349 | 362 | evt.Appsec.Vars = map[string]string{}
|
350 | 363 | }
|
351 | 364 |
|
352 |
| - req.Tx.Variables().All(func(v variables.RuleVariable, col collection.Collection) bool { |
353 |
| - for _, variable := range col.FindAll() { |
354 |
| - r.logger.Tracef("variable: %s.%s = %s\n", variable.Variable().Name(), variable.Key(), variable.Value()) |
355 |
| - key := variable.Variable().Name() |
356 |
| - if variable.Key() != "" { |
357 |
| - key += "." + variable.Key() |
358 |
| - } |
| 365 | + txCollection := req.Tx.Variables().TX() |
359 | 366 |
|
360 |
| - if variable.Value() == "" { |
361 |
| - continue |
362 |
| - } |
| 367 | + txMatchedData := txCollection.FindAll() |
| 368 | + |
| 369 | + for _, match := range txMatchedData { |
| 370 | + if slices.Contains(CRSAnomalyScores, match.Key()) { |
| 371 | + evt.Appsec.Vars["TX."+match.Key()] = match.Value() |
| 372 | + } |
| 373 | + } |
363 | 374 |
|
364 |
| - for _, collectionToKeep := range r.AppsecRuntime.CompiledVariablesTracking { |
365 |
| - match := collectionToKeep.MatchString(key) |
366 |
| - if match { |
367 |
| - evt.Appsec.Vars[key] = variable.Value() |
368 |
| - r.logger.Debugf("%s.%s = %s", variable.Variable().Name(), variable.Key(), variable.Value()) |
369 |
| - } else { |
370 |
| - r.logger.Debugf("%s.%s != %s (%s) (not kept)", variable.Variable().Name(), variable.Key(), collectionToKeep, variable.Value()) |
| 375 | + if len(r.AppsecRuntime.CompiledVariablesTracking) > 0 { |
| 376 | + req.Tx.Variables().All(func(v variables.RuleVariable, col collection.Collection) bool { |
| 377 | + for _, variable := range col.FindAll() { |
| 378 | + r.logger.Tracef("variable: %s.%s = %s\n", variable.Variable().Name(), variable.Key(), variable.Value()) |
| 379 | + key := variable.Variable().Name() |
| 380 | + if variable.Key() != "" { |
| 381 | + key += "." + variable.Key() |
| 382 | + } |
| 383 | + |
| 384 | + if variable.Value() == "" { |
| 385 | + continue |
| 386 | + } |
| 387 | + |
| 388 | + for _, collectionToKeep := range r.AppsecRuntime.CompiledVariablesTracking { |
| 389 | + match := collectionToKeep.MatchString(key) |
| 390 | + if match { |
| 391 | + evt.Appsec.Vars[key] = variable.Value() |
| 392 | + r.logger.Debugf("%s.%s = %s", variable.Variable().Name(), variable.Key(), variable.Value()) |
| 393 | + } else { |
| 394 | + r.logger.Debugf("%s.%s != %s (%s) (not kept)", variable.Variable().Name(), variable.Key(), collectionToKeep, variable.Value()) |
| 395 | + } |
371 | 396 | }
|
372 | 397 | }
|
373 |
| - } |
374 | 398 |
|
375 |
| - return true |
376 |
| - }) |
| 399 | + return true |
| 400 | + }) |
| 401 | + } |
377 | 402 |
|
378 | 403 | for _, rule := range req.Tx.MatchedRules() {
|
379 | 404 | // Drop the rule if it has no message (it's likely a CRS setup rule)
|
|
0 commit comments