Each server runs an ultralight probe (scratch image, ~7 MiB RSS) that tails the logs, ships raw lines to the hub over HTTPS with an on-disk spool, and applies enforce decisions locally. The hub keeps one node per probe (primary node = the 1.x data dir, unchanged), does all analysis and serves the UI. Peer federation is removed. 'urutau import' brings 1.x data dirs in with hash and count verification. UI: probes page, host qualified incidents, IPs never wrap, egress heatmap labels, live and replay globes no longer auto-rotate, replay repeat, live traffic metrics. BREAKING CHANGE: URUTAU_PEERS, URUTAU_PEER_* and /api/v1/peer/* are gone; log path variables move from the hub to the probe. See docs/migration-2.0.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
174 lines
6.0 KiB
Go
174 lines
6.0 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
|
|
)
|
|
|
|
type fakeGeo struct{}
|
|
|
|
func (fakeGeo) Geo(ip string) core.GeoInfo {
|
|
switch {
|
|
case strings.HasPrefix(ip, "200.1.1."):
|
|
return core.GeoInfo{CC: "BR", Country: "Brazil", Region: "Pernambuco", City: "Recife", Lat: -8.05, Lon: -34.9, ASN: 64500, ASOrg: "Net A"}
|
|
case strings.HasPrefix(ip, "200.2.2."):
|
|
return core.GeoInfo{CC: "BR", Country: "Brazil", Region: "Sao Paulo", City: "Campinas"} // no coordinates
|
|
case strings.HasPrefix(ip, "200.3.3."):
|
|
return core.GeoInfo{CC: "BR", Country: "Brazil", City: "Nowhere"} // empty region
|
|
case strings.HasPrefix(ip, "9.9.9."):
|
|
return core.GeoInfo{CC: "US", Region: "Texas", City: "Austin", Lat: 30.2, Lon: -97.7}
|
|
}
|
|
return core.GeoInfo{}
|
|
}
|
|
func (fakeGeo) UA(string) core.UAInfo { return core.UAInfo{} }
|
|
|
|
func geoSetup(t *testing.T) (http.Handler, map[string]string) {
|
|
st := store.New(store.Config{Name: "oracle", Version: "t", Location: &store.Location{Lat: 1, Lon: 2, Label: "Home"}})
|
|
st.SetEnricher(fakeGeo{})
|
|
now := time.Now()
|
|
ing := func(ip string, c core.Category, n int, strong bool) {
|
|
for i := 0; i < n; i++ {
|
|
st.Ingest(core.Event{Time: now, IP: ip, Cat: c, Strong: strong, Source: "traefik", Method: "GET", Path: "/x"})
|
|
}
|
|
}
|
|
ing("200.1.1.1", core.Exploit, 8, true) // 64 points: hostile
|
|
ing("200.1.1.2", core.Scanner, 2, false)
|
|
ing("200.1.1.3", core.NotFound, 5, false) // not malicious: ignored
|
|
ing("200.2.2.2", core.Scanner, 5, false)
|
|
ing("200.3.3.3", core.BruteForce, 1, false)
|
|
ing("9.9.9.9", core.SSHFail, 3, false)
|
|
ing("8.8.8.8", core.Exploit, 1, false) // unknown geo: skipped
|
|
h := New(Options{Token: utok, Version: "t", Store: st})
|
|
return h, map[string]string{"Authorization": "Bearer " + utok}
|
|
}
|
|
|
|
func getJSON(t *testing.T, h http.Handler, path string, hd map[string]string) map[string]any {
|
|
w := do(h, "GET", path, cloneHdr(hd), nil)
|
|
if w.Code != 200 {
|
|
t.Fatalf("%s: %d %s", path, w.Code, w.Body)
|
|
}
|
|
var m map[string]any
|
|
if err := json.Unmarshal(w.Body.Bytes(), &m); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return m
|
|
}
|
|
|
|
func cloneHdr(h map[string]string) map[string]string {
|
|
o := map[string]string{}
|
|
for k, v := range h {
|
|
o[k] = v
|
|
}
|
|
return o
|
|
}
|
|
|
|
func TestGeoAggregation(t *testing.T) {
|
|
h, u := geoSetup(t)
|
|
m := getJSON(t, h, "/api/v1/geo?range=24h&host=all", u)
|
|
cs := m["countries"].([]any)
|
|
if m["range"] != "24h" || len(cs) != 2 {
|
|
t.Fatalf("%v", m)
|
|
}
|
|
br := cs[0].(map[string]any)
|
|
if br["cc"] != "BR" || br["events"].(float64) != 8+2+5+1 || br["ips"].(float64) != 4 || br["hostile"].(float64) != 1 {
|
|
t.Fatalf("BR %v", br)
|
|
}
|
|
if c := br["cats"].(map[string]any); c["exploit"].(float64) != 8 || c["scanner"].(float64) != 7 || c["notfound"] != nil {
|
|
t.Fatalf("cats %v", c)
|
|
}
|
|
if cs[1].(map[string]any)["cc"] != "US" {
|
|
t.Fatalf("order %v", cs)
|
|
}
|
|
|
|
d := getJSON(t, h, "/api/v1/geo?cc=BR", u)
|
|
if d["cc"] != "BR" || len(d["regions"].([]any)) != 3 || len(d["cities"].([]any)) != 3 || len(d["top_ips"].([]any)) != 4 {
|
|
t.Fatalf("detail %v", d)
|
|
}
|
|
top := d["top_ips"].([]any)[0].(map[string]any)
|
|
if top["ip"] != "200.1.1.1" || top["level"] != "hostile" || top["city"] != "Recife" || top["asn"].(float64) != 64500 {
|
|
t.Fatalf("top ip %v", top)
|
|
}
|
|
for _, c := range d["cities"].([]any) {
|
|
c := c.(map[string]any)
|
|
_, hasLat := c["lat"]
|
|
if (c["name"] == "Recife") != hasLat {
|
|
t.Fatalf("coordinates only for cities that have them: %v", c)
|
|
}
|
|
}
|
|
empty := false
|
|
for _, r := range d["regions"].([]any) {
|
|
empty = empty || r.(map[string]any)["name"] == ""
|
|
}
|
|
if !empty {
|
|
t.Fatal("empty region must be kept as an empty name")
|
|
}
|
|
|
|
r := getJSON(t, h, "/api/v1/geo?cc=BR®ion=Pernambuco", u)
|
|
if r["events"].(float64) != 10 || len(r["cities"].([]any)) != 1 || len(r["top_ips"].([]any)) != 2 || r["ips"].(float64) != 2 || r["region"] != "Pernambuco" {
|
|
t.Fatalf("region %v", r)
|
|
}
|
|
if w := do(h, "GET", "/api/v1/geo?cc=br", cloneHdr(u), nil); w.Code != 400 {
|
|
t.Fatalf("lowercase cc: %d", w.Code)
|
|
}
|
|
if w := do(h, "GET", "/api/v1/geo?host=nope", cloneHdr(u), nil); w.Code != 404 {
|
|
t.Fatalf("unknown host: %d", w.Code)
|
|
}
|
|
if w := do(h, "GET", "/api/v1/geo", nil, nil); w.Code != 401 {
|
|
t.Fatalf("anonymous: %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestLocationOnSummaryAndRecent(t *testing.T) {
|
|
h, u := geoSetup(t)
|
|
m := getJSON(t, h, "/api/v1/summary", u)
|
|
loc := m["hosts"].([]any)[0].(map[string]any)["location"].(map[string]any)
|
|
if loc["lat"].(float64) != 1 || loc["label"] != "Home" {
|
|
t.Fatalf("%v", loc)
|
|
}
|
|
rc := getJSON(t, h, "/api/v1/live/recent?limit=3", u)["events"].([]any)
|
|
if len(rc) != 3 {
|
|
t.Fatalf("limit: %d", len(rc))
|
|
}
|
|
all := getJSON(t, h, "/api/v1/live/recent?limit=9999", u)["events"].([]any)
|
|
if len(all) != 8+2+5+1+3+1 {
|
|
t.Fatalf("only malicious events: %d", len(all))
|
|
}
|
|
var seenSrc bool
|
|
for _, e := range all {
|
|
e := e.(map[string]any)
|
|
if e["host"] != "oracle" || e["dst"].(map[string]any)["label"] != "Home" || e["cat"] == "notfound" {
|
|
t.Fatalf("event %v", e)
|
|
}
|
|
if s, ok := e["src"].(map[string]any); ok && e["ip"] == "200.1.1.1" {
|
|
seenSrc = s["cc"] == "BR" && s["city"] == "Recife"
|
|
}
|
|
if e["ip"] == "8.8.8.8" && e["src"] != nil {
|
|
t.Fatal("src omitted when unknown")
|
|
}
|
|
}
|
|
if !seenSrc {
|
|
t.Fatal("src missing")
|
|
}
|
|
}
|
|
|
|
func TestStaticAssetsWhitelist(t *testing.T) {
|
|
h, _ := setup(t)
|
|
if w := do(h, "GET", "/style.css", nil, nil); w.Code != 200 || !strings.HasPrefix(w.Header().Get("Content-Type"), "text/css") || w.Header().Get("Content-Security-Policy") == "" {
|
|
t.Fatalf("css %d %v", w.Code, w.Header())
|
|
}
|
|
if w := do(h, "GET", "/logo.png", nil, nil); w.Code != 200 || w.Header().Get("Content-Type") != "image/png" {
|
|
t.Fatalf("png %d", w.Code)
|
|
}
|
|
for _, p := range []string{"/index.html", "/login.html", "/missing.js", "/Style.css", "/web", "/..%2fserver.go", "/%2e%2e/go.mod", "/a.go", "/.js", "/app.js/", "/x/app.js"} {
|
|
if w := do(h, "GET", p, nil, nil); w.Code == 200 {
|
|
t.Errorf("%s must not be served (200)", p)
|
|
}
|
|
}
|
|
}
|