package app import ( "log/slog" "net" "net/netip" "net/url" "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/fed" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/intel" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/server" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store" ) // newEgress builds the egress analysis, or returns nil when it is off (or in // auto mode without the host log). func newEgress(c Config, st *store.Store, log *slog.Logger) (*egress.Source, error) { mode := strings.ToLower(c.Egress) if mode == "off" || c.EgressLog == "" { return nil, nil } if mode == "" || mode == "auto" { if _, err := os.Stat(c.EgressLog); err != nil { return nil, nil } } allow := egressAllow(c) 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 } eng, err := egress.New(egress.Config{ Workloads: c.EgressWorkloads, Resolvers: c.EgressResolvers, Allow: allow, Containers: c.EgressContainers, 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, "" }, }) if err != nil { return nil, err } server.SetEgress(eng) log.Info("egress analysis on", "log", c.EgressLog) return egress.NewSource(c.EgressLog, eng, log), nil } // egressExtraAllow adds to the egress Allow list the addresses of the peers and // the CIDRs of URUTAU_EGRESS_ALLOW (legitimate periodic pollers). func egressExtraAllow(c Config) []netip.Prefix { var out []netip.Prefix for _, ps := range c.Peers { p, err := fed.ParsePeer(ps) if err != nil { continue } if u, err := url.Parse(p.URL); err == nil && u.Hostname() != "" { for _, a := range resolveHost(u.Hostname()) { out = append(out, netip.PrefixFrom(a, a.BitLen())) } } } for _, s := range strings.Split(os.Getenv("URUTAU_EGRESS_ALLOW"), ",") { 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 federation peers, URUTAU_EGRESS_ALLOW and the local public // addresses. func egressAllow(c Config) []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(c)...) return append(allow, ownPublicPrefixes()...) }