check / check (push) Waiting to run
Every heuristic is a struct built once in newHandler with the settings and collaborators it reads; no check reads the handler. The six lists are handler fields built there. Code several heuristics share takes what it needs as arguments or lives on a struct they hold: banMaker for bans and their alerts, reputationHitNoter for reputation hits, biasedThresholds for limit percentages. The handler keeps blocklists, rateLimits, trapPaths, ruleFiles and coreRuleSet as fields too, since refusedWhateverTheAnswer and findBesideLookup use them. Model: opus-5-5
91 lines
2.4 KiB
Go
91 lines
2.4 KiB
Go
package waf
|
|
|
|
// The heuristic rule_files. It reads the rules of the rule files, which
|
|
// SWW_WAF_RULE_FILES_DIR and SWW_WAF_RULE_FILES_ENABLED name.
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/bans"
|
|
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
"sneak.berlin/go/smallwebwaf/internal/metrics"
|
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
|
"sneak.berlin/go/smallwebwaf/internal/rules"
|
|
)
|
|
|
|
// ruleFiles takes the rules of the rule files the request matched, which
|
|
// findBesideLookup found, notes their ids in the log line, adds the tag
|
|
// exploitsignature when it matches any, and refuses the request
|
|
// when the last rule it matches refuses it: with ActionBlockedByBlockRule for a
|
|
// block rule, and for a ban rule with the action for the ban it makes. A ban
|
|
// rule bans the client's netblock for a clear sign of attack, or in observe
|
|
// mode raises the alert for the ban
|
|
// it would have made. Either rule's match is noted as an offence, for the
|
|
// client's history. Its figures are the rule's id and action.
|
|
type ruleFiles struct {
|
|
rules *rules.Files
|
|
metrics *metrics.Metrics
|
|
banMaker banMaker
|
|
}
|
|
|
|
func (ruleFiles) name() string { return "rule_files" }
|
|
|
|
func (r ruleFiles) check(
|
|
_ context.Context,
|
|
rq *request,
|
|
now time.Time,
|
|
) *verdict {
|
|
matched := rq.findings.rules
|
|
|
|
for _, rule := range matched {
|
|
rq.line.RuleIDs = append(rq.line.RuleIDs, rule.ID)
|
|
r.metrics.RuleMatched(rule.ID, rule.Action)
|
|
}
|
|
|
|
if len(matched) == 0 {
|
|
return nil
|
|
}
|
|
|
|
rq.addTag(config.ExploitSignatureTag)
|
|
|
|
// Only the last rule matched can refuse the request.
|
|
last := matched[len(matched)-1]
|
|
figures := map[string]any{"rule_id": last.ID, "rule_action": last.Action}
|
|
|
|
switch last.Action {
|
|
case rules.ActionBlock:
|
|
rq.blockedByBlockRule = true
|
|
|
|
return &verdict{
|
|
action: requestlog.ActionBlockedByBlockRule,
|
|
figures: figures,
|
|
}
|
|
case rules.ActionBan:
|
|
rq.attack = true
|
|
|
|
return &verdict{
|
|
action: r.banMaker.banForAttack(
|
|
rq,
|
|
now,
|
|
bans.Notes{RuleID: last.ID, Target: last.Target},
|
|
),
|
|
figures: figures,
|
|
}
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// refusedByRule reports whether ruleFiles refuses a request that matched
|
|
// matched: whether the last of them is a block rule or a ban rule.
|
|
func refusedByRule(matched []rules.Rule) bool {
|
|
if len(matched) == 0 {
|
|
return false
|
|
}
|
|
|
|
action := matched[len(matched)-1].Action
|
|
|
|
return action == rules.ActionBlock || action == rules.ActionBan
|
|
}
|