Files
urutau/internal/intel/verdict.go
T
Lucas RayanandClaude Opus 5.5 b7462c14fa
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 27s
CI / Dependency audit (push) Successful in 18s
CI / Tests (Linux, race detector) (push) Successful in 58s
Release / Plan (reuse the published image?) (push) Successful in 4s
Release / ci (push) Skipped
Release / Build image (amd64) (push) Skipped
Release / Build image (arm64) (push) Skipped
Release / Publish tags (push) Successful in 13s
Release / Gitea release (push) Successful in 5s
feat: 1.2.0 attack intelligence, replay, 30-day history and new interface
Intelligence: attack families mapped to MITRE ATT&CK, network type from
ASN/rDNS without external APIs, request cadence, sessions with
recon/probe/exploit/access/banned stages, campaigns across IPs, verdict
with reasons, recommended actions, exposure detection (2xx with a body
on sensitive paths), STIX 2.1 export, FCrDNS for search engine bots,
optional Tor exit list.

Data: 30-day retention, twelve ranges plus custom from/to, an on-disk
event log (daily files, hourly gzip members, size cap, corruption and
disk-full tolerant), /events, /period, /metrics with stable per-hour
metrics, an extended /ip/{ip}, and peer /period merged on the hub.

Interface: layout system with fixed card sizes, range picker in the
URL, IP analysis page, Replay page, Geography down to state and city
(Natural Earth admin-1), Live page redesigned with stable metrics and a
reusable attack map. Demo data and documentation screenshots.

Security: bounded and deadline-limited period computations, linear
campaign clustering, singleflight caches, sanitized peer studies, event
query budgets, gzip bomb guard.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 21:45:59 -03:00

173 lines
4.9 KiB
Go

package intel
import (
"sort"
"strconv"
)
// Reason is one stable-coded explanation for a verdict. Text is an i18n key
// ("intel.reason.<code>"); 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
}