package app import ( "fmt" "sync" "time" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/egress" ) const probeMarker = "URUTAU-PROBE:" // probeMaxKeys bounds the aggregation map; when it is full the entries older // than a minute are dropped. const probeMaxKeys = 100000 type probeKey struct { ip string port uint16 proto string } // probeParser turns "URUTAU-PROBE:" kernel lines into scanner events, at most // one per (ip, port, proto) per minute, so a SYN flood does not inflate the store. type probeParser struct { mu sync.Mutex last map[probeKey]int64 // minute of the last event kept now func() time.Time } func newProbeParser() *probeParser { return &probeParser{last: map[probeKey]int64{}, now: time.Now} } func (p *probeParser) parse(b []byte) (core.Event, error) { k, err := egress.ParseKernel(b, probeMarker, p.now()) if err != nil { return core.Event{}, err } if (k.Proto != "tcp" && k.Proto != "udp") || k.DPort == 0 || !k.Src.IsValid() { return core.Event{}, fmt.Errorf("not a tcp/udp probe") } key := probeKey{k.Src.String(), k.DPort, k.Proto} min := k.Time.Unix() / 60 p.mu.Lock() if m, ok := p.last[key]; ok && m >= min { p.mu.Unlock() return core.Event{}, core.ErrIgnored } if len(p.last) >= probeMaxKeys { cut := p.now().Unix()/60 - 1 for kk, m := range p.last { if m < cut { delete(p.last, kk) } } if len(p.last) >= probeMaxKeys { p.last = map[probeKey]int64{} } } p.last[key] = min p.mu.Unlock() return core.Event{Time: k.Time, IP: k.Src.String(), Cat: core.Scanner, Source: "probe", Method: upper(k.Proto), Path: k.Proto + "/" + itoa(int(k.DPort)) + serviceSuffix(k.Proto, k.DPort), Proto: k.Proto, Port: k.DPort}, nil } func upper(s string) string { if s == "udp" { return "UDP" } return "TCP" } func itoa(n int) string { return fmt.Sprint(n) } func serviceSuffix(proto string, port uint16) string { if s := core.PortService(proto, port); s != "" { return " " + s } return "" }