feat(ingress): firewall telemetry of inbound attempts (host kit --ingress)

Observation-only nftables table (prerouting -150, counters and a
rate-limited log, never drops or modifies), a per-minute counters file,
probe sources ingress and ingresscnt, a per-host hourly aggregate in the
hub (exact totals, sampled ports/ASNs/countries, banned share by the
hub's own ban state, no ban feedback loop), GET /api/v1/ingress, a
Defense section, doctor check, docs EN/pt-BR and tests.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Lucas RayanandClaude Sonnet 5.5 committed 2026-11-23 09:09:16 -03:00
1 parent 4143bf599e
commit aefdcea8d2
40 files changed
+1684 -11

No files matched your search

+1
View File
@@ -65,6 +65,7 @@ func Doctor(c Config, w io.Writer, asJSON bool) bool {
add("data-dir", "ok", fmt.Sprintf("%s writable", filepath.Clean(c.DataDir)))
}
doctorEnforce(c, add)
doctorIngress(c, add)
geoInfo := doctorGeo(c, add)
stateInfo := doctorState(c, add)
ok := true
+35
View File
@@ -0,0 +1,35 @@
package app
import (
"log/slog"
"path/filepath"
"strconv"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/ingress"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
)
// IngressPath is the aggregate of the firewall telemetry inside a node directory.
func IngressPath(dir string) string { return filepath.Join(dir, "ingress.json") }
// newIngress builds the firewall telemetry aggregate of one node. A source
// address is "banned" by the node's own ban state (the jails it has read from
// fail2ban.log, the urutau jail included). The aggregate only reads that state:
// it has no way to write an event, a score or a decision, so attempts from a
// banned address never become evidence.
func newIngress(st *store.Store, dir string, log *slog.Logger) *ingress.Agg {
a := ingress.New()
a.Banned = st.Banned
a.Geo = func(ip string) (string, string, string) {
g := st.GeoOf(ip)
asn := ""
if g.ASN != 0 {
asn = "AS" + strconv.FormatUint(uint64(g.ASN), 10)
}
return g.CC, asn, g.ASOrg
}
if err := a.Load(IngressPath(dir)); err != nil {
log.Warn("ingress state ignored", "dir", dir, "err", err)
}
return a
}
+60
View File
@@ -0,0 +1,60 @@
package app
import (
"fmt"
"os"
"path/filepath"
"sort"
"time"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/ingress"
)
// ingressFresh is how old the last counters line may be before the firewall
// telemetry of a host counts as stale: the kit writes one every minute.
const ingressFresh = 5 * time.Minute
// doctorIngress reports, per host that ever sent it, whether the firewall
// telemetry (host kit --ingress) is present and fresh. It reads the saved
// aggregate of each node; no host sending it is "off", not a failure.
func doctorIngress(c Config, add func(name, status, detail string)) {
dirs := map[string]string{c.Name: c.DataDir}
if ents, err := os.ReadDir(filepath.Join(c.DataDir, "probes")); err == nil {
for _, e := range ents {
if e.IsDir() {
dirs[e.Name()] = filepath.Join(c.DataDir, "probes", e.Name())
}
}
}
names := make([]string, 0, len(dirs))
for n := range dirs {
names = append(names, n)
}
sort.Strings(names)
any := false
for _, n := range names {
p := IngressPath(dirs[n])
if _, err := os.Stat(p); err != nil {
continue
}
a := ingress.New()
if err := a.Load(p); err != nil {
add("ingress "+n, "warn", err.Error())
any = true
continue
}
in := a.Info()
any = true
switch {
case in.LastCounters == 0:
add("ingress "+n, "warn", "sampled log seen but no counters yet: check ingress.counters and urutau-ingress-tick.timer")
case time.Since(time.Unix(in.LastCounters, 0)) > ingressFresh:
add("ingress "+n, "warn", fmt.Sprintf("stale: last counters %s ago (is urutau-ingress-tick.timer running?)", time.Since(time.Unix(in.LastCounters, 0)).Round(time.Second)))
default:
add("ingress "+n, "ok", fmt.Sprintf("fresh: counters %s ago, %d sampled lines", time.Since(time.Unix(in.LastCounters, 0)).Round(time.Second), in.Lines))
}
}
if !any {
add("ingress", "off", "no firewall telemetry received (host kit: install.sh --ingress)")
}
}
+77
View File
@@ -0,0 +1,77 @@
package app
import (
"fmt"
"io"
"log/slog"
"testing"
"time"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/wire"
)
func ingressNode(t *testing.T) (*Runner, *store.Store) {
t.Helper()
st := store.New(store.Config{Name: "t", Version: "t", Retention: 30 * 24 * time.Hour})
log := slog.New(slog.NewTextHandler(io.Discard, nil))
r, err := NewRemoteRunner(Config{DataDir: t.TempDir()}, st, log, t.TempDir())
if err != nil {
t.Fatal(err)
}
r.StartRemote(nil, nil)
r.IngressFeed = newIngress(st, t.TempDir(), log).Ingest
return r, st
}
func lines(src string, ls ...string) []wire.Line {
var out []wire.Line
for i, l := range ls {
out = append(out, wire.Line{Src: src, Text: l, End: wire.Pos{Inode: 7, Offset: int64(i+1) * 100}})
}
return out
}
// Attempts from a banned address are classified from the node's ban state and
// never become events, scores or evidence: the store learns nothing from them.
func TestIngressNeverFeedsTheStore(t *testing.T) {
r, st := ingressNode(t)
// the node knows 203.0.113.9 is banned by a jail
r.ApplyBatch(lines(wire.SrcFail2ban, "2026-11-20 10:00:00,100 fail2ban.actions [1]: NOTICE [sshd] Ban 203.0.113.9"))
if !st.Banned("203.0.113.9") || st.Banned("198.51.100.1") {
t.Fatal("ban state not read")
}
before := st.IPCount()
now := time.Now().UTC().Format("2006-01-02T15:04:05+00:00")
var in []string
for i := 0; i < 500; i++ {
in = append(in, fmt.Sprintf("%s h kernel: URUTAU-IN: IN=eth0 OUT= SRC=203.0.113.9 DST=10.0.0.5 PROTO=TCP SPT=1 DPT=%d SYN", now, 1000+i%40))
in = append(in, fmt.Sprintf("%s h kernel: URUTAU-IN: IN=eth0 OUT= SRC=198.51.100.%d DST=10.0.0.5 PROTO=TCP SPT=1 DPT=23 SYN", now, 1+i%200))
}
acc, _ := r.ApplyBatch(lines(wire.SrcIngress, in...))
if acc != 1000 {
t.Fatalf("accepted %d", acc)
}
r.ApplyBatch(lines(wire.SrcIngressCnt, "ts=1 total=oops"))
if st.IPCount() != before {
t.Fatalf("ingress created IP records: %d -> %d", before, st.IPCount())
}
if st.KnownHostile("198.51.100.1") {
t.Fatal("an ingress attempt became evidence")
}
// the sources are not events: no runner source exists for them
if r.find("ingress") != nil || r.find("ingresscnt") != nil {
t.Fatal("ingress sources must not be event sources")
}
}
func TestIngressDuplicateLinesAreDropped(t *testing.T) {
r, _ := ingressNode(t)
l := lines(wire.SrcIngress, "x URUTAU-IN: SRC=198.51.100.1 DST=1.1.1.1 PROTO=TCP DPT=23 SYN")
if a, _ := r.ApplyBatch(l); a != 1 {
t.Fatal("first not accepted")
}
if a, d := r.ApplyBatch(l); a != 0 || d != 1 {
t.Fatalf("replay accepted %d dup %d", a, d)
}
}
+11
View File
@@ -16,6 +16,7 @@ import (
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/evlog"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/hub"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/incidents"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/ingress"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/rdns"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/server"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
@@ -57,6 +58,8 @@ type Node struct {
Inc *incidents.Store
Enf *enforce.Engine
Q *hub.CmdQueue
// Ing aggregates the firewall telemetry (ingress.log and ingress.counters).
Ing *ingress.Agg
sh *shared
egr atomic.Pointer[egress.Engine]
@@ -113,6 +116,8 @@ func (sh *shared) newNode(name string, primary bool, rec hub.Probe) (*Node, erro
e.Ingest(l)
}
}
n.Ing = newIngress(st, dir, sh.log)
run.IngressFeed = n.Ing.Ingest
if n.Inc, err = incidents.Open(filepath.Join(dir, "incidents.json"), sh.log); err != nil {
return nil, err
}
@@ -169,6 +174,9 @@ func (n *Node) maintain() {
if err := n.Run.Save(); err != nil {
n.sh.log.Warn("snapshot failed", "node", n.name, "err", err)
}
if err := n.Ing.Save(IngressPath(n.Dir)); err != nil {
n.sh.log.Warn("ingress state not saved", "node", n.name, "err", err)
}
case <-prune.C:
if k := n.St.Prune(); k > 0 {
n.sh.log.Info("pruned old IPs", "node", n.name, "n", k)
@@ -179,6 +187,9 @@ func (n *Node) maintain() {
// Close writes the final snapshot and flushes the queue and the tickets.
func (n *Node) Close() {
if err := n.Ing.Save(IngressPath(n.Dir)); err != nil {
n.sh.log.Warn("final ingress state not saved", "node", n.name, "err", err)
}
if err := n.Run.Save(); err != nil {
n.sh.log.Warn("final snapshot failed", "node", n.name, "err", err)
}
+8
View File
@@ -23,6 +23,7 @@ import (
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/geo"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/hub"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/incidents"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/ingress"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/rdns"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/server"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
@@ -78,6 +79,13 @@ func (n nodeSet) Egress(h string) *egress.Engine {
return nil
}
func (n nodeSet) Ingress(h string) *ingress.Agg {
if nd := n.node(h); nd != nil {
return nd.Ing
}
return nil
}
func (n nodeSet) Incidents(h string) *incidents.Store {
if nd := n.node(h); nd != nil {
return nd.Inc
+10
View File
@@ -115,6 +115,9 @@ type Runner struct {
Log *slog.Logger
// EgressFeed receives the "egress" lines of a probe (nil: they are only counted).
EgressFeed func(line []byte)
// IngressFeed receives the "ingress" and "ingresscnt" lines of a probe
// (nil: they are only counted). They never become events.
IngressFeed func(src string, line []byte)
mu sync.Mutex // commit mutex: held while a line is applied and by snapshots
@@ -236,6 +239,13 @@ func (r *Runner) feedLocked(src string, text []byte, end store.Offset) bool {
r.EgressFeed(text)
}()
}
} else if src == wire.SrcIngress || src == wire.SrcIngressCnt {
if r.IngressFeed != nil {
func() {
defer func() { recover() }()
r.IngressFeed(src, text)
}()
}
} else if s := r.find(name); s != nil {
r.handle(s)(text)
}