CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 28s
CI / Dependency audit (push) Successful in 16s
CI / Tests (Linux, race detector) (push) Successful in 1m3s
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 3s
Optional second firewall (URUTAU_ENFORCE off/observe/on) through the host fail2ban, outbound traffic analysis (beaconing, mining, spam, DNS bypass, unauthorised scans) with authorised scanner workloads, host kit in deploy/host and a printable HTML report. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
331 lines
10 KiB
Go
331 lines
10 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"sort"
|
|
"strconv"
|
|
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/intel"
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
|
|
)
|
|
|
|
type reportFigures struct {
|
|
Requests uint64 `json:"requests"`
|
|
Attacks uint64 `json:"attacks"`
|
|
UniqueIPs int `json:"unique_ips"`
|
|
HostileIPs int `json:"hostile_ips"`
|
|
Bans int `json:"bans"`
|
|
Visitors uint64 `json:"visitors"`
|
|
NonHuman uint64 `json:"nonhuman"`
|
|
PageViews uint64 `json:"page_views"`
|
|
}
|
|
|
|
type reportTopIP struct {
|
|
IP string `json:"ip"`
|
|
Score float64 `json:"score"`
|
|
Level string `json:"level"`
|
|
CC string `json:"cc,omitempty"`
|
|
Country string `json:"country,omitempty"`
|
|
ASOrg string `json:"as_org,omitempty"`
|
|
Events uint32 `json:"events"`
|
|
Banned bool `json:"banned"`
|
|
}
|
|
|
|
type reportExposure struct {
|
|
T int64 `json:"t"`
|
|
IP string `json:"ip"`
|
|
Host string `json:"host"`
|
|
Domain string `json:"domain"`
|
|
Path string `json:"path"`
|
|
Status int `json:"status"`
|
|
Fam string `json:"fam"`
|
|
}
|
|
|
|
type reportOut struct {
|
|
Range store.RangeInfo `json:"range"`
|
|
Generated int64 `json:"generated"`
|
|
Hosts []string `json:"hosts"`
|
|
Summary struct {
|
|
reportFigures
|
|
Prev reportFigures `json:"prev"`
|
|
DeltaPct map[string]float64 `json:"delta_pct"`
|
|
HasPrev bool `json:"has_prev"`
|
|
} `json:"summary"`
|
|
Timeline struct {
|
|
Start int64 `json:"start"`
|
|
Step int64 `json:"step"`
|
|
Values []uint32 `json:"values"`
|
|
} `json:"timeline"`
|
|
Families []store.Count `json:"families"`
|
|
Techniques []store.Count `json:"techniques"`
|
|
Countries []store.Count `json:"countries"`
|
|
ASNs []store.Count `json:"asns"`
|
|
TopIPs []reportTopIP `json:"top_ips"`
|
|
Campaigns []intel.Campaign `json:"campaigns"`
|
|
Escalations []store.EscalationOut `json:"escalations"`
|
|
Exposures struct {
|
|
Total int `json:"total"`
|
|
ByFamily []store.Count `json:"by_family"`
|
|
Items []reportExposure `json:"items"`
|
|
} `json:"exposures"`
|
|
Defense struct {
|
|
MTTBMedianS float64 `json:"mttb_median_s"`
|
|
MTTBP90S float64 `json:"mttb_p90_s"`
|
|
MTTBPrevMedianS float64 `json:"mttb_prev_median_s"`
|
|
MTTBN int `json:"mttb_n"`
|
|
BanCoverage float64 `json:"ban_coverage"`
|
|
BanCoveragePrev float64 `json:"ban_coverage_prev"`
|
|
BlocklistCover float64 `json:"blocklist_coverage"`
|
|
ExposureRate float64 `json:"exposure_rate"`
|
|
ExploitsRefused float64 `json:"exploits_refused_pct"`
|
|
} `json:"defense"`
|
|
Truncated bool `json:"truncated,omitempty"`
|
|
}
|
|
|
|
func init() {
|
|
registerUserRoute("GET /api/v1/report", func(s *Server) http.HandlerFunc { return s.report })
|
|
}
|
|
|
|
// figuresOf builds the headline numbers; visitors, non-human clients and page
|
|
// views come from the traffic study (real browsers, deduplicated per IP and day).
|
|
func figuresOf(t store.Totals, tr store.TrafficData) reportFigures {
|
|
return reportFigures{Requests: t.Requests, Attacks: t.Malicious, UniqueIPs: t.UniqueIPs, HostileIPs: t.HostileIPs,
|
|
Bans: t.ActiveBans, Visitors: tr.Visitors, NonHuman: tr.NonHuman, PageViews: tr.Pages}
|
|
}
|
|
|
|
// trafficOf is what /api/v1/traffic answers for all domains, merged with the peers.
|
|
func (s *Server) trafficOf(r *http.Request, rg store.Range, host string) store.TrafficData {
|
|
var parts []store.TrafficData
|
|
self := s.opt.Store.Name()
|
|
if host == "all" || host == self {
|
|
parts = append(parts, s.opt.Store.TrafficFor(rg, "all"))
|
|
}
|
|
if s.opt.Hub != nil && host != self {
|
|
only := ""
|
|
if host != "all" {
|
|
only = host
|
|
}
|
|
parts = append(parts, s.opt.Hub.TrafficFor(r.Context(), rg, "all", only)...)
|
|
}
|
|
return store.MergeTraffic(host, parts)
|
|
}
|
|
|
|
func pctChange(v, prev float64) (float64, bool) {
|
|
if prev <= 0 {
|
|
return 0, false
|
|
}
|
|
return (v - prev) / prev * 100, true
|
|
}
|
|
|
|
var attackCats = []string{"scanner", "exploit", "bruteforce", "ssh-fail", "ssh-probe"}
|
|
|
|
// report: GET /api/v1/report, the aggregate behind the executive report.
|
|
func (s *Server) report(w http.ResponseWriter, r *http.Request) {
|
|
f := r.URL.Query().Get("format")
|
|
if f != "" && f != "json" && f != "html" {
|
|
badRequest(w, "format must be json or html")
|
|
return
|
|
}
|
|
rc, ok := s.buildReport(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
if f == "html" {
|
|
s.writeReportHTML(w, r, rc)
|
|
return
|
|
}
|
|
writeJSON(w, 200, rc.out)
|
|
}
|
|
|
|
// reportCtx is the computed report plus the window it covers.
|
|
type reportCtx struct {
|
|
out reportOut
|
|
rg store.Range
|
|
host string
|
|
from, to int64
|
|
}
|
|
|
|
func (s *Server) buildReport(w http.ResponseWriter, r *http.Request) (*reportCtx, bool) {
|
|
if r.URL.Query().Get("range") == "" && r.URL.Query().Get("from") == "" {
|
|
q := r.URL.Query()
|
|
q.Set("range", "7d")
|
|
r.URL.RawQuery = q.Encode()
|
|
}
|
|
rg, ok := s.rangeOf(w, r)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
host := r.URL.Query().Get("host")
|
|
if host == "" {
|
|
host = "all"
|
|
}
|
|
if !s.knownHost(w, host) {
|
|
return nil, false
|
|
}
|
|
now := s.opt.Now().Unix()
|
|
info := rg.Info(now)
|
|
var out reportOut
|
|
out.Range, out.Generated = info, now
|
|
|
|
// period study (families, techniques, ASNs, campaigns, escalations)
|
|
per, ok := s.periodStudy(w, r)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
// summary of the period and of the previous one
|
|
parts, all := s.parts(r.Context(), rg, host)
|
|
var sum store.Summary
|
|
if len(parts) > 0 {
|
|
sum = store.Merge(host, parts)
|
|
}
|
|
out.Hosts = []string{}
|
|
for _, hi := range all {
|
|
if host == "all" || hi.Name == host {
|
|
out.Hosts = append(out.Hosts, hi.Name)
|
|
}
|
|
}
|
|
sort.Strings(out.Hosts)
|
|
out.Summary.reportFigures = figuresOf(sum.Totals, s.trafficOf(r, rg, host))
|
|
span := info.To - info.From
|
|
if span > 0 {
|
|
if prg, err := s.opt.Store.ResolveRange("", strconv.FormatInt(info.From-span, 10), strconv.FormatInt(info.From, 10)); err == nil {
|
|
pp, _ := s.parts(r.Context(), prg, host)
|
|
if len(pp) > 0 {
|
|
ps := store.Merge(host, pp)
|
|
out.Summary.Prev = figuresOf(ps.Totals, s.trafficOf(r, prg, host))
|
|
out.Summary.HasPrev = ps.Totals.Requests > 0 || ps.Totals.Malicious > 0
|
|
}
|
|
}
|
|
}
|
|
out.Summary.DeltaPct = map[string]float64{}
|
|
if out.Summary.HasPrev {
|
|
cur, prev := out.Summary.reportFigures, out.Summary.Prev
|
|
for k, p := range map[string][2]float64{
|
|
"requests": {float64(cur.Requests), float64(prev.Requests)}, "attacks": {float64(cur.Attacks), float64(prev.Attacks)},
|
|
"unique_ips": {float64(cur.UniqueIPs), float64(prev.UniqueIPs)}, "hostile_ips": {float64(cur.HostileIPs), float64(prev.HostileIPs)},
|
|
"bans": {float64(cur.Bans), float64(prev.Bans)}, "visitors": {float64(cur.Visitors), float64(prev.Visitors)},
|
|
"nonhuman": {float64(cur.NonHuman), float64(prev.NonHuman)}, "page_views": {float64(cur.PageViews), float64(prev.PageViews)},
|
|
} {
|
|
if d, ok := pctChange(p[0], p[1]); ok {
|
|
out.Summary.DeltaPct[k] = d
|
|
}
|
|
}
|
|
}
|
|
// attacks timeline
|
|
tl := sum.Timeline
|
|
out.Timeline.Start, out.Timeline.Step = tl.Start, tl.Step
|
|
out.Timeline.Values = []uint32{}
|
|
for _, c := range attackCats {
|
|
for i, v := range tl.Series[c] {
|
|
for len(out.Timeline.Values) <= i {
|
|
out.Timeline.Values = append(out.Timeline.Values, 0)
|
|
}
|
|
out.Timeline.Values[i] += v
|
|
}
|
|
}
|
|
// tops
|
|
out.Families, out.Techniques, out.ASNs = capCounts(per.TopFams, 10), capCounts(per.TopTechs, 10), capCounts(per.TopASNs, 10)
|
|
out.Countries = capCounts(sum.Countries, 10)
|
|
out.Campaigns = per.Campaigns
|
|
if len(out.Campaigns) > 8 {
|
|
out.Campaigns = out.Campaigns[:8]
|
|
}
|
|
if out.Campaigns == nil {
|
|
out.Campaigns = []intel.Campaign{}
|
|
}
|
|
out.Escalations = per.Escalations
|
|
if len(out.Escalations) > 8 {
|
|
out.Escalations = out.Escalations[:8]
|
|
}
|
|
if out.Escalations == nil {
|
|
out.Escalations = []store.EscalationOut{}
|
|
}
|
|
out.Truncated = per.Truncated
|
|
ips := append([]store.IPSummary(nil), sum.IPs...)
|
|
sort.SliceStable(ips, func(i, j int) bool { return ips[i].Score > ips[j].Score })
|
|
out.TopIPs = []reportTopIP{}
|
|
out.Exposures.ByFamily, out.Exposures.Items = []store.Count{}, []reportExposure{}
|
|
fam := map[string]uint32{}
|
|
for i, x := range ips {
|
|
var ev uint32
|
|
for _, n := range x.Counts {
|
|
ev += n
|
|
}
|
|
if i < 10 {
|
|
out.TopIPs = append(out.TopIPs, reportTopIP{IP: x.IP, Score: x.Score, Level: string(x.Level), CC: x.CC, Country: x.Country, ASOrg: x.ASOrg, Events: ev, Banned: x.Banned})
|
|
}
|
|
if i < 30 {
|
|
if inf, ok := s.opt.Store.IPInfoOf(x.IP); ok {
|
|
for _, e := range inf.Expo {
|
|
if e.T < info.From || e.T > info.To {
|
|
continue
|
|
}
|
|
out.Exposures.Total++
|
|
if e.Fam != "" {
|
|
fam[e.Fam]++
|
|
}
|
|
if len(out.Exposures.Items) < 10 {
|
|
out.Exposures.Items = append(out.Exposures.Items, reportExposure{T: e.T, IP: x.IP, Host: e.Host, Domain: e.Domain, Path: e.Path, Status: e.Status, Fam: e.Fam})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for k, n := range fam {
|
|
out.Exposures.ByFamily = append(out.Exposures.ByFamily, store.Count{Key: k, N: n})
|
|
}
|
|
sort.Slice(out.Exposures.ByFamily, func(i, j int) bool {
|
|
a, b := out.Exposures.ByFamily[i], out.Exposures.ByFamily[j]
|
|
return a.N > b.N || (a.N == b.N && a.Key < b.Key)
|
|
})
|
|
sort.Slice(out.Exposures.Items, func(i, j int) bool { return out.Exposures.Items[i].T > out.Exposures.Items[j].T })
|
|
|
|
// defence metrics (same study and slot as /metrics)
|
|
m, ok := s.reportMetrics(w, r, rg, host)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
d := &out.Defense
|
|
d.MTTBMedianS, d.MTTBP90S, d.MTTBPrevMedianS, d.MTTBN = m.MTTB.MedianS, m.MTTB.P90S, m.MTTB.PrevMed, m.MTTB.N
|
|
d.BanCoverage, d.BanCoveragePrev = m.BanCoverage.Value, m.BanCoverage.Prev
|
|
d.BlocklistCover, d.ExposureRate, d.ExploitsRefused = m.BlocklistCoverage.Value, m.ExposureRate.Value, m.ExploitsRefused.Value
|
|
f, t := rg.Bounds(now)
|
|
return &reportCtx{out: out, rg: rg, host: host, from: f, to: t}, true
|
|
}
|
|
|
|
func capCounts(c []store.Count, n int) []store.Count {
|
|
if len(c) > n {
|
|
c = c[:n]
|
|
}
|
|
if c == nil {
|
|
return []store.Count{}
|
|
}
|
|
return c
|
|
}
|
|
|
|
func (s *Server) reportMetrics(w http.ResponseWriter, r *http.Request, rg store.Range, host string) (store.MetricsOut, bool) {
|
|
local, peers, only, ok := s.studyHosts(w, host)
|
|
if !ok {
|
|
return store.MetricsOut{}, false
|
|
}
|
|
if !s.userSlot(w) {
|
|
return store.MetricsOut{}, false
|
|
}
|
|
defer func() { <-s.user }()
|
|
var parts []store.MetricsOut
|
|
var studies map[string]store.PeerStudy
|
|
if peers {
|
|
from, to := rg.Bounds(s.opt.Now().Unix())
|
|
studies = s.opt.Hub.StudiesFor(r.Context(), store.PeriodQuery{From: from, To: to}, only)
|
|
}
|
|
if local || len(studies) == 0 {
|
|
parts = append(parts, s.opt.Store.Metrics(rg))
|
|
}
|
|
for _, p := range s.opt.Hub.PeerNames() {
|
|
if st, ok := studies[p]; ok {
|
|
parts = append(parts, st.Metrics)
|
|
}
|
|
}
|
|
return store.MergeMetrics(parts), true
|
|
}
|