Files
urutau/internal/intel/campaign_perf_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

138 lines
4.1 KiB
Go

package intel
import (
"context"
"fmt"
"net/netip"
"strings"
"testing"
"time"
)
// letters encodes i with letters g-z only (digits and hex runs are normalised away).
func letters(i int) string {
s := "x"
for ; i > 0; i /= 20 {
s += string(rune('g' + i%20))
}
return s
}
func synthSessions(n int, mode string) ([]Session, map[string]Signature) {
ss := make([]Session, n)
sigs := make(map[string]Signature, n)
for i := 0; i < n; i++ {
id := fmt.Sprintf("s%d", i)
ss[i] = Session{ID: id, IP: fmt.Sprintf("2001:db8:%x::%x", i%65536, i), Start: at(i), End: at(i)}
var paths []string
switch mode {
case "distinct": // every session unique, all share "/"
paths = []string{"/", "/a" + letters(i), "/b" + letters(i), "/c" + letters(i), "/d" + letters(i)}
case "pool": // random-ish 5-subsets of a 40-path pool
for k := 0; k < 5; k++ {
paths = append(paths, "/p"+letters((i*7+k*k*13+k*i)%40))
}
default: // same signature
paths = []string{"/.env", "/admin", "/wp-login.php", "/.git/config", "/x"}
}
sigs[id] = SignatureFrom(paths, "nuclei")
}
return ss, sigs
}
func TestCampaignsScale(t *testing.T) {
for _, mode := range []string{"same", "distinct", "pool"} {
ss, sigs := synthSessions(50000, mode)
start := time.Now()
cs := Campaigns(ss, sigs, 0)
el := time.Since(start)
t.Logf("%s: 50000 sessions -> %d campaigns in %v", mode, len(cs), el)
if el > 8*time.Second {
t.Errorf("%s too slow: %v", mode, el)
}
if mode == "distinct" && len(cs) != 0 {
t.Errorf("distinct sessions must not cluster: %d", len(cs))
}
if mode == "same" && len(cs) == 0 {
t.Error("same signature must form campaigns")
}
}
}
func TestCampaignsCtxPartial(t *testing.T) {
ss, sigs := synthSessions(5000, "same")
ctx, cancel := context.WithCancel(context.Background())
cancel()
if cs := CampaignsCtx(ctx, ss, sigs, 0); len(cs) != 0 {
t.Errorf("cancelled before start should give nothing, got %d", len(cs))
}
ctx2, cancel2 := context.WithTimeout(context.Background(), time.Nanosecond)
defer cancel2()
time.Sleep(time.Millisecond)
CampaignsCtx(ctx2, ss, sigs, 0) // must return, partial
}
func TestJaccardNoAlloc(t *testing.T) {
a := []string{"/a", "/b", "/c", "/d", "/e"}
b := []string{"/a", "/b", "/c", "/d", "/f"}
if n := testing.AllocsPerRun(100, func() { Jaccard(a, b) }); n != 0 {
t.Errorf("allocs: %v", n)
}
if j := Jaccard(a, b); j < 0.666 || j > 0.667 {
t.Errorf("%v", j)
}
if Jaccard([]string{"a", "a"}, []string{"a"}) != 1 {
t.Error("duplicates")
}
big := strings.Split("a b c d e f g h i j", " ")
if Jaccard(big, big[:5]) != 0.5 {
t.Error("map path")
}
}
func TestSTIXPrefix(t *testing.T) {
now := time.Date(2026, 11, 14, 0, 0, 0, 0, time.UTC)
for _, in := range []string{"2001:db8::/64", "2001:db8::1234/64"} {
b, err := STIXBundle(in, at(0), at(10), 3, []string{"sqli"}, nil, "", now)
if err != nil || ValidateSTIX(b) != nil {
t.Fatalf("%s: %v", in, err)
}
if !strings.Contains(string(b), "[ipv6-addr:value = '2001:db8::/64']") {
t.Errorf("%s: %s", in, b)
}
}
p4, err := STIXBundlePrefix(netip.MustParsePrefix("203.0.113.77/24"), at(0), at(1), 1, nil, nil, "", now)
if err != nil || !strings.Contains(string(p4), "[ipv4-addr:value = '203.0.113.0/24']") {
t.Errorf("%v %s", err, p4)
}
one, _ := STIXBundlePrefix(netip.MustParsePrefix("1.2.3.4/32"), at(0), at(1), 1, nil, nil, "", now)
if !strings.Contains(string(one), "[ipv4-addr:value = '1.2.3.4']") {
t.Error("/32 is an address")
}
if _, err := STIXBundlePrefix(netip.Prefix{}, at(0), at(1), 1, nil, nil, "", now); err == nil {
t.Error("invalid prefix")
}
for _, bad := range []string{"1.2.3.4/99", "x/24", "/", "1.2.3.4/"} {
if _, err := STIXBundle(bad, at(0), at(1), 1, nil, nil, "", now); err == nil {
t.Errorf("accepted %q", bad)
}
}
}
func BenchmarkCampaigns50k(b *testing.B) {
ss, sigs := synthSessions(50000, "same")
b.ResetTimer()
for i := 0; i < b.N; i++ {
Campaigns(ss, sigs, 0)
}
}
func BenchmarkJaccard(b *testing.B) {
x := []string{"/a", "/b", "/c", "/d", "/e"}
y := []string{"/a", "/b", "/c", "/d", "/f"}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
Jaccard(x, y)
}
}