From 3ae72e27d68e115dfb648e0cf476948e94f9cc82 Mon Sep 17 00:00:00 2001 From: Lucas Rayan Date: Sun, 15 Nov 2026 08:51:42 -0300 Subject: [PATCH] fix: 1.4.8 egress false positives (non-SYN replies, IMDS on host, Akamai/Telegram) Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 9 +++- CHANGELOG.pt-BR.md | 9 +++- deploy/host/urutau-egress.nft | 4 ++ internal/app/version.go | 2 +- internal/egress/egress_test.go | 78 +++++++++++++++++++++++++++++++++- internal/egress/engine.go | 60 +++++++++++++++++++++++--- internal/egress/parse.go | 20 ++++++++- internal/egress/source.go | 6 ++- 8 files changed, 175 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8408011..26b34ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,14 @@ Format based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/); versions follow SemVer. -## [Unreleased] +## [1.4.8] - 2026-11-15 + +### Fixed + +- Egress analysis, false positives: the nftables kit now logs only TCP connection attempts (SYN without ACK), so replies of banned clients (conntrack recreated mid-flow) no longer show up as `bad_dst` "attacked us". **Re-run `deploy/host/install.sh --enforce` on the host to apply the new nft rules.** +- Hosts with the old kit: the parser drops TCP lines without a pure SYN and counts them as `non_syn_dropped` in the egress source (not as `malformed`). +- Cloud metadata services (169.254.169.254, `fd00:c1::a9fe:a9fe`, `fd00:ec2::254`, link-local) polled by the host itself are `infra` (info), not beacon; a container reaching them stays a finding (SSRF/credential theft). +- Regular connections to Akamai (AS20940/AS16625 and its main ranges) and Telegram (AS62041, 149.154.160.0/20, 91.108.0.0/16) are `low` ("large CDN/cloud network"). Hetzner (e.g. SURBL updates) stays `medium`: there is no name correlation, and no IP is hardcoded. ## [1.4.7] - 2026-11-15 diff --git a/CHANGELOG.pt-BR.md b/CHANGELOG.pt-BR.md index 479e70a..6cf53b2 100644 --- a/CHANGELOG.pt-BR.md +++ b/CHANGELOG.pt-BR.md @@ -4,7 +4,14 @@ Formato baseado no [Keep a Changelog](https://keepachangelog.com/pt-BR/1.1.0/); versionamento segue SemVer. -## [Unreleased] +## [1.4.8] - 2026-11-15 + +### Corrigido + +- Análise de egress, falsos positivos: o kit nftables agora registra só tentativas de conexão TCP (SYN sem ACK), então respostas a clientes banidos (conntrack recriado no meio do fluxo) não aparecem mais como `bad_dst` "nos atacou". **Rode `deploy/host/install.sh --enforce` de novo no host para aplicar o nft novo.** +- Hosts com o kit antigo: o parser descarta linhas TCP sem SYN puro e as conta como `non_syn_dropped` na fonte de egress (não como `malformed`). +- Serviços de metadados de nuvem (169.254.169.254, `fd00:c1::a9fe:a9fe`, `fd00:ec2::254`, link-local) consultados pelo próprio host são `infra` (info), não beacon; container acessando continua alerta (SSRF/roubo de credencial). +- Conexões regulares para Akamai (AS20940/AS16625 e faixas principais) e Telegram (AS62041, 149.154.160.0/20, 91.108.0.0/16) ficam `low` ("rede grande de CDN/nuvem"). Hetzner (ex.: atualização do SURBL) continua `medium`: não há correlação por nome e nenhum IP foi fixado no código. ## [1.4.7] - 2026-11-15 diff --git a/deploy/host/urutau-egress.nft b/deploy/host/urutau-egress.nft index f3a28ed..a587fba 100644 --- a/deploy/host/urutau-egress.nft +++ b/deploy/host/urutau-egress.nft @@ -22,6 +22,10 @@ table inet urutau_egress { set seen6 { type ipv6_addr . ipv6_addr . inet_proto . inet_service; flags dynamic, timeout; timeout 2h; size 131072; } chain egress { + # only real connection attempts (SYN without ACK): replies of flows whose + # conntrack entry was recreated mid-flow (nf_conntrack_tcp_loose=1, e.g. + # after fail2ban drops only the original direction) are not egress + meta l4proto tcp tcp flags & (syn | ack) != syn return # known pair: always log, refresh its timer meta nfproto ipv4 meta l4proto { tcp, udp } ip saddr . ip daddr . meta l4proto . th dport @seen4 update @seen4 { ip saddr . ip daddr . meta l4proto . th dport } log prefix "URUTAU-EGRESS: " level info return meta nfproto ipv6 meta l4proto { tcp, udp } ip6 saddr . ip6 daddr . meta l4proto . th dport @seen6 update @seen6 { ip6 saddr . ip6 daddr . meta l4proto . th dport } log prefix "URUTAU-EGRESS: " level info return diff --git a/internal/app/version.go b/internal/app/version.go index c307b69..6e71e37 100644 --- a/internal/app/version.go +++ b/internal/app/version.go @@ -2,4 +2,4 @@ package app // Version is the release version (SemVer). Release builds may override it with // -ldflags "-X .../internal/app.Version=...". -var Version = "1.4.7" +var Version = "1.4.8" diff --git a/internal/egress/egress_test.go b/internal/egress/egress_test.go index 781484c..7a5efef 100644 --- a/internal/egress/egress_test.go +++ b/internal/egress/egress_test.go @@ -1,6 +1,7 @@ package egress import ( + "errors" "fmt" "math/rand" "net/netip" @@ -70,7 +71,7 @@ func TestParse(t *testing.T) { t.Fatalf("host: %+v %v", ev, err) } // December line read in January belongs to the previous year - dec := `Dec 31 23:59:00 h kernel: URUTAU-EGRESS: IN=a OUT=b SRC=1.2.3.4 DST=5.6.7.8 PROTO=TCP DPT=1` + dec := `Dec 31 23:59:00 h kernel: URUTAU-EGRESS: IN=a OUT=b SRC=1.2.3.4 DST=5.6.7.8 PROTO=TCP DPT=1 SYN` if ev, err = ParseLine([]byte(dec), time.Date(2027, 1, 1, 0, 5, 0, 0, time.UTC)); err != nil || ev.Time.Year() != 2026 { t.Fatalf("year: %+v %v", ev, err) } @@ -605,3 +606,78 @@ func TestTorRelayFinding(t *testing.T) { t.Fatalf("tor: %+v", f) } } + +func TestNonSYNDropped(t *testing.T) { + now := time.Date(2026, 11, 14, 23, 0, 0, 0, time.UTC) + reply := `URUTAU-EGRESS: IN=br-85247344d531 OUT=enp0s6 SRC=10.0.15.3 DST=72.63.248.159 LEN=76 PROTO=TCP SPT=443 DPT=31432 WINDOW=63 RES=0x00 ACK PSH URGP=0` + synack := `URUTAU-EGRESS: IN=br-1 OUT=enp0s6 SRC=10.0.15.3 DST=72.63.248.159 PROTO=TCP SPT=443 DPT=31432 RES=0x00 ACK SYN URGP=0` + syn := `URUTAU-EGRESS: IN=br-1 OUT=enp0s6 SRC=10.0.15.3 DST=72.63.248.159 PROTO=TCP SPT=40000 DPT=443 RES=0x00 SYN URGP=0` + udp := `URUTAU-EGRESS: IN=br-1 OUT=enp0s6 SRC=10.0.15.3 DST=1.1.1.1 PROTO=UDP SPT=40000 DPT=53` + for _, l := range []string{reply, synack} { + if _, err := ParseLine([]byte(l), now); !errors.Is(err, ErrNonSYN) { + t.Fatalf("want ErrNonSYN: %v", err) + } + } + for _, l := range []string{syn, udp} { + if _, err := ParseLine([]byte(l), now); err != nil { + t.Fatalf("kept line: %v", err) + } + } + e := newEng(t, Config{BadDst: func(a netip.Addr) (bool, string) { return a == ip("72.63.248.159"), "attacker" }}) + e.Ingest([]byte(reply)) + e.Ingest([]byte(synack)) + e.Ingest([]byte("URUTAU-EGRESS: garbage")) + i := e.Info() + if i.NonSYNDropped != 2 || i.Malformed != 1 || i.Lines != 0 || len(rep(e).Findings) != 0 { + t.Fatalf("info %+v findings %+v", i, rep(e).Findings) + } +} + +func TestBeaconMetadataAndKnownCDN(t *testing.T) { + geo := func(s string) (string, string, string) { + if s == "78.47.239.92" { + return "DE", "AS24940", "Hetzner" + } + return "BR", "AS2", "y" + } + e := newEng(t, Config{Geo: geo}) + rnd := rand.New(rand.NewSource(3)) + for _, d := range []string{"23.5.1.1", "149.154.167.220", "78.47.239.92", "169.254.169.254"} { + feedBeacon(e, "10.0.3.5", d, 80, t0, 50, 60*time.Second, 0.05, rnd) + } + // the host itself polling IMDS is infra; a container doing it is not + for _, d := range []string{"fd00:c1::a9fe:a9fe", "169.254.169.254"} { + tt := t0 + for i := 0; i < 50; i++ { + ev := mk(tt, "10.0.0.2", d, "tcp", 80) + ev.Host = true + e.IngestEvent(ev) + tt = tt.Add(60 * time.Second) + } + } + containerIMDS := false + got := map[string]Finding{} + for _, f := range rep(e).Findings { + if f.Dst == "169.254.169.254" && f.Kind == "beacon" && f.Severity != "info" { + containerIMDS = true + continue + } + got[f.Dst] = f + } + if !containerIMDS { + t.Fatal("container polling IMDS must stay a beacon finding") + } + for _, d := range []string{"fd00:c1::a9fe:a9fe", "169.254.169.254"} { + if f := got[d]; f.Kind != "infra" || f.Severity != "info" { + t.Fatalf("%s: %+v", d, f) + } + } + for _, d := range []string{"23.5.1.1", "149.154.167.220"} { + if f := got[d]; f.Kind != "beacon" || f.Severity != "low" || !strings.Contains(f.Detail, "large CDN/cloud") { + t.Fatalf("%s: %+v", d, f) + } + } + if f := got["78.47.239.92"]; f.Kind != "beacon" || f.Severity != "medium" { + t.Fatalf("surbl: %+v", f) + } +} diff --git a/internal/egress/engine.go b/internal/egress/engine.go index 2d4d2af..28a4090 100644 --- a/internal/egress/engine.go +++ b/internal/egress/engine.go @@ -2,6 +2,7 @@ package egress import ( "container/list" + "errors" "fmt" "hash/fnv" "math" @@ -223,9 +224,9 @@ type Engine struct { closed []BurstOut maxT int64 - lines, malformed atomic.Uint64 - last atomic.Int64 - stateFn atomic.Pointer[func() string] + lines, malformed, nonSYN atomic.Uint64 + last atomic.Int64 + stateFn atomic.Pointer[func() string] } // New builds an engine. @@ -356,6 +357,10 @@ func (e *Engine) reloadMap() { func (e *Engine) Ingest(line []byte) { ev, err := ParseLine(line, e.now()) if err != nil { + if errors.Is(err, ErrNonSYN) { + e.nonSYN.Add(1) + return + } if len(line) > 0 { e.malformed.Add(1) } @@ -761,6 +766,9 @@ func (e *Engine) beacon(ws *wstate, ev Event, dport uint16, dns bool, p *pairSta } } } + if ev.Host && isCloudMetadata(ev.Dst) { + kind = "infra" + } if f := e.addFinding(kind, "", ws.name, ev.Dst.String(), dport, ev.Proto, p.last, p.interval, p.cv, "", 1); f != nil { if n := len(f.Samples); n == 0 || f.Samples[n-1] != p.last { if n >= MaxSamples { @@ -827,16 +835,23 @@ func (e *Engine) beacon(ws *wstate, ev Event, dport uint16, dns bool, p *pairSta bad, sev, why = true, "high", why+"; destination: "+r } } - if !bad && e.cfg.Geo != nil { + if !bad && ev.Host && isCloudMetadata(ev.Dst) { + // only the host itself: a container reaching IMDS stays a finding (SSRF/credential theft) + sev, kind = "info", "infra" + why += "; provider service (cloud metadata)" + } else if !bad && e.cfg.Geo != nil { _, asn, org := e.cfg.Geo(ev.Dst.String()) switch { case ev.Host && e.providerASN(asn): sev, kind = "info", "infra" why = fmt.Sprintf("%s; provider service (%s)", why, org) - case commonCloudASNs[asn]: + case commonCloudASNs[asn] || inCommonCDN(ev.Dst): sev = "low" why += "; large CDN/cloud network: regular connections are common for API calls" } + } else if !bad && inCommonCDN(ev.Dst) { + sev = "low" + why += "; large CDN/cloud network: regular connections are common for API calls" } p.flagged, p.interval, p.cv = true, med, jit f := e.addFinding(kind, sev, ws.name, ev.Dst.String(), dport, ev.Proto, p.last, med, jit, why, 0) @@ -851,7 +866,40 @@ func (e *Engine) beacon(ws *wstate, ev Event, dport uint16, dns bool, p *pairSta // commonCloudASNs are large CDNs/clouds whose regular connections usually are // API calls. -var commonCloudASNs = map[string]bool{"AS13335": true, "AS15169": true, "AS14618": true, "AS16509": true, "AS8075": true, "AS20940": true, "AS54113": true} +var commonCloudASNs = map[string]bool{"AS13335": true, "AS15169": true, "AS14618": true, "AS16509": true, "AS8075": true, "AS20940": true, "AS54113": true, + "AS16625": true, // Akamai + "AS62041": true, // Telegram +} + +// commonCDNPrefixes back the ASN list when the geo database has no entry: +// Akamai and the Telegram API. +var commonCDNPrefixes = mustPrefixes("23.0.0.0/12", "104.64.0.0/10", "184.24.0.0/13", "2.16.0.0/13", + "149.154.160.0/20", "91.108.0.0/16") + +// cloudMetadata are the metadata services of the clouds (IMDS), IPv4 and +// IPv6; link-local addresses are included. +var cloudMetadata = mustPrefixes("169.254.0.0/16", "fe80::/10", "fd00:c1::a9fe:a9fe/128", "fd00:ec2::254/128") + +func mustPrefixes(s ...string) []netip.Prefix { + out := make([]netip.Prefix, len(s)) + for i, v := range s { + out[i] = netip.MustParsePrefix(v) + } + return out +} + +func inPrefixes(a netip.Addr, ps []netip.Prefix) bool { + a = a.Unmap() + for _, p := range ps { + if p.Contains(a) { + return true + } + } + return false +} + +func inCommonCDN(a netip.Addr) bool { return inPrefixes(a, commonCDNPrefixes) } +func isCloudMetadata(a netip.Addr) bool { return inPrefixes(a, cloudMetadata) } func (e *Engine) providerASN(asn string) bool { if asn == "" { diff --git a/internal/egress/parse.go b/internal/egress/parse.go index 553b397..29f3b70 100644 --- a/internal/egress/parse.go +++ b/internal/egress/parse.go @@ -25,16 +25,28 @@ type Event struct { SPort, DPort uint16 In, Out string Host bool // IN= empty: output of the host itself + // SYN and ACK are the TCP flags present in the log line. + SYN, ACK bool } // ErrNotEgress marks a line that is not an egress log line. var ErrNotEgress = errors.New("not an egress line") +// ErrNonSYN marks a TCP egress line that is not a connection attempt (no SYN, +// or SYN+ACK): replies of a flow whose conntrack entry was recreated in the +// middle (nf_conntrack_tcp_loose), logged by an old kit that did not filter +// on the SYN flag. +var ErrNonSYN = errors.New("tcp line without a pure SYN") + // ParseLine parses a kernel/rsyslog line. now is only used to infer the year // of the "Nov 14 22:01:02" rsyslog format (and as the time of a line whose // timestamp is unreadable). func ParseLine(b []byte, now time.Time) (Event, error) { - return ParseKernel(b, marker, now) + ev, err := ParseKernel(b, marker, now) + if err == nil && ev.Proto == "tcp" && (!ev.SYN || ev.ACK) { + return ev, ErrNonSYN + } + return ev, err } // ParseKernel parses an nftables log line carrying the given prefix marker @@ -51,6 +63,12 @@ func ParseKernel(b []byte, marker string, now time.Time) (Event, error) { for _, f := range strings.Fields(line[i+len(marker):]) { k, v, ok := strings.Cut(f, "=") if !ok { + switch f { + case "SYN": + ev.SYN = true + case "ACK": + ev.ACK = true + } continue } switch k { diff --git a/internal/egress/source.go b/internal/egress/source.go index f2e1224..e761379 100644 --- a/internal/egress/source.go +++ b/internal/egress/source.go @@ -96,7 +96,9 @@ type SourceInfo struct { State string `json:"state"` Lines uint64 `json:"lines"` Malformed uint64 `json:"malformed"` - Last int64 `json:"last"` + // NonSYNDropped counts TCP lines without a pure SYN (old kit); not malformed. + NonSYNDropped uint64 `json:"non_syn_dropped"` + Last int64 `json:"last"` } // Info reports the source state (state "disabled" when no source is attached). @@ -106,5 +108,5 @@ func (e *Engine) Info() SourceInfo { if f := e.stateFn.Load(); f != nil { st = (*f)() } - return SourceInfo{State: st, Lines: l, Malformed: m, Last: last} + return SourceInfo{State: st, Lines: l, Malformed: m, NonSYNDropped: e.nonSYN.Load(), Last: last} }