package app import ( "net" "net/netip" "os" "strconv" "strings" "sync" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/egress" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/hub" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/intel" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store" ) // egressMode reports whether the egress analysis may exist at all. func egressOff(c Config) bool { return strings.ToLower(c.Egress) == "off" } // newEgressEngine builds the egress engine of one node from its own // configuration (workloads, resolvers, allow); the primary probe falls back // to the URUTAU_EGRESS_* defaults of the hub. func newEgressEngine(c Config, st *store.Store, eg hub.Egress, primary bool) (*egress.Engine, error) { workloads, resolvers, extra := eg.Workloads, eg.Resolvers, eg.Allow if primary { workloads, resolvers = orDefault(workloads, c.EgressWorkloads), orDefault(resolvers, c.EgressResolvers) extra = orDefault(extra, c.EgressAllow) } var provMu sync.Mutex var provCache []string provider := func() []string { provMu.Lock() defer provMu.Unlock() if provCache == nil { provCache = providerASNs(os.Getenv("URUTAU_EGRESS_PROVIDER_ASN"), func(ip string) uint32 { return st.GeoOf(ip).ASN }) } return provCache } return egress.New(egress.Config{ Workloads: workloads, Resolvers: resolvers, Allow: egressAllow(c, extra), ResolvConf: []string{}, // the probe sends its own resolv.conf TorRelay: func(a netip.Addr) bool { return intel.IsTorRelay(a) && !intel.IsTorExit(a) }, ProviderASNs: provider, Geo: func(ip string) (string, string, string) { g := st.GeoOf(ip) asn := "" if g.ASN != 0 { asn = "AS" + strconv.FormatUint(uint64(g.ASN), 10) } return g.CC, asn, g.ASOrg }, BadDst: func(a netip.Addr) (bool, string) { if intel.IsTorExit(a) { return true, "tor-exit" } if st.KnownHostile(a.String()) { return true, "attacked-us" } return false, "" }, }) } // egressExtraAllow parses a comma separated list of CIDRs and addresses // (URUTAU_EGRESS_ALLOW: legitimate periodic pollers). func egressExtraAllow(spec string) []netip.Prefix { var out []netip.Prefix for _, s := range strings.Split(spec, ",") { if s = strings.TrimSpace(s); s == "" { continue } if p, err := netip.ParsePrefix(s); err == nil { out = append(out, p.Masked()) } else if a, err := netip.ParseAddr(s); err == nil { a = a.Unmap() out = append(out, netip.PrefixFrom(a, a.BitLen())) } } return out } // ownPublicPrefixes returns the global unicast addresses of the local // interfaces: the server's own addresses never produce a finding. func ownPublicPrefixes() []netip.Prefix { var out []netip.Prefix for _, a := range localPublicAddrs() { out = append(out, netip.PrefixFrom(a, a.BitLen())) } return out } func localPublicAddrs() []netip.Addr { addrs, err := net.InterfaceAddrs() if err != nil { return nil } var out []netip.Addr for _, ad := range addrs { ipn, ok := ad.(*net.IPNet) if !ok { continue } a, ok := netip.AddrFromSlice(ipn.IP) if !ok { continue } a = a.Unmap() if a.IsGlobalUnicast() && !a.IsPrivate() && !a.IsLoopback() && !a.IsLinkLocalUnicast() && !(a.Is4() && netip.MustParsePrefix("100.64.0.0/10").Contains(a)) { out = append(out, a) } } return out } // providerASNs lists the hosting provider's ASNs ("AS31898"): the explicit // list (env, "31898,AS47583") wins, otherwise the ASN of the server's public // interface addresses. nil (retried later) when none is known yet. func providerASNs(explicit string, asnOf func(ip string) uint32) []string { var out []string seen := map[string]bool{} add := func(n uint64) { k := "AS" + strconv.FormatUint(n, 10) if n != 0 && !seen[k] { seen[k] = true out = append(out, k) } } for _, s := range strings.Split(explicit, ",") { s = strings.TrimPrefix(strings.ToUpper(strings.TrimSpace(s)), "AS") if n, err := strconv.ParseUint(s, 10, 32); err == nil { add(n) } } if len(out) > 0 { return out } for _, a := range localPublicAddrs() { add(uint64(asnOf(a.String()))) } return out } // egressAllow is the list of destinations that never produce a finding: the // trusted origins (our own servers, an invalid item does not discard the // others), the extra list of the probe and the local public addresses. func egressAllow(c Config, extra string) []netip.Prefix { var allow []netip.Prefix for _, part := range strings.Split(c.Trusted, ",") { if nets, err := core.ParseCIDRs(part); err == nil { allow = append(allow, nets...) } } allow = append(allow, egressExtraAllow(extra)...) return append(allow, ownPublicPrefixes()...) }