Files
urutau/internal/fed/v2_test.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

116 lines
4.5 KiB
Go

package fed_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/evlog"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/fed"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/server"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
)
func TestHubOnDemandRangesRatesAndOldPeers(t *testing.T) {
peerTok := "peer-secret-0123456789-abcdefghijklmnopqr"
ps := store.New(store.Config{Name: "remote", Version: "t", Retention: 720 * time.Hour})
for i := 0; i < 4; i++ {
ps.Ingest(core.Event{Time: time.Now().Add(-time.Duration(i) * time.Minute), IP: "5.5.5.5", Cat: core.Exploit, Source: "traefik", Path: "/x", Method: "GET"})
}
ps.Ingest(core.Event{Time: time.Now().Add(-10 * 24 * time.Hour), IP: "6.6.6.6", Cat: core.Scanner, Source: "traefik", Path: "/y", Method: "GET"})
ps.Traffic(core.Event{Time: time.Now(), IP: "9.9.9.9", Cat: core.OK, Source: "traefik", Domain: "a.example", Method: "GET", Path: "/", Status: 200})
var calls []string
inner := server.New(server.Options{Token: "main-token-0123456789-abcdefghijklmnopqrs", PeerTokens: []string{peerTok}, Version: "t", Store: ps})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls = append(calls, r.URL.RawQuery)
inner.ServeHTTP(w, r)
}))
defer srv.Close()
p, _ := fed.ParsePeer("hostinger=" + srv.URL)
hub := fed.NewHub([]fed.Peer{p}, peerTok)
hub.Refresh(context.Background())
if len(calls) != len(store.BackgroundRanges) {
t.Fatalf("background fetches: %v", calls)
}
// a window outside the background set is fetched on demand, then cached
rg := store.Ranges["15d"]
n := len(calls)
sums := hub.SummariesFor(context.Background(), rg, "")
if len(sums) != 1 || sums[0].Range.Name != "15d" || sums[0].Totals.Counts["scanner"] != 1 || sums[0].Rates.Last1h != 4 {
t.Fatalf("15d %+v", sums)
}
hub.SummariesFor(context.Background(), rg, "")
if len(calls) != n+1 {
t.Fatalf("on-demand windows must be cached: %v", calls[n:])
}
// a custom window goes as from/to
now := time.Now().Unix()
local := store.New(store.Config{Name: "hub", Retention: 720 * time.Hour})
cr, err := local.ResolveRange("", fmt.Sprint(now-3*3600), "")
if err != nil {
t.Fatal(err)
}
cs := hub.SummariesFor(context.Background(), cr, "")
if len(cs) != 1 || cs[0].Totals.Counts["exploit"] != 4 || !strings.Contains(calls[len(calls)-1], "from=") {
t.Fatalf("custom %+v calls %v", cs, calls[len(calls)-1])
}
if tr := hub.TrafficFor(context.Background(), cr, "all", ""); len(tr) != 1 || tr[0].Requests != 1 {
t.Fatalf("traffic %+v", tr)
}
if got := hub.IPDetail("6.6.6.6"); len(got) != 1 {
t.Fatal("the 30 day export must carry the 10 day old address")
}
m := store.Merge("all", []store.Summary{local.Summary("24h"), sums[0]})
if m.Rates.Last1h != 4 {
t.Fatalf("merged rates %+v", m.Rates)
}
}
func TestOldPeerStringRangeStillParses(t *testing.T) {
var s store.Summary
if err := json.Unmarshal([]byte(`{"range":"24h","host":"x"}`), &s); err != nil || s.Range.Name != "24h" {
t.Fatalf("legacy range: %v %+v", err, s.Range)
}
if err := json.Unmarshal([]byte(`{"range":{"name":"7d","from":1,"to":2,"clipped":true}}`), &s); err != nil || !s.Range.Clipped {
t.Fatal(err)
}
}
func TestPeerEventsAreRegisteredWithHostAndLocalID(t *testing.T) {
var conns atomic.Int32
srv := fakeStream(t, &conns, func(w http.ResponseWriter) {
fmt.Fprint(w, ev(`{"id":999999,"ip":"6.6.6.6","cat":"exploit","host":"evil","ua":"sqlmap","fam":"vcs","tech":["T1190"],"flags":"SZ!"}`))
})
defer srv.Close()
p, _ := fed.ParsePeer("edge=" + srv.URL)
h := fed.NewHub([]fed.Peer{p}, "tok")
local := store.New(store.Config{Name: "hub"})
l, _ := evlog.Open(evlog.Options{Dir: t.TempDir() + "/events"})
local.SetEventLog(l)
h.OnLive = local.RecordPeerEvent
ch, stop := h.Subscribe()
defer stop()
ctx, cancel := context.WithCancel(context.Background())
defer func() { cancel(); h.WaitStream() }()
h.Stream(ctx)
select {
case e := <-ch:
if e.Host != "edge" || e.ID == 0 || e.ID == 999999 || e.Flags != "S" {
t.Fatalf("relayed %+v", e)
}
case <-time.After(5 * time.Second):
t.Fatal("no event")
}
pg, _ := local.QueryEvents(store.EventsQuery{From: time.Now().Unix() - 3600, To: time.Now().Unix() + 10, Host: "edge"})
if len(pg.Events) != 1 || !strings.Contains(string(pg.Events[0]), `"host":"edge"`) {
t.Fatalf("registry %v", pg.Events)
}
}