feat: Urutau 1.0.0, lightweight threat intelligence and web analytics
Release / Plan (reuse the published image?) (push) Successful in 4s
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 1m29s
CI / Tests (Linux, race detector) (push) Failing after 1m48s
CI / Dependency audit (push) Successful in 21s
Release / ci (push) Failing after 1m50s
Release / Build image (amd64) (push) Skipped
Release / Build image (arm64) (push) Skipped
Release / Publish tags (push) Skipped
Release / Gitea release (push) Skipped
Release / Plan (reuse the published image?) (push) Successful in 4s
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 1m29s
CI / Tests (Linux, race detector) (push) Failing after 1m48s
CI / Dependency audit (push) Successful in 21s
Release / ci (push) Failing after 1m50s
Release / Build image (amd64) (push) Skipped
Release / Build image (arm64) (push) Skipped
Release / Publish tags (push) Skipped
Release / Gitea release (push) Skipped
Always-on Go service (stdlib only) that follows the Traefik JSON access log, the sshd log and the fail2ban log of a host, classifies attacks, scores sources, keeps bounded aggregates with snapshots, and serves an embedded dashboard with live updates, offline geolocation (DB-IP Lite), privacy-friendly traffic analytics, an exportable blocklist and token-authenticated federation between instances. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
b4a2c2c9ec
99 files changed
+15403
No files matched your search
@@ -0,0 +1,185 @@
|
||||
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
|
||||
for _, p := range parts {
|
||||
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
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user