Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
222 lines
6.2 KiB
Go
222 lines
6.2 KiB
Go
package store
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
|
|
)
|
|
|
|
// Own traffic: web requests whose client is an own or trusted origin (--trusted
|
|
// and the --ignore ranges, private networks by default). They are counted in
|
|
// aggregates only, apart from every attack and visitor aggregate: they never
|
|
// create an IP record, score, session, campaign, technique, family, ban
|
|
// evidence or page view, and they leave no event record. The classes are the
|
|
// two names below.
|
|
const (
|
|
OwnTrusted = "trusted" // an address of --trusted (URUTAU_TRUSTED)
|
|
OwnInternal = "internal" // an address of an --ignore range (private networks by default)
|
|
)
|
|
|
|
const (
|
|
ownCap = 64 // distinct origins, domains and paths kept per hour and class-agnostic
|
|
ownPath = 120
|
|
)
|
|
|
|
// ownHour is the own traffic of one hour. Class index: 0 trusted, 1 internal.
|
|
type ownHour struct {
|
|
N [2]uint32
|
|
S [2][4]uint32 // 2xx 3xx 4xx 5xx
|
|
Origins, Domains, Paths map[string]uint32
|
|
}
|
|
|
|
func ownIdx(class string) int {
|
|
if class == OwnInternal {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// Own counts one web request of an own or trusted origin (class OwnTrusted or
|
|
// OwnInternal). Only the Traefik source is recorded.
|
|
func (s *Store) Own(ev core.Event, class string) {
|
|
now := s.now()
|
|
if ev.Time.Before(now.Add(-s.cfg.Retention)) || ev.IP == "" {
|
|
return
|
|
}
|
|
ts := ev.Time.Unix()
|
|
if max := now.Unix() + 300; ts > max {
|
|
ts = max
|
|
}
|
|
ci := ownIdx(class)
|
|
m, h := ts/60, ts/3600
|
|
i := int(m % slots)
|
|
path := ev.Path
|
|
if j := strings.IndexAny(path, "?#"); j >= 0 {
|
|
path = path[:j]
|
|
}
|
|
if len(path) > ownPath {
|
|
path = path[:ownPath]
|
|
}
|
|
origin := class[:1] + "|" + KeyOf(ev.IP)
|
|
s.mu.Lock()
|
|
if s.ownSt[i] != m {
|
|
s.ownSt[i], s.ownN[i] = m, [2]uint32{}
|
|
}
|
|
s.ownN[i][ci]++
|
|
hs := &s.hours[int(h%hourSlots)]
|
|
if hs.Hour != h {
|
|
*hs = hourSlot{Hour: h}
|
|
}
|
|
if hs.Own == nil {
|
|
hs.Own = &ownHour{}
|
|
}
|
|
o := hs.Own
|
|
o.N[ci]++
|
|
if k := ev.Status/100 - 2; k >= 0 && k < 4 {
|
|
o.S[ci][k]++
|
|
}
|
|
bumpCap(&o.Origins, origin, ownCap)
|
|
bumpCap(&o.Domains, normDomain(ev.Domain), ownCap)
|
|
bumpCap(&o.Paths, path, ownCap)
|
|
s.mu.Unlock()
|
|
}
|
|
|
|
// OwnTraffic is the own and trusted traffic of a window. It is not part of
|
|
// totals.requests: RequestsAll in the totals adds it.
|
|
type OwnTraffic struct {
|
|
Trusted uint64 `json:"trusted"`
|
|
Internal uint64 `json:"internal"`
|
|
Status [4]uint64 `json:"status"` // 2xx 3xx 4xx 5xx
|
|
Start int64 `json:"start"`
|
|
Step int64 `json:"step"`
|
|
Series map[string][]uint32 `json:"series"` // "trusted" and "internal"
|
|
Origins []Count `json:"origins"` // Key: address (an IPv6 /64 as a prefix), Label: class
|
|
Domains []Count `json:"domains"`
|
|
Paths []Count `json:"paths"`
|
|
}
|
|
|
|
// ownLocked adds the own traffic of the grid to the totals and, when o is not
|
|
// nil, to the breakdown. The caller holds s.mu.
|
|
func (s *Store) ownLocked(g grid, to int64, tot *Totals) OwnTraffic {
|
|
o := OwnTraffic{Start: g.Start, Step: g.Step, Series: map[string][]uint32{
|
|
OwnTrusted: make([]uint32, g.N), OwnInternal: make([]uint32, g.N)}}
|
|
if g.minute() {
|
|
for m := g.Start / 60; m <= to/60; m++ {
|
|
i := int(m % slots)
|
|
if s.ownSt[i] != m {
|
|
continue
|
|
}
|
|
if b := int((m*60 - g.Start) / g.Step); b >= 0 && b < g.N {
|
|
o.Series[OwnTrusted][b] += s.ownN[i][0]
|
|
o.Series[OwnInternal][b] += s.ownN[i][1]
|
|
}
|
|
}
|
|
}
|
|
origins, doms, paths := map[string]uint32{}, map[string]uint32{}, map[string]uint32{}
|
|
for h := g.Start / 3600; h <= to/3600; h++ {
|
|
hs := &s.hours[int(h%hourSlots)]
|
|
if hs.Hour != h || hs.Own == nil {
|
|
continue
|
|
}
|
|
oh := hs.Own
|
|
o.Trusted += uint64(oh.N[0])
|
|
o.Internal += uint64(oh.N[1])
|
|
for c := range oh.S {
|
|
for k, v := range oh.S[c] {
|
|
o.Status[k] += uint64(v)
|
|
}
|
|
}
|
|
if !g.minute() {
|
|
if b := int(h - g.Start/3600); b >= 0 && b < g.N {
|
|
o.Series[OwnTrusted][b] += oh.N[0]
|
|
o.Series[OwnInternal][b] += oh.N[1]
|
|
}
|
|
}
|
|
for _, p := range [...]struct{ from, to map[string]uint32 }{{oh.Origins, origins}, {oh.Domains, doms}, {oh.Paths, paths}} {
|
|
for k, v := range p.from {
|
|
p.to[k] += v
|
|
}
|
|
}
|
|
}
|
|
tot.Trusted, tot.Internal = o.Trusted, o.Internal
|
|
tot.RequestsAll = tot.Requests + o.Trusted + o.Internal
|
|
if o.Trusted > 0 {
|
|
tot.Counts[OwnTrusted] = o.Trusted
|
|
}
|
|
if o.Internal > 0 {
|
|
tot.Counts[OwnInternal] = o.Internal
|
|
}
|
|
o.Origins = ownOrigins(topN(origins, 40))
|
|
o.Domains, o.Paths = topN(doms, 20), topN(paths, 20)
|
|
return o
|
|
}
|
|
|
|
// ownOrigins splits the stored "<class>|<address>" keys into key and label.
|
|
func ownOrigins(in []Count) []Count {
|
|
for i, c := range in {
|
|
if len(c.Key) > 2 && c.Key[1] == '|' {
|
|
if c.Key[0] == 't' {
|
|
in[i].Label = OwnTrusted
|
|
} else {
|
|
in[i].Label = OwnInternal
|
|
}
|
|
in[i].Key = c.Key[2:]
|
|
}
|
|
}
|
|
return in
|
|
}
|
|
|
|
// mergeOwn adds the own traffic of several hosts. Series are aligned by
|
|
// timestamp like the attack timeline.
|
|
func mergeOwn(parts []Summary, grid Timeline) OwnTraffic {
|
|
n := 0
|
|
for _, c := range grid.Cats {
|
|
n = len(grid.Series[c])
|
|
break
|
|
}
|
|
out := OwnTraffic{Start: grid.Start, Step: grid.Step, Series: map[string][]uint32{
|
|
OwnTrusted: make([]uint32, n), OwnInternal: make([]uint32, n)}}
|
|
var origins, doms, paths [][]Count
|
|
for _, p := range parts {
|
|
o := p.Own
|
|
out.Trusted += o.Trusted
|
|
out.Internal += o.Internal
|
|
for k, v := range o.Status {
|
|
out.Status[k] += v
|
|
}
|
|
if o.Step == out.Step && o.Step > 0 {
|
|
shift := (o.Start - out.Start) / out.Step
|
|
for _, c := range []string{OwnTrusted, OwnInternal} {
|
|
for i, v := range o.Series[c] {
|
|
if j := int64(i) + shift; j >= 0 && j < int64(n) {
|
|
out.Series[c][j] += v
|
|
}
|
|
}
|
|
}
|
|
}
|
|
origins = append(origins, o.Origins)
|
|
doms = append(doms, o.Domains)
|
|
paths = append(paths, o.Paths)
|
|
}
|
|
out.Origins = mergeOwnOrigins(40, origins)
|
|
out.Domains, out.Paths = mergeCounts(20, doms...), mergeCounts(20, paths...)
|
|
return out
|
|
}
|
|
|
|
func mergeOwnOrigins(n int, lists [][]Count) []Count {
|
|
m, lab := map[string]uint32{}, map[string]string{}
|
|
for _, l := range lists {
|
|
for _, c := range l {
|
|
k := c.Label + "|" + c.Key
|
|
m[k] += c.N
|
|
lab[k] = c.Label
|
|
}
|
|
}
|
|
out := topN(m, n)
|
|
for i := range out {
|
|
out[i].Label = lab[out[i].Key]
|
|
out[i].Key = out[i].Key[len(out[i].Label)+1:]
|
|
}
|
|
return out
|
|
}
|