package enforce import ( "strings" "testing" "time" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core" ) func probe(ip string, at time.Time, port uint16) core.Event { return core.Event{Time: at, IP: ip, Cat: core.Scanner, Source: "probe", Proto: "tcp", Port: port} } func TestPortScanBan(t *testing.T) { r := newRig(t, ModeOn, nil) for i := 0; i < 9; i++ { r.e.process(probe("203.0.114.60", r.now.Add(-time.Duration(i)*time.Second), uint16(1000+i)), "") } // the same port again does not add up r.e.process(probe("203.0.114.60", r.now, 1000), "") if len(r.lines(t)) != 0 { t.Fatal("banned below 10 distinct ports") } r.e.process(probe("203.0.114.60", r.now, 3389), "") if l := r.lines(t); len(l) != 1 || !strings.Contains(l[0], "reason=port-scan") { t.Fatalf("%q", l) } } func TestPortScanExcludedAndOld(t *testing.T) { r := newRig(t, ModeOn, nil) for i := 0; i < 12; i++ { // trusted network r.e.process(probe("45.1.1.1", r.now, uint16(2000+i)), "") } for i := 0; i < 12; i++ { // outside the window r.e.process(probe("203.0.114.61", r.now.Add(-time.Hour), uint16(2000+i)), "") } if len(r.lines(t)) != 0 { t.Fatal("excluded or old probes banned") } } func TestPortScanReasonValid(t *testing.T) { if !ValidReason(ReasonPortScan) { t.Fatal("port-scan reason not valid") } }