package server import ( "encoding/json" "net/http" "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/store" ) type fakeRDNS struct{ calls *int } func (fakeRDNS) Lookup(string) string { return "" } func (f fakeRDNS) FCrDNS(ip string, suffixes []string) bool { *f.calls++ return ip == "66.249.66.1" && suffixes[0] == "googlebot.com" } func TestIPDetailGapsClosed(t *testing.T) { st := store.New(store.Config{Name: "oracle", Version: "t", Retention: 720 * time.Hour, AdminUsers: []string{"lucas"}}) l, _ := evlog.Open(evlog.Options{Dir: filepath.Join(t.TempDir(), "events")}) st.SetEventLog(l) calls := 0 st.SetRDNS(fakeRDNS{&calls}) now := time.Now() // a crawler that claims to be Googlebot: verified; a plain scanner: null st.Ingest(core.Event{Time: now, IP: "66.249.66.1", Cat: core.Scanner, Source: "traefik", Method: "GET", Path: "/.env", Status: 404, UA: "Mozilla/5.0 (compatible; Googlebot/2.1)"}) st.Ingest(core.Event{Time: now, IP: "6.6.6.6", Cat: core.Scanner, Source: "traefik", Method: "GET", Path: "/.env", Status: 404, UA: "Googlebot/2.1"}) st.Ingest(core.Event{Time: now, IP: "1.1.1.1", Cat: core.Scanner, Source: "traefik", Method: "GET", Path: "/.env", Status: 404, UA: "curl/8"}) // a forged monitor, hostile enough to be listed for i := 0; i < 7; i++ { st.Ingest(core.Event{Time: now.Add(-time.Duration(i) * time.Minute), IP: "9.9.9.9", Cat: core.Exploit, Strong: true, Forged: true, Source: "traefik", Method: "GET", Path: "/.git/config", Status: 404, UA: "Uptime-Kuma/1.0", Domain: "a.example"}) } // SSH failures with an admin user among them for _, u := range []string{"root", "lucas", "root"} { st.Ingest(core.Event{Time: now, IP: "9.9.9.9", Cat: core.SSHFail, Source: "ssh", User: u}) } // three addresses doing the same: a campaign for _, ip := range []string{"20.0.0.1", "20.0.0.2", "20.0.0.3"} { for _, p := range []string{"/wp-login.php", "/xmlrpc.php"} { st.Ingest(core.Event{Time: now.Add(-time.Hour), IP: ip, Cat: core.Scanner, Source: "traefik", Method: "GET", Path: p, Status: 404, UA: "zgrab/0.x"}) } } h := New(Options{Token: userTok, Version: "t", Store: st}) auth := map[string]string{"Authorization": "Bearer " + userTok} id := func(ip string) map[string]any { return getJSON(t, h, "/api/v1/ip/"+ip, auth)["identity"].(map[string]any) } if id("66.249.66.1")["fcrdns_ok"] != true || id("6.6.6.6")["fcrdns_ok"] != false || id("1.1.1.1")["fcrdns_ok"] != nil { t.Fatalf("fcrdns: %v %v %v", id("66.249.66.1"), id("6.6.6.6"), id("1.1.1.1")) } if calls == 0 || calls > 4 { t.Fatalf("only claimants are verified: %d", calls) } d := getJSON(t, h, "/api/v1/ip/9.9.9.9", auth) if d["tools"].(map[string]any)["forged"] != true { t.Fatalf("forged: %v", d["tools"]) } if getJSON(t, h, "/api/v1/ip/1.1.1.1", auth)["tools"].(map[string]any)["forged"] != false { t.Fatal("forged must be false for others") } bl := d["blocklist"].(map[string]any) since := bl["since"].(float64) if bl["listed"] != true || since <= 0 || since > float64(now.Unix()) || since < float64(now.Add(-8*time.Minute).Unix()) { t.Fatalf("blocklist %v", bl) } users := d["ssh"].(map[string]any)["users"].([]any) adm := map[string]bool{} for _, u := range users { m := u.(map[string]any) adm[m["user"].(string)] = m["admin"].(bool) } if len(adm) != 2 || !adm["lucas"] || adm["root"] { t.Fatalf("ssh users %v", users) } rel := getJSON(t, h, "/api/v1/ip/20.0.0.1", auth)["related"].(map[string]any) camps := rel["campaigns"].([]any) if len(camps) != 1 || camps[0].(map[string]any)["ips_n"].(float64) != 3 { t.Fatalf("campaigns %v", rel) } if rel["same_asn_n"] == nil || len(rel["same_prefix"].([]any)) != 2 { t.Fatalf("related %v", rel) } var m map[string]any w := do(h, "GET", "/api/v1/ip/9.9.9.9", cloneHdr(auth), nil) json.Unmarshal(w.Body.Bytes(), &m) if w.Code != http.StatusOK { t.Fatal(w.Code) } }