feat: 1.1.0 interface, geography drill-down and live attack map
Release / Plan (reuse the published image?) (push) Successful in 4s
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 19s
CI / Tests (Linux, race detector) (push) Successful in 35s
CI / Dependency audit (push) Successful in 16s
Release / ci (push) Successful in 35s
Release / Build image (arm64) (push) Successful in 51s
Release / Build image (amd64) (push) Successful in 1m11s
Release / Publish tags (push) Successful in 13s
Release / Gitea release (push) Skipped
Release / Plan (reuse the published image?) (push) Successful in 4s
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 19s
CI / Tests (Linux, race detector) (push) Successful in 35s
CI / Dependency audit (push) Successful in 16s
Release / ci (push) Successful in 35s
Release / Build image (arm64) (push) Successful in 51s
Release / Build image (amd64) (push) Successful in 1m11s
Release / Publish tags (push) Successful in 13s
Release / Gitea release (push) Skipped
A new console interface (side rail, seven pages, opening sentence and active slice on every page, IP drawer, light and dark design tokens); a Geography page that drills from the world to a country's regions and cities; a Live page drawing attacks as arcs on a flat map or a globe. Backend: GET /api/v1/geo, GET /api/v1/live/recent, host/src/dst on live events, --location, a strict static file pattern, and live federation through GET /api/v1/peer/stream (peer token only, bounded and sanitized relay on the hub). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ff143ec4a2
commit
f4b295f582
37 files changed
+5312
-1000
No files matched your search
@@ -0,0 +1,242 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"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, PeerTokens: []string{ptok}, 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", map[string]string{"Authorization": "Bearer " + ptok}, nil); w.Code != 403 {
|
||||
t.Fatalf("peer token: %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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPeerStreamIsPeerOnly(t *testing.T) {
|
||||
old := sseHeartbeat
|
||||
sseHeartbeat = 50 * time.Millisecond
|
||||
defer func() { sseHeartbeat = old }()
|
||||
h, _ := geoSetup(t)
|
||||
if w := do(h, "GET", "/api/v1/peer/stream", map[string]string{"Authorization": "Bearer " + utok}, nil); w.Code != 401 {
|
||||
t.Fatalf("user token: %d", w.Code)
|
||||
}
|
||||
if w := do(h, "GET", "/api/v1/peer/stream", nil, nil); w.Code != 401 {
|
||||
t.Fatalf("anonymous: %d", w.Code)
|
||||
}
|
||||
for _, p := range []string{"/api/v1/events/stream"} {
|
||||
if w := do(h, "GET", p, map[string]string{"Authorization": "Bearer " + ptok}, nil); w.Code != 403 {
|
||||
t.Fatalf("peer token on %s: %d", p, w.Code)
|
||||
}
|
||||
}
|
||||
h2 := New(Options{Token: utok, PeerAllow: mustPrefixes("203.0.113.0/24"), PeerTokens: []string{ptok}, Version: "t", Store: store.New(store.Config{Name: "x"})})
|
||||
if w := do(h2, "GET", "/api/v1/peer/stream", map[string]string{"Authorization": "Bearer " + ptok}, nil); w.Code != 403 {
|
||||
t.Fatalf("--peer-allow: %d", w.Code)
|
||||
}
|
||||
h3 := New(Options{Token: utok, Version: "t", Store: store.New(store.Config{Name: "x"})})
|
||||
if w := do(h3, "GET", "/api/v1/peer/stream", map[string]string{"Authorization": "Bearer " + ptok}, nil); w.Code != 404 {
|
||||
t.Fatalf("federation off: %d", w.Code)
|
||||
}
|
||||
|
||||
st := store.New(store.Config{Name: "edge", Version: "t", Location: &store.Location{Lat: 3, Lon: 4, Label: "Edge"}})
|
||||
st.SetEnricher(fakeGeo{})
|
||||
ts := httptest.NewServer(New(Options{Token: utok, PeerTokens: []string{ptok}, Version: "t", Store: st}))
|
||||
defer ts.Close()
|
||||
req, _ := http.NewRequest("GET", ts.URL+"/api/v1/peer/stream", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+ptok)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
t.Fatal(err, resp)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
go func() {
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
st.Ingest(core.Event{Time: time.Now(), IP: "200.1.1.9", Cat: core.OK, Source: "traefik"}) // not malicious, not sent
|
||||
st.Ingest(core.Event{Time: time.Now(), IP: "200.1.1.9", Cat: core.Scanner, Source: "traefik", Method: "GET", Path: "/wp"})
|
||||
}()
|
||||
sc := bufio.NewScanner(resp.Body)
|
||||
deadline := time.After(5 * time.Second)
|
||||
got := make(chan string, 1)
|
||||
go func() {
|
||||
for sc.Scan() {
|
||||
if strings.HasPrefix(sc.Text(), "data: ") {
|
||||
got <- sc.Text()[6:]
|
||||
return
|
||||
}
|
||||
}
|
||||
got <- ""
|
||||
}()
|
||||
select {
|
||||
case d := <-got:
|
||||
var e map[string]any
|
||||
if json.Unmarshal([]byte(d), &e) != nil || e["ip"] != "200.1.1.9" || e["cat"] != "scanner" || e["dst"].(map[string]any)["label"] != "Edge" || e["src"].(map[string]any)["cc"] != "BR" {
|
||||
t.Fatalf("peer event %s", d)
|
||||
}
|
||||
case <-deadline:
|
||||
t.Fatal("no event")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user