// Package ingress aggregates the firewall telemetry of the host kit: the exact // once-a-minute counters of the inbound observation table and a rate-limited // sample of the connection attempts it saw. // // It is telemetry only. Nothing in this package emits an event, a score or a // ban decision: attempts, banned sources included, are never evidence (a ban // would otherwise feed on its own dropped packets). It deliberately does not // import the store, the intelligence or the enforce packages. package ingress import ( "errors" "net/netip" "strconv" "strings" "time" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/egress" ) // Kernel log prefixes written by deploy/host/urutau-ingress.nft: IN for a port // the host does not serve, INO for one it does. const ( markerClosed = "URUTAU-IN:" markerOpen = "URUTAU-INO:" ) // Hostile input limits. const ( maxLogLine = 2048 maxCounterLine = 256 maxCounter = 1 << 50 // packets since the table was loaded; far beyond any real count ) var ( ErrNotIngress = errors.New("not an ingress line") ErrTooLong = errors.New("line too long") ErrBadLine = errors.New("invalid ingress line") ) // Hit is one sampled inbound attempt. type Hit struct { Time time.Time Src netip.Addr Proto string // tcp, udp, icmp DPort uint16 // 0 for icmp Open bool // the host serves this port } // ParseHit parses one line of ingress.log. now is the fallback time of a line // whose timestamp is unreadable. func ParseHit(b []byte, now time.Time) (Hit, error) { if len(b) > maxLogLine { return Hit{}, ErrTooLong } line := string(b) open := false marker := markerClosed switch { case strings.Contains(line, markerOpen): open, marker = true, markerOpen case strings.Contains(line, markerClosed): default: return Hit{}, ErrNotIngress } k, err := egress.ParseKernel(b, marker, now) if err != nil { return Hit{}, ErrBadLine } switch k.Proto { case "tcp": if !k.SYN || k.ACK || k.DPort == 0 { return Hit{}, ErrBadLine } case "udp": if k.DPort == 0 { return Hit{}, ErrBadLine } case "icmp": k.DPort, open = 0, false default: return Hit{}, ErrBadLine } // Defense in depth: the kit already skips these, a forged line must not // attribute traffic to a local or reserved address either. a := k.Src if !a.IsValid() || !a.IsGlobalUnicast() || a.IsPrivate() || a.IsLoopback() || a.IsLinkLocalUnicast() || a.IsMulticast() { return Hit{}, ErrBadLine } return Hit{Time: k.Time, Src: a, Proto: k.Proto, DPort: k.DPort, Open: open}, nil } // Counters is one line of ingress.counters. Values are cumulative since the // nftables table was loaded. type Counters struct { TS int64 Total, TCP, UDP, ICMP, Open uint64 } // ParseCounters parses "ts=1767225600 total=120 tcp=100 udp=15 icmp=5 open=30". // Unknown keys are ignored (a newer kit); a missing key, a duplicate or a value // out of range makes the line invalid. func ParseCounters(b []byte) (Counters, error) { if len(b) > maxCounterLine { return Counters{}, ErrTooLong } var c Counters seen := 0 for _, f := range strings.Fields(string(b)) { k, v, ok := strings.Cut(f, "=") if !ok { return Counters{}, ErrBadLine } n, err := strconv.ParseUint(v, 10, 64) if err != nil || n > maxCounter { if k == "ts" || k == "total" || k == "tcp" || k == "udp" || k == "icmp" || k == "open" { return Counters{}, ErrBadLine } continue } bit := 0 switch k { case "ts": c.TS, bit = int64(n), 1 case "total": c.Total, bit = n, 2 case "tcp": c.TCP, bit = n, 4 case "udp": c.UDP, bit = n, 8 case "icmp": c.ICMP, bit = n, 16 case "open": c.Open, bit = n, 32 } if bit == 0 { continue } if seen&bit != 0 { return Counters{}, ErrBadLine } seen |= bit } if seen != 63 || c.TS <= 0 { return Counters{}, ErrBadLine } // A line that contradicts itself is not trusted. if c.TCP+c.UDP+c.ICMP > c.Total+1000 || c.Open > c.TCP+c.UDP { return Counters{}, ErrBadLine } return c, nil }