336 lines
11 KiB
Go
336 lines
11 KiB
Go
// Package enforce is the optional "second firewall": it decides which source
|
|
// addresses deserve a ban and appends one line per decision to a file. The
|
|
// process never gains privileges: a host side fail2ban jail reads the file.
|
|
package enforce
|
|
|
|
import (
|
|
"net/netip"
|
|
"os"
|
|
"time"
|
|
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/intel"
|
|
)
|
|
|
|
// Modes.
|
|
const (
|
|
ModeOff = "off"
|
|
ModeObserve = "observe"
|
|
ModeOn = "on"
|
|
)
|
|
|
|
// Fixed reason slugs. Anything else is refused (also from peers).
|
|
const (
|
|
ReasonScannerFlood = "scanner-flood"
|
|
ReasonExploitFlood = "exploit-flood"
|
|
ReasonWebBrute = "web-bruteforce"
|
|
ReasonWPBrute = "wp-bruteforce"
|
|
ReasonSSHBrute = "ssh-bruteforce"
|
|
ReasonRCE = "rce"
|
|
ReasonTraversal = "path-traversal"
|
|
ReasonEnvProbe = "env-probe"
|
|
ReasonVCSProbe = "vcs-probe"
|
|
ReasonWebshell = "webshell"
|
|
ReasonSQLi = "sqli"
|
|
ReasonLog4Shell = "log4shell"
|
|
ReasonPortScan = "port-scan"
|
|
ReasonKnownAttacker = "known-attacker"
|
|
// ReasonNeighbor: another address of a /24 (v4) or /64 (v6) where several
|
|
// addresses were banned in the last 24 h attacked again.
|
|
ReasonNeighbor = "neighbor-campaign"
|
|
)
|
|
|
|
var reasons = map[string]bool{
|
|
ReasonScannerFlood: true, ReasonExploitFlood: true, ReasonWebBrute: true, ReasonWPBrute: true,
|
|
ReasonSSHBrute: true, ReasonRCE: true, ReasonTraversal: true, ReasonEnvProbe: true,
|
|
ReasonVCSProbe: true, ReasonWebshell: true, ReasonSQLi: true, ReasonLog4Shell: true,
|
|
ReasonPortScan: true, ReasonKnownAttacker: true, ReasonNeighbor: true,
|
|
}
|
|
|
|
// ValidReason reports whether s is one of the fixed slugs.
|
|
func ValidReason(s string) bool { return reasons[s] }
|
|
|
|
// criticalFam maps the attack families that justify a ban on one hit.
|
|
var criticalFam = map[string]string{
|
|
intel.FamRCE: ReasonRCE,
|
|
intel.FamTraversal: ReasonTraversal,
|
|
intel.FamEnvCreds: ReasonEnvProbe,
|
|
intel.FamVCS: ReasonVCSProbe,
|
|
intel.FamWebshell: ReasonWebshell,
|
|
intel.FamSQLi: ReasonSQLi,
|
|
intel.FamLog4Shell: ReasonLog4Shell,
|
|
}
|
|
|
|
// Policy constants.
|
|
const (
|
|
Window = 10 * time.Minute
|
|
LegitWindow = 24 * time.Hour
|
|
SSHThreshold = 5
|
|
// PortScanPorts distinct ports probed in Window justify a ban.
|
|
PortScanPorts = 10
|
|
// CritHits is how many critical events of one address (in Window) justify a ban.
|
|
CritHits = 2
|
|
// SSH24Threshold SSH failures, or Web24Threshold web scan/exploit events, in
|
|
// LongWindow justify a ban (slow attackers never cross the 10 minute rules).
|
|
SSH24Threshold = 10
|
|
Web24Threshold = 20
|
|
LongWindow = 24 * time.Hour
|
|
// WeekWindow is the slow-attacker window: SSH7Threshold SSH failures (an
|
|
// ssh-probe counts half), or Web7Threshold web scan/exploit events without a
|
|
// legitimate visit, in 7 days justify a ban.
|
|
WeekWindow = 7 * 24 * time.Hour
|
|
SSH7Threshold = 10
|
|
Web7Threshold = 10
|
|
// ProbeWeight is the weight of one ssh-probe against the SSH rules (in halves).
|
|
// NeighborIPs distinct banned addresses of one prefix in NeighborWindow make
|
|
// the prefix a campaign: one attack event of another address of it is enough.
|
|
NeighborIPs = 3
|
|
NeighborWindow = 24 * time.Hour
|
|
// KnownSuspiciousScore: a "suspicious" address at or above this score counts as a known attacker.
|
|
KnownSuspiciousScore = 100
|
|
)
|
|
|
|
// ttls is the advisory duration by number of previous bans.
|
|
var ttls = []time.Duration{time.Hour, 6 * time.Hour, 24 * time.Hour, 7 * 24 * time.Hour}
|
|
|
|
// TTLFor is the advisory TTL of a ban preceded by n earlier bans.
|
|
func TTLFor(offences int) time.Duration {
|
|
if offences < 0 {
|
|
offences = 0
|
|
}
|
|
if offences >= len(ttls) {
|
|
offences = len(ttls) - 1
|
|
}
|
|
return ttls[offences]
|
|
}
|
|
|
|
// Skip reasons, also the keys of Counts.Skipped.
|
|
const (
|
|
SkipTrusted = "trusted"
|
|
SkipPrivate = "private"
|
|
SkipCrawler = "crawler"
|
|
SkipLegit = "legit_visitor"
|
|
SkipOperator = "operator_allow"
|
|
SkipPeer = "peer"
|
|
// SkipUnconfirmed: a peer's decision about an address this node never saw attacking.
|
|
SkipUnconfirmed = "peer_unconfirmed"
|
|
)
|
|
|
|
// Actions of a Decision.
|
|
const (
|
|
ActNone = "" // nothing to do
|
|
ActBan = "ban" // write (or would write)
|
|
ActSkip = "skip" // excluded
|
|
ActAlready = "already" // fail2ban already blocks it
|
|
ActActive = "active" // an advisory ban is still valid
|
|
)
|
|
|
|
// Input is everything Decide needs about one address.
|
|
type Input struct {
|
|
Now time.Time
|
|
Attacks int // counted attack events in the window
|
|
Top string // reason slug of the dominant attack in the window
|
|
SSHFails int // failed SSH logins in the window
|
|
SSHProbes int // ssh-probe events in the window (each counts half a failure)
|
|
Probes int // distinct (proto, port) probed in the window
|
|
Crit string // reason of a critical family hit just seen, "" if none
|
|
Crits int // critical events of the address in the window (a single hit is not enough)
|
|
Legit bool // a document 2xx from a browser within the last 24 h
|
|
Exclude string // Excluded() result or "crawler"
|
|
Banned bool // fail2ban already blocks it
|
|
Active bool // an advisory ban is still valid
|
|
Offences int
|
|
Known bool // the store's verdict is hostile (or suspicious with a high score)
|
|
SSH24 int // failed SSH logins in LongWindow
|
|
Web24 int // web scan/exploit events in LongWindow
|
|
Probe24 int // ssh-probe events in LongWindow
|
|
SSH7 int // failed SSH logins in WeekWindow
|
|
Probe7 int // ssh-probe events in WeekWindow
|
|
Web7 int // web scan/exploit events in WeekWindow
|
|
Neighbor bool // an attack event just seen, in a prefix with NeighborIPs banned addresses
|
|
}
|
|
|
|
// sshEff is failures plus half the probes.
|
|
func sshEff(fails, probes int) int { return fails + probes/2 }
|
|
|
|
// Params are the thresholds.
|
|
type Params struct{ Threshold int }
|
|
|
|
// Decision is the verdict of Decide.
|
|
type Decision struct {
|
|
Action string
|
|
Reason string
|
|
Score int
|
|
TTL time.Duration
|
|
Skip string
|
|
}
|
|
|
|
// Decide is the pure policy. It returns ActNone while nothing justifies a ban.
|
|
func Decide(in Input, p Params) Decision {
|
|
th, sshTh := p.Threshold, SSHThreshold
|
|
if th < 1 {
|
|
th = 5
|
|
}
|
|
if in.Legit { // a (forgeable) legitimate visit only relaxes the volumetric thresholds
|
|
th, sshTh = th*2, sshTh*2
|
|
}
|
|
var reason string
|
|
score := 0
|
|
switch {
|
|
case in.Crit != "" && in.Crits >= CritHits:
|
|
reason, score = in.Crit, 90
|
|
case sshEff(in.SSHFails, in.SSHProbes) >= sshTh:
|
|
n := sshEff(in.SSHFails, in.SSHProbes)
|
|
reason, score = ReasonSSHBrute, min(100, 60+(n-sshTh)*5)
|
|
case in.Probes >= PortScanPorts:
|
|
reason, score = ReasonPortScan, min(100, 60+(in.Probes-PortScanPorts)*3)
|
|
case in.Attacks >= th:
|
|
reason, score = in.Top, min(100, 55+(in.Attacks-th)*5)
|
|
if !ValidReason(reason) {
|
|
reason = ReasonScannerFlood
|
|
}
|
|
case in.Neighbor && !in.Legit:
|
|
reason, score = ReasonNeighbor, 70
|
|
case in.Known:
|
|
reason, score = ReasonKnownAttacker, 80
|
|
case sshEff(in.SSH24, in.Probe24) >= SSH24Threshold:
|
|
reason, score = ReasonSSHBrute, min(100, 60+(sshEff(in.SSH24, in.Probe24)-SSH24Threshold))
|
|
case in.Web24 >= Web24Threshold:
|
|
reason, score = ReasonScannerFlood, min(100, 55+(in.Web24-Web24Threshold)/2)
|
|
case sshEff(in.SSH7, in.Probe7) >= SSH7Threshold:
|
|
reason, score = ReasonSSHBrute, min(100, 60+(sshEff(in.SSH7, in.Probe7)-SSH7Threshold)/2)
|
|
case in.Web7 >= Web7Threshold && !in.Legit:
|
|
reason, score = ReasonScannerFlood, min(100, 55+(in.Web7-Web7Threshold)/2)
|
|
default:
|
|
return Decision{}
|
|
}
|
|
switch {
|
|
case in.Exclude != "":
|
|
return Decision{Action: ActSkip, Skip: in.Exclude}
|
|
case in.Banned:
|
|
return Decision{Action: ActAlready}
|
|
case in.Active:
|
|
return Decision{Action: ActActive}
|
|
}
|
|
return Decision{Action: ActBan, Reason: reason, Score: score, TTL: TTLFor(in.Offences)}
|
|
}
|
|
|
|
// reservedV4/V6 are special purpose ranges not covered by netip helpers.
|
|
var reserved = func() []netip.Prefix {
|
|
var out []netip.Prefix
|
|
for _, s := range []string{"0.0.0.0/8", "100.64.0.0/10", "192.0.0.0/24", "192.0.2.0/24", "198.18.0.0/15",
|
|
"198.51.100.0/24", "203.0.113.0/24", "240.0.0.0/4", "2001:db8::/32", "100::/64", "fc00::/7", "fe80::/10", "ff00::/8"} {
|
|
out = append(out, netip.MustParsePrefix(s))
|
|
}
|
|
return out
|
|
}()
|
|
|
|
// demoReservedPublic is a hidden switch ONLY for the documentation screenshots
|
|
// (scripts/screenshots.sh): the demo data uses reserved documentation ranges, which
|
|
// production policy rightly treats as private. Never set URUTAU_DEMO_RESERVED_PUBLIC
|
|
// outside the screenshot run.
|
|
var demoReservedPublic = os.Getenv("URUTAU_DEMO_RESERVED_PUBLIC") == "1"
|
|
|
|
func demoRanges(p netip.Prefix) bool {
|
|
switch p.String() {
|
|
case "198.18.0.0/15", "203.0.113.0/24", "198.51.100.0/24", "192.0.2.0/24", "2001:db8::/32":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Public reports whether a is a routable unicast address worth blocking.
|
|
func Public(a netip.Addr) bool {
|
|
a = a.Unmap()
|
|
if !a.IsValid() || a.Zone() != "" || a.IsUnspecified() || a.IsLoopback() || a.IsPrivate() || a.IsMulticast() ||
|
|
a.IsLinkLocalUnicast() || a.IsLinkLocalMulticast() || a.IsInterfaceLocalMulticast() {
|
|
return false
|
|
}
|
|
for _, p := range reserved {
|
|
if demoReservedPublic && demoRanges(p) {
|
|
continue
|
|
}
|
|
if p.Contains(a) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// Exclusions are the addresses that are never banned.
|
|
type Exclusions struct {
|
|
Trusted []netip.Prefix // --trusted and --ignore
|
|
Peers map[netip.Addr]bool // addresses of the peers
|
|
Infra map[netip.Addr]bool // hub, local interfaces (resolved at runtime)
|
|
}
|
|
|
|
var criticalReasons = func() map[string]bool {
|
|
m := map[string]bool{}
|
|
for _, r := range criticalFam {
|
|
m[r] = true
|
|
}
|
|
return m
|
|
}()
|
|
|
|
// Excluded returns the skip reason of a, or "".
|
|
func (x Exclusions) Excluded(a netip.Addr) string {
|
|
a = a.Unmap()
|
|
if !Public(a) {
|
|
return SkipPrivate
|
|
}
|
|
if x.Peers[a] {
|
|
return SkipPeer
|
|
}
|
|
for _, p := range x.Trusted {
|
|
if p.Contains(a) {
|
|
return SkipTrusted
|
|
}
|
|
}
|
|
if x.Infra[a] || IsInfrastructure(a) {
|
|
return SkipInfra
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// attackSlug is the reason slug of one attack event, and whether it is critical.
|
|
func attackSlug(ev core.Event, fam string) (slug string, crit bool) {
|
|
if ev.Cat == core.SSHFail || ev.Cat == core.SSHProbe {
|
|
return ReasonSSHBrute, false
|
|
}
|
|
if r, ok := criticalFam[fam]; ok {
|
|
return r, true
|
|
}
|
|
switch {
|
|
case fam == intel.FamWordPress:
|
|
return ReasonWPBrute, false
|
|
case ev.Cat == core.BruteForce:
|
|
return ReasonWebBrute, false
|
|
case ev.Cat == core.Exploit:
|
|
return ReasonExploitFlood, false
|
|
}
|
|
return ReasonScannerFlood, false
|
|
}
|
|
|
|
// CountsAsAttack reports whether an HTTP event is an attack the policy counts:
|
|
// an attack category that was not merely framed, and whose answer is not a
|
|
// client, application or proxy error.
|
|
func CountsAsAttack(ev core.Event) bool {
|
|
if ev.Source != "traefik" || ev.Framed {
|
|
return false
|
|
}
|
|
if ev.Cat != core.Scanner && ev.Cat != core.Exploit && ev.Cat != core.BruteForce {
|
|
return false
|
|
}
|
|
if ev.Status >= 400 {
|
|
c := core.ErrorClass(ev, false)
|
|
return c == core.ClassAttack || c == core.ClassBlocked
|
|
}
|
|
return true
|
|
}
|
|
|
|
// LegitVisit reports whether ev is a browser loading a page successfully.
|
|
func LegitVisit(ev core.Event) bool {
|
|
return ev.Source == "traefik" && ev.Cat == core.OK && ev.Status >= 200 && ev.Status < 300 &&
|
|
ev.FetchDest == "document" && !ev.E.Tool && !ev.E.Bot && !ev.Prefetch
|
|
}
|