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
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>
237 lines
7.9 KiB
Go
237 lines
7.9 KiB
Go
package evlog
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
var base = time.Date(2026, 11, 14, 12, 0, 0, 0, time.UTC)
|
|
|
|
func line(t int64, id uint64, ip, cat string) []byte {
|
|
return []byte(fmt.Sprintf(`{"id":%d,"t":%d,"ip":"%s","cat":"%s","source":"traefik","host":"h","domain":"a.example","path":"/.env","status":404}`, id, t, ip, cat))
|
|
}
|
|
|
|
func open(t *testing.T, now *time.Time, max int64) *Log {
|
|
t.Helper()
|
|
l, err := Open(Options{Dir: filepath.Join(t.TempDir(), "events"), MaxBytes: max, Now: func() time.Time { return *now }})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return l
|
|
}
|
|
|
|
func TestAppendQueryPaginationAndOrder(t *testing.T) {
|
|
now := base
|
|
l := open(t, &now, 0)
|
|
for i := 0; i < 25; i++ {
|
|
l.Append(base.Unix()-int64(i), line(base.Unix()-int64(i), uint64(i+1), "1.2.3.4", "exploit"))
|
|
}
|
|
l.Append(base.Unix(), line(base.Unix(), 100, "5.6.7.8", "scanner"))
|
|
f := Filter{From: base.Unix() - 3600, To: base.Unix() + 10}
|
|
var all []uint64
|
|
cur := ""
|
|
for page := 0; page < 10; page++ {
|
|
r, err := l.Query(Query{Filter: f, Limit: 10, Cursor: cur, Desc: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, it := range r.Items {
|
|
all = append(all, it.Rec.ID)
|
|
}
|
|
if r.Next == "" {
|
|
break
|
|
}
|
|
cur = r.Next
|
|
}
|
|
if len(all) != 26 {
|
|
t.Fatalf("pagination returned %d events", len(all))
|
|
}
|
|
seen := map[uint64]bool{}
|
|
for _, id := range all {
|
|
if seen[id] {
|
|
t.Fatal("duplicate across pages")
|
|
}
|
|
seen[id] = true
|
|
}
|
|
asc, _ := l.Query(Query{Filter: f, Limit: 5})
|
|
for i := 1; i < len(asc.Items); i++ {
|
|
if asc.Items[i].Rec.T < asc.Items[i-1].Rec.T {
|
|
t.Fatal("ascending order broken")
|
|
}
|
|
}
|
|
if r, _ := l.Query(Query{Filter: Filter{From: f.From, To: f.To, IP: "5.6.7.8"}}); len(r.Items) != 1 {
|
|
t.Fatalf("ip filter: %d", len(r.Items))
|
|
}
|
|
if r, _ := l.Query(Query{Filter: Filter{From: f.From, To: f.To, Cats: map[string]bool{"scanner": true}}}); len(r.Items) != 1 {
|
|
t.Fatal("cat filter")
|
|
}
|
|
if r, _ := l.Query(Query{Filter: Filter{From: f.From, To: f.To, Q: "ENV"}, Limit: 100}); len(r.Items) != 26 || r.TotalEstimate != 26 {
|
|
t.Fatalf("q filter %d est %d", len(r.Items), r.TotalEstimate)
|
|
}
|
|
if _, err := l.Query(Query{Cursor: "!!!"}); !errors.Is(err, ErrCursor) {
|
|
t.Fatal("bad cursor must fail")
|
|
}
|
|
}
|
|
|
|
func TestRotationCompressionRangeIndexAndRetention(t *testing.T) {
|
|
now := base
|
|
l := open(t, &now, 0)
|
|
old := base.Add(-72 * time.Hour)
|
|
for h := 0; h < 24; h++ {
|
|
ts := old.Truncate(24 * time.Hour).Add(time.Duration(h) * time.Hour).Unix()
|
|
l.Append(ts, line(ts, uint64(h+1), "9.9.9.9", "exploit"))
|
|
}
|
|
l.Append(base.Unix(), line(base.Unix(), 500, "1.1.1.1", "scanner"))
|
|
l.Flush()
|
|
now = base.Add(time.Minute)
|
|
l.Close()
|
|
l.Maintain()
|
|
ents, _ := os.ReadDir(l.opt.Dir)
|
|
var names []string
|
|
for _, e := range ents {
|
|
names = append(names, e.Name())
|
|
}
|
|
joined := strings.Join(names, " ")
|
|
if !strings.Contains(joined, ".jsonl.gz") || !strings.Contains(joined, ".idx") {
|
|
t.Fatalf("old day must be compressed with an index: %v", names)
|
|
}
|
|
oldDay := old.Format("2006-01-02")
|
|
if _, err := os.Stat(filepath.Join(l.opt.Dir, oldDay+".jsonl")); err == nil {
|
|
t.Fatal("plain file of the old day must be gone")
|
|
}
|
|
day := old.Truncate(24 * time.Hour).Unix()
|
|
// whole day, then one hour only (index)
|
|
if r, _ := l.Query(Query{Filter: Filter{From: day, To: day + 86399}, Limit: 100}); len(r.Items) != 24 {
|
|
t.Fatalf("whole day %d", len(r.Items))
|
|
}
|
|
var info ScanInfo
|
|
n := 0
|
|
info = l.Scan(Filter{From: day + 5*3600, To: day + 5*3600 + 100}, 0, false, func(Rec, []byte) bool { n++; return true })
|
|
if n != 1 || info.Lines > 2 {
|
|
t.Fatalf("hour index must avoid reading the whole day: matched %d lines read %d", n, info.Lines)
|
|
}
|
|
// a late event for the compressed day is appended as a new member
|
|
late := day + 7*3600 + 5
|
|
l.Append(late, line(late, 900, "8.8.8.8", "exploit"))
|
|
l.Close()
|
|
now = base.Add(2 * time.Minute)
|
|
l.Maintain()
|
|
if r, _ := l.Query(Query{Filter: Filter{From: day, To: day + 86399}, Limit: 100}); len(r.Items) != 25 {
|
|
t.Fatalf("late event lost: %d", len(r.Items))
|
|
}
|
|
if r, _ := l.Query(Query{Filter: Filter{From: day + 7*3600, To: day + 7*3600 + 60}, Limit: 100}); len(r.Items) != 2 {
|
|
t.Fatalf("index after append: %d", len(r.Items))
|
|
}
|
|
// retention
|
|
now = base.Add(31 * 24 * time.Hour)
|
|
l.Maintain()
|
|
if r, _ := l.Query(Query{Filter: Filter{From: day, To: day + 86399}}); len(r.Items) != 0 {
|
|
t.Fatal("expired day must be deleted")
|
|
}
|
|
}
|
|
|
|
func TestSizeCapDeletesOldestDays(t *testing.T) {
|
|
now := base
|
|
l := open(t, &now, 700)
|
|
for d := 6; d >= 1; d-- {
|
|
ts := base.Add(-time.Duration(d) * 24 * time.Hour).Unix()
|
|
for i := 0; i < 8; i++ {
|
|
l.Append(ts+int64(i), line(ts+int64(i), uint64(d*100+i), "1.1.1.1", "exploit"))
|
|
}
|
|
l.Close()
|
|
l.Maintain()
|
|
}
|
|
if sz := l.dirSize(); sz > 700 {
|
|
t.Fatalf("size %d over the cap", sz)
|
|
}
|
|
fs := l.list()
|
|
if len(fs) == 0 || len(fs) == 6 {
|
|
t.Fatalf("expected some, not all, days to remain: %d", len(fs))
|
|
}
|
|
if fs[len(fs)-1].day != (base.Unix()-86400)/86400 {
|
|
t.Fatal("the newest days must survive")
|
|
}
|
|
}
|
|
|
|
func TestCorruptedLinesAreIgnored(t *testing.T) {
|
|
now := base
|
|
l := open(t, &now, 0)
|
|
l.Append(base.Unix(), line(base.Unix(), 1, "1.1.1.1", "exploit"))
|
|
l.Close()
|
|
p := l.path(base.Unix() / 86400)
|
|
f, _ := os.OpenFile(p, os.O_APPEND|os.O_WRONLY, 0o600)
|
|
f.WriteString("{not json\n\x00\x01garbage\n" + strings.Repeat("x", 300<<10) + "\n")
|
|
f.Close()
|
|
l2, _ := Open(Options{Dir: l.opt.Dir, Now: func() time.Time { return now }})
|
|
l2.Append(base.Unix()+1, line(base.Unix()+1, 2, "1.1.1.1", "exploit"))
|
|
if r, err := l2.Query(Query{Filter: Filter{From: base.Unix() - 10, To: base.Unix() + 10}}); err != nil || len(r.Items) != 2 {
|
|
t.Fatalf("corruption must be skipped: %v %d", err, len(r.Items))
|
|
}
|
|
if l2.LastID() != 1 {
|
|
t.Fatalf("last id %d", l2.LastID())
|
|
}
|
|
// a truncated gzip tail is tolerated too
|
|
now = base.Add(48 * time.Hour)
|
|
l2.Close()
|
|
l2.Maintain()
|
|
gz := filepath.Join(l.opt.Dir, base.Format("2006-01-02")+".jsonl.gz")
|
|
b, _ := os.ReadFile(gz)
|
|
os.WriteFile(gz, b[:len(b)-6], 0o600)
|
|
l2.Query(Query{Filter: Filter{From: base.Unix() - 10, To: base.Unix() + 10}})
|
|
}
|
|
|
|
type failFile struct{}
|
|
|
|
func (failFile) Write([]byte) (int, error) { return 0, errors.New("no space left on device") }
|
|
func (failFile) Sync() error { return errors.New("no space") }
|
|
func (failFile) Close() error { return nil }
|
|
|
|
func TestDiskFullDoesNotStopTheService(t *testing.T) {
|
|
now := base
|
|
l := open(t, &now, 0)
|
|
l.create = func(string) (file, error) { return failFile{}, nil }
|
|
for i := 0; i < 5000; i++ { // more than the bufio buffer: the write error surfaces
|
|
l.Append(base.Unix(), line(base.Unix(), uint64(i+1), "1.1.1.1", "exploit"))
|
|
}
|
|
if l.Dropped() == 0 {
|
|
t.Fatal("events must be counted as dropped")
|
|
}
|
|
l.create = func(string) (file, error) { return nil, errors.New("disk full") }
|
|
l.Append(base.Unix(), line(base.Unix(), 1, "1.1.1.1", "exploit"))
|
|
l.Flush()
|
|
l.Close()
|
|
// recovery: the disk works again
|
|
l.create = func(p string) (file, error) { return os.OpenFile(p, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600) }
|
|
l.Append(base.Unix(), line(base.Unix(), 7777, "2.2.2.2", "exploit"))
|
|
if r, _ := l.Query(Query{Filter: Filter{From: base.Unix() - 5, To: base.Unix() + 5, IP: "2.2.2.2"}}); len(r.Items) != 1 {
|
|
t.Fatalf("log must recover: %d", len(r.Items))
|
|
}
|
|
}
|
|
|
|
func TestDayCapOnlyCounts(t *testing.T) {
|
|
now := base
|
|
l, _ := Open(Options{Dir: filepath.Join(t.TempDir(), "e"), DayCap: 10, Now: func() time.Time { return now }})
|
|
for i := 0; i < 30; i++ {
|
|
l.Append(base.Unix(), line(base.Unix(), uint64(i+1), "1.1.1.1", "exploit"))
|
|
}
|
|
if r, _ := l.Query(Query{Filter: Filter{From: base.Unix() - 5, To: base.Unix() + 5}, Limit: 100}); len(r.Items) != 10 || l.Dropped() != 20 {
|
|
t.Fatalf("stored %d dropped %d", len(r.Items), l.Dropped())
|
|
}
|
|
}
|
|
|
|
func TestIDsSurviveRestart(t *testing.T) {
|
|
now := base
|
|
l := open(t, &now, 0)
|
|
l.Append(base.Unix(), line(base.Unix(), 41, "1.1.1.1", "exploit"))
|
|
l.Close()
|
|
l2, _ := Open(Options{Dir: l.opt.Dir})
|
|
if l2.NextID() != 42 {
|
|
t.Fatal("ids must continue after a restart")
|
|
}
|
|
}
|