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,425 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
|
||||
)
|
||||
|
||||
// Range describes a dashboard time window.
|
||||
type Range struct {
|
||||
Name string
|
||||
Dur time.Duration
|
||||
Step int64 // timeline bucket size in seconds
|
||||
}
|
||||
|
||||
// Ranges are the supported windows.
|
||||
var Ranges = map[string]Range{
|
||||
"1h": {"1h", time.Hour, 60},
|
||||
"24h": {"24h", 24 * time.Hour, 900},
|
||||
"7d": {"7d", 7 * 24 * time.Hour, 3600},
|
||||
}
|
||||
|
||||
// ParseRange returns the named range, defaulting to 24h.
|
||||
func ParseRange(name string) Range {
|
||||
if r, ok := Ranges[name]; ok {
|
||||
return r
|
||||
}
|
||||
return Ranges["24h"]
|
||||
}
|
||||
|
||||
// Count is one top-list row.
|
||||
type Count struct {
|
||||
Key string `json:"key"`
|
||||
N uint32 `json:"n"`
|
||||
Label string `json:"label,omitempty"`
|
||||
}
|
||||
|
||||
// Timeline holds per-category counters on a fixed grid.
|
||||
type Timeline struct {
|
||||
Start int64 `json:"start"`
|
||||
Step int64 `json:"step"`
|
||||
Cats []string `json:"cats"`
|
||||
Series map[string][]uint32 `json:"series"`
|
||||
}
|
||||
|
||||
// Totals are the headline numbers.
|
||||
type Totals struct {
|
||||
Requests uint64 `json:"requests"`
|
||||
Malicious uint64 `json:"malicious"`
|
||||
HostileIPs int `json:"hostile_ips"`
|
||||
SuspiciousIPs int `json:"suspicious_ips"`
|
||||
UniqueIPs int `json:"unique_ips"`
|
||||
ActiveBans int `json:"active_bans"`
|
||||
Monitoring uint64 `json:"monitoring"`
|
||||
Counts map[string]uint64 `json:"counts"`
|
||||
}
|
||||
|
||||
// IPSummary describes one IP. Samples, UAs and Domains are only filled in the
|
||||
// detail view and in federation exchanges.
|
||||
type IPSummary struct {
|
||||
IP string `json:"ip"`
|
||||
Score float64 `json:"score"`
|
||||
Level core.Level `json:"level"`
|
||||
Counts map[string]uint32 `json:"counts"`
|
||||
Hosts []string `json:"hosts"`
|
||||
Domains []string `json:"domains,omitempty"`
|
||||
Samples []string `json:"samples,omitempty"`
|
||||
UAs []string `json:"uas,omitempty"`
|
||||
First int64 `json:"first"`
|
||||
Last int64 `json:"last"`
|
||||
Banned bool `json:"banned"`
|
||||
Jails []string `json:"jails,omitempty"`
|
||||
RDNS string `json:"rdns,omitempty"`
|
||||
Strong bool `json:"strong,omitempty"`
|
||||
Admin bool `json:"admin,omitempty"`
|
||||
CC string `json:"cc,omitempty"`
|
||||
Country string `json:"country,omitempty"`
|
||||
City string `json:"city,omitempty"`
|
||||
ASN uint32 `json:"asn,omitempty"`
|
||||
ASOrg string `json:"as_org,omitempty"`
|
||||
UALabels []string `json:"ua_labels,omitempty"`
|
||||
}
|
||||
|
||||
// SourceStatus reports one log source.
|
||||
type SourceStatus struct {
|
||||
Host string `json:"host,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
State string `json:"state"` // following, waiting, backfill, error, disabled
|
||||
Lines uint64 `json:"lines"`
|
||||
Malformed uint64 `json:"malformed"`
|
||||
LastEvent int64 `json:"last_event"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// HostInfo is the per-instance card.
|
||||
type HostInfo struct {
|
||||
Name string `json:"name"`
|
||||
Self bool `json:"self"`
|
||||
Online bool `json:"online"`
|
||||
Version string `json:"version,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
LastSeen int64 `json:"last_seen"`
|
||||
Requests uint64 `json:"requests"`
|
||||
Malicious uint64 `json:"malicious"`
|
||||
HostileIPs int `json:"hostile_ips"`
|
||||
ActiveBans int `json:"active_bans"`
|
||||
LatencyMS int64 `json:"latency_ms,omitempty"`
|
||||
}
|
||||
|
||||
// Summary is the mergeable dashboard payload.
|
||||
type Summary struct {
|
||||
Version string `json:"version"`
|
||||
Host string `json:"host"`
|
||||
Range string `json:"range"`
|
||||
Generated int64 `json:"generated"`
|
||||
Hosts []HostInfo `json:"hosts"`
|
||||
Totals Totals `json:"totals"`
|
||||
Timeline Timeline `json:"timeline"`
|
||||
IPs []IPSummary `json:"ips"`
|
||||
TopPaths []Count `json:"top_paths"`
|
||||
TopDomains []Count `json:"top_domains"`
|
||||
TopUAs []Count `json:"top_uas"`
|
||||
TopUsers []Count `json:"top_users"`
|
||||
Countries []Count `json:"countries"`
|
||||
Recent []FeedEvent `json:"recent"`
|
||||
Sources []SourceStatus `json:"sources"`
|
||||
Attribution string `json:"attribution,omitempty"`
|
||||
}
|
||||
|
||||
const (
|
||||
summaryIPs = 500
|
||||
topKeep = 50
|
||||
)
|
||||
|
||||
func catStrings() []string {
|
||||
out := make([]string, len(core.Categories))
|
||||
for i, c := range core.Categories {
|
||||
out[i] = string(c)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func attackKinds(c [core.NumCats]uint32) int {
|
||||
n := 0
|
||||
for _, cat := range []core.Category{core.Scanner, core.Exploit, core.BruteForce, core.SSHFail, core.SSHProbe} {
|
||||
if c[cat.Index()] > 0 {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// rowOf builds the output row of one table entry.
|
||||
func (s *Store) rowOf(ip string, r *IPRec, detail bool) IPSummary {
|
||||
cm := make(map[string]uint32, 4)
|
||||
for i, n := range r.Counts {
|
||||
if n > 0 {
|
||||
cm[string(core.Categories[i])] = n
|
||||
}
|
||||
}
|
||||
strong := r.Strong || attackKinds(r.Counts) >= 2
|
||||
admin := r.adminAt(s.now().Unix())
|
||||
out := IPSummary{IP: ip, Score: round1(r.Score), Level: core.LevelOf(r.Score, strong, admin), Counts: cm, Hosts: []string{s.cfg.Name},
|
||||
First: r.First, Last: r.Last, Banned: len(r.Jails) > 0, Strong: strong, Admin: admin}
|
||||
if len(r.Jails) > 0 {
|
||||
out.Jails = append([]string(nil), r.Jails...)
|
||||
}
|
||||
if detail {
|
||||
out.Domains = append([]string(nil), r.Domains...)
|
||||
out.Samples = append([]string(nil), r.Samples...)
|
||||
out.UAs = append([]string(nil), r.UAs...)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func round1(f float64) float64 { return float64(int64(f*10+0.5)) / 10 }
|
||||
|
||||
// AllIPs returns every IP seen inside the range, highest score first.
|
||||
func (s *Store) AllIPs(rg Range, detail bool) []IPSummary {
|
||||
cut := s.now().Add(-rg.Dur).Unix()
|
||||
s.mu.RLock()
|
||||
out := make([]IPSummary, 0, len(s.ips))
|
||||
for ip, r := range s.ips {
|
||||
if r.Last >= cut {
|
||||
out = append(out, s.rowOf(ip, r, detail))
|
||||
}
|
||||
}
|
||||
s.mu.RUnlock()
|
||||
SortIPs(out, "score", true)
|
||||
return out
|
||||
}
|
||||
|
||||
// SortIPs orders in place by key (score, last, first, events, ip).
|
||||
func SortIPs(l []IPSummary, key string, desc bool) {
|
||||
ev := func(x IPSummary) uint32 {
|
||||
var n uint32
|
||||
for _, v := range x.Counts {
|
||||
n += v
|
||||
}
|
||||
return n
|
||||
}
|
||||
less := func(a, b IPSummary) bool {
|
||||
switch key {
|
||||
case "last":
|
||||
return a.Last < b.Last
|
||||
case "first":
|
||||
return a.First < b.First
|
||||
case "events":
|
||||
return ev(a) < ev(b)
|
||||
case "ip":
|
||||
return a.IP < b.IP
|
||||
}
|
||||
if a.Score != b.Score {
|
||||
return a.Score < b.Score
|
||||
}
|
||||
return a.Last < b.Last
|
||||
}
|
||||
sort.SliceStable(l, func(i, j int) bool {
|
||||
if desc {
|
||||
return less(l[j], l[i])
|
||||
}
|
||||
return less(l[i], l[j])
|
||||
})
|
||||
}
|
||||
|
||||
// IP returns the detail of one address.
|
||||
func (s *Store) IP(ip string) (IPSummary, bool) {
|
||||
ip = KeyOf(ip)
|
||||
s.mu.RLock()
|
||||
r := s.ips[ip]
|
||||
var out IPSummary
|
||||
if r != nil {
|
||||
out = s.rowOf(ip, r, true)
|
||||
}
|
||||
s.mu.RUnlock()
|
||||
if r == nil {
|
||||
return IPSummary{}, false
|
||||
}
|
||||
if s.rdns != nil {
|
||||
out.RDNS = s.rdns.Lookup(ip)
|
||||
}
|
||||
s.Enrich(&out)
|
||||
for _, ua := range out.UAs {
|
||||
if l := s.UALabel(ua); l != "" {
|
||||
out.UALabels = append(out.UALabels, l)
|
||||
}
|
||||
}
|
||||
return out, true
|
||||
}
|
||||
|
||||
func topN(m map[string]uint32, n int) []Count {
|
||||
out := make([]Count, 0, len(m))
|
||||
for k, v := range m {
|
||||
out = append(out, Count{Key: k, N: v})
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool {
|
||||
if out[i].N != out[j].N {
|
||||
return out[i].N > out[j].N
|
||||
}
|
||||
return out[i].Key < out[j].Key
|
||||
})
|
||||
if len(out) > n {
|
||||
out = out[:n]
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Summary builds the local summary for a range (cached for two seconds).
|
||||
func (s *Store) Summary(rangeName string) Summary {
|
||||
rg := ParseRange(rangeName)
|
||||
s.cacheMu.Lock()
|
||||
if c, ok := s.cache[rg.Name]; ok && time.Since(c.at) < 2*time.Second {
|
||||
s.cacheMu.Unlock()
|
||||
return c.s
|
||||
}
|
||||
s.cacheMu.Unlock()
|
||||
sum := s.buildSummary(rg)
|
||||
s.cacheMu.Lock()
|
||||
s.cache[rg.Name] = cachedSummary{time.Now(), sum}
|
||||
s.cacheMu.Unlock()
|
||||
return sum
|
||||
}
|
||||
|
||||
func (s *Store) buildSummary(rg Range) Summary {
|
||||
now := s.now()
|
||||
nowU := now.Unix()
|
||||
cats := catStrings()
|
||||
nb := int(rg.Dur.Seconds()) / int(rg.Step)
|
||||
endB := nowU / rg.Step
|
||||
start := (endB - int64(nb) + 1) * rg.Step
|
||||
tl := Timeline{Start: start, Step: rg.Step, Cats: cats, Series: make(map[string][]uint32, len(cats))}
|
||||
for _, c := range cats {
|
||||
tl.Series[c] = make([]uint32, nb)
|
||||
}
|
||||
tot := Totals{Counts: map[string]uint64{}}
|
||||
|
||||
s.mu.RLock()
|
||||
startMin := start / 60
|
||||
for m := startMin; m <= nowU/60; m++ {
|
||||
i := int(m % slots)
|
||||
if s.stamps[i] != m {
|
||||
continue
|
||||
}
|
||||
b := int((m*60 - start) / rg.Step)
|
||||
if b < 0 || b >= nb {
|
||||
continue
|
||||
}
|
||||
for ci, n := range s.counts[i] {
|
||||
if n == 0 {
|
||||
continue
|
||||
}
|
||||
tl.Series[cats[ci]][b] += n
|
||||
tot.Counts[cats[ci]] += uint64(n)
|
||||
c := core.Categories[ci]
|
||||
if c.IsHTTP() {
|
||||
tot.Requests += uint64(n)
|
||||
}
|
||||
if c.IsMalicious() {
|
||||
tot.Malicious += uint64(n)
|
||||
}
|
||||
}
|
||||
}
|
||||
for m := startMin; m <= nowU/60; m++ {
|
||||
if i := int(m % slots); s.monSt[i] == m {
|
||||
tot.Monitoring += uint64(s.monN[i])
|
||||
}
|
||||
}
|
||||
paths, doms, uas, users, ctry := map[string]uint32{}, map[string]uint32{}, map[string]uint32{}, map[string]uint32{}, map[string]uint32{}
|
||||
firstHour := (nowU - int64(rg.Dur.Seconds())) / 3600
|
||||
for i := range s.hours {
|
||||
h := &s.hours[i]
|
||||
if h.Hour == 0 || h.Hour < firstHour || h.Hour > nowU/3600+1 {
|
||||
continue
|
||||
}
|
||||
for _, p := range [...]struct {
|
||||
from map[string]uint32
|
||||
to map[string]uint32
|
||||
}{{h.Paths, paths}, {h.Domains, doms}, {h.UAs, uas}, {h.Users, users}, {h.Countries, ctry}} {
|
||||
for k, v := range p.from {
|
||||
p.to[k] += v
|
||||
}
|
||||
}
|
||||
}
|
||||
cut := nowU - int64(rg.Dur.Seconds())
|
||||
ips := make([]IPSummary, 0, 1024)
|
||||
for ip, r := range s.ips {
|
||||
if len(r.Jails) > 0 {
|
||||
tot.ActiveBans++
|
||||
}
|
||||
if r.Last < cut {
|
||||
continue
|
||||
}
|
||||
tot.UniqueIPs++
|
||||
row := s.rowOf(ip, r, true)
|
||||
switch row.Level {
|
||||
case core.LevelHostile:
|
||||
tot.HostileIPs++
|
||||
case core.LevelSuspicious:
|
||||
tot.SuspiciousIPs++
|
||||
}
|
||||
if r.Score >= 1 || len(r.Jails) > 0 {
|
||||
ips = append(ips, row)
|
||||
}
|
||||
}
|
||||
recent := s.recentLocked(60)
|
||||
s.mu.RUnlock()
|
||||
|
||||
SortIPs(ips, "score", true)
|
||||
if len(ips) > summaryIPs {
|
||||
ips = ips[:summaryIPs]
|
||||
}
|
||||
if s.enr != nil {
|
||||
for i := range ips {
|
||||
s.Enrich(&ips[i])
|
||||
for _, ua := range ips[i].UAs {
|
||||
if l := s.UALabel(ua); l != "" {
|
||||
ips[i].UALabels = append(ips[i].UALabels, l)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
topUAs := topN(uas, topKeep)
|
||||
for i := range topUAs {
|
||||
topUAs[i].Label = s.UALabel(topUAs[i].Key)
|
||||
}
|
||||
if s.rdns != nil {
|
||||
for i := range ips {
|
||||
if i >= 25 {
|
||||
break
|
||||
}
|
||||
ips[i].RDNS = s.rdns.Lookup(ips[i].IP)
|
||||
}
|
||||
}
|
||||
var srcs []SourceStatus
|
||||
if s.sources != nil {
|
||||
srcs = s.sources()
|
||||
for i := range srcs {
|
||||
srcs[i].Host = s.cfg.Name
|
||||
srcs[i].Path = "" // never leak host paths to the dashboard or to peers
|
||||
}
|
||||
}
|
||||
return Summary{
|
||||
Version: s.cfg.Version, Host: s.cfg.Name, Range: rg.Name, Generated: nowU,
|
||||
Hosts: []HostInfo{{Name: s.cfg.Name, Self: true, Online: true, Version: s.cfg.Version, LastSeen: nowU,
|
||||
Requests: tot.Requests, Malicious: tot.Malicious, HostileIPs: tot.HostileIPs, ActiveBans: tot.ActiveBans}},
|
||||
Totals: tot, Timeline: tl, IPs: ips,
|
||||
TopPaths: topN(paths, topKeep), TopDomains: topN(doms, topKeep), TopUAs: topUAs, TopUsers: topN(users, topKeep), Countries: topN(ctry, 120),
|
||||
Recent: recent, Sources: srcs, Attribution: s.attrib,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Store) recentLocked(n int) []FeedEvent {
|
||||
total := len(s.recent)
|
||||
out := make([]FeedEvent, 0, n)
|
||||
for i := 0; i < total && len(out) < n; i++ {
|
||||
idx := (s.rhead - 1 - i) % total
|
||||
if idx < 0 {
|
||||
idx += total
|
||||
}
|
||||
out = append(out, s.recent[idx])
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in new issue
Block a user