feat: 1.4.0 second firewall, egress analysis and HTML report
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 28s
CI / Dependency audit (push) Successful in 16s
CI / Tests (Linux, race detector) (push) Successful in 1m3s
Release / Plan (reuse the published image?) (push) Successful in 4s
Release / ci (push) Skipped
Release / Build image (amd64) (push) Skipped
Release / Build image (arm64) (push) Skipped
Release / Publish tags (push) Successful in 13s
Release / Gitea release (push) Successful in 3s
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 28s
CI / Dependency audit (push) Successful in 16s
CI / Tests (Linux, race detector) (push) Successful in 1m3s
Release / Plan (reuse the published image?) (push) Successful in 4s
Release / ci (push) Skipped
Release / Build image (amd64) (push) Skipped
Release / Build image (arm64) (push) Skipped
Release / Publish tags (push) Successful in 13s
Release / Gitea release (push) Successful in 3s
Optional second firewall (URUTAU_ENFORCE off/observe/on) through the host fail2ban, outbound traffic analysis (beaconing, mining, spam, DNS bypass, unauthorised scans) with authorised scanner workloads, host kit in deploy/host and a printable HTML report. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3adaa007ec
commit
c33cca22e3
59 files changed
+7364
-24
No files matched your search
@@ -0,0 +1,280 @@
|
||||
// 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"
|
||||
)
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
// 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
|
||||
// CritHits is how many critical events of one address (in Window) justify a ban.
|
||||
CritHits = 2
|
||||
)
|
||||
|
||||
// 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"
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
// 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 in.SSHFails >= sshTh:
|
||||
reason, score = ReasonSSHBrute, min(100, 60+(in.SSHFails-sshTh)*5)
|
||||
case in.Attacks >= th:
|
||||
reason, score = in.Top, min(100, 55+(in.Attacks-th)*5)
|
||||
if !ValidReason(reason) {
|
||||
reason = ReasonScannerFlood
|
||||
}
|
||||
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 {
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user