package store import ( "sort" "strings" "time" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/evlog" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/intel" ) // TechniqueRow is one MITRE ATT&CK technique seen in the window. type TechniqueRow struct { ID string `json:"id"` Name string `json:"name"` Events int `json:"events"` IPs int `json:"ips"` First int64 `json:"first"` Last int64 `json:"last"` TrendPct *float64 `json:"trend_pct"` // against the previous window of the same length; null without one } // TacticRow groups the techniques of one tactic. type TacticRow struct { ID string `json:"id"` Name string `json:"name"` Techniques []TechniqueRow `json:"techniques"` } // TechniquesOut is the matrix behind GET /api/v1/techniques. type TechniquesOut struct { Enabled bool `json:"enabled"` Truncated bool `json:"truncated,omitempty"` Tactics []TacticRow `json:"tactics"` Total int `json:"total"` // events that carry at least one technique } type tactic struct{ id, name string } // ATT&CK enterprise tactics in matrix order. var tacticOrder = []tactic{ {"TA0043", "Reconnaissance"}, {"TA0042", "Resource Development"}, {"TA0001", "Initial Access"}, {"TA0002", "Execution"}, {"TA0003", "Persistence"}, {"TA0004", "Privilege Escalation"}, {"TA0005", "Defense Evasion"}, {"TA0006", "Credential Access"}, {"TA0007", "Discovery"}, {"TA0008", "Lateral Movement"}, {"TA0009", "Collection"}, {"TA0011", "Command and Control"}, {"TA0010", "Exfiltration"}, {"TA0040", "Impact"}, } // techTactic maps a technique id to the tactic id the matrix puts it under. var techTactic = map[string]string{ "T1190": "TA0001", "T1078": "TA0001", "T1133": "TA0001", "T1566": "TA0001", "T1059": "TA0002", "T1203": "TA0002", "T1505": "TA0003", "T1098": "TA0003", "T1110": "TA0006", "T1552": "TA0006", "T1555": "TA0006", "T1528": "TA0006", "T1083": "TA0007", "T1087": "TA0007", "T1082": "TA0007", "T1046": "TA0007", "T1018": "TA0007", "T1213": "TA0009", "T1005": "TA0009", "T1530": "TA0009", "T1595": "TA0043", "T1592": "TA0043", "T1590": "TA0043", "T1589": "TA0043", "T1593": "TA0043", "T1105": "TA0011", "T1071": "TA0011", "T1499": "TA0040", "T1498": "TA0040", } // TacticOf returns the tactic id of a technique or sub-technique ("" when unknown). func TacticOf(id string) string { if t, ok := techTactic[id]; ok { return t } if i := strings.IndexByte(id, '.'); i > 0 { return techTactic[id[:i]] } return "" } type techAcc struct { cur, prev int ips map[string]struct{} first, last int64 } // Techniques groups the techniques observed in [q.From, q.To] by MITRE // tactic, with the trend against the previous window of the same length. // Same caps, deadline and 30 s cache as Period. func (s *Store) Techniques(q PeriodQuery) TechniquesOut { k := studyKey{kind: "techniques", from: q.From, to: q.To, host: q.Host} return s.cached(k, func() any { return s.techniques(q) }).(TechniquesOut) } func (s *Store) techniques(q PeriodQuery) TechniquesOut { out := TechniquesOut{Tactics: []TacticRow{}} if s.elog == nil { return out } out.Enabled = true span := q.To - q.From if span < 1 { span = 1 } deadline := time.Now().Add(studyDead) acc := map[string]*techAcc{} n := 0 info := s.elog.Scan(evlog.Filter{From: q.From - span, To: q.To, Host: q.Host}, studyBudget, false, func(r evlog.Rec, _ []byte) bool { if n++; n > maxStudyEvents || n%4096 == 0 && time.Now().After(deadline) { out.Truncated = true return false } if len(r.Tech) == 0 { return true } inCur := r.T >= q.From if inCur { out.Total++ } for _, id := range r.Tech { a := acc[id] if a == nil { a = &techAcc{ips: map[string]struct{}{}} acc[id] = a } if !inCur { a.prev++ continue } a.cur++ a.ips[KeyOf(r.IP)] = struct{}{} if a.first == 0 || r.T < a.first { a.first = r.T } if r.T > a.last { a.last = r.T } } return true }) out.Truncated = out.Truncated || info.Truncated by := map[string][]TechniqueRow{} for id, a := range acc { if a.cur == 0 { continue } name := intel.TechniqueName(id) if name == "" { name = id } row := TechniqueRow{ID: id, Name: name, Events: a.cur, IPs: len(a.ips), First: a.first, Last: a.last} if a.prev > 0 { v := float64(a.cur-a.prev) / float64(a.prev) * 100 row.TrendPct = &v } by[TacticOf(id)] = append(by[TacticOf(id)], row) } add := func(t tactic) { rows := by[t.id] if len(rows) == 0 { return } sort.Slice(rows, func(i, j int) bool { if rows[i].Events != rows[j].Events { return rows[i].Events > rows[j].Events } return rows[i].ID < rows[j].ID }) out.Tactics = append(out.Tactics, TacticRow{ID: t.id, Name: t.name, Techniques: rows}) } for _, t := range tacticOrder { add(t) } add(tactic{"", "Other"}) return out }