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>
131 lines
3.5 KiB
Go
131 lines
3.5 KiB
Go
// Package egress analyses the outbound connections of the host and its
|
|
// containers (nftables "URUTAU-EGRESS: " log lines) to spot a compromised
|
|
// workload: beaconing, C2, mining, spam, unauthorised scanning.
|
|
//
|
|
// Central principle: traffic shaped like a scan (fan-out) is suppressed only
|
|
// for workloads with role=scanner. Traffic that is not scan shaped is judged
|
|
// the same for every workload, the scanner included.
|
|
package egress
|
|
|
|
import (
|
|
"errors"
|
|
"net/netip"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const marker = "URUTAU-EGRESS:"
|
|
|
|
// Event is one new outbound connection.
|
|
type Event struct {
|
|
Time time.Time
|
|
Src, Dst netip.Addr
|
|
Proto string // tcp, udp, icmp, or the lowercased PROTO value
|
|
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) {
|
|
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
|
|
// (for instance "URUTAU-EGRESS:" or "URUTAU-PROBE:"). It is shared by every
|
|
// source that reads kernel log lines.
|
|
func ParseKernel(b []byte, marker string, now time.Time) (Event, error) {
|
|
line := string(b)
|
|
i := strings.Index(line, marker)
|
|
if i < 0 {
|
|
return Event{}, ErrNotEgress
|
|
}
|
|
ev := Event{Time: parseTS(line[:i], now)}
|
|
var haveSrc, haveDst bool
|
|
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 {
|
|
case "IN":
|
|
ev.In = v
|
|
case "OUT":
|
|
ev.Out = v
|
|
case "SRC":
|
|
a, err := netip.ParseAddr(v)
|
|
if err != nil {
|
|
return Event{}, errors.New("bad SRC")
|
|
}
|
|
ev.Src, haveSrc = a.Unmap(), true
|
|
case "DST":
|
|
a, err := netip.ParseAddr(v)
|
|
if err != nil {
|
|
return Event{}, errors.New("bad DST")
|
|
}
|
|
ev.Dst, haveDst = a.Unmap(), true
|
|
case "PROTO":
|
|
p := strings.ToLower(v)
|
|
if strings.HasPrefix(p, "icmp") {
|
|
p = "icmp"
|
|
}
|
|
ev.Proto = p
|
|
case "SPT":
|
|
n, _ := strconv.ParseUint(v, 10, 16)
|
|
ev.SPort = uint16(n)
|
|
case "DPT":
|
|
n, _ := strconv.ParseUint(v, 10, 16)
|
|
ev.DPort = uint16(n)
|
|
}
|
|
}
|
|
if !haveSrc || !haveDst || ev.Proto == "" {
|
|
return Event{}, errors.New("incomplete egress line")
|
|
}
|
|
ev.Host = ev.In == ""
|
|
return ev, nil
|
|
}
|
|
|
|
func parseTS(prefix string, now time.Time) time.Time {
|
|
f := strings.Fields(prefix)
|
|
if len(f) == 0 {
|
|
return now
|
|
}
|
|
if t, err := time.Parse(time.RFC3339Nano, f[0]); err == nil {
|
|
return t
|
|
}
|
|
if len(f) >= 3 {
|
|
if t, err := time.ParseInLocation("Jan 2 15:04:05", f[0]+" "+f[1]+" "+f[2], now.Location()); err == nil {
|
|
t = t.AddDate(now.Year(), 0, 0)
|
|
if t.After(now.Add(48 * time.Hour)) {
|
|
t = t.AddDate(-1, 0, 0)
|
|
}
|
|
return t
|
|
}
|
|
}
|
|
return now
|
|
}
|