package store import ( "encoding/json" "math" "path/filepath" "strings" "sync" "sync/atomic" "testing" "time" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/intel" ) func TestListedSinceAndMigration(t *testing.T) { s := store30("x") s.SetAdminUsers([]string{"lucas"}) var at int64 for i := 0; i < 8; i++ { e := ev("1.1.1.1", core.Exploit, time.Duration(8-i)*time.Minute) e.Strong = true s.Ingest(e) // 8 per event: crosses 50 on the 7th if r := s.ips["1.1.1.1"]; r.ListedSince != 0 && at == 0 { at = r.ListedSince } } if at != e0(t0, 2*time.Minute) { t.Fatalf("ListedSince %d, want the instant of the 7th event", at) } s.ips["1.1.1.1"].ListedSince = 0 // a record saved before the field existed path := filepath.Join(t.TempDir(), "s.gob") if err := s.Save(path, nil); err != nil { t.Fatal(err) } s2 := store30("x") if _, err := s2.Load(path); err != nil { t.Fatal(err) } if got := s2.ips["1.1.1.1"].ListedSince; got == 0 || got != s2.ips["1.1.1.1"].Last { t.Fatalf("migration must estimate it: %d", got) } if info, _ := s2.IPInfoOf("1.1.1.1"); info.ListedSince == 0 { t.Fatal("info") } // an admin login ends it s.ips["1.1.1.1"].ListedSince = at s.Ingest(core.Event{Time: t0.Add(time.Second), IP: "1.1.1.1", Cat: core.SSHOK, Source: "ssh", User: "lucas"}) if s.ips["1.1.1.1"].ListedSince != 0 { t.Fatalf("an admin login ends the listing: %d", s.ips["1.1.1.1"].ListedSince) } if !s.IsAdminUser("lucas") || s.IsAdminUser("root") { t.Fatal("IsAdminUser") } } func e0(t time.Time, ago time.Duration) int64 { return t.Add(-ago).Unix() } func TestIPLogForgedFlag(t *testing.T) { s := store30("x") s.SetEventLog(openLog(t)) e := ev("1.1.1.1", core.Exploit, time.Minute) e.Forged = true s.Ingest(e) s.Ingest(ev("2.2.2.2", core.Exploit, time.Minute)) if !s.IPLogOf("1.1.1.1", t0.Unix()-3600, t0.Unix()).Forged || s.IPLogOf("2.2.2.2", t0.Unix()-3600, t0.Unix()).Forged { t.Fatal("forged flag") } } func samplePeriod(host string, ips ...string) PeriodOut { p := PeriodOut{Enabled: true, Events: 10, SessionsN: len(ips), NewVsReturning: map[string]int{"new": len(ips)}, TopFams: []Count{{Key: "vcs", N: 3}}, TopTechs: []Count{{Key: "T1190", N: 2}}} for _, ip := range ips { p.Sessions = append(p.Sessions, intel.Session{ID: "s-" + ip, IP: ip, Start: t0.Add(-time.Hour), End: t0, N: 3, Hosts: []string{host}, Stage: "exploit", Cats: map[string]int{"exploit": 3}}) } p.FirstSeenTech = []FirstTech{{ID: "T1190", Name: "x", T: t0.Unix() - 100, IP: ips[0]}} p.TopTargets = []Target{{Host: host, Domain: "a.example", N: 5}} return p } func TestSanitizePeerStudyAndMerge(t *testing.T) { st := PeerStudy{V: ExportVersion, Period: samplePeriod("EVIL", "1.2.3.4", "not-an-ip", "5.6.7.8"), Metrics: MetricsOut{}} st.Period.Sessions[0].Cats = map[string]int{} for i := 0; i < 500; i++ { st.Period.Sessions[0].Cats[strings.Repeat("c", 1+i)] = i st.Period.Sessions = append(st.Period.Sessions, st.Period.Sessions[2]) } st.Period.Sessions[2].Start = time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC) st.Period.FirstSeenTech[0].Name = "