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>
215 lines
8.8 KiB
Go
215 lines
8.8 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
"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/store"
|
|
)
|
|
|
|
const (
|
|
userTok = "user-token-0123456789-abcdefghijklmnop"
|
|
peerTok = "peer-token-0123456789-abcdefghijklmnopq"
|
|
)
|
|
|
|
func setupLog(t *testing.T) (http.Handler, *store.Store, map[string]string) {
|
|
st := store.New(store.Config{Name: "oracle", Version: "test", Retention: 720 * time.Hour})
|
|
l, err := evlog.Open(evlog.Options{Dir: filepath.Join(t.TempDir(), "events")})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st.SetEventLog(l)
|
|
now := time.Now()
|
|
for i := 0; i < 6; i++ {
|
|
st.Ingest(core.Event{Time: now.Add(-time.Duration(i) * time.Minute), IP: "45.94.31.4", Cat: core.Exploit, Strong: true, Status: 200, Bytes: 321, Source: "traefik",
|
|
Domain: "a.example", Method: "GET", Path: "/.env", UA: "sqlmap/1.7"})
|
|
}
|
|
st.Ingest(core.Event{Time: now.Add(-3 * time.Hour), IP: "45.94.31.9", Cat: core.Scanner, Source: "traefik", Domain: "a.example", Method: "GET", Path: "/wp-login.php", Status: 404})
|
|
st.Ingest(core.Event{Time: now.Add(-time.Minute), IP: "45.94.31.4", Cat: core.Ban, Source: "fail2ban", Jail: "sshd"})
|
|
h := New(Options{Token: userTok, PeerTokens: []string{peerTok}, Version: "test", Store: st})
|
|
return h, st, map[string]string{"Authorization": "Bearer " + userTok}
|
|
}
|
|
|
|
func TestEveryRangeAndValidation(t *testing.T) {
|
|
h, _, auth := setupLog(t)
|
|
for _, rg := range store.RangeOrder {
|
|
for _, p := range []string{"summary", "traffic", "ips", "geo", "blocklist.json", "metrics"} {
|
|
if w := do(h, "GET", "/api/v1/"+p+"?range="+rg, cloneHdr(auth), nil); w.Code != 200 {
|
|
t.Errorf("%s range=%s: %d %s", p, rg, w.Code, w.Body)
|
|
}
|
|
}
|
|
}
|
|
now := time.Now().Unix()
|
|
if w := do(h, "GET", fmt.Sprintf("/api/v1/summary?from=%d", now-7200), cloneHdr(auth), nil); w.Code != 200 {
|
|
t.Fatalf("custom: %d", w.Code)
|
|
}
|
|
var m map[string]any
|
|
w := do(h, "GET", fmt.Sprintf("/api/v1/summary?from=%d&to=%d", now-7200, now-60), cloneHdr(auth), nil)
|
|
json.Unmarshal(w.Body.Bytes(), &m)
|
|
rg := m["range"].(map[string]any)
|
|
if rg["from"].(float64) != float64(now-7200) || rg["clipped"] != false || rg["name"] != "custom" {
|
|
t.Fatalf("range object %v", rg)
|
|
}
|
|
w = do(h, "GET", fmt.Sprintf("/api/v1/summary?from=%d", now-60*86400), cloneHdr(auth), nil)
|
|
if w.Code != 400 {
|
|
t.Fatalf("over 30 days: %d", w.Code)
|
|
}
|
|
st := store.New(store.Config{Name: "o", Retention: 48 * time.Hour})
|
|
h2 := New(Options{Token: userTok, Version: "t", Store: st})
|
|
w = do(h2, "GET", fmt.Sprintf("/api/v1/summary?from=%d&to=%d", now-5*86400, now-86400), cloneHdr(auth), nil)
|
|
json.Unmarshal(w.Body.Bytes(), &m)
|
|
if w.Code != 200 || m["range"].(map[string]any)["clipped"] != true {
|
|
t.Fatalf("clipped: %d %s", w.Code, w.Body)
|
|
}
|
|
for _, bad := range []string{"range=4h", "range=%00", "from=abc", "from=1&to=1", "to=100", "from=-1", "range=7d&from=x", "from=99999999999999999999"} {
|
|
for _, p := range []string{"summary", "traffic", "ips", "geo", "blocklist.txt", "events", "events/sessions", "period", "metrics", "ip/45.94.31.4"} {
|
|
if w := do(h, "GET", "/api/v1/"+p+"?"+bad, cloneHdr(auth), nil); w.Code != 400 {
|
|
t.Errorf("%s?%s must be 400, got %d", p, bad, w.Code)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestEventsAPI(t *testing.T) {
|
|
h, _, auth := setupLog(t)
|
|
m := getJSON(t, h, "/api/v1/events?limit=3", auth)
|
|
evs := m["events"].([]any)
|
|
if len(evs) != 3 || m["next"] == "" || m["total_estimate"].(float64) < 8 {
|
|
t.Fatalf("page %v", m)
|
|
}
|
|
first := evs[0].(map[string]any)
|
|
if first["host"] != "oracle" || first["id"] == nil || first["t"] == nil {
|
|
t.Fatalf("event %v", first)
|
|
}
|
|
// walk all pages, newest first
|
|
seen, cur := 0, ""
|
|
var lastT float64 = 1e18
|
|
for i := 0; i < 10; i++ {
|
|
m = getJSON(t, h, "/api/v1/events?limit=3&cursor="+cur, auth)
|
|
for _, e := range m["events"].([]any) {
|
|
tt := e.(map[string]any)["t"].(float64)
|
|
if tt > lastT {
|
|
t.Fatal("descending order broken")
|
|
}
|
|
lastT = tt
|
|
seen++
|
|
}
|
|
if m["next"] == "" {
|
|
break
|
|
}
|
|
cur = m["next"].(string)
|
|
}
|
|
if seen != 8 {
|
|
t.Fatalf("paged %d events", seen)
|
|
}
|
|
if m = getJSON(t, h, "/api/v1/events?order=asc&ip=45.94.31.9&cat=scanner", auth); len(m["events"].([]any)) != 1 {
|
|
t.Fatalf("filters %v", m)
|
|
}
|
|
if m = getJSON(t, h, "/api/v1/events?q=SQLMAP&host=oracle", auth); len(m["events"].([]any)) != 6 {
|
|
t.Fatalf("q filter: %v", len(m["events"].([]any)))
|
|
}
|
|
for _, bad := range []string{"limit=0", "limit=x", "order=up", "cat=nope", "ip=zzz", "cursor=!!", "host=" + strings.Repeat("a", 100)} {
|
|
if w := do(h, "GET", "/api/v1/events?"+bad, cloneHdr(auth), nil); w.Code != 400 {
|
|
t.Errorf("%s: %d", bad, w.Code)
|
|
}
|
|
}
|
|
if m = getJSON(t, h, "/api/v1/events?limit=99999", auth); len(m["events"].([]any)) != 8 {
|
|
t.Fatal("limit must clamp to 5000")
|
|
}
|
|
ss := getJSON(t, h, "/api/v1/events/sessions?gap=900", auth)["sessions"].([]any)
|
|
if len(ss) != 2 || ss[0].(map[string]any)["ip"] != "45.94.31.9" {
|
|
t.Fatalf("sessions %v", ss)
|
|
}
|
|
if w := do(h, "GET", "/api/v1/events/sessions?gap=5", cloneHdr(auth), nil); w.Code != 400 {
|
|
t.Fatal("gap")
|
|
}
|
|
// the peer token opens none of the new routes
|
|
peer := map[string]string{"Authorization": "Bearer " + peerTok}
|
|
for _, p := range []string{"events", "events/sessions", "period", "period/events", "metrics", "ip/45.94.31.4", "ip/45.94.31.4/stix"} {
|
|
if w := do(h, "GET", "/api/v1/"+p, cloneHdr(peer), nil); w.Code != 403 {
|
|
t.Errorf("%s with the peer token: %d", p, w.Code)
|
|
}
|
|
if w := do(h, "GET", "/api/v1/"+p, nil, nil); w.Code != 401 {
|
|
t.Errorf("%s without auth: %d", p, w.Code)
|
|
}
|
|
}
|
|
pe := getJSON(t, h, "/api/v1/period/events?limit=2", auth)
|
|
if len(pe["events"].([]any)) != 2 {
|
|
t.Fatal("period events")
|
|
}
|
|
if a, b := pe["events"].([]any)[0].(map[string]any)["t"].(float64), pe["events"].([]any)[1].(map[string]any)["t"].(float64); a > b {
|
|
t.Fatal("replay is ascending")
|
|
}
|
|
}
|
|
|
|
func TestIPDetailPeriodMetricsAndSTIX(t *testing.T) {
|
|
h, _, auth := setupLog(t)
|
|
d := getJSON(t, h, "/api/v1/ip/45.94.31.4", auth)
|
|
for _, k := range []string{"ip", "score", "level", "first", "last", "counts", "timeline", "hosts", "domains", "statuses", "methods", "paths", "uas", "users", "jails",
|
|
"ban_history", "score_breakdown", "strong_reasons", "sessions", "neighbors", "blocklisted", "admin", "verdict", "exposure", "actions", "identity", "geo", "time",
|
|
"cadence", "techniques", "families", "targets", "tools", "ssh", "status_hist", "related", "bans", "ttb_s", "blocklist", "links", "range"} {
|
|
if _, ok := d[k]; !ok {
|
|
t.Errorf("ip detail misses %q", k)
|
|
}
|
|
}
|
|
paths := d["paths"].([]any)
|
|
if len(paths) != 1 || paths[0].(map[string]any)["n"].(float64) != 6 {
|
|
t.Fatalf("paths %v", paths)
|
|
}
|
|
if u := d["uas"].([]any); len(u) != 1 || u[0].(map[string]any)["key"] != "sqlmap/1.7" {
|
|
t.Fatalf("uas %v", u)
|
|
}
|
|
if d["blocklisted"] != true && d["score"].(float64) >= 50 {
|
|
t.Fatalf("blocklisted %v", d["blocklisted"])
|
|
}
|
|
if len(d["exposure"].([]any)) == 0 || len(d["ban_history"].([]any)) != 1 {
|
|
t.Fatalf("exposure/ban %v %v", d["exposure"], d["ban_history"])
|
|
}
|
|
v := d["verdict"].(map[string]any)
|
|
if len(v["reasons"].([]any)) == 0 || v["level"] != "hostile" {
|
|
t.Fatalf("verdict %v", v)
|
|
}
|
|
if len(d["links"].(map[string]any)) != 7 {
|
|
t.Fatal("links")
|
|
}
|
|
p := getJSON(t, h, "/api/v1/period", auth)
|
|
if p["sessions_n"].(float64) != 2 || p["events_cursor"] == "" {
|
|
t.Fatalf("period %v", p)
|
|
}
|
|
for _, k := range []string{"sessions", "campaigns", "first_seen_technique", "escalations", "spikes", "new_vs_returning", "top_targets", "top_fams", "top_techs", "top_users", "top_asns"} {
|
|
if _, ok := p[k]; !ok {
|
|
t.Errorf("period misses %q", k)
|
|
}
|
|
}
|
|
mt := getJSON(t, h, "/api/v1/metrics?range=24h", auth)
|
|
for _, k := range []string{"attacks_per_hour_24h", "moving_avg_7d_per_hour", "trend", "active_attackers_24h", "active_attackers_1h", "new_attackers", "returning_rate",
|
|
"sessions_per_day", "exposure_rate", "exploits_refused_pct", "mttb", "ban_coverage", "blocklist_coverage", "blocklist_leak", "ssh", "concentration"} {
|
|
if _, ok := mt[k]; !ok {
|
|
t.Errorf("metrics misses %q", k)
|
|
}
|
|
}
|
|
for _, k := range []string{"value", "prev", "delta_pct", "spark"} {
|
|
if _, ok := mt["attacks_per_hour_24h"].(map[string]any)[k]; !ok {
|
|
t.Errorf("metric misses %q", k)
|
|
}
|
|
}
|
|
w := do(h, "GET", "/api/v1/ip/45.94.31.4/stix", cloneHdr(auth), nil)
|
|
if w.Code != 200 || !strings.HasPrefix(w.Header().Get("Content-Type"), "application/stix+json;version=2.1") || !strings.Contains(w.Header().Get("Content-Disposition"), "attachment") ||
|
|
!strings.Contains(w.Body.String(), `"type":"bundle"`) && !strings.Contains(w.Body.String(), `"type": "bundle"`) {
|
|
t.Fatalf("stix %d %s", w.Code, w.Header())
|
|
}
|
|
if w := do(h, "GET", "/api/v1/ip/1.2.3.4/stix", cloneHdr(auth), nil); w.Code != 404 {
|
|
t.Fatal("unknown ip stix")
|
|
}
|
|
if w := do(h, "GET", "/api/v1/ip/not-an-ip/stix", cloneHdr(auth), nil); w.Code != 400 {
|
|
t.Fatal("bad ip stix")
|
|
}
|
|
}
|