package server import ( "encoding/json" "net/http" "net/netip" "sort" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/egress" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/incidents" ) func init() { registerUserRoute("GET /api/v1/ip/{ip}/egress", func(s *Server) http.HandlerFunc { return s.ipEgressAPI }) } type ipEgressHost struct { Host string `json:"host"` Conns uint64 `json:"conns"` CC string `json:"cc"` ASN string `json:"asn"` Org string `json:"org"` Workloads []string `json:"workloads"` Ports []uint16 `json:"ports"` } type ipEgressInc struct { ID string `json:"id"` Host string `json:"host"` Title string `json:"title"` Status string `json:"status"` Severity string `json:"severity"` Kind string `json:"kind"` Workload string `json:"workload"` Last int64 `json:"last_seen"` } // ipEgressAPI: GET /api/v1/ip/{ip}/egress. What the node(s) did towards this // address (egress connections, findings, incidents) and when it was last seen // as an inbound source. func (s *Server) ipEgressAPI(w http.ResponseWriter, r *http.Request) { ip, err := netip.ParseAddr(r.PathValue("ip")) if err != nil || ip.Zone() != "" { writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid ip"}) return } ip = ip.Unmap() rg, ok := s.rangeOf(w, r) if !ok { return } host := r.URL.Query().Get("host") if len(host) > 253 { writeJSON(w, http.StatusBadRequest, map[string]string{"error": "bad host"}) return } local, peers, only, ok := s.studyHosts(w, host) if !ok { return } from, to := rg.Bounds(s.opt.Store.Now().Unix()) self := s.opt.Store.Name() out := struct { IP string `json:"ip"` Egress []ipEgressHost `json:"egress"` Findings []egress.Finding `json:"findings"` Incidents []ipEgressInc `json:"incidents"` LastSeenInbound int64 `json:"last_seen_inbound"` }{IP: ip.String(), Egress: []ipEgressHost{}, Findings: []egress.Finding{}, Incidents: []ipEgressInc{}} add := func(h string, d egress.DstOut, fs []egress.Finding) { if d.Conns > 0 || len(d.Workloads) > 0 { out.Egress = append(out.Egress, ipEgressHost{Host: h, Conns: d.Conns, CC: d.CC, ASN: d.ASN, Org: d.Org, Workloads: append([]string{}, d.Workloads...), Ports: append([]uint16{}, d.Ports...)}) } for _, f := range fs { f.Host = h out.Findings = append(out.Findings, f) } } if eng := egressEngine.Load(); local && eng != nil { if d, fs, ok := eng.Dst(from, to, ip); ok { add(self, d, fs) } } if peers && s.opt.Hub != nil { raws := s.opt.Hub.EgressFor(r.Context(), from, to, only) names := make([]string, 0, len(raws)) for n := range raws { names = append(names, n) } sort.Strings(names) for _, n := range names { var pr egress.Report if json.Unmarshal(raws[n], &pr) != nil { continue } pr.Sanitize(n) var fs []egress.Finding for _, f := range pr.Findings { if f.Dst == ip.String() { fs = append(fs, f) } } var d egress.DstOut for _, x := range pr.TopDst { // peers only report their top destinations if x.IP == ip.String() { d = x } } add(n, d, fs) } } sort.Slice(out.Findings, func(i, j int) bool { return out.Findings[i].Last > out.Findings[j].Last }) if len(out.Findings) > 200 { out.Findings = out.Findings[:200] } for _, h := range s.incHosts() { if (host != "" && host != "all" && host != h) || len(out.Incidents) >= 100 { continue } st := s.incStore(h) if st == nil { continue } for _, t := range st.All(incidents.Query{Q: ip.String()}) { if t.Dst != ip.String() { continue } out.Incidents = append(out.Incidents, ipEgressInc{ID: h + "/" + t.ID, Host: h, Title: t.Title, Status: t.Status, Severity: t.Severity, Kind: t.Kind, Workload: t.Workload, Last: t.LastSeen}) } } sort.Slice(out.Incidents, func(i, j int) bool { return out.Incidents[i].Last > out.Incidents[j].Last }) key := ip.String() if d, ok := s.opt.Store.IP(key); ok { out.LastSeenInbound = d.Last } if s.opt.Hub != nil { for _, d := range s.opt.Hub.IPDetail(key) { if d.Last > out.LastSeenInbound { out.LastSeenInbound = d.Last } } } writeJSON(w, 200, out) }