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>
121 lines
4.7 KiB
Go
121 lines
4.7 KiB
Go
package fed_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"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 logged(t *testing.T, name string) *store.Store {
|
|
s := store.New(store.Config{Name: name, Version: "t", Retention: 720 * time.Hour})
|
|
l, err := evlog.Open(evlog.Options{Dir: filepath.Join(t.TempDir(), "events")})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s.SetEventLog(l)
|
|
return s
|
|
}
|
|
|
|
func TestHubMergesPeriodAndMetrics(t *testing.T) {
|
|
const userTok, peerTok = "main-token-0123456789-abcdefghijklmnopqrs", "peer-secret-0123456789-abcdefghijklmnopqr"
|
|
peer := logged(t, "hostinger")
|
|
hubSt := logged(t, "oracle")
|
|
now := time.Now()
|
|
for i := 0; i < 4; i++ {
|
|
peer.Ingest(core.Event{Time: now.Add(-time.Duration(i) * time.Minute), IP: "5.5.5.5", Cat: core.Exploit, Source: "traefik", Method: "GET", Path: "/.env", Status: 404, Domain: "p.example"})
|
|
}
|
|
hubSt.Ingest(core.Event{Time: now.Add(-time.Hour), IP: "6.6.6.6", Cat: core.Scanner, Source: "traefik", Method: "GET", Path: "/wp-login.php", Status: 404, Domain: "h.example"})
|
|
// a copy of the peer's event relayed live to the hub must not be counted twice
|
|
le := store.LiveEvent{FeedEvent: store.FeedEvent{T: now.Unix() - 30, IP: "5.5.5.5", Cat: "exploit", Source: "traefik", Host: "hostinger"}}
|
|
hubSt.RecordPeerEvent(&le)
|
|
|
|
var peerCalls []string
|
|
pinner := server.New(server.Options{Token: "other-token-0123456789-abcdefghijklmnopqrs", PeerTokens: []string{peerTok}, Version: "t", Store: peer})
|
|
psrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
peerCalls = append(peerCalls, r.URL.Path)
|
|
pinner.ServeHTTP(w, r)
|
|
}))
|
|
defer psrv.Close()
|
|
p, _ := fed.ParsePeer("hostinger=" + psrv.URL)
|
|
hub := fed.NewHub([]fed.Peer{p}, peerTok)
|
|
hub.Refresh(context.Background())
|
|
h := server.New(server.Options{Token: userTok, Version: "t", Store: hubSt, Hub: hub})
|
|
get := func(path string) map[string]any {
|
|
r := httptest.NewRequest("GET", path, nil)
|
|
r.Header.Set("Authorization", "Bearer "+userTok)
|
|
r.RemoteAddr = "198.51.100.1:1"
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, r)
|
|
if w.Code != 200 {
|
|
t.Fatalf("%s: %d %s", path, w.Code, w.Body)
|
|
}
|
|
var m map[string]any
|
|
json.Unmarshal(w.Body.Bytes(), &m)
|
|
return m
|
|
}
|
|
pd := get("/api/v1/period?range=24h")
|
|
if pd["sessions_n"].(float64) != 2 || pd["events"].(float64) != 5 {
|
|
t.Fatalf("merged period: sessions %v events %v (relayed copies must not double count)", pd["sessions_n"], pd["events"])
|
|
}
|
|
hosts := map[string]bool{}
|
|
for _, s := range pd["sessions"].([]any) {
|
|
hosts[s.(map[string]any)["hosts"].([]any)[0].(string)] = true
|
|
}
|
|
if !hosts["oracle"] || !hosts["hostinger"] {
|
|
t.Fatalf("sessions must carry their host: %v", hosts)
|
|
}
|
|
if only := get("/api/v1/period?range=24h&host=hostinger"); only["sessions_n"].(float64) != 1 {
|
|
t.Fatalf("one peer: %v", only["sessions_n"])
|
|
}
|
|
if loc := get("/api/v1/period?range=24h&host=oracle"); loc["sessions_n"].(float64) != 1 {
|
|
t.Fatalf("local only: %v", loc["sessions_n"])
|
|
}
|
|
mt := get("/api/v1/metrics?range=24h")
|
|
if v := mt["attacks_per_hour_24h"].(map[string]any)["value"].(float64); v < 0.2 || v > 0.3 { // 5 events / 24 h, rounded to 0.1
|
|
t.Fatalf("merged attacks per hour: %v", v)
|
|
}
|
|
n := len(peerCalls)
|
|
get("/api/v1/period?range=24h")
|
|
for _, c := range peerCalls[n:] {
|
|
if c == "/api/v1/peer/period" {
|
|
t.Fatal("studies are cached for 30 s")
|
|
}
|
|
}
|
|
// the peer route: only the peer token, never the user token
|
|
for tok, want := range map[string]int{peerTok: 200, "other-token-0123456789-abcdefghijklmnopqrs": 401, "bad": 401} {
|
|
r := httptest.NewRequest("GET", "/api/v1/peer/period?from=1&to=2", nil)
|
|
r.Header.Set("Authorization", "Bearer "+tok)
|
|
r.RemoteAddr = "198.51.100.1:1"
|
|
w := httptest.NewRecorder()
|
|
pinner.ServeHTTP(w, r)
|
|
if tok == peerTok {
|
|
r = httptest.NewRequest("GET", "/api/v1/peer/period?from="+itoa(now.Unix()-3600)+"&to="+itoa(now.Unix()), nil)
|
|
r.Header.Set("Authorization", "Bearer "+tok)
|
|
r.RemoteAddr = "198.51.100.1:1"
|
|
w = httptest.NewRecorder()
|
|
pinner.ServeHTTP(w, r)
|
|
}
|
|
if w.Code != want {
|
|
t.Errorf("peer route with %q: %d, want %d", tok[:3], w.Code, want)
|
|
}
|
|
}
|
|
// a hub that is offline for the peer still answers (local + relayed)
|
|
psrv.Close()
|
|
hub.Refresh(context.Background())
|
|
if pd := get("/api/v1/period?range=24h"); pd["sessions_n"].(float64) < 1 {
|
|
t.Fatal("must answer without the peer")
|
|
}
|
|
}
|
|
|
|
func itoa(n int64) string { b, _ := json.Marshal(n); return string(b) }
|