check / check (push) Waiting to run
BytesCounted, LookupSource, BlocklistAction, DNSBLAbuseIPDBAction and CoreRuleSetMode become named string types declared in config, with constants for their values. Every comparison in waf and sww uses the constants, and the private duplicates fileSource and deny go. The two action settings share ReputationAction. The values and the strings written to logs, metrics and alerts are unchanged. Model: opus-5-5
120 lines
3.6 KiB
Go
120 lines
3.6 KiB
Go
package waf
|
|
|
|
// The heuristic core_rule_set. It reads SWW_WAF_CORE_RULE_SET_MODE,
|
|
// SWW_WAF_CORE_RULE_SET_EXEMPT_PATHS and SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD.
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
|
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
"sneak.berlin/go/smallwebwaf/internal/coreruleset"
|
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
|
)
|
|
|
|
// coreRuleSet takes what the Core Rule Set found of the request, which
|
|
// findBesideLookup inspected with it unless SWW_WAF_CORE_RULE_SET_MODE is off
|
|
// or SWW_WAF_CORE_RULE_SET_EXEMPT_PATHS exempts its path, as pathExempt
|
|
// decides. It notes the rules the request matched and its score in the log
|
|
// line, and the rules in the metrics. A score at or over
|
|
// SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD is a match: it adds the tag
|
|
// exploitsignature, raises the core_rule_set_match alert, and in block mode
|
|
// refuses the request with 403, which is an offence its client's history
|
|
// counts. Its figures are the score and the threshold.
|
|
type coreRuleSet struct{}
|
|
|
|
func (coreRuleSet) name() string { return "core_rule_set" }
|
|
|
|
func (coreRuleSet) check(
|
|
_ context.Context,
|
|
rq *request,
|
|
_ time.Time,
|
|
) *verdict {
|
|
findings := rq.findings
|
|
if !findings.inspected {
|
|
return nil
|
|
}
|
|
|
|
cfg := rq.h.config
|
|
result := findings.inspection
|
|
rq.requestBytes = int64(len(findings.read))
|
|
|
|
rq.line.DurationCoreRuleSetMS = new(
|
|
requestlog.Milliseconds(findings.inspectionTime),
|
|
)
|
|
rq.line.CoreRuleSetRuleIDs = result.RuleIDs
|
|
rq.line.CoreRuleSetScore = &result.Score
|
|
|
|
for _, id := range result.RuleIDs {
|
|
rq.h.metrics.CoreRuleSetMatched(string(cfg.CoreRuleSetMode), id)
|
|
}
|
|
|
|
threshold := cfg.CoreRuleSetScoreThreshold
|
|
if threshold == 0 || result.Score < threshold {
|
|
return nil
|
|
}
|
|
|
|
rq.addTag(config.ExploitSignatureTag)
|
|
rq.alertCoreRuleSetMatch(result)
|
|
|
|
if !rq.refusedByCoreRuleSet(result) {
|
|
return nil
|
|
}
|
|
|
|
rq.blockedByCoreRuleSet = true
|
|
|
|
return &verdict{
|
|
action: requestlog.ActionBlockedByCoreRuleSet,
|
|
figures: map[string]any{
|
|
figureScore: result.Score,
|
|
"threshold": threshold,
|
|
},
|
|
}
|
|
}
|
|
|
|
// refusedByCoreRuleSet reports whether coreRuleSet refuses a request the Core
|
|
// Rule Set found result of: whether the score is at or over
|
|
// SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD in block mode.
|
|
func (rq *request) refusedByCoreRuleSet(result coreruleset.Result) bool {
|
|
cfg := rq.h.config
|
|
threshold := cfg.CoreRuleSetScoreThreshold
|
|
|
|
return cfg.CoreRuleSetMode == config.CoreRuleSetModeBlock &&
|
|
threshold != 0 && result.Score >= threshold
|
|
}
|
|
|
|
// alertCoreRuleSetMatch raises the core_rule_set_match alert for the request,
|
|
// which the Core Rule Set scored at result, at or over
|
|
// SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD. Its detail gives the rule ids, the
|
|
// score, the method and the path with the query, and, for a request that is
|
|
// not refused for it, the mode: detect, or observe in observe mode.
|
|
func (rq *request) alertCoreRuleSetMatch(result coreruleset.Result) {
|
|
detail := map[string]any{
|
|
"rule_ids": result.RuleIDs,
|
|
"score": result.Score,
|
|
"method": rq.in.Method,
|
|
"path": rq.in.URL.RequestURI(),
|
|
}
|
|
|
|
switch {
|
|
case rq.h.config.CoreRuleSetMode == config.CoreRuleSetModeDetect:
|
|
detail["mode"] = string(config.CoreRuleSetModeDetect)
|
|
case rq.h.config.Observe:
|
|
detail["mode"] = "observe"
|
|
}
|
|
|
|
rq.h.alerts.Raise(alerts.Alert{
|
|
Event: alerts.EventCoreRuleSetMatch,
|
|
Host: rq.site,
|
|
Client: rq.client,
|
|
Netblock: clientGroup(rq.h.config, rq.client),
|
|
ASN: rq.line.ASN,
|
|
ASName: rq.line.ASName,
|
|
Country: rq.line.Country,
|
|
Reason: "scored by the Core Rule Set at or over " +
|
|
"SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD",
|
|
Detail: detail,
|
|
})
|
|
}
|