Files
urutau/internal/egress/parse.go
T
Lucas RayanandClaude Opus 5.5 432f521ed1
Release / Publish tags (push) Successful in 18s
Release / Plan (reuse the published image?) (push) Successful in 5s
Release / ci (push) Successful in 1m47s
Release / Build image (amd64) (push) Successful in 21s
Release / Build image (arm64) (push) Successful in 28s
CI / Format and vet (push) Successful in 23s
CI / Tests (Linux, race detector) (push) Successful in 1m24s
CI / Dependency audit (push) Successful in 24s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 5s
feat: 1.4.1 pagination, network probes, survival by blocker, report polish
Backend pagination with one pager for sessions, IPs, campaigns and events;
network probe source (port-scan, T1046, second firewall reason port-scan);
host kit logs inbound probes; urutau call on the report cover.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 00:21:43 -03:00

113 lines
3.0 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
}
// ErrNotEgress marks a line that is not an egress log line.
var ErrNotEgress = errors.New("not an egress line")
// 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)
}
// 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 {
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
}