Files
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

137 lines
3.0 KiB
Go

package intel
import (
"math"
"sort"
"time"
)
// Trend is the relative change from prev to cur ((cur-prev)/prev). With no
// previous activity it is 0 if cur is 0 and 1 (+100%) otherwise.
func Trend(cur, prev float64) float64 {
if prev == 0 {
if cur == 0 {
return 0
}
return 1
}
return (cur - prev) / prev
}
// Spike is an hour whose volume stands out from the previous 7 days.
type Spike struct {
At time.Time `json:"at"`
Value int `json:"value"`
Baseline float64 `json:"baseline"` // median of the previous 7 days
Threshold float64 `json:"threshold"` // median + 3*MAD
}
// Spike parameters.
const (
SpikeMin = 10
spikeWindow = 7 * 24 * time.Hour
spikeMinPrior = 24
maxSpikeInput = 24 * 90
)
func median(v []float64) float64 {
if len(v) == 0 {
return 0
}
s := append([]float64(nil), v...)
sort.Float64s(s)
m := len(s) / 2
if len(s)%2 == 1 {
return s[m]
}
return (s[m-1] + s[m]) / 2
}
// Spikes flags hours (hourly[i] at at[i], in chronological order) whose count
// exceeds median + 3*MAD of the preceding 7 days and is at least 10. An hour
// needs at least 24 samples in its window to be judged. Input is capped at
// 90 days.
func Spikes(hourly []int, at []time.Time) []Spike {
n := len(hourly)
if len(at) < n {
n = len(at)
}
if n > maxSpikeInput {
hourly, at, n = hourly[n-maxSpikeInput:n], at[n-maxSpikeInput:n], maxSpikeInput
}
var out []Spike
lo := 0
for i := 0; i < n; i++ {
for lo < i && at[i].Sub(at[lo]) > spikeWindow {
lo++
}
if i-lo < spikeMinPrior || hourly[i] < SpikeMin {
continue
}
w := make([]float64, 0, i-lo)
for j := lo; j < i; j++ {
w = append(w, float64(hourly[j]))
}
med := median(w)
dev := make([]float64, len(w))
for k, x := range w {
dev[k] = math.Abs(x - med)
}
thr := med + 3*median(dev)
if float64(hourly[i]) > thr {
out = append(out, Spike{At: at[i], Value: hourly[i], Baseline: med, Threshold: thr})
}
}
return out
}
// MTTB returns the median and 90th percentile (nearest rank) of the time from
// first sighting to ban, given [first, banned] pairs. Negative spans are
// ignored.
func MTTB(pairs [][2]time.Time) (med, p90 time.Duration) {
d := make([]float64, 0, len(pairs))
for _, p := range pairs {
if x := p[1].Sub(p[0]); x >= 0 {
d = append(d, float64(x))
}
}
if len(d) == 0 {
return 0, 0
}
sort.Float64s(d)
med = time.Duration(median(d))
idx := int(math.Ceil(0.9*float64(len(d)))) - 1
if idx < 0 {
idx = 0
}
return med, time.Duration(d[idx])
}
// Concentration is the share (0..1) of the total held by the top largest
// entries of counts.
func Concentration(counts map[string]int, top int) float64 {
if top <= 0 {
return 0
}
v := make([]int, 0, len(counts))
total := 0
for _, c := range counts {
if c > 0 {
v = append(v, c)
total += c
}
}
if total == 0 {
return 0
}
sort.Sort(sort.Reverse(sort.IntSlice(v)))
if top > len(v) {
top = len(v)
}
s := 0
for _, c := range v[:top] {
s += c
}
return float64(s) / float64(total)
}