Files
urutau/internal/store/roster.go
T

183 lines
5.0 KiB
Go

package store
import (
"strings"
"time"
"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
Events int // malicious events recorded for the address
Est Tally24 // 24 h estimate from the per-day counters (the registry may not cover the whole window)
}
// dayEstimate estimates the SSH, ssh-probe and web malicious events of the last
// 24 h (SSH, Web, Probe) and of the last 7 days (SSH7, Web7, Probe7) from the
// per-day counters: today plus the share of the oldest day still inside the window.
func dayEstimate(r *IPRec, nowU int64) Tally24 {
if len(r.DayCats) == 0 {
return Tally24{}
}
today := nowU / 86400
frac := 1 - float64(nowU%86400)/86400
var t Tally24
for k := int64(0); k <= 7; k++ {
i := int(r.DayBase - (today - k))
if i < 0 || i >= len(r.DayCats) {
continue
}
c := r.DayCats[i]
ssh, probe := float64(c[core.SSHFail.Index()]), float64(c[core.SSHProbe.Index()])
web := float64(c[core.Scanner.Index()] + c[core.Exploit.Index()] + c[core.BruteForce.Index()])
if k <= 1 {
w := 1.0
if k == 1 {
w = frac
}
t.SSH += int(ssh*w + 0.999)
t.Web += int(web*w + 0.999)
t.Probe += int(probe*w + 0.999)
}
if k <= 6 {
w := 1.0
if k == 6 {
w = frac
}
t.SSH7 += int(ssh*w + 0.999)
t.Web7 += int(web*w + 0.999)
t.Probe7 += int(probe*w + 0.999)
}
}
return t
}
// Tally24 counts the malicious events of one address in a window.
// SSH, Web and Probe cover the last 24 h; SSH7, Web7 and Probe7 the whole scanned
// window (7 days for the enforce sweep).
type Tally24 struct{ SSH, Web, Probe, SSH7, Web7, Probe7 int }
// maxTallyIPs bounds the memory of one tally pass.
const maxTallyIPs = 100000
// 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, Events: int(malOf(r.Counts)), Est: dayEstimate(r, nowU)})
}
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.SSHProbe): true, string(core.Scanner): true, string(core.Exploit): true, string(core.BruteForce): true}
func tallyAdd(m map[string]*Tally24, r evlog.Rec, cut24 int64) {
t := m[r.IP]
if t == nil {
if len(m) >= maxTallyIPs {
return
}
t = &Tally24{}
m[r.IP] = t
}
recent := r.T >= cut24
switch {
case r.Cat == string(core.SSHFail):
t.SSH7++
if recent {
t.SSH++
}
case r.Cat == string(core.SSHProbe):
t.Probe7++
if recent {
t.Probe++
}
case r.Source == "traefik" && !strings.Contains(r.Flags, "F"):
t.Web7++
if recent {
t.Web++
}
}
}
// AttackTallies counts, in one pass over the event log, the SSH failures, ssh
// probes 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
}
cut24 := s.now().Add(-24 * time.Hour).Unix()
m := map[string]*Tally24{}
s.elog.Scan(evlog.Filter{From: from, Cats: tallyCats}, 4<<30, false, func(r evlog.Rec, _ []byte) bool {
tallyAdd(m, r, cut24)
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 {
var est Tally24
s.mu.RLock()
if r := s.ips[KeyOf(ip)]; r != nil {
est = dayEstimate(r, s.now().Unix())
}
s.mu.RUnlock()
if s.elog == nil {
return est
}
cut24 := s.now().Add(-24 * time.Hour).Unix()
m := map[string]*Tally24{}
s.elog.Scan(evlog.Filter{From: from, IP: ip, Cats: tallyCats}, 1<<30, false, func(r evlog.Rec, _ []byte) bool {
tallyAdd(m, r, cut24)
return true
})
var t Tally24
for _, v := range m {
t.SSH += v.SSH
t.Web += v.Web
t.Probe += v.Probe
t.SSH7 += v.SSH7
t.Web7 += v.Web7
t.Probe7 += v.Probe7
}
return MaxTally(t, est)
}
// MaxTally is the field-wise maximum.
func MaxTally(a, b Tally24) Tally24 {
return Tally24{max(a.SSH, b.SSH), max(a.Web, b.Web), max(a.Probe, b.Probe), max(a.SSH7, b.SSH7), max(a.Web7, b.Web7), max(a.Probe7, b.Probe7)}
}