perf: serve long-range studies from a decoded closed-day cache
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4143bf599e
commit
4ca9c7bf0c
16 files changed
+846
-83
No files matched your search
+80
-34
@@ -180,6 +180,65 @@ type evLite struct {
|
||||
ua string
|
||||
}
|
||||
|
||||
// sessStudy is the part of a period study that several views share: the
|
||||
// events of the window grouped by address and the sessions made from them.
|
||||
type sessStudy struct {
|
||||
byIP map[string][]evlog.Ref
|
||||
sessions []intel.Session
|
||||
events int
|
||||
truncated bool
|
||||
}
|
||||
|
||||
// evs rebuilds the events of one address.
|
||||
func (st *sessStudy) evs(ip string) []intel.Ev {
|
||||
refs := st.byIP[ip]
|
||||
out := make([]intel.Ev, len(refs))
|
||||
for i, r := range refs {
|
||||
out[i] = toEv(r.Rec())
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func normGap(g int64) time.Duration {
|
||||
switch {
|
||||
case g <= 0:
|
||||
return intel.DefaultGap
|
||||
case g < 60:
|
||||
return 60 * time.Second
|
||||
}
|
||||
return time.Duration(g) * time.Second
|
||||
}
|
||||
|
||||
// sessionStudy scans the window once and builds its sessions; period, funnel
|
||||
// and metrics of the same window (and filters) share the result for the cache
|
||||
// lifetime instead of each decoding and grouping the same events.
|
||||
func (s *Store) sessionStudy(q PeriodQuery) *sessStudy {
|
||||
run := func() *sessStudy {
|
||||
st := &sessStudy{byIP: map[string][]evlog.Ref{}}
|
||||
deadline := time.Now().Add(studyDead)
|
||||
info := s.elog.ScanRefs(evlog.Filter{From: q.From, To: q.To, Host: q.Host, IP: q.IP, Cats: catSet(q.Cat), Fam: q.Fam}, studyBudget,
|
||||
func(ref evlog.Ref) bool {
|
||||
if st.events >= maxStudyEvents || st.events%4096 == 0 && time.Now().After(deadline) {
|
||||
st.truncated = true
|
||||
return false
|
||||
}
|
||||
ip := KeyOf(ref.IP())
|
||||
st.byIP[ip] = append(st.byIP[ip], ref)
|
||||
st.events++
|
||||
return true
|
||||
})
|
||||
st.truncated = st.truncated || info.Truncated
|
||||
ips := make([]string, 0, len(st.byIP))
|
||||
for ip := range st.byIP {
|
||||
ips = append(ips, ip)
|
||||
}
|
||||
st.sessions = intel.SessionsLazy(ips, st.evs, normGap(q.Gap))
|
||||
return st
|
||||
}
|
||||
k := studyKey{"sess", q.From, q.To, q.Host, q.IP, q.Cat, q.Fam, int64(normGap(q.Gap) / time.Second)}
|
||||
return s.cached(k, func() any { return run() }).(*sessStudy)
|
||||
}
|
||||
|
||||
// Period studies [From, To] from the registry: sessions with stages,
|
||||
// campaigns, first sightings, escalations, spikes and tops. At most 256 MB of
|
||||
// registry is read and 10 s spent; the result is cached for 30 s.
|
||||
@@ -206,38 +265,31 @@ func (s *Store) period(q PeriodQuery) PeriodOut {
|
||||
return out
|
||||
}
|
||||
out.Enabled = true
|
||||
gap := time.Duration(q.Gap) * time.Second
|
||||
switch {
|
||||
case q.Gap <= 0:
|
||||
gap = intel.DefaultGap
|
||||
case q.Gap < 60:
|
||||
gap = 60 * time.Second
|
||||
}
|
||||
deadline := time.Now().Add(studyDead)
|
||||
ctx, cancel := context.WithDeadline(context.Background(), deadline)
|
||||
defer cancel()
|
||||
var evs []intel.Ev
|
||||
st := s.sessionStudy(q)
|
||||
out.Truncated = st.truncated
|
||||
byIP := map[string][]evLite{}
|
||||
fams, techs, users, asns := map[string]uint32{}, map[string]uint32{}, map[string]uint32{}, map[string]uint32{}
|
||||
tgt := map[[2]string]int{}
|
||||
firstT := map[string]FirstTech{}
|
||||
ipSeen := map[string]bool{}
|
||||
info := s.elog.Scan(evlog.Filter{From: q.From, To: q.To, Host: q.Host, IP: q.IP, Cats: catSet(q.Cat), Fam: q.Fam}, studyBudget, false,
|
||||
func(r evlog.Rec, _ []byte) bool {
|
||||
if len(evs) >= maxStudyEvents || len(evs)%4096 == 0 && time.Now().After(deadline) {
|
||||
out.Truncated = true
|
||||
n := 0
|
||||
info := s.elog.ScanRecs(evlog.Filter{From: q.From, To: q.To, Host: q.Host, IP: q.IP, Cats: catSet(q.Cat), Fam: q.Fam}, studyBudget, false,
|
||||
func(r evlog.Rec) bool {
|
||||
if n++; n > maxStudyEvents || n%4096 == 0 && time.Now().After(deadline) {
|
||||
return false
|
||||
}
|
||||
e := toEv(r)
|
||||
evs = append(evs, e)
|
||||
ip := KeyOf(r.IP)
|
||||
if r.Source == "traefik" {
|
||||
byIP[e.IP] = append(byIP[e.IP], evLite{r.T, r.Path, r.UA})
|
||||
byIP[ip] = append(byIP[ip], evLite{r.T, r.Path, r.UA})
|
||||
}
|
||||
bumpMap(fams, r.Fam)
|
||||
for _, t := range r.Tech {
|
||||
bumpMap(techs, t)
|
||||
if f, ok := firstT[t]; !ok || r.T < f.T {
|
||||
firstT[t] = FirstTech{ID: t, Name: intel.TechniqueName(t), T: r.T, IP: e.IP}
|
||||
firstT[t] = FirstTech{ID: t, Name: intel.TechniqueName(t), T: r.T, IP: ip}
|
||||
}
|
||||
}
|
||||
if r.Cat == string(core.SSHFail) {
|
||||
@@ -248,12 +300,12 @@ func (s *Store) period(q PeriodQuery) PeriodOut {
|
||||
tgt[[2]string{r.Host, r.Domain}]++
|
||||
}
|
||||
}
|
||||
ipSeen[e.IP] = true
|
||||
ipSeen[ip] = true
|
||||
return true
|
||||
})
|
||||
out.Truncated = out.Truncated || info.Truncated
|
||||
out.Events = len(evs)
|
||||
sessions := intel.Sessions(evs, gap)
|
||||
out.Events = st.events
|
||||
sessions := append([]intel.Session(nil), st.sessions...)
|
||||
out.SessionsN = len(sessions)
|
||||
|
||||
// signatures and campaigns: events and sessions of an address are both in
|
||||
@@ -327,16 +379,12 @@ func (s *Store) period(q PeriodQuery) PeriodOut {
|
||||
sort.SliceStable(order, func(a, b int) bool {
|
||||
return intel.StageRank(sessions[order[a]].Stage) > intel.StageRank(sessions[order[b]].Stage)
|
||||
})
|
||||
evByIP := map[string][]intel.Ev{}
|
||||
for _, e := range evs {
|
||||
evByIP[e.IP] = append(evByIP[e.IP], e)
|
||||
}
|
||||
for _, i := range order {
|
||||
ss := sessions[i]
|
||||
if intel.StageRank(ss.Stage) < intel.StageRank(intel.StageExploit) || len(out.Escalations) >= maxEscalations {
|
||||
break
|
||||
}
|
||||
out.Escalations = append(out.Escalations, EscalationOut{Session: ss.ID, IP: ss.IP, Stage: ss.Stage, Steps: intel.Escalation(ss, evByIP[ss.IP])})
|
||||
out.Escalations = append(out.Escalations, EscalationOut{Session: ss.ID, IP: ss.IP, Stage: ss.Stage, Steps: intel.Escalation(ss, st.evs(ss.IP))})
|
||||
}
|
||||
// sessions list: the heaviest, in time order
|
||||
if len(sessions) > maxPeriodSessions {
|
||||
@@ -358,7 +406,10 @@ func (s *Store) period(q PeriodQuery) PeriodOut {
|
||||
if out.TopTargets[i].N != out.TopTargets[j].N {
|
||||
return out.TopTargets[i].N > out.TopTargets[j].N
|
||||
}
|
||||
return out.TopTargets[i].Domain < out.TopTargets[j].Domain
|
||||
if out.TopTargets[i].Domain != out.TopTargets[j].Domain {
|
||||
return out.TopTargets[i].Domain < out.TopTargets[j].Domain
|
||||
}
|
||||
return out.TopTargets[i].Host < out.TopTargets[j].Host
|
||||
})
|
||||
if len(out.TopTargets) > 20 {
|
||||
out.TopTargets = out.TopTargets[:20]
|
||||
@@ -612,15 +663,10 @@ func (s *Store) metrics(rg Range) MetricsOut {
|
||||
// from the registry: sessions, exposure, refused exploits, ssh login after brute force
|
||||
out.SessionsPerDay, out.ExposureRate, out.ExploitsRefused, out.SSH.OKAfterFail = mk(0, 0, nil), mk(0, 0, nil), mk(0, 0, nil), mk(0, 0, nil)
|
||||
if s.elog != nil {
|
||||
var evs []intel.Ev
|
||||
var expl, refused int
|
||||
deadline := time.Now().Add(studyDead)
|
||||
info := s.elog.Scan(evlog.Filter{From: from, To: to}, studyBudget, false, func(r evlog.Rec, _ []byte) bool {
|
||||
if len(evs) >= maxStudyEvents || len(evs)%4096 == 0 && time.Now().After(deadline) {
|
||||
out.Truncated = true
|
||||
return false
|
||||
}
|
||||
evs = append(evs, toEv(r))
|
||||
st := s.sessionStudy(PeriodQuery{From: from, To: to})
|
||||
out.Truncated = st.truncated
|
||||
info := s.elog.ScanRecs(evlog.Filter{From: from, To: to, Cats: map[string]bool{string(core.Exploit): true}}, studyBudget, false, func(r evlog.Rec) bool {
|
||||
if r.Cat == string(core.Exploit) && r.Status > 0 {
|
||||
expl++
|
||||
if r.Status >= 400 {
|
||||
@@ -630,7 +676,7 @@ func (s *Store) metrics(rg Range) MetricsOut {
|
||||
return true
|
||||
})
|
||||
out.Truncated = out.Truncated || info.Truncated
|
||||
sess := intel.Sessions(evs, 0)
|
||||
sess := st.sessions
|
||||
perDay := map[int64]int{}
|
||||
succ, okAfter := 0, 0
|
||||
for _, ss := range sess {
|
||||
|
||||
Reference in new issue
Block a user