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 Est Tally24 // 24 h estimate from the per-day counters (the registry may not cover the whole window) } // dayEstimate estimates the SSH and web malicious events of the last 24 h from // the per-day counters: today plus the share of yesterday 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 <= 1; k++ { i := int(r.DayBase - (today - k)) if i < 0 || i >= len(r.DayCats) { continue } w := 1.0 if k == 1 { w = frac } c := r.DayCats[i] t.SSH += int(float64(c[core.SSHFail.Index()])*w + 0.999) t.Web += int(float64(c[core.Scanner.Index()]+c[core.Exploit.Index()]+c[core.BruteForce.Index()])*w + 0.999) } return t } // 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, 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.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}, 4<<30, 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 { 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 } 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) return true }) var t Tally24 for _, v := range m { t.SSH += v.SSH t.Web += v.Web } t.SSH, t.Web = max(t.SSH, est.SSH), max(t.Web, est.Web) return t }