package app import ( "context" "encoding/json" "log/slog" "net/http" "net/http/httptest" "net/netip" "os" "path/filepath" "strings" "testing" "time" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/geo" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/intel" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/server" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store" ) const realGeo = "/tmp/claude-1000/-home-lucas-Documents-Pessoal-keys/cd25aef5-8f8d-45f2-8ce0-f6644a45cb62/scratchpad/geo/real" // Known addresses against the real DB-IP Lite databases: the ASN is filled and // the network type follows. Skipped where the databases are not available. func TestNetTypeWithRealGeoDatabases(t *testing.T) { if _, err := os.Stat(realGeo); err != nil { t.Skip("real geo databases not available") } gr, err := geo.Open(realGeo) if err != nil { t.Fatal(err) } st := store.New(store.Config{Name: "o", Retention: 720 * time.Hour}) st.SetEnricher(enricher{g: gr}) h := server.New(server.Options{Token: "user-token-0123456789-abcdefghijklmnop", Version: "t", Store: st}) cases := []struct { ip string asn float64 typ string orgPart string }{ {"159.89.1.1", 14061, "hosting", "DigitalOcean"}, {"78.46.1.1", 24940, "hosting", "Hetzner"}, {"129.146.1.1", 31898, "hosting", "Oracle"}, {"189.6.1.1", 28573, "isp/mobile", "Claro"}, // Brazilian residential/mobile operator {"71.6.135.131", 10439, "research-scanner", "CariNet"}, } for _, c := range cases { st.Ingest(core.Event{Time: time.Now(), IP: c.ip, Cat: core.Scanner, Source: "traefik", Method: "GET", Path: "/.env", Status: 404}) r := httptest.NewRequest("GET", "/api/v1/ip/"+c.ip, nil) r.Header.Set("Authorization", "Bearer user-token-0123456789-abcdefghijklmnop") r.RemoteAddr = "198.51.100.1:1" w := httptest.NewRecorder() h.ServeHTTP(w, r) var d map[string]any if err := json.Unmarshal(w.Body.Bytes(), &d); err != nil || w.Code != 200 { t.Fatalf("%s: %d %s", c.ip, w.Code, w.Body) } id := d["identity"].(map[string]any) if id["asn"].(float64) != c.asn || id["net_type"] != c.typ || !strings.Contains(id["as_org"].(string), c.orgPart) || id["net_type_reason"] == "" { t.Errorf("%s: %v", c.ip, id) } if d["geo"].(map[string]any)["cc"] == "" { t.Errorf("%s: no country", c.ip) } } // the Tor exit list changes the type of a listed address a := netip.MustParseAddr("159.89.1.1") intel.SetTorExits([]netip.Addr{a}) defer intel.SetTorExits(nil) if typ, _ := intel.NetTypeIP(a, 14061, "DigitalOcean, LLC", ""); typ != "tor" { t.Fatalf("tor exit: %s", typ) } } func TestTorExitsFile(t *testing.T) { p := filepath.Join(t.TempDir(), "tor.txt") os.WriteFile(p, []byte("# exits\n185.220.101.1\n\n 2001:db8::5 # v6\nnot-an-ip\n10.0.0.1%eth0\n185.220.101.2\n"), 0o600) l, err := LoadTorExits(p) if err != nil || len(l) != 3 { t.Fatalf("%v %v", l, err) } big := filepath.Join(t.TempDir(), "big.txt") os.WriteFile(big, []byte(strings.Repeat("1.2.3.4\n", 2<<20)), 0o600) if _, err := LoadTorExits(big); err == nil { t.Fatal("oversized file must be refused") } if _, err := LoadTorExits(t.TempDir()); err == nil { t.Fatal("a directory is not a list") } ctx, cancel := context.WithCancel(context.Background()) defer cancel() defer intel.SetTorExits(nil) watchTorExits(ctx, p, time.Hour, slog.New(slog.NewTextHandler(os.Stderr, nil))) if !intel.IsTorExit(netip.MustParseAddr("185.220.101.1")) { t.Fatal("exit list not applied") } watchTorExits(ctx, "", time.Hour, nil) // off: nothing happens c, _ := ParseFlags("serve", []string{"--tor-exits", p}) if c.TorExits != p { t.Fatal("flag") } _ = http.StatusOK }