package waf // What the reputation heuristics share: blocklists, dnsbl and abuseipdb. import ( "sneak.berlin/go/smallwebwaf/internal/alerts" "sneak.berlin/go/smallwebwaf/internal/bans" "sneak.berlin/go/smallwebwaf/internal/metrics" ) // reputationHitNoter notes the reputation sources that list a request's // client, for the reputation heuristics. type reputationHitNoter struct { metrics *metrics.Metrics alerts *alerts.Queue ipv6GroupPrefix int // SWW_WAF_IPV6_GROUP_PREFIX } // noteListed notes each of sources, the URLs of the blocklists or the // DNSBL zones, their keys masked, that list rq's client, as noteHit does, // with reason. func (n reputationHitNoter) noteListed( rq *request, sources []string, reason string, ) { for _, source := range sources { n.noteHit(rq, bans.ReputationHit{Source: source}, reason) } } // noteHit adds hit's source, which lists rq's client, to the log line's // reputation, and hit to the notes of a ban the request makes, counts the // source in the metrics, and raises a reputation_hit alert with reason, // whose detail gives hit's source and score. func (n reputationHitNoter) noteHit( rq *request, hit bans.ReputationHit, reason string, ) { detail := map[string]any{"source": hit.Source} if hit.Score != nil { detail["score"] = *hit.Score } rq.line.Reputation = append(rq.line.Reputation, hit.Source) rq.reputation = append(rq.reputation, hit) n.metrics.ReputationHit(hit.Source) n.alerts.Raise(alerts.Alert{ Event: alerts.EventReputationHit, Host: rq.site, Client: rq.client, Netblock: clientGroup(n.ipv6GroupPrefix, rq.client), ASN: rq.lookupAnswer.ASN, ASName: rq.lookupAnswer.ASName, Country: rq.lookupAnswer.Country, Reason: reason, Detail: detail, }) }