| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- package adapters
- // impostor_test.go — #1358: alerta wifi-ssid-impostor desde wifi_scans +
- // BSSIDs/SSIDs propios del registry. BD real en dir temporal (wifi_scans
- // la crea db.Open), engine en memoria (l.engine de NewLive).
- import (
- "testing"
- "time"
- "github.com/gnacho/netpulse/agent/probe"
- "github.com/gnacho/netpulse/server-go/internal/db"
- )
- // newImpostorLive: Live con db real (wifi_scans) y registry con la flota
- // mínima: un agente ("ap1") cuyo SSID propio es "temiscira".
- func newImpostorLive(t *testing.T) (*Live, *AgentRegistry) {
- t.Helper()
- d, err := db.Open(t.TempDir())
- if err != nil {
- t.Fatalf("db.Open: %v", err)
- }
- t.Cleanup(func() { d.Close() })
- reg := NewAgentRegistry(90 * time.Second)
- reg.Ingest(&probe.Payload{
- Router: "ap1",
- Version: "3.0.16",
- Ts: time.Now().Unix(),
- Data: probe.PayloadData{
- Wireless: &probe.WirelessData{
- OwnBssids: []probe.OwnBSSID{
- {Iface: "wlan0", BSSID: "AA:BB:CC:00:00:01", SSID: "temiscira"},
- },
- },
- },
- })
- l := NewLive(nil, d, []RouterConfig{{ID: "ap1", Name: "AP Uno"}}, nil)
- l.SetAgents(reg)
- return l, reg
- }
- // insertScan: sighting directo en wifi_scans (lo que haría SaveScan).
- func insertScan(t *testing.T, l *Live, routerID, bssid, ssid string, ts time.Time) {
- t.Helper()
- _, err := l.db.Exec(
- `INSERT INTO wifi_scans (router_id, iface, bssid, ssid, channel, freq, signal_dbm, width_mhz, ts)
- VALUES (?, 'wlan0', ?, ?, 6, 2437, -55, 20, ?)`,
- routerID, bssid, ssid, ts.Unix())
- if err != nil {
- t.Fatalf("insert scan: %v", err)
- }
- }
- func impostorAlerts(l *Live) []AlertEvent {
- var out []AlertEvent
- for _, a := range l.engine.List() {
- if a.Type == "wifi-ssid-impostor" {
- out = append(out, a)
- }
- }
- return out
- }
- func TestImpostorSingleSightingDoesNotAlert(t *testing.T) {
- l, _ := newImpostorLive(t)
- now := time.Now()
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:01", "temiscira", now)
- l.EvalSsidImpostors()
- if got := impostorAlerts(l); len(got) != 0 {
- t.Fatalf("un sighting suelto no debe alertar; alertas: %+v", got)
- }
- }
- func TestImpostorConfirmedAfterDwell(t *testing.T) {
- l, _ := newImpostorLive(t)
- now := time.Now()
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:02", "temiscira", now.Add(-30*time.Minute))
- l.EvalSsidImpostors()
- if got := impostorAlerts(l); len(got) != 0 {
- t.Fatalf("un solo scan real aunque sea viejo no debe alertar")
- }
- // Segundo scan real (otro ts) separado >= dwell: confirma.
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:02", "temiscira", now)
- l.EvalSsidImpostors()
- got := impostorAlerts(l)
- if len(got) != 1 {
- t.Fatalf("dwell cumplido debe alertar una vez; alertas: %d", len(got))
- }
- a := got[0]
- if !a.Urgent || a.Severity != "critical" || a.Category != "clients" {
- t.Fatalf("severidad/categoría/urgencia: %+v", a)
- }
- if a.Vars["bssid"] != "DE:AD:BE:EF:00:02" || a.Vars["ssid"] != "temiscira" || a.Vars["router"] != "AP Uno" {
- t.Fatalf("vars: %+v", a.Vars)
- }
- // Re-evaluar con la misma evidencia NO duplica (ID estable + tracking).
- l.EvalSsidImpostors()
- if got := impostorAlerts(l); len(got) != 1 {
- t.Fatalf("re-evaluación no debe duplicar; alertas: %d", len(got))
- }
- }
- func TestImpostorOwnBssidIgnored(t *testing.T) {
- l, _ := newImpostorLive(t)
- now := time.Now()
- // BSSID propio (del registry) anunciando el SSID propio: normal.
- insertScan(t, l, "ap1", "AA:BB:CC:00:00:01", "temiscira", now.Add(-40*time.Minute))
- insertScan(t, l, "ap1", "aa:bb:cc:00:00:01", "temiscira", now) // minúsculas
- l.EvalSsidImpostors()
- if got := impostorAlerts(l); len(got) != 0 {
- t.Fatalf("BSSID propio no es impostor; alertas: %+v", got)
- }
- }
- func TestImpostorForeignSsidIgnored(t *testing.T) {
- l, _ := newImpostorLive(t)
- now := time.Now()
- // SSID que no es de la flota: vecino normal.
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:03", "wifi-del-vecino", now.Add(-40*time.Minute))
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:03", "wifi-del-vecino", now)
- l.EvalSsidImpostors()
- if got := impostorAlerts(l); len(got) != 0 {
- t.Fatalf("SSID ajeno no alerta; alertas: %+v", got)
- }
- }
- // #1402: la unidad cuyo payload no trae ownBssids (y no se sondea por SSH)
- // se reconoce por routers.mac: su bridge MAC y sus radios derivadas (+1/+2)
- // no deben alertar como impostor. Un BSSID realmente ajeno SÍ sigue alertando.
- func TestImpostorFleetMacSeeded(t *testing.T) {
- l, _ := newImpostorLive(t)
- // Unidad de flota sin agente: solo su MAC conocida en routers.mac.
- if _, err := l.db.Exec(
- `INSERT INTO routers (id, name, host, type, created_at, mac) VALUES ('ap2', 'AP Dos', '192.168.8.3', 'openwrt', 1, 'aa:bb:cc:00:10:00')`,
- ); err != nil {
- t.Fatalf("insert router: %v", err)
- }
- now := time.Now()
- // Radio = mac+1 anunciando el SSID de la flota: propia, no impostor.
- insertScan(t, l, "ap1", "AA:BB:CC:00:10:01", "temiscira", now.Add(-40*time.Minute))
- insertScan(t, l, "ap1", "AA:BB:CC:00:10:01", "temiscira", now)
- l.EvalSsidImpostors()
- if got := impostorAlerts(l); len(got) != 0 {
- t.Fatalf("radio derivada de routers.mac no es impostor; alertas: %+v", got)
- }
- // BSSID realmente ajeno: la regla sigue viva.
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:09", "temiscira", now.Add(-40*time.Minute))
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:09", "temiscira", now)
- l.EvalSsidImpostors()
- if got := impostorAlerts(l); len(got) != 1 {
- t.Fatalf("BSSID ajeno debía alertar; alertas: %+v", got)
- }
- }
- func TestImpostorSamePayloadFrozenDoesNotConfirm(t *testing.T) {
- l, _ := newImpostorLive(t)
- now := time.Now()
- // El mismo scan (mismo ts) duplicado por re-push del payload congelado:
- // NO son dos scans reales.
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:04", "temiscira", now.Add(-30*time.Minute))
- insertScan(t, l, "ap2", "DE:AD:BE:EF:00:04", "temiscira", now.Add(-30*time.Minute))
- l.EvalSsidImpostors()
- if got := impostorAlerts(l); len(got) != 0 {
- t.Fatalf("mismo ts en dos routers no confirma; alertas: %+v", got)
- }
- }
- func TestImpostorAutoResolve(t *testing.T) {
- l, _ := newImpostorLive(t)
- now := time.Now()
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:05", "temiscira", now.Add(-90*time.Minute))
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:05", "temiscira", now.Add(-60*time.Minute))
- l.EvalSsidImpostors()
- if got := impostorAlerts(l); len(got) != 1 {
- t.Fatalf("debió alertar (dwell cumplido y reciente); alertas: %d", len(got))
- }
- // Pasan >impostorRecent sin nuevos sightings: resolve. La ventana de la
- // query usa el reloj real; simulamos el paso del tiempo retirando la
- // evidencia (equivale a que los scans envejecieron fuera de la ventana).
- _, _ = l.db.Exec(`DELETE FROM wifi_scans`)
- l.EvalSsidImpostors()
- if l.engine.Has("alert-ssid-impostor-DE:AD:BE:EF:00:05") {
- t.Fatal("impostor desaparecido debe auto-resolverse")
- }
- if len(l.impostorActive) != 0 {
- t.Fatalf("tracking residual: %+v", l.impostorActive)
- }
- }
- func TestLocalSsids(t *testing.T) {
- reg := NewAgentRegistry(90 * time.Second)
- reg.Ingest(&probe.Payload{
- Router: "ap1", Version: "3.0.16", Ts: time.Now().Unix(),
- Data: probe.PayloadData{
- Wireless: &probe.WirelessData{
- OwnBssids: []probe.OwnBSSID{
- {Iface: "wlan0", BSSID: "AA:BB:CC:00:00:01", SSID: "temiscira"},
- {Iface: "wlan1", BSSID: "AA:BB:CC:00:00:02", SSID: "temiscira-guest"},
- {Iface: "wlan2", BSSID: "AA:BB:CC:00:00:03", SSID: ""}, // sin SSID: fuera
- },
- },
- Dawn: &probe.DawnData{SSIDs: map[string]probe.DawnSSID{
- "temiscira": {APs: []probe.DawnAP{{BSSID: "AA:BB:CC:00:00:01", Local: true}}},
- "vecina": {APs: []probe.DawnAP{{BSSID: "11:22:33:44:55:66", Local: false}}}, // no local: fuera
- }},
- },
- })
- got := reg.LocalSsids()
- want := map[string]bool{"temiscira": true, "temiscira-guest": true}
- if len(got) != len(want) {
- t.Fatalf("LocalSsids = %v", got)
- }
- for _, s := range got {
- if !want[s] {
- t.Fatalf("SSID inesperado: %q (got %v)", s, got)
- }
- }
- }
- func TestImpostorAlertUsesLatestSighting(t *testing.T) {
- l, _ := newImpostorLive(t)
- now := time.Now()
- // El sighting RECIENTE insertado primero (rowid menor: la BD lo
- // devuelve antes sin ORDER BY). Sin inicializar router/señal en la
- // creación del tracking, la alerta salía con esos campos vacíos.
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:06", "temiscira", now)
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:06", "temiscira", now.Add(-30*time.Minute))
- l.EvalSsidImpostors()
- got := impostorAlerts(l)
- if len(got) != 1 {
- t.Fatalf("debió alertar; alertas: %d", len(got))
- }
- if got[0].Vars["router"] != "AP Uno" || got[0].Vars["signal"] != "-55" {
- t.Fatalf("vars deben ser del sighting más reciente: %+v", got[0].Vars)
- }
- }
- func TestImpostorApprovedDoesNotAlert(t *testing.T) {
- l, _ := newImpostorLive(t)
- now := time.Now()
- // #1400: BSSID aprobado a mano (AP UniFi ajeno a la flota NetPulse).
- if err := l.ApproveImpostorBssid("de:ad:be:ef:00:10"); err != nil {
- t.Fatalf("approve: %v", err)
- }
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:10", "temiscira", now.Add(-40*time.Minute))
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:10", "temiscira", now)
- l.EvalSsidImpostors()
- if got := impostorAlerts(l); len(got) != 0 {
- t.Fatalf("BSSID aprobado no debe alertar; alertas: %+v", got)
- }
- if got := l.ImpostorApproved(); len(got) != 1 || got[0] != "DE:AD:BE:EF:00:10" {
- t.Fatalf("ImpostorApproved = %v", got)
- }
- }
- func TestImpostorApproveResolvesActiveAlert(t *testing.T) {
- l, _ := newImpostorLive(t)
- now := time.Now()
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:11", "temiscira", now.Add(-40*time.Minute))
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:11", "temiscira", now)
- l.EvalSsidImpostors()
- if got := impostorAlerts(l); len(got) != 1 {
- t.Fatalf("debió alertar; alertas: %d", len(got))
- }
- if err := l.ApproveImpostorBssid("DE:AD:BE:EF:00:11"); err != nil {
- t.Fatalf("approve: %v", err)
- }
- if got := impostorAlerts(l); len(got) != 0 {
- t.Fatalf("aprobar debe resolver la alerta activa; alertas: %+v", got)
- }
- if len(l.impostorActive) != 0 {
- t.Fatalf("tracking residual tras aprobar: %+v", l.impostorActive)
- }
- // Re-evaluar con la evidencia intacta: no re-alerta (aprobado).
- l.EvalSsidImpostors()
- if got := impostorAlerts(l); len(got) != 0 {
- t.Fatalf("aprobado no debe re-alertar; alertas: %+v", got)
- }
- }
- func TestImpostorRevokeReAlerts(t *testing.T) {
- l, _ := newImpostorLive(t)
- now := time.Now()
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:12", "temiscira", now.Add(-40*time.Minute))
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:12", "temiscira", now)
- if err := l.ApproveImpostorBssid("DE:AD:BE:EF:00:12"); err != nil {
- t.Fatalf("approve: %v", err)
- }
- l.EvalSsidImpostors()
- if got := impostorAlerts(l); len(got) != 0 {
- t.Fatalf("aprobado no debe alertar; alertas: %+v", got)
- }
- l.RevokeImpostorBssid("DE:AD:BE:EF:00:12")
- if got := l.ImpostorApproved(); len(got) != 0 {
- t.Fatalf("revoke debe vaciar la lista; got %v", got)
- }
- l.EvalSsidImpostors()
- if got := impostorAlerts(l); len(got) != 1 {
- t.Fatalf("revocado y visible debe re-alertar; alertas: %d", len(got))
- }
- }
- func TestImpostorApproveInvalidBssid(t *testing.T) {
- l, _ := newImpostorLive(t)
- for _, bad := range []string{"", "no-es-mac", "DE:AD:BE:EF:00", "zz:zz:zz:zz:zz:zz"} {
- if err := l.ApproveImpostorBssid(bad); err == nil {
- t.Fatalf("bssid inválido %q debe dar error", bad)
- }
- }
- if got := l.ImpostorApproved(); len(got) != 0 {
- t.Fatalf("ningún inválido debe entrar en la lista; got %v", got)
- }
- }
- func TestImpostorAllowlistPersistsAcrossRestart(t *testing.T) {
- l, _ := newImpostorLive(t)
- if err := l.ApproveImpostorBssid("DE:AD:BE:EF:00:13"); err != nil {
- t.Fatalf("approve: %v", err)
- }
- // Simula un reinicio: Live NUEVO sobre la misma BD (mismo dir).
- dir := t.TempDir() // dummy para no confundir; la BD real es la de l
- _ = dir
- d2 := l.db // misma BD abierta
- l2 := NewLive(nil, d2, []RouterConfig{{ID: "ap1", Name: "AP Uno"}}, nil)
- if got := l2.ImpostorApproved(); len(got) != 1 || got[0] != "DE:AD:BE:EF:00:13" {
- t.Fatalf("la lista debe sobrevivir al reinicio; got %v", got)
- }
- }
- func TestImpostorRuleDisabledNoAlerts(t *testing.T) {
- l, _ := newImpostorLive(t)
- now := time.Now()
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:20", "temiscira", now.Add(-40*time.Minute))
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:20", "temiscira", now)
- l.SetImpostorRuleEnabled(false)
- if l.ImpostorRuleEnabled() {
- t.Fatal("la regla debe quedar desactivada")
- }
- l.EvalSsidImpostors()
- if got := impostorAlerts(l); len(got) != 0 {
- t.Fatalf("regla desactivada no debe alertar; alertas: %+v", got)
- }
- // Persistencia: un Live nuevo sobre la misma BD arranca desactivado.
- l2 := NewLive(nil, l.db, []RouterConfig{{ID: "ap1", Name: "AP Uno"}}, nil)
- if l2.ImpostorRuleEnabled() {
- t.Fatal("el interruptor debe sobrevivir al reinicio")
- }
- }
- func TestImpostorRuleDisableResolvesActive(t *testing.T) {
- l, _ := newImpostorLive(t)
- now := time.Now()
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:21", "temiscira", now.Add(-40*time.Minute))
- insertScan(t, l, "ap1", "DE:AD:BE:EF:00:21", "temiscira", now)
- l.EvalSsidImpostors()
- if got := impostorAlerts(l); len(got) != 1 {
- t.Fatalf("debió alertar; alertas: %d", len(got))
- }
- l.SetImpostorRuleEnabled(false)
- if got := impostorAlerts(l); len(got) != 0 {
- t.Fatalf("desactivar debe resolver las activas; alertas: %+v", got)
- }
- if len(l.impostorActive) != 0 {
- t.Fatalf("tracking residual tras desactivar: %+v", l.impostorActive)
- }
- // Re-activar: la evidencia sigue ahí, vuelve a alertar.
- l.SetImpostorRuleEnabled(true)
- l.EvalSsidImpostors()
- if got := impostorAlerts(l); len(got) != 1 {
- t.Fatalf("re-activar debe re-alertar con evidencia vigente; alertas: %d", len(got))
- }
- }
|