Files
urutau/internal/store/roster.go
T
Lucas RayanandClaude Opus 5.5 1ef5a0ce9f
Release / Publish tags (push) Successful in 15s
Release / Plan (reuse the published image?) (push) Successful in 5s
Release / ci (push) Successful in 1m2s
Release / Build image (amd64) (push) Successful in 21s
Release / Build image (arm64) (push) Successful in 1m10s
CI / Format and vet (push) Successful in 26s
CI / Tests (Linux, race detector) (push) Successful in 20s
CI / Dependency audit (push) Successful in 35s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 4s
fix: 1.4.5 second firewall uses history, reputation and a periodic sweep
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 01:37:25 -03:00

103 lines
2.7 KiB
Go

package store
import (
"strings"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/evlog"
)
// RosterRow is one address with malicious activity, as the second firewall needs it.
type RosterRow struct {
IP string
Score float64
Level core.Level
Jails []string
Last int64
}
// Tally24 counts the malicious events of one address in a window.
type Tally24 struct{ SSH, Web int }
// AttackRoster lists the addresses with malicious activity since from (unix).
func (s *Store) AttackRoster(from int64) []RosterRow {
nowU := s.now().Unix()
s.mu.RLock()
defer s.mu.RUnlock()
out := make([]RosterRow, 0, 256)
for ip, r := range s.ips {
if r.Last < from || !isAttacker(r) {
continue
}
strong := r.Strong || attackKinds(r.Counts) >= 2
out = append(out, RosterRow{IP: ip, Score: round1(r.Score), Level: core.LevelOf(r.Score, strong, r.adminAt(nowU)),
Jails: append([]string(nil), r.Jails...), Last: r.Last})
}
return out
}
// Reputation is the verdict of one address as the UI shows it.
func (s *Store) Reputation(ip string) (float64, core.Level, bool) {
ip = KeyOf(ip)
s.mu.RLock()
defer s.mu.RUnlock()
r := s.ips[ip]
if r == nil {
return 0, core.LevelLow, false
}
strong := r.Strong || attackKinds(r.Counts) >= 2
return r.Score, core.LevelOf(r.Score, strong, r.adminAt(s.now().Unix())), true
}
var tallyCats = map[string]bool{string(core.SSHFail): true, string(core.Scanner): true, string(core.Exploit): true, string(core.BruteForce): true}
func tallyAdd(m map[string]*Tally24, r evlog.Rec) {
t := m[r.IP]
if t == nil {
t = &Tally24{}
m[r.IP] = t
}
switch {
case r.Cat == string(core.SSHFail):
t.SSH++
case r.Source == "traefik" && !strings.Contains(r.Flags, "F"):
t.Web++
}
}
// AttackTallies counts, in one pass over the event log, the SSH failures and web
// scan/exploit events of every address since from. Nil without an event log.
func (s *Store) AttackTallies(from int64) map[string]Tally24 {
if s.elog == nil {
return nil
}
m := map[string]*Tally24{}
s.elog.Scan(evlog.Filter{From: from, Cats: tallyCats}, 256<<20, false, func(r evlog.Rec, _ []byte) bool {
tallyAdd(m, r)
return true
})
out := make(map[string]Tally24, len(m))
for k, v := range m {
out[k] = *v
}
return out
}
// AttackTally is AttackTallies for one address.
func (s *Store) AttackTally(ip string, from int64) Tally24 {
if s.elog == nil {
return Tally24{}
}
m := map[string]*Tally24{}
s.elog.Scan(evlog.Filter{From: from, IP: ip, Cats: tallyCats}, 64<<20, false, func(r evlog.Rec, _ []byte) bool {
tallyAdd(m, r)
return true
})
var t Tally24
for _, v := range m {
t.SSH += v.SSH
t.Web += v.Web
}
return t
}