package intel import ( "sort" "strconv" ) // Reason is one stable-coded explanation for a verdict. Text is an i18n key // ("intel.reason."); Params carry the values to interpolate. type Reason struct { Code string `json:"code"` Text string `json:"text"` Weight float64 `json:"weight"` N int `json:"n"` Params map[string]string `json:"params,omitempty"` } // ReasonInput is what the backend knows about a source. type ReasonInput struct { ExploitsRefused map[string]int // refused (4xx/5xx) exploit attempts by family ToolUA string // attack-tool label, "" when none BannedJails []string // jails that banned it Categories int // distinct malicious categories seen Exposure map[string]int // 2xx answers on sensitive families SSHOKAfterBrute bool // ssh login after brute force Admin bool // the source is an administrator Immune bool // the source is whitelisted } // Reason codes. const ( ReasonExploitsRefused = "exploits_refused" ReasonToolUA = "tool_ua" ReasonBannedJail = "banned_jail" ReasonMultiCategory = "multi_category" ReasonExposure = "exposure" ReasonSSHAfterBrute = "ssh_login_after_bruteforce" ReasonAdmin = "admin" ReasonImmune = "immune" ) func mkReason(code string, w float64, n int, params map[string]string) Reason { return Reason{Code: code, Text: "intel.reason." + code, Weight: w, N: n, Params: params} } func sumMap(m map[string]int) (n int, keys []string) { for k, v := range m { if v > 0 { n += v keys = append(keys, k) } } sort.Strings(keys) return } func join(keys []string) string { s := "" for i, k := range keys { if i >= 5 { break } if i > 0 { s += ", " } s += k } return s } // Reasons explains why a source is considered hostile, heaviest first. func Reasons(in ReasonInput) []Reason { var out []Reason if n, fams := sumMap(in.ExploitsRefused); n > 0 { out = append(out, mkReason(ReasonExploitsRefused, 0.35, n, map[string]string{"families": join(fams), "n": strconv.Itoa(n)})) } if in.ToolUA != "" { out = append(out, mkReason(ReasonToolUA, 0.15, 1, map[string]string{"tool": clipStr(in.ToolUA, 80)})) } if n := len(in.BannedJails); n > 0 { out = append(out, mkReason(ReasonBannedJail, 0.2, n, map[string]string{"jails": join(in.BannedJails)})) } if in.Categories >= 2 { out = append(out, mkReason(ReasonMultiCategory, 0.1, in.Categories, map[string]string{"n": strconv.Itoa(in.Categories)})) } if n, fams := sumMap(in.Exposure); n > 0 { out = append(out, mkReason(ReasonExposure, 0.5, n, map[string]string{"families": join(fams), "n": strconv.Itoa(n)})) } if in.SSHOKAfterBrute { out = append(out, mkReason(ReasonSSHAfterBrute, 0.9, 1, nil)) } if in.Admin { out = append(out, mkReason(ReasonAdmin, -0.5, 1, nil)) } if in.Immune { out = append(out, mkReason(ReasonImmune, -1, 1, nil)) } sort.SliceStable(out, func(i, j int) bool { return out[i].Weight > out[j].Weight }) return out } // Action is a suggested response. type Action struct { Code string `json:"code"` Priority int `json:"priority"` // 1 = most urgent Reason string `json:"reason"` // code of the Reason behind it } // Action codes. const ( ActionBlock = "block" ActionReport = "report" ActionInvestigate = "investigate" ActionIncident = "incident" ) // ActionInput feeds Actions. type ActionInput struct { Reasons []Reason Banned bool // already blocked Immune bool Admin bool Stage string // highest session stage reached } // Actions suggests responses. Immune or admin sources get none. A successful // login after brute force or a 2xx on a sensitive path raises an incident and // an investigation; block applies when not yet banned and the evidence is // strong (score >= 0.5 or exploit stage or above); report when the score // reaches 0.5. func Actions(in ActionInput) []Action { if in.Immune || in.Admin { return nil } score := 0.0 has := map[string]bool{} for _, r := range in.Reasons { has[r.Code] = true if r.Code == ReasonAdmin || r.Code == ReasonImmune { return nil } score += r.Weight } var out []Action switch { case has[ReasonSSHAfterBrute]: out = append(out, Action{ActionIncident, 1, ReasonSSHAfterBrute}) case has[ReasonExposure]: out = append(out, Action{ActionIncident, 1, ReasonExposure}) } if has[ReasonSSHAfterBrute] || has[ReasonExposure] { code := ReasonExposure if has[ReasonSSHAfterBrute] { code = ReasonSSHAfterBrute } out = append(out, Action{ActionInvestigate, 2, code}) } if !in.Banned && (score >= 0.5 || StageRank(in.Stage) >= StageRank(StageExploit)) { out = append(out, Action{ActionBlock, 3, topCode(in.Reasons)}) } if score >= 0.5 { out = append(out, Action{ActionReport, 4, topCode(in.Reasons)}) } return out } func topCode(rs []Reason) string { if len(rs) == 0 { return "" } return rs[0].Code }