check / check (push) Waiting to run
Each fixed set of string constants becomes a named string type in the package that owns it, and the fields and parameters that carry it use the type: bans.Cause, ratelimit.LimitKind, alerts.Event, alerts.DestinationName, rules.Action, requestlog.Action, requestlog.Offence, requestlog.WAFLineKind and waf.Disposition. Every value keeps its string, so the logs, state files, alerts and metrics do not change. The switches exhaustive now checks list their missing cases. Model: opus-5-5
323 lines
9.2 KiB
Go
323 lines
9.2 KiB
Go
package waf
|
|
|
|
// The heuristic active_ban, and the code that makes bans, which several
|
|
// heuristics share. active_ban reads SWW_WAF_MODE; the bans are the ban ledger's.
|
|
|
|
import (
|
|
"context"
|
|
"net/netip"
|
|
"time"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
|
"sneak.berlin/go/smallwebwaf/internal/bans"
|
|
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
|
)
|
|
|
|
// banResponse is a refusal answered with SWW_WAF_BAN_RESPONSE, and logged
|
|
// with action.
|
|
func (rq *request) banResponse(action requestlog.Action) *refusal {
|
|
return &refusal{status: rq.h.config.BanResponse, action: action}
|
|
}
|
|
|
|
// activeBan refuses a client a ban covers: a ban on a netblock the client is
|
|
// in, in force at now. It notes for the log line when that ban ends. A request
|
|
// that makes the ban permanent, or in observe mode would have, raises the alert
|
|
// for it. Its figures are the ban's netblock and when it ends.
|
|
type activeBan struct{}
|
|
|
|
func (activeBan) name() string { return "active_ban" }
|
|
|
|
func (activeBan) check(
|
|
_ context.Context,
|
|
rq *request,
|
|
now time.Time,
|
|
) *verdict {
|
|
check := rq.h.ledger.Check
|
|
if rq.h.config.Observe {
|
|
check = rq.h.ledger.Find // the ban refuses nothing, and stays as it is
|
|
}
|
|
|
|
ban, banned, madePermanent := check(rq.client, now)
|
|
if banned {
|
|
rq.line.BanExpires = banExpires(ban)
|
|
}
|
|
|
|
if madePermanent {
|
|
ban.Expires = time.Time{} // the ban made permanent, which Find leaves as it is
|
|
rq.alertBan(ban)
|
|
}
|
|
|
|
if !banned {
|
|
return nil
|
|
}
|
|
|
|
return &verdict{
|
|
action: banAction(ban.Permanent()),
|
|
figures: map[string]any{
|
|
figureNetblock: ban.Netblock.String(), "ban_expires": rq.line.BanExpires,
|
|
},
|
|
}
|
|
}
|
|
|
|
// banAction is the action of a request refused under a ban or for making one:
|
|
// permanent says the ban never runs out.
|
|
func banAction(permanent bool) requestlog.Action {
|
|
if permanent {
|
|
return requestlog.ActionPermanentlyBanned
|
|
}
|
|
|
|
return requestlog.ActionTemporarilyBanned
|
|
}
|
|
|
|
// limitFigures are the figures of a heuristic that broke hit, a limit: its
|
|
// window, the count and the limit.
|
|
func limitFigures(hit ratelimit.Hit) map[string]any {
|
|
return map[string]any{
|
|
figureWindow: hit.Window, figureCount: hit.Count, figureLimit: hit.Limit,
|
|
}
|
|
}
|
|
|
|
// countedBytes returns the request's bytes, once it has ended, as the
|
|
// byte limits and the anomaly thresholds count them: the response's body
|
|
// bytes, the request's, or both, as SWW_WAF_BYTES_COUNTED says.
|
|
func (rq *request) countedBytes() int64 {
|
|
response, request := rq.out.bytes, rq.requestBytes
|
|
|
|
switch rq.h.config.BytesCounted {
|
|
case config.BytesCountedResponse:
|
|
return response
|
|
case config.BytesCountedRequest:
|
|
return request
|
|
case config.BytesCountedBoth:
|
|
}
|
|
|
|
return response + request
|
|
}
|
|
|
|
// banForLimit bans the client's netblock at now for a broken limit, the
|
|
// one hit names, notes the offence for the log line and counts the hit in
|
|
// the metrics. status is what the client was sent, or is sent:
|
|
// SWW_WAF_BAN_RESPONSE for a request over a rate limit, what the report says for
|
|
// one whose bytes broke a byte limit, the refusal for one that broke the
|
|
// refusal limit. The ban's notes give the client's limit percentage for a
|
|
// rate limit or a byte limit; the refusal limit is not lowered. The ban sets
|
|
// the client's counters back to zero. In observe mode, banClient says what
|
|
// happens instead.
|
|
func (rq *request) banForLimit(
|
|
now time.Time,
|
|
hit ratelimit.Hit,
|
|
status int,
|
|
) {
|
|
switch hit.Kind {
|
|
case ratelimit.KindBytes:
|
|
rq.line.LimitHit = hit.Window + "_bytes" // as counts names the byte totals
|
|
case ratelimit.KindRefusals:
|
|
rq.line.LimitHit = requestlog.LimitHitMinuteRefusals
|
|
case ratelimit.KindRequests:
|
|
rq.line.LimitHit = hit.Window
|
|
}
|
|
|
|
rq.line.Offence = requestlog.OffenceLimit
|
|
rq.h.metrics.LimitHit(hit)
|
|
|
|
notes := bans.Notes{
|
|
Kind: hit.Kind,
|
|
Limit: hit.Limit,
|
|
Window: hit.Window,
|
|
Count: hit.Count,
|
|
}
|
|
|
|
switch hit.Kind {
|
|
case ratelimit.KindRequests:
|
|
notes.LimitPercent, notes.LimitPercentSetting = rq.rateLimitPercent.logged()
|
|
case ratelimit.KindBytes:
|
|
notes.LimitPercent, notes.LimitPercentSetting = rq.bytesLimitPercent.logged()
|
|
case ratelimit.KindRefusals: // the refusal limit is not lowered
|
|
}
|
|
|
|
rq.banClient(
|
|
now,
|
|
status,
|
|
bans.Grounds{Cause: bans.CauseLimit, Notes: notes},
|
|
)
|
|
}
|
|
|
|
// banForAttack bans the client's netblock at now for a clear sign of
|
|
// attack, which notes name: the ban rule that matched, or the trap path
|
|
// asked for. It returns the action for that ban. In observe mode, banClient
|
|
// says what happens instead.
|
|
func (rq *request) banForAttack(
|
|
now time.Time, notes bans.Notes,
|
|
) requestlog.Action {
|
|
return rq.banClient(
|
|
now,
|
|
rq.h.config.BanResponse,
|
|
bans.Grounds{Cause: bans.CauseAttack, Notes: notes},
|
|
)
|
|
}
|
|
|
|
// banClient bans the client's netblock at now on grounds, and returns the
|
|
// action for that ban. It adds to the notes what the request tells of the
|
|
// client, with status, what the client was sent. A ban for a broken limit
|
|
// sets the client's counters back to zero.
|
|
//
|
|
// In observe mode it makes no ban and sets nothing back. It raises the
|
|
// alert for the ban it would have made, if that alert would be sent.
|
|
func (rq *request) banClient(
|
|
now time.Time,
|
|
status int,
|
|
grounds bans.Grounds,
|
|
) requestlog.Action {
|
|
netblock := banNetblock(rq.h.config, rq.client)
|
|
|
|
if rq.h.config.Observe {
|
|
if !rq.wouldAlertBan(netblock, now, grounds.Cause) {
|
|
return banAction(
|
|
rq.h.ledger.WouldBePermanent(netblock, now, grounds.Cause),
|
|
)
|
|
}
|
|
|
|
grounds.Notes = rq.withClientNotes(
|
|
grounds.Notes,
|
|
now,
|
|
status,
|
|
netblock,
|
|
)
|
|
|
|
ban, wouldBan := rq.h.ledger.WouldBan(netblock, now, grounds)
|
|
if wouldBan {
|
|
rq.alertBan(ban)
|
|
}
|
|
|
|
return banAction(ban.Permanent())
|
|
}
|
|
|
|
grounds.Notes = rq.withClientNotes(grounds.Notes, now, status, netblock)
|
|
|
|
ban, made := rq.h.ledger.Ban(netblock, now, grounds)
|
|
if grounds.Cause == bans.CauseLimit {
|
|
rq.h.limiter.Reset(clientGroup(rq.h.config, rq.client))
|
|
}
|
|
|
|
rq.line.BanExpires = banExpires(ban)
|
|
|
|
if made {
|
|
rq.alertBan(ban)
|
|
}
|
|
|
|
return banAction(ban.Permanent())
|
|
}
|
|
|
|
// withClientNotes is notes with what the request tells of the client
|
|
// added: its AS, country and reputation, the request at now with status,
|
|
// and how many requests netblock, the client's, has sent.
|
|
func (rq *request) withClientNotes(
|
|
notes bans.Notes,
|
|
now time.Time,
|
|
status int,
|
|
netblock netip.Prefix,
|
|
) bans.Notes {
|
|
notes.ASN = rq.line.ASN
|
|
notes.ASName = rq.line.ASName
|
|
notes.Country = rq.line.Country
|
|
notes.Reputation = rq.reputation
|
|
notes.Request = rq.noted(now, status)
|
|
notes.Requests = rq.netblockRequests(netblock)
|
|
|
|
return notes
|
|
}
|
|
|
|
// wouldAlertBan reports whether the alert for a ban on netblock for cause
|
|
// made at now would be sent. In observe mode the ban the request would
|
|
// have made is worked out only then, at most once per
|
|
// SWW_WAF_ALERT_COOLDOWN and never with no webhook set: its notes count the
|
|
// netblock's requests, which can mean going through every client.
|
|
func (rq *request) wouldAlertBan(
|
|
netblock netip.Prefix, now time.Time, cause bans.Cause,
|
|
) bool {
|
|
event := alerts.EventBan
|
|
if rq.h.ledger.WouldBePermanent(netblock, now, cause) {
|
|
event = alerts.EventPermanentBan
|
|
}
|
|
|
|
return rq.h.alerts.WouldSend(event, netblock)
|
|
}
|
|
|
|
// alertBan raises the alert for ban, which the request made, or made
|
|
// permanent: permanent_ban for a permanent ban, ban for another. Its
|
|
// detail gives the ban's cause, when it ends, and its notes, and in
|
|
// observe mode, where ban is the ban that would have been made, or made
|
|
// permanent, mode, observe.
|
|
func (rq *request) alertBan(ban bans.Ban) {
|
|
event := alerts.EventBan
|
|
if ban.Permanent() {
|
|
event = alerts.EventPermanentBan
|
|
}
|
|
|
|
detail := map[string]any{
|
|
"cause": string(ban.Cause),
|
|
"ban_expires": banExpires(ban),
|
|
"notes": ban.Notes,
|
|
}
|
|
if rq.h.config.Observe {
|
|
detail["mode"] = "observe"
|
|
}
|
|
|
|
rq.h.alerts.Raise(alerts.Alert{
|
|
Event: event,
|
|
Host: rq.site,
|
|
Client: rq.client,
|
|
Netblock: ban.Netblock,
|
|
ASN: ban.Notes.ASN,
|
|
ASName: ban.Notes.ASName,
|
|
Country: ban.Notes.Country,
|
|
Reason: ban.Reason,
|
|
Detail: detail,
|
|
})
|
|
}
|
|
|
|
// noted is the request, at now, with status, what the client was sent, or
|
|
// in observe mode would have been, as the notes of the ban it makes keep
|
|
// it.
|
|
func (rq *request) noted(now time.Time, status int) bans.Request {
|
|
return bans.Request{
|
|
Time: now,
|
|
Method: rq.line.Method,
|
|
Host: rq.line.Host,
|
|
Path: rq.requestURI,
|
|
Status: status,
|
|
UserAgent: rq.line.UserAgent,
|
|
}
|
|
}
|
|
|
|
// netblockRequests is how many requests netblock has sent since it was
|
|
// first seen, this one included: the histories count it only once it has
|
|
// ended.
|
|
func (rq *request) netblockRequests(netblock netip.Prefix) int64 {
|
|
return rq.h.limiter.Requests(netblock) + 1
|
|
}
|
|
|
|
// banNetblock is the netblock a ban on client covers: its IPv4 address,
|
|
// widened to SWW_WAF_BAN_SCOPE_V4_PREFIX, or the IPv6 group clientGroup
|
|
// counts it in.
|
|
func banNetblock(cfg *config.WAF, client netip.Addr) netip.Prefix {
|
|
addr := client.Unmap()
|
|
if addr.Is4() {
|
|
return netip.PrefixFrom(addr, cfg.BanScopeV4Prefix).Masked()
|
|
}
|
|
|
|
return clientGroup(cfg, addr)
|
|
}
|
|
|
|
// banExpires is when ban ends, as the log line gives it: a time, or
|
|
// permanent.
|
|
func banExpires(ban bans.Ban) string {
|
|
if ban.Permanent() {
|
|
return permanent
|
|
}
|
|
|
|
return requestlog.FormatTime(ban.Expires)
|
|
}
|