Files
urutau/internal/intel/cadence.go
T
Lucas RayanandClaude Opus 5.5 b7462c14fa
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
feat: 1.2.0 attack intelligence, replay, 30-day history and new interface
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>
2026-11-14 21:45:59 -03:00

178 lines
3.8 KiB
Go

package intel
import (
"math"
"sort"
"time"
)
// Cadence summarises the rhythm of a source's requests.
type Cadence struct {
N int `json:"n"`
MedianS float64 `json:"median_s"`
P10S float64 `json:"p10_s"`
P90S float64 `json:"p90_s"`
CV float64 `json:"cv"`
BurstMax60 int `json:"burst_max_60"`
Verdict string `json:"verdict"`
}
// Cadence verdicts.
const (
VerdictAutomated = "automated"
VerdictHuman = "human-like"
VerdictMixed = "mixed"
VerdictInsufficient = "insufficient"
)
func verdict(n int, median, cv float64) string {
switch {
case n < 3:
return VerdictInsufficient
case median < 2 || (cv < 0.3 && n >= 10):
return VerdictAutomated
case median > 5 && cv > 1:
return VerdictHuman
}
return VerdictMixed
}
const (
cadBuckets = 16
burstCap = 4096
)
// CadenceAcc accumulates request times online in constant memory: Welford
// for mean/variance of the inter-arrival times, a 16 bucket log2 histogram
// for approximate percentiles and a bounded window for the 60 s burst peak.
// Bucket 0 holds gaps under 1 s; bucket b>=1 holds [2^(b-1), 2^b) seconds.
type CadenceAcc struct {
n int
last time.Time
cnt int // number of intervals
mean float64
m2 float64
hist [cadBuckets]int
win []time.Time
burst int
}
func bucketOf(d float64) int {
if d < 1 {
return 0
}
b := 1 + int(math.Floor(math.Log2(d)))
if b >= cadBuckets {
b = cadBuckets - 1
}
return b
}
// Add records one request at t.
func (a *CadenceAcc) Add(t time.Time) {
if a.n > 0 {
d := math.Abs(t.Sub(a.last).Seconds())
a.cnt++
delta := d - a.mean
a.mean += delta / float64(a.cnt)
a.m2 += delta * (d - a.mean)
a.hist[bucketOf(d)]++
}
if a.n == 0 || t.After(a.last) {
a.last = t
}
a.n++
cut := t.Add(-60 * time.Second)
i := 0
for i < len(a.win) && !a.win[i].After(cut) {
i++
}
a.win = append(a.win[i:], t)
if len(a.win) > burstCap {
a.win = a.win[len(a.win)-burstCap:]
}
if len(a.win) > a.burst {
a.burst = len(a.win)
}
}
func (a *CadenceAcc) pct(p float64) float64 {
if a.cnt == 0 {
return 0
}
target := p * float64(a.cnt)
cum := 0.0
for b := 0; b < cadBuckets; b++ {
c := float64(a.hist[b])
if c > 0 && cum+c >= target {
lo, hi := 0.0, 1.0
if b > 0 {
lo = math.Exp2(float64(b - 1))
hi = math.Exp2(float64(b))
}
return lo + (hi-lo)*(target-cum)/c
}
cum += c
}
return math.Exp2(cadBuckets - 1)
}
// Result returns the current cadence summary.
func (a *CadenceAcc) Result() Cadence {
c := Cadence{N: a.n, BurstMax60: a.burst}
if a.cnt > 0 {
c.MedianS, c.P10S, c.P90S = a.pct(.5), a.pct(.1), a.pct(.9)
if a.mean > 0 {
c.CV = math.Sqrt(a.m2/float64(a.cnt)) / a.mean
}
}
c.Verdict = verdict(c.N, c.MedianS, c.CV)
return c
}
func quantile(sorted []float64, p float64) float64 {
if len(sorted) == 0 {
return 0
}
pos := p * float64(len(sorted)-1)
lo := int(math.Floor(pos))
hi := int(math.Ceil(pos))
return sorted[lo] + (sorted[hi]-sorted[lo])*(pos-float64(lo))
}
// CadenceFrom computes the exact cadence of a set of request times (any order).
func CadenceFrom(ts []time.Time) Cadence {
v := append([]time.Time(nil), ts...)
sort.Slice(v, func(i, j int) bool { return v[i].Before(v[j]) })
c := Cadence{N: len(v)}
if len(v) > 1 {
d := make([]float64, len(v)-1)
var sum float64
for i := 1; i < len(v); i++ {
d[i-1] = v[i].Sub(v[i-1]).Seconds()
sum += d[i-1]
}
mean := sum / float64(len(d))
var ss float64
for _, x := range d {
ss += (x - mean) * (x - mean)
}
if mean > 0 {
c.CV = math.Sqrt(ss/float64(len(d))) / mean
}
sort.Float64s(d)
c.MedianS, c.P10S, c.P90S = quantile(d, .5), quantile(d, .1), quantile(d, .9)
}
j := 0
for i := range v {
for v[i].Sub(v[j]) >= 60*time.Second {
j++
}
if i-j+1 > c.BurstMax60 {
c.BurstMax60 = i - j + 1
}
}
c.Verdict = verdict(c.N, c.MedianS, c.CV)
return c
}