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>
217 lines
6.5 KiB
Go
217 lines
6.5 KiB
Go
package store
|
|
|
|
import (
|
|
"sort"
|
|
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
|
|
)
|
|
|
|
func unionStrings(a, b []string, max int) []string {
|
|
for _, v := range b {
|
|
a = addUnique(a, v, max)
|
|
}
|
|
return a
|
|
}
|
|
|
|
// MergeIPs merges IP lists by address: scores and counts add up, hosts are
|
|
// united, the first/last seen widen, a ban in any host marks the IP banned.
|
|
func MergeIPs(lists ...[]IPSummary) []IPSummary {
|
|
idx := map[string]*IPSummary{}
|
|
var order []string
|
|
for _, l := range lists {
|
|
for _, x := range l {
|
|
cur, ok := idx[x.IP]
|
|
if !ok {
|
|
c := x
|
|
c.Counts = make(map[string]uint32, len(x.Counts))
|
|
for k, v := range x.Counts {
|
|
c.Counts[k] = v
|
|
}
|
|
c.Hosts = append([]string(nil), x.Hosts...)
|
|
c.Domains = append([]string(nil), x.Domains...)
|
|
c.Samples = append([]string(nil), x.Samples...)
|
|
c.UAs = append([]string(nil), x.UAs...)
|
|
c.Jails = append([]string(nil), x.Jails...)
|
|
c.UALabels = append([]string(nil), x.UALabels...)
|
|
idx[x.IP] = &c
|
|
order = append(order, x.IP)
|
|
continue
|
|
}
|
|
cur.Score += x.Score
|
|
for k, v := range x.Counts {
|
|
cur.Counts[k] += v
|
|
}
|
|
cur.Hosts = unionStrings(cur.Hosts, x.Hosts, 16)
|
|
cur.Domains = unionStrings(cur.Domains, x.Domains, 8)
|
|
cur.Samples = unionStrings(cur.Samples, x.Samples, 10)
|
|
cur.UAs = unionStrings(cur.UAs, x.UAs, 5)
|
|
cur.Jails = unionStrings(cur.Jails, x.Jails, 12)
|
|
cur.UALabels = unionStrings(cur.UALabels, x.UALabels, 6)
|
|
if cur.CC == "" {
|
|
cur.CC, cur.Country, cur.City, cur.ASN, cur.ASOrg = x.CC, x.Country, x.City, x.ASN, x.ASOrg
|
|
}
|
|
if x.First != 0 && (cur.First == 0 || x.First < cur.First) {
|
|
cur.First = x.First
|
|
}
|
|
if x.Last > cur.Last {
|
|
cur.Last = x.Last
|
|
}
|
|
cur.Banned = cur.Banned || x.Banned
|
|
cur.Strong, cur.Admin = cur.Strong || x.Strong, cur.Admin || x.Admin
|
|
if cur.RDNS == "" {
|
|
cur.RDNS = x.RDNS
|
|
}
|
|
}
|
|
}
|
|
out := make([]IPSummary, 0, len(order))
|
|
for _, ip := range order {
|
|
c := idx[ip]
|
|
c.Score = round1(c.Score)
|
|
c.Level = core.LevelOf(c.Score, c.Strong, c.Admin)
|
|
sort.Strings(c.Hosts)
|
|
out = append(out, *c)
|
|
}
|
|
SortIPs(out, "score", true)
|
|
return out
|
|
}
|
|
|
|
func mergeCounts(n int, lists ...[]Count) []Count {
|
|
m := map[string]uint32{}
|
|
labels := map[string]string{}
|
|
for _, l := range lists {
|
|
for _, c := range l {
|
|
m[c.Key] += c.N
|
|
if c.Label != "" {
|
|
labels[c.Key] = c.Label
|
|
}
|
|
}
|
|
}
|
|
out := topN(m, n)
|
|
for i := range out {
|
|
out[i].Label = labels[out[i].Key]
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Merge combines summaries of the same range into one. The timeline grids are
|
|
// aligned by timestamp, so a peer whose clock is a few seconds off still lines up.
|
|
func Merge(host string, parts []Summary) Summary {
|
|
if len(parts) == 0 {
|
|
return Summary{Host: host}
|
|
}
|
|
base := parts[0]
|
|
out := Summary{Attribution: base.Attribution, Version: base.Version, Host: host, Range: base.Range, Generated: base.Generated,
|
|
Timeline: Timeline{Start: base.Timeline.Start, Step: base.Timeline.Step, Cats: base.Timeline.Cats,
|
|
Series: map[string][]uint32{}},
|
|
Totals: Totals{Counts: map[string]uint64{}}}
|
|
nb := 0
|
|
for _, c := range base.Timeline.Cats {
|
|
nb = len(base.Timeline.Series[c])
|
|
out.Timeline.Series[c] = make([]uint32, nb)
|
|
}
|
|
var ipLists [][]IPSummary
|
|
var paths, doms, uas, users, ctry [][]Count
|
|
var recent []FeedEvent
|
|
var rates []Rates
|
|
for _, p := range parts {
|
|
rates = append(rates, p.Rates)
|
|
if p.Generated > out.Generated {
|
|
out.Generated = p.Generated
|
|
}
|
|
out.Hosts = append(out.Hosts, p.Hosts...)
|
|
out.Sources = append(out.Sources, p.Sources...)
|
|
out.Totals.Requests += p.Totals.Requests
|
|
out.Totals.Malicious += p.Totals.Malicious
|
|
out.Totals.ActiveBans += p.Totals.ActiveBans
|
|
out.Totals.Monitoring += p.Totals.Monitoring
|
|
for k, v := range p.Totals.Counts {
|
|
out.Totals.Counts[k] += v
|
|
}
|
|
if p.Timeline.Step == out.Timeline.Step {
|
|
shift := (p.Timeline.Start - out.Timeline.Start) / out.Timeline.Step
|
|
for _, c := range out.Timeline.Cats {
|
|
src := p.Timeline.Series[c]
|
|
dst := out.Timeline.Series[c]
|
|
for i, v := range src {
|
|
j := int64(i) + shift
|
|
if j >= 0 && j < int64(len(dst)) {
|
|
dst[j] += v
|
|
}
|
|
}
|
|
}
|
|
}
|
|
ipLists = append(ipLists, p.IPs)
|
|
paths = append(paths, p.TopPaths)
|
|
doms = append(doms, p.TopDomains)
|
|
uas = append(uas, p.TopUAs)
|
|
users = append(users, p.TopUsers)
|
|
ctry = append(ctry, p.Countries)
|
|
recent = append(recent, p.Recent...)
|
|
}
|
|
out.IPs = MergeIPs(ipLists...)
|
|
// Peers only ship their top IPs, so the merged list can undercount; never
|
|
// report less than the largest single instance, and exact for one part.
|
|
hostile, susp := 0, 0
|
|
for _, ip := range out.IPs {
|
|
switch ip.Level {
|
|
case core.LevelHostile:
|
|
hostile++
|
|
case core.LevelSuspicious:
|
|
susp++
|
|
}
|
|
}
|
|
out.Totals.HostileIPs, out.Totals.SuspiciousIPs, out.Totals.UniqueIPs = hostile, susp, len(out.IPs)
|
|
for _, p := range parts {
|
|
if p.Totals.HostileIPs > out.Totals.HostileIPs {
|
|
out.Totals.HostileIPs = p.Totals.HostileIPs
|
|
}
|
|
if p.Totals.SuspiciousIPs > out.Totals.SuspiciousIPs {
|
|
out.Totals.SuspiciousIPs = p.Totals.SuspiciousIPs
|
|
}
|
|
if p.Totals.UniqueIPs > out.Totals.UniqueIPs {
|
|
out.Totals.UniqueIPs = p.Totals.UniqueIPs
|
|
}
|
|
}
|
|
out.Rates = mergeRates(rates, out.Totals.Malicious, out.Timeline, hostile)
|
|
for i, p := range parts {
|
|
_ = i
|
|
out.Rates.ActiveAttackers = max(out.Rates.ActiveAttackers, p.Rates.ActiveAttackers)
|
|
out.Rates.NewAttackers = max(out.Rates.NewAttackers, p.Rates.NewAttackers)
|
|
}
|
|
if len(out.IPs) > summaryIPs {
|
|
out.IPs = out.IPs[:summaryIPs]
|
|
}
|
|
out.TopPaths, out.TopDomains = mergeCounts(topKeep, paths...), mergeCounts(topKeep, doms...)
|
|
out.TopUAs, out.TopUsers = mergeCounts(topKeep, uas...), mergeCounts(topKeep, users...)
|
|
out.Countries = mergeCounts(120, ctry...)
|
|
sort.SliceStable(recent, func(i, j int) bool { return recent[i].T > recent[j].T })
|
|
if len(recent) > 60 {
|
|
recent = recent[:60]
|
|
}
|
|
out.Recent = recent
|
|
return out
|
|
}
|
|
|
|
// mergeRates adds the per-host rates up. Attacker counts cannot be added
|
|
// exactly (the same address can hit several hosts) so the caller takes the
|
|
// larger of the merged list and the busiest host.
|
|
func mergeRates(parts []Rates, malicious uint64, tl Timeline, _ int) Rates {
|
|
var out Rates
|
|
var prevOK bool
|
|
for _, p := range parts {
|
|
out.Last15m += p.Last15m
|
|
out.Last1h += p.Last1h
|
|
out.Last24h += p.Last24h
|
|
out.PerHourAvg7d += p.PerHourAvg7d
|
|
out.PrevPeriod += p.PrevPeriod
|
|
prevOK = prevOK || p.ChangePct != nil
|
|
}
|
|
out.PerHourAvg24h = avg1(out.Last24h, 24)
|
|
out.PerHourAvg7d = round1(out.PerHourAvg7d)
|
|
if prevOK {
|
|
out.ChangePct = pct(malicious, out.PrevPeriod)
|
|
}
|
|
out.PeakHour = PeakOf(tl)
|
|
return out
|
|
}
|