package app import ( "context" "fmt" "net" "net/netip" "path/filepath" "strings" "time" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/enforce" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store" ) // newEnforce builds the second firewall of one node and hooks it into the // node's store; nil when off. The decisions go to sink (the probe's queue). func newEnforce(c Config, st *store.Store, name, dir string, sink func(kind, line string) error, en enricher, ready func() bool) *enforce.Engine { mode := strings.ToLower(c.Enforce) if mode != enforce.ModeObserve && mode != enforce.ModeOn { return nil } trusted, _ := core.ParseCIDRs(c.Ignore + "," + c.Trusted) e := enforce.New(enforce.Config{ Mode: mode, Sink: sink, StatePath: filepath.Join(dir, "enforce.json"), Host: name, Threshold: c.EnforceThreshold, MaxPerMin: c.EnforceMaxPerMin, Share: strings.EqualFold(c.EnforceShare, "on"), Trusted: trusted, Resolve: resolveHost, Crawler: func(ip string, uas []string) bool { if v := st.FCrDNS(ip, uas); v != nil && *v { return true } return st.VerifiedCrawler(ip) // forward-confirmed rDNS excludes it whatever the User-Agent says }, Rep: st.Reputation, Tally: func(ip string, from int64) enforce.Tally { return tallyOf(st.AttackTally(ip, from)) }, Roster: func(from int64) []enforce.Seen { rows := st.AttackRoster(from) out := make([]enforce.Seen, len(rows)) for i, r := range rows { out[i] = enforce.Seen{IP: r.IP, Score: r.Score, Level: r.Level, Jails: r.Jails, Events: r.Events, Last: r.Last, Est: tallyOf(r.Est)} } return out }, Tallies: func(from int64) map[string]enforce.Tally { m := st.AttackTallies(from) out := make(map[string]enforce.Tally, len(m)) for k, v := range m { out[k] = tallyOf(v) } return out }, Ready: ready, Geo: func(ip string) (string, uint32, string) { if en.g == nil { return "", 0, "" } g := en.Geo(ip) return g.CC, g.ASN, g.ASOrg }, }) st.SetIngestHook(e.Observe) return e } func resolveHost(h string) []netip.Addr { if a, err := netip.ParseAddr(h); err == nil { return []netip.Addr{a.Unmap()} } ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() ips, err := net.DefaultResolver.LookupNetIP(ctx, "ip", h) if err != nil { return nil } for i := range ips { ips[i] = ips[i].Unmap() } return ips } // shareDecisions relays, every 30 s, the decisions and unbans of each node to // the others (ENFORCE_SHARE=on); each receiver applies its own exclusions. func shareDecisions(ctx context.Context, nodes func() []*Node) { type mark struct{ dec, un int64 } last := map[[2]string]mark{} // [from, to] t := time.NewTicker(30 * time.Second) defer t.Stop() for { all := nodes() for _, from := range all { if from.Enf == nil { continue } for _, to := range all { if to == from || to.Enf == nil { continue } k := [2]string{from.name, to.name} m := last[k] start := time.Now().Unix() if d := from.Enf.LocalSince(m.dec); len(d) > 0 { to.Enf.AcceptPeer(from.name, d) } if u := from.Enf.UnbansSince(m.un); len(u) > 0 { to.Enf.AcceptPeerUnban(from.name, u) } last[k] = mark{start - 5, start} } } select { case <-ctx.Done(): return case <-t.C: } } } // doctorEnforce reports the mode of the second firewall. Decisions are queued // for the probes, which write them on their hosts (the probe reports a // command that cannot be written as cmd_errors). func doctorEnforce(c Config, add func(name, status, detail string)) { mode := strings.ToLower(c.Enforce) if mode == "" || mode == "off" { add("enforce", "off", "disabled (--enforce=off)") return } add("enforce", "ok", fmt.Sprintf("mode=%s share=%s threshold=%d (decisions are queued for each probe)", mode, strings.ToLower(orDefault(c.EnforceShare, "off")), c.EnforceThreshold)) } func tallyOf(t store.Tally24) enforce.Tally { return enforce.Tally{SSH: t.SSH, Web: t.Web, Probe: t.Probe, SSH7: t.SSH7, Web7: t.Web7, Probe7: t.Probe7} }