check / check (push) Waiting to run
The tags, what observe mode would have done, the broken limit and the three reputation flags are fields of decision, each naming what sets it. The log line is written from them and from the lookup answer, and no heuristic, ban note or alert reads it back. A ban's notes take the request's method, Host and User-Agent from decision, since the report's steps have no request to read them from. Model: opus-5-5
366 lines
10 KiB
Go
366 lines
10 KiB
Go
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)
|
|
|
|
var expires string
|
|
if banned {
|
|
expires = banExpires(ban)
|
|
rq.line.BanExpires = expires
|
|
}
|
|
|
|
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": expires,
|
|
},
|
|
}
|
|
}
|
|
|
|
// 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, banClient says what
|
|
// happens instead.
|
|
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.brokeLimit = true
|
|
rq.line.Offence = requestlog.OffenceLimit
|
|
|
|
b.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()
|
|
}
|
|
|
|
b.banClient(
|
|
rq,
|
|
now,
|
|
status,
|
|
bans.Grounds{Cause: bans.CauseLimit, Notes: notes},
|
|
)
|
|
}
|
|
|
|
// 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 returns the action for that ban. In observe mode, banClient
|
|
// says what happens instead.
|
|
func (b banMaker) banForAttack(
|
|
rq *request, now time.Time, notes bans.Notes,
|
|
) string {
|
|
return b.banClient(
|
|
rq,
|
|
now,
|
|
b.banResponse,
|
|
bans.Grounds{Cause: bans.CauseAttack, Notes: notes},
|
|
)
|
|
}
|
|
|
|
// banClient bans the netblock of rq's client 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 (b banMaker) banClient(
|
|
rq *request,
|
|
now time.Time,
|
|
status int,
|
|
grounds bans.Grounds,
|
|
) string {
|
|
netblock := banNetblock(b.banScopeV4Prefix, b.ipv6GroupPrefix, rq.client)
|
|
|
|
if b.observe {
|
|
if !b.wouldAlertBan(netblock, now, grounds.Cause) {
|
|
return banAction(
|
|
b.ledger.WouldBePermanent(netblock, now, grounds.Cause),
|
|
)
|
|
}
|
|
|
|
grounds.Notes = b.withClientNotes(
|
|
rq,
|
|
grounds.Notes,
|
|
now,
|
|
status,
|
|
netblock,
|
|
)
|
|
|
|
ban, wouldBan := b.ledger.WouldBan(netblock, now, grounds)
|
|
if wouldBan {
|
|
b.alertBan(rq, ban)
|
|
}
|
|
|
|
return banAction(ban.Permanent())
|
|
}
|
|
|
|
grounds.Notes = b.withClientNotes(
|
|
rq,
|
|
grounds.Notes,
|
|
now,
|
|
status,
|
|
netblock,
|
|
)
|
|
|
|
ban, made := b.ledger.Ban(netblock, now, grounds)
|
|
if grounds.Cause == bans.CauseLimit {
|
|
b.limiter.Reset(clientGroup(b.ipv6GroupPrefix, rq.client))
|
|
}
|
|
|
|
rq.line.BanExpires = banExpires(ban)
|
|
|
|
if made {
|
|
b.alertBan(rq, ban)
|
|
}
|
|
|
|
return banAction(ban.Permanent())
|
|
}
|
|
|
|
// withClientNotes is notes with what rq tells of its client added: its AS,
|
|
// country and reputation, the request at now with status, and how many
|
|
// requests netblock, the client's, has sent.
|
|
func (b banMaker) withClientNotes(
|
|
rq *request,
|
|
notes bans.Notes,
|
|
now time.Time,
|
|
status int,
|
|
netblock netip.Prefix,
|
|
) bans.Notes {
|
|
notes.ASN = rq.lookupAnswer.ASN
|
|
notes.ASName = rq.lookupAnswer.ASName
|
|
notes.Country = rq.lookupAnswer.Country
|
|
notes.Reputation = rq.reputation
|
|
notes.Request = rq.noted(now, status)
|
|
notes.Requests = b.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 (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.method,
|
|
Host: rq.site,
|
|
Path: rq.requestURI,
|
|
Status: status,
|
|
UserAgent: rq.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)
|
|
}
|