package waf // The heuristic active_ban, and banMaker, which makes the bans of several // heuristics. 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/metrics" "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 string) *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 { banMaker banMaker } func (activeBan) name() string { return "active_ban" } func (a activeBan) check( _ context.Context, rq *request, now time.Time, ) *verdict { ledger := a.banMaker.ledger check := ledger.Check if a.banMaker.observe { check = 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 a.banMaker.alertBan(rq, 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) string { 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 bytesCounted, SWW_WAF_BYTES_COUNTED, // says. func (rq *request) countedBytes(bytesCounted config.BytesCounted) int64 { response, request := rq.out.bytes, rq.requestBytes switch bytesCounted { case config.BytesCountedResponse: return response case config.BytesCountedRequest: return request case config.BytesCountedBoth: } return response + request } // banMaker makes the bans of the heuristics that ban a client, in the ban // ledger, and raises the alert for each. In observe mode it makes no ban, and // raises the alert for the ban it would have made. type banMaker struct { ledger *bans.Ledger // limiter is the table of clients the rate limits count. A ban for a limit // sets the client's counters back to zero, and a ban's notes give how many // requests its netblock has sent. limiter *ratelimit.Limiter metrics *metrics.Metrics alerts *alerts.Queue // observe is true in observe mode (SWW_WAF_MODE). observe bool // banResponse is SWW_WAF_BAN_RESPONSE, the status the notes of a ban for a // clear sign of attack give. banResponse int // banScopeV4Prefix is SWW_WAF_BAN_SCOPE_V4_PREFIX and ipv6GroupPrefix // SWW_WAF_IPV6_GROUP_PREFIX, which set the netblock a ban covers. banScopeV4Prefix int ipv6GroupPrefix int } // banForLimit bans the netblock of rq's client 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 it makes no ban and // sets nothing back, and raises the alert for the ban it would have made, // if that alert would be sent. func (b banMaker) banForLimit( rq *request, 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 default: rq.line.LimitHit = hit.Window } rq.line.Offence = requestlog.OffenceLimit b.metrics.LimitHit(hit) netblock := banNetblock(b.banScopeV4Prefix, b.ipv6GroupPrefix, rq.client) if b.observe && !b.wouldAlertBan(netblock, now, bans.CauseLimit) { return } notes := bans.Notes{ ASN: rq.line.ASN, ASName: rq.line.ASName, Country: rq.line.Country, Kind: hit.Kind, Limit: hit.Limit, Window: hit.Window, Count: hit.Count, Reputation: rq.reputation, Request: rq.noted(now, status), Requests: b.netblockRequests(netblock), } switch hit.Kind { case ratelimit.KindRequests: notes.LimitPercent, notes.LimitPercentSetting = rq.rateLimitPercent.logged() case ratelimit.KindBytes: notes.LimitPercent, notes.LimitPercentSetting = rq.bytesLimitPercent.logged() } if b.observe { ban, wouldBan := b.ledger.WouldBanForLimit(netblock, now, notes) if wouldBan { b.alertBan(rq, ban) } return } ban, made := b.ledger.BanForLimit(netblock, now, notes) b.limiter.Reset(clientGroup(b.ipv6GroupPrefix, rq.client)) rq.line.BanExpires = banExpires(ban) if made { b.alertBan(rq, ban) } } // banForAttack bans the netblock of rq's client at now for a clear sign of // attack, which notes name: the ban rule that matched, or the trap path // asked for. It fills in the rest of the notes. In observe mode it makes // no ban, and raises the alert for the ban it would have made, if that // alert would be sent. It returns the action for that ban. func (b banMaker) banForAttack( rq *request, now time.Time, notes bans.Notes, ) string { netblock := banNetblock(b.banScopeV4Prefix, b.ipv6GroupPrefix, rq.client) if b.observe && !b.wouldAlertBan(netblock, now, bans.CauseAttack) { return banAction( b.ledger.WouldBePermanent(netblock, now, bans.CauseAttack), ) } notes.ASN = rq.line.ASN notes.ASName = rq.line.ASName notes.Country = rq.line.Country notes.Reputation = rq.reputation notes.Request = rq.noted(now, b.banResponse) notes.Requests = b.netblockRequests(netblock) if b.observe { ban, wouldBan := b.ledger.WouldBanForAttack(netblock, now, notes) if wouldBan { b.alertBan(rq, ban) } return banAction(ban.Permanent()) } ban, made := b.ledger.BanForAttack(netblock, now, notes) rq.line.BanExpires = banExpires(ban) if made { b.alertBan(rq, ban) } return banAction(ban.Permanent()) } // 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 (b banMaker) wouldAlertBan( netblock netip.Prefix, now time.Time, cause string, ) bool { event := alerts.EventBan if b.ledger.WouldBePermanent(netblock, now, cause) { event = alerts.EventPermanentBan } return b.alerts.WouldSend(event, netblock) } // alertBan raises the alert for ban, which rq 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 (b banMaker) alertBan(rq *request, ban bans.Ban) { event := alerts.EventBan if ban.Permanent() { event = alerts.EventPermanentBan } detail := map[string]any{ "cause": ban.Cause, "ban_expires": banExpires(ban), "notes": ban.Notes, } if b.observe { detail["mode"] = "observe" } b.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, }) } // netblockRequests is how many requests netblock has sent since it was // first seen, the request being banned for included: the histories count it // only once it has ended. func (b banMaker) netblockRequests(netblock netip.Prefix) int64 { return b.limiter.Requests(netblock) + 1 } // 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, } } // banNetblock is the netblock a ban on client covers: its IPv4 address, // widened to banScopeV4Prefix, SWW_WAF_BAN_SCOPE_V4_PREFIX, or the IPv6 group // clientGroup counts it in, of length ipv6GroupPrefix. func banNetblock( banScopeV4Prefix, ipv6GroupPrefix int, client netip.Addr, ) netip.Prefix { addr := client.Unmap() if addr.Is4() { return netip.PrefixFrom(addr, banScopeV4Prefix).Masked() } return clientGroup(ipv6GroupPrefix, 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) }