fix: 1.4.8 egress false positives (non-SYN replies, IMDS on host, Akamai/Telegram)
Release / Publish tags (push) Successful in 13s
Release / Plan (reuse the published image?) (push) Successful in 5s
Release / ci (push) Successful in 1m20s
Release / Build image (amd64) (push) Successful in 24s
Release / Build image (arm64) (push) Successful in 1m11s
Release / Gitea release (push) Skipped
CI / Format and vet (push) Successful in 51s
CI / Tests (Linux, race detector) (push) Successful in 50s
CI / Dependency audit (push) Successful in 29s
CI / Docker image (build and smoke test) (push) Skipped
Release / Publish tags (push) Successful in 13s
Release / Plan (reuse the published image?) (push) Successful in 5s
Release / ci (push) Successful in 1m20s
Release / Build image (amd64) (push) Successful in 24s
Release / Build image (arm64) (push) Successful in 1m11s
Release / Gitea release (push) Skipped
CI / Format and vet (push) Successful in 51s
CI / Tests (Linux, race detector) (push) Successful in 50s
CI / Dependency audit (push) Successful in 29s
CI / Docker image (build and smoke test) (push) Skipped
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
873219416f
commit
3ae72e27d6
8 files changed
+175
-13
No files matched your search
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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 == "" {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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}
|
||||
}
|
||||
Reference in new issue
Block a user