feat: 1.2.0 attack intelligence, replay, 30-day history and new interface
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 27s
CI / Dependency audit (push) Successful in 18s
CI / Tests (Linux, race detector) (push) Successful in 58s
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 5s

Intelligence: attack families mapped to MITRE ATT&CK, network type from
ASN/rDNS without external APIs, request cadence, sessions with
recon/probe/exploit/access/banned stages, campaigns across IPs, verdict
with reasons, recommended actions, exposure detection (2xx with a body
on sensitive paths), STIX 2.1 export, FCrDNS for search engine bots,
optional Tor exit list.

Data: 30-day retention, twelve ranges plus custom from/to, an on-disk
event log (daily files, hourly gzip members, size cap, corruption and
disk-full tolerant), /events, /period, /metrics with stable per-hour
metrics, an extended /ip/{ip}, and peer /period merged on the hub.

Interface: layout system with fixed card sizes, range picker in the
URL, IP analysis page, Replay page, Geography down to state and city
(Natural Earth admin-1), Live page redesigned with stable metrics and a
reusable attack map. Demo data and documentation screenshots.

Security: bounded and deadline-limited period computations, linear
campaign clustering, singleflight caches, sanitized peer studies, event
query budgets, gzip bomb guard.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lucas RayanandClaude Opus 5.5 committed 2026-11-14 21:45:59 -03:00
1 parent 8f1ac462d1
commit b7462c14fa
325 files changed
+19117 -1307

No files matched your search

+418 -14
View File
@@ -6,15 +6,18 @@ import (
"sort"
"strings"
"sync"
"sync/atomic"
"time"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/evlog"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/intel"
)
const (
slots = 7 * 24 * 60 // minute buckets
hourSlots = 7 * 24
topCap = 64 // entries kept per top-list per hour
slots = 49 * 60 // minute buckets (48 h plus one hour of margin)
hourSlots = 30 * 24 // hour buckets, 30 days
topCap = 64 // entries kept per top-list per hour
maxSample = 4
maxUAs = 2
maxDomain = 3
@@ -59,11 +62,79 @@ type IPRec struct {
StrongAt int64
// FramedScore: the part of Score that came from framed requests (bounded by framedCap).
FramedScore float64
// Per-IP detail, all small and capped (about 1 KB per IP at the caps). The
// rest of the detail view is derived from the event log on demand.
ScoreBy [core.NumCats]float32 // score contribution per category
StrongWhy uint8 // whyTool | whySignal | whyBan
FirstMal int64 // first malicious event
TTB int64 // seconds from the first malicious event to the first ban
Users []UserN // SSH users tried (<= maxUsers)
Status [6]uint32 // answer classes: none, 2xx, 3xx, 4xx, 5xx, other
Fams []FamN // attack families (<= maxFams)
Techs []TechN // techniques (<= maxTechs)
Expo []Expo // successful answers to attack requests (<= maxExpo)
CadAcc []float64 // reserved for the cadence accumulator (<= 16 values)
DayBase int64 // epoch day of bit 0 of Days
Days uint32 // active days: bit k = active on DayBase-k
DayEst bool // DayCats was estimated from the totals (record saved before the counters existed)
DayCats [][core.NumCats]uint16 // events per day and category, DayCats[k] = DayBase-k (<= 30 days)
ListedSince int64 // when the address started to satisfy the blocklist rule (0: not listed)
}
// Reasons an address is strong (IPRec.StrongWhy).
const (
whyTool = 1
whySignal = 2
whyBan = 4
)
const (
maxUsers = 32
maxFams = 16
maxTechs = 16
maxExpo = 8
maxExamp = 3
)
// UserN is one SSH user tried by an address.
type UserN struct {
U string
N uint32
First int64
}
// FamN counts one attack family of an address.
type FamN struct {
Fam string
N uint32
S [4]uint32 // 2xx 3xx 4xx 5xx
Ex []string
}
// TechN counts one technique of an address.
type TechN struct {
ID string
N uint32
First, Last int64
}
// Expo is a successful answer to an attack request.
type Expo struct {
T int64
Host string
Domain string
Path string
Status int
Bytes int64
Fam string
}
type hourSlot struct {
Hour int64
Paths, Domains, UAs, Users, Countries map[string]uint32
C [core.NumCats]uint32 // events per category in the hour
Mon uint32 // monitoring requests in the hour
}
// FeedEvent is one entry of the live feed.
@@ -82,6 +153,15 @@ type FeedEvent struct {
Unban bool `json:"unban,omitempty"`
Forged bool `json:"forged,omitempty"`
CC string `json:"cc,omitempty"`
UA string `json:"ua,omitempty"`
ID uint64 `json:"id,omitempty"`
// Detail for the replay and the investigation views.
Fam string `json:"fam,omitempty"`
Tech []string `json:"tech,omitempty"`
Marker string `json:"marker,omitempty"`
Bytes int64 `json:"bytes,omitempty"`
DurMS int64 `json:"dur_ms,omitempty"`
Flags string `json:"flags,omitempty"` // S strong, W weak, G forged, F framed, X soft
}
// SetAttribution sets the credit line shown in the dashboard footer (geo data licence).
@@ -93,6 +173,53 @@ func (s *Store) SetEnricher(e core.Enricher) { s.enr = e }
// RDNS resolves addresses asynchronously; Lookup never blocks.
type RDNS interface{ Lookup(ip string) string }
// FCrDNSVerifier is implemented by resolvers that can confirm a crawler.
type FCrDNSVerifier interface {
FCrDNS(ip string, suffixes []string) bool
}
var crawlerSuffixes = []struct {
ua string
suffixes []string
}{
{"googlebot", []string{"googlebot.com", "google.com"}},
{"bingbot", []string{"search.msn.com"}},
}
// FCrDNS verifies an address that claims, through one of its User-Agents, to
// be Googlebot or Bingbot. It returns nil when no UA makes the claim (or the
// address cannot be checked), true or false otherwise.
func (s *Store) FCrDNS(ip string, uas []string) *bool {
v, ok := s.rdns.(FCrDNSVerifier)
if !ok || s.rdns == nil {
return nil
}
if a, err := netip.ParseAddr(ip); err != nil || a.Zone() != "" {
return nil // an IPv6 /64 key is not an address
}
var want []string
for _, ua := range uas {
l := strings.ToLower(ua)
for _, c := range crawlerSuffixes {
if strings.Contains(l, c.ua) {
want = append(want, c.suffixes...)
}
}
}
if len(want) == 0 {
return nil
}
res := v.FCrDNS(ip, want)
return &res
}
// IsAdminUser reports whether u is listed in --admin-users.
func (s *Store) IsAdminUser(u string) bool {
s.mu.RLock()
defer s.mu.RUnlock()
return s.isAdminUser(u)
}
// Store is the whole in-memory state of one instance.
type Store struct {
cfg Config
@@ -121,8 +248,22 @@ type Store struct {
cacheMu sync.Mutex
cache map[string]cachedSummary
ids atomic.Uint64 // event ids, increasing
elog *evlog.Log
class Classifier
ipx ipxCache
study studyCache
sizes map[string]map[int64]uint32 // per domain: typical 404 / SPA body sizes
}
// Classifier fills the attack family, the techniques and the marker of an
// event (wired to internal/intel); nil or empty results are fine.
type Classifier func(core.Event) (fam string, tech []string, marker string)
// SetClassifier installs the classifier.
func (s *Store) SetClassifier(c Classifier) { s.class = c }
type cachedSummary struct {
at time.Time
s Summary
@@ -130,14 +271,17 @@ type cachedSummary struct {
// New creates an empty store.
func New(cfg Config) *Store {
if cfg.Retention <= 0 || cfg.Retention > 7*24*time.Hour {
cfg.Retention = 7 * 24 * time.Hour
if cfg.Retention <= 0 {
cfg.Retention = 7 * 24 * time.Hour // zero value; the daemon always passes --retention
}
if cfg.Retention > DefaultRetention {
cfg.Retention = DefaultRetention
}
if cfg.MaxIPs <= 0 {
cfg.MaxIPs = 20000
}
return &Store{cfg: cfg, now: time.Now, ips: make(map[string]*IPRec), subs: map[chan FeedEvent]struct{}{},
cache: map[string]cachedSummary{}, started: time.Now()}
cache: map[string]cachedSummary{}, started: time.Now(), class: IntelClassifier}
}
// Name of this instance.
@@ -255,6 +399,11 @@ func (s *Store) Ingest(ev core.Event) {
}
scored := !(ev.Cat == core.Ban && (ev.Unban || ev.Restore))
malHTTP := ev.Source == "traefik" && (ev.Cat == core.Scanner || ev.Cat == core.Exploit || ev.Cat == core.BruteForce)
var fam, marker string
var tech []string
if s.class != nil && (ev.Cat.IsMalicious() || ev.Cat == core.NotFound || ev.Cat == core.OK) {
fam, tech, marker = s.class(ev)
}
s.mu.Lock()
if scored {
@@ -272,6 +421,7 @@ func (s *Store) Ingest(ev core.Event) {
if hs.Hour != h {
*hs = hourSlot{Hour: h}
}
hs.C[ci]++
switch {
case malHTTP:
bump(&hs.Paths, ev.Path)
@@ -299,7 +449,9 @@ func (s *Store) Ingest(ev core.Event) {
r = &IPRec{First: ts, Last: ts}
s.ips[key] = r
}
suspicious := false // the address was already suspicious or hostile before this event
if r != nil {
suspicious = core.LevelOf(r.Score, r.Strong || attackKinds(r.Counts) >= 2, r.adminAt(now.Unix())) != core.LevelLow
if ts < r.First {
r.First = ts
}
@@ -320,20 +472,30 @@ func (s *Store) Ingest(ev core.Event) {
r.FramedScore += w
}
r.Score += w
r.ScoreBy[ci] += float32(w)
if framed {
// never a strong signal: the headers prove nothing either way
} else if ev.E.Tool && ev.Cat != core.OK && ev.Cat != core.SSHOK && ev.Cat != core.Ban {
r.Score += core.ToolBonus
r.ScoreBy[ci] += core.ToolBonus
r.Strong = true
r.StrongWhy |= whyTool
r.StrongAt = max(r.StrongAt, ts)
}
if (ev.Strong && !framed) || ev.Cat == core.Ban {
r.Strong = true
if ev.Cat == core.Ban {
r.StrongWhy |= whyBan
} else {
r.StrongWhy |= whySignal
}
r.StrongAt = max(r.StrongAt, ts)
}
if ev.Cat == core.SSHOK && ts > r.SSHOK && s.isAdminUser(ev.User) {
r.SSHOK = ts
}
s.detailLocked(r, ev, ts, fam, tech, suspicious)
updateListed(r, ts, now.Unix())
}
if malHTTP {
r.Domains = addUnique(r.Domains, clipS(ev.Domain, maxText), maxDomain)
@@ -360,23 +522,259 @@ func (s *Store) Ingest(ev core.Event) {
}
var fe FeedEvent
feed := ev.Cat != core.OK && ev.Cat != core.NotFound && !ev.Restore
if feed {
fe = FeedEvent{T: ts, IP: ev.IP, Cat: string(ev.Cat), Source: ev.Source, Host: s.cfg.Name, Domain: ev.Domain,
Method: ev.Method, Path: ev.Path, User: ev.User, Jail: ev.Jail, Status: ev.Status, Unban: ev.Unban, Forged: ev.Forged, CC: ev.E.CC}
if len(s.recent) < FeedSize {
s.recent = append(s.recent, fe)
} else {
s.recent[s.rhead] = fe
s.rhead = (s.rhead + 1) % FeedSize
// The event log also keeps a 404 of an attack tool and any answer to an
// address that is already suspicious or hostile (to show what it got);
// never an address that looks like a visitor.
record := feed || (!ev.Restore && (ev.Cat == core.NotFound && ev.E.Tool || suspicious && (ev.Cat == core.OK || ev.Cat == core.NotFound)))
if record {
fe = s.feedOf(ev, ts, fam, tech, marker)
if feed {
if len(s.recent) < FeedSize {
s.recent = append(s.recent, fe)
} else {
s.recent[s.rhead] = fe
s.rhead = (s.rhead + 1) % FeedSize
}
}
}
s.noteSizeLocked(ev, fam) // after the decision: an answer never defines its own "typical" size
s.mu.Unlock()
if record {
s.logEvent(fe)
}
if feed && now.Unix()-ts < 300 {
s.broadcast(fe)
}
}
func (s *Store) feedOf(ev core.Event, ts int64, fam string, tech []string, marker string) FeedEvent {
flags := ""
for _, f := range []struct {
on bool
c string
}{{ev.Strong, "S"}, {ev.Weak, "W"}, {ev.Forged, "G"}, {ev.Framed, "F"}, {ev.Soft, "X"}} {
if f.on {
flags += f.c
}
}
return FeedEvent{T: ts, IP: ev.IP, Cat: string(ev.Cat), Source: ev.Source, Host: s.cfg.Name, Domain: ev.Domain,
Method: ev.Method, Path: ev.Path, User: ev.User, Jail: ev.Jail, Status: ev.Status, Unban: ev.Unban, Forged: ev.Forged, CC: ev.E.CC,
UA: clipS(ev.UA, 200), ID: s.ids.Add(1), Fam: clipS(fam, 40), Tech: tech, Marker: clipS(marker, 80),
Bytes: ev.Bytes, DurMS: ev.DurationNS / 1e6, Flags: flags}
}
// listedRule is the blocklist rule: score of 50, a strong signal and not an admin.
func listedRule(r *IPRec, now int64) bool {
return r.Score >= core.HostileScore && (r.Strong || attackKinds(r.Counts) >= 2) && !r.adminAt(now)
}
func updateListed(r *IPRec, ts, now int64) {
switch ok := listedRule(r, now); {
case ok && r.ListedSince == 0:
r.ListedSince = ts
case !ok && r.ListedSince != 0:
r.ListedSince = 0
}
}
func statusClass(st int) int {
switch {
case st == 0:
return 0
case st >= 200 && st < 300:
return 1
case st >= 300 && st < 400:
return 2
case st >= 400 && st < 500:
return 3
case st >= 500 && st < 600:
return 4
}
return 5
}
// detailLocked updates the capped per-IP detail of an address.
func (s *Store) detailLocked(r *IPRec, ev core.Event, ts int64, fam string, tech []string, wasSusp bool) {
if ev.Cat.IsMalicious() && (r.FirstMal == 0 || ts < r.FirstMal) {
r.FirstMal = ts
}
if ev.Cat == core.Ban && !ev.Unban && r.FirstMal > 0 && r.TTB == 0 && ts >= r.FirstMal {
r.TTB = ts - r.FirstMal
}
touchDay(r, ts, ev.Cat.Index())
if ev.Source == "traefik" {
r.Status[statusClass(ev.Status)]++
}
if ev.Cat == core.SSHFail && ev.User != "" {
u := clipS(ev.User, 64)
found := false
for i := range r.Users {
if r.Users[i].U == u {
r.Users[i].N++
found = true
break
}
}
if !found && len(r.Users) < maxUsers {
r.Users = append(r.Users, UserN{U: u, N: 1, First: ts})
}
}
if fam != "" && ev.Source == "traefik" {
var f *FamN
for i := range r.Fams {
if r.Fams[i].Fam == fam {
f = &r.Fams[i]
break
}
}
if f == nil && len(r.Fams) < maxFams {
r.Fams = append(r.Fams, FamN{Fam: fam})
f = &r.Fams[len(r.Fams)-1]
}
if f != nil {
f.N++
if c := statusClass(ev.Status); c >= 1 && c <= 4 {
f.S[c-1]++
}
if len(f.Ex) < maxExamp {
f.Ex = addUnique(f.Ex, clipS(ev.Method+" "+ev.Path, maxText), maxExamp)
}
}
}
for _, id := range tech {
var t *TechN
for i := range r.Techs {
if r.Techs[i].ID == id {
t = &r.Techs[i]
break
}
}
if t == nil && len(r.Techs) < maxTechs {
r.Techs = append(r.Techs, TechN{ID: id, First: ts})
t = &r.Techs[len(r.Techs)-1]
}
if t != nil {
t.N++
if ts < t.First {
t.First = ts
}
t.Last = max(t.Last, ts)
}
}
// exposure: a successful answer (2xx, with a body that is not the typical
// 404 or SPA page of the domain) to a request for a sensitive family
if ev.Source == "traefik" && s.exposedLocked(ev, wasSusp) && len(r.Expo) < maxExpo {
r.Expo = append(r.Expo, Expo{T: ts, Host: s.cfg.Name, Domain: clipS(ev.Domain, 100), Path: clipS(ev.Path, 160),
Status: ev.Status, Bytes: ev.Bytes, Fam: intel.PathFamily(ev.Path)})
}
}
// sensitiveFams are the families whose 2xx answer is an exposure. The public
// WordPress login page and robots.txt are not.
var sensitiveFams = map[string]bool{
intel.FamEnvCreds: true, intel.FamVCS: true, intel.FamBackup: true,
intel.FamActuator: true, intel.FamPhpMyAdmin: true, intel.FamAdminPanel: true,
}
// exposedLocked applies the exposure rule. Admin panels and phpMyAdmin pages
// are public by design on some sites, so they also need a strong sign.
func (s *Store) exposedLocked(ev core.Event, wasSusp bool) bool {
if ev.Status < 200 || ev.Status > 299 || ev.Bytes <= 0 {
return false
}
fam := intel.PathFamily(ev.Path)
if !sensitiveFams[fam] {
return false
}
if (fam == intel.FamAdminPanel || fam == intel.FamPhpMyAdmin) && !(ev.Strong || ev.E.Tool || wasSusp) {
return false
}
if t := s.sizes[normDomain(ev.Domain)]; t != nil && t[ev.Bytes] >= 2 {
return false // the same body the domain serves for a missing page or for its SPA
}
return true
}
// noteSizeLocked remembers the body sizes of 404 answers and of ordinary 2xx
// pages per domain (at most 32 domains and 8 sizes each): the typical size of
// a "not found" page or of a SPA shell.
func (s *Store) noteSizeLocked(ev core.Event, fam string) {
if ev.Source != "traefik" || ev.Bytes <= 0 || ev.Domain == "" {
return
}
if ev.Status != 404 && !(ev.Status >= 200 && ev.Status < 300 && fam == "" && intel.PathFamily(ev.Path) == "") {
return
}
d := normDomain(ev.Domain)
if s.sizes == nil {
s.sizes = map[string]map[int64]uint32{}
}
m := s.sizes[d]
if m == nil {
if len(s.sizes) >= 32 {
return
}
m = map[int64]uint32{}
s.sizes[d] = m
}
if _, ok := m[ev.Bytes]; !ok && len(m) >= 8 {
var minK int64
var minV uint32 = ^uint32(0)
for k, v := range m {
if v < minV {
minK, minV = k, v
}
}
delete(m, minK)
}
m[ev.Bytes]++
}
// touchDay counts an event on its day: the 30 day bitmap of active days and
// the per-day per-category counters (the coarse timeline of an address, kept
// for the part of its history the event registry does not cover).
func touchDay(r *IPRec, ts int64, ci int) {
day := ts / 86400
switch {
case r.DayBase == 0:
r.DayBase = day
case day > r.DayBase:
d := int(day - r.DayBase)
if d >= 32 {
r.Days, r.DayCats = 0, nil
} else {
r.Days = r.Days << uint(d) & (1<<30 - 1)
if len(r.DayCats) > 0 {
n := min(len(r.DayCats)+d, 30)
nc := make([][core.NumCats]uint16, n)
for k := range r.DayCats {
if k+d < n {
nc[k+d] = r.DayCats[k]
}
}
r.DayCats = nc
}
}
r.DayBase = day
}
k := int(r.DayBase - day)
if k < 0 || k >= 30 {
return
}
r.Days |= 1 << uint(k)
for len(r.DayCats) <= k {
r.DayCats = append(r.DayCats, [core.NumCats]uint16{})
}
if c := &r.DayCats[k][ci]; *c < 65535 {
*c++
}
}
func malHTTPCat(c core.Category) bool {
return c == core.Scanner || c == core.Exploit || c == core.BruteForce
}
// isAdminUser: without an explicit --admin-users list nobody is immune (a
// local user can forge an sshd "Accepted" line with logger).
func (s *Store) isAdminUser(u string) bool {
@@ -555,10 +953,16 @@ func (s *Store) Monitoring(t time.Time) {
}
m := ts / 60
i := int(m % slots)
h := ts / 3600
s.mu.Lock()
if s.monSt[i] != m {
s.monSt[i], s.monN[i] = m, 0
}
s.monN[i]++
hs := &s.hours[int(h%hourSlots)]
if hs.Hour != h {
*hs = hourSlot{Hour: h}
}
hs.Mon++
s.mu.Unlock()
}