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>
283 lines
6.2 KiB
Go
283 lines
6.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"math"
|
|
"math/big"
|
|
"net/netip"
|
|
"sort"
|
|
)
|
|
|
|
// Writer MMDB mínimo (layout DB-IP/GeoIP2), copiado do writer de teste de
|
|
// internal/geo e usado só para gerar a base de geolocalização de demonstração.
|
|
|
|
type f32 float32
|
|
type u16 uint16
|
|
type u32 uint32
|
|
type u64 uint64
|
|
type u128 struct{ *big.Int }
|
|
type i32 int32
|
|
|
|
type mmEnc struct {
|
|
buf bytes.Buffer
|
|
strs map[string]int
|
|
ptrs bool
|
|
}
|
|
|
|
func ctrlBytes(typ byte, size int) []byte {
|
|
var first byte
|
|
var ext []byte
|
|
if typ <= 7 {
|
|
first = typ << 5
|
|
} else {
|
|
ext = []byte{typ - 7}
|
|
}
|
|
var sz []byte
|
|
switch {
|
|
case size < 29:
|
|
first |= byte(size)
|
|
case size < 285:
|
|
first |= 29
|
|
sz = []byte{byte(size - 29)}
|
|
case size < 65821:
|
|
first |= 30
|
|
v := size - 285
|
|
sz = []byte{byte(v >> 8), byte(v)}
|
|
default:
|
|
first |= 31
|
|
v := size - 65821
|
|
sz = []byte{byte(v >> 16), byte(v >> 8), byte(v)}
|
|
}
|
|
return append(append([]byte{first}, ext...), sz...)
|
|
}
|
|
|
|
func (e *mmEnc) pointer(off int) {
|
|
switch {
|
|
case off < 2048:
|
|
e.buf.Write([]byte{0x20 | byte(off>>8), byte(off)})
|
|
case off < 2048+(1<<19):
|
|
v := off - 2048
|
|
e.buf.Write([]byte{0x28 | byte(v>>16), byte(v >> 8), byte(v)})
|
|
case off < 526336+(1<<27):
|
|
v := off - 526336
|
|
e.buf.Write([]byte{0x30 | byte(v>>24), byte(v >> 16), byte(v >> 8), byte(v)})
|
|
default:
|
|
e.buf.Write([]byte{0x38, byte(off >> 24), byte(off >> 16), byte(off >> 8), byte(off)})
|
|
}
|
|
}
|
|
|
|
func uintBytes(v uint64) []byte {
|
|
var b [8]byte
|
|
binary.BigEndian.PutUint64(b[:], v)
|
|
i := 0
|
|
for i < 8 && b[i] == 0 {
|
|
i++
|
|
}
|
|
return b[i:]
|
|
}
|
|
|
|
func (e *mmEnc) str(s string) {
|
|
if e.ptrs {
|
|
if off, ok := e.strs[s]; ok {
|
|
e.pointer(off)
|
|
return
|
|
}
|
|
e.strs[s] = e.buf.Len()
|
|
}
|
|
e.buf.Write(ctrlBytes(2, len(s)))
|
|
e.buf.WriteString(s)
|
|
}
|
|
|
|
func (e *mmEnc) value(v any) {
|
|
switch x := v.(type) {
|
|
case string:
|
|
e.str(x)
|
|
case float64:
|
|
e.buf.Write(ctrlBytes(3, 8))
|
|
var b [8]byte
|
|
binary.BigEndian.PutUint64(b[:], math.Float64bits(x))
|
|
e.buf.Write(b[:])
|
|
case f32:
|
|
e.buf.Write(ctrlBytes(15, 4))
|
|
var b [4]byte
|
|
binary.BigEndian.PutUint32(b[:], math.Float32bits(float32(x)))
|
|
e.buf.Write(b[:])
|
|
case []byte:
|
|
e.buf.Write(ctrlBytes(4, len(x)))
|
|
e.buf.Write(x)
|
|
case u16:
|
|
b := uintBytes(uint64(x))
|
|
e.buf.Write(ctrlBytes(5, len(b)))
|
|
e.buf.Write(b)
|
|
case u32:
|
|
b := uintBytes(uint64(x))
|
|
e.buf.Write(ctrlBytes(6, len(b)))
|
|
e.buf.Write(b)
|
|
case uint32:
|
|
e.value(u32(x))
|
|
case u64:
|
|
b := uintBytes(uint64(x))
|
|
e.buf.Write(ctrlBytes(9, len(b)))
|
|
e.buf.Write(b)
|
|
case int:
|
|
e.value(u32(x))
|
|
case u128:
|
|
b := x.Bytes()
|
|
e.buf.Write(ctrlBytes(10, len(b)))
|
|
e.buf.Write(b)
|
|
case i32:
|
|
var b [4]byte
|
|
binary.BigEndian.PutUint32(b[:], uint32(x))
|
|
e.buf.Write(ctrlBytes(8, 4))
|
|
e.buf.Write(b[:])
|
|
case bool:
|
|
n := 0
|
|
if x {
|
|
n = 1
|
|
}
|
|
e.buf.Write(ctrlBytes(14, n))
|
|
case []any:
|
|
e.buf.Write(ctrlBytes(11, len(x)))
|
|
for _, it := range x {
|
|
e.value(it)
|
|
}
|
|
case map[string]any:
|
|
keys := make([]string, 0, len(x))
|
|
for k := range x {
|
|
keys = append(keys, k)
|
|
}
|
|
sort.Strings(keys)
|
|
e.buf.Write(ctrlBytes(7, len(x)))
|
|
for _, k := range keys {
|
|
e.str(k)
|
|
e.value(x[k])
|
|
}
|
|
default:
|
|
panic("tipo não suportado no writer")
|
|
}
|
|
}
|
|
|
|
type mmEntry struct {
|
|
pfx netip.Prefix
|
|
val any
|
|
}
|
|
|
|
type trieNode struct {
|
|
child [2]*trieNode
|
|
data int // índice+1 em entries; 0 = nenhum
|
|
}
|
|
|
|
// buildMMDB gera um arquivo MMDB sintético.
|
|
func buildMMDB(ipVersion, recSize int, entries []mmEntry, usePointers bool, buildEpoch uint64, dbType string) []byte {
|
|
root := &trieNode{}
|
|
for i, en := range entries {
|
|
a := en.pfx.Addr().Unmap()
|
|
bits := en.pfx.Bits()
|
|
var raw []byte
|
|
if a.Is4() {
|
|
a4 := a.As4()
|
|
if ipVersion == 6 {
|
|
raw = append(make([]byte, 12), a4[:]...)
|
|
bits += 96
|
|
} else {
|
|
raw = a4[:]
|
|
}
|
|
} else {
|
|
a16 := a.As16()
|
|
raw = a16[:]
|
|
}
|
|
n := root
|
|
for b := 0; b < bits; b++ {
|
|
bit := (raw[b/8] >> (7 - uint(b%8))) & 1
|
|
if n.child[bit] == nil {
|
|
n.child[bit] = &trieNode{}
|
|
}
|
|
n = n.child[bit]
|
|
}
|
|
n.data = i + 1
|
|
}
|
|
// herança: prefixos mais específicos não podem apagar a cobertura do mais amplo
|
|
var fill func(n *trieNode, inh int)
|
|
fill = func(n *trieNode, inh int) {
|
|
if n.child[0] == nil && n.child[1] == nil {
|
|
return
|
|
}
|
|
if n.data != 0 {
|
|
inh = n.data
|
|
}
|
|
for i := range n.child {
|
|
if n.child[i] == nil && inh != 0 {
|
|
n.child[i] = &trieNode{data: inh}
|
|
} else if n.child[i] != nil {
|
|
fill(n.child[i], inh)
|
|
}
|
|
}
|
|
}
|
|
fill(root, 0)
|
|
// numerar nós (pré-ordem), folhas com dados não são nós
|
|
var nodes []*trieNode
|
|
idx := map[*trieNode]int{}
|
|
var walk func(n *trieNode)
|
|
walk = func(n *trieNode) {
|
|
idx[n] = len(nodes)
|
|
nodes = append(nodes, n)
|
|
for _, c := range n.child {
|
|
if c != nil && (c.child[0] != nil || c.child[1] != nil) {
|
|
walk(c)
|
|
}
|
|
}
|
|
}
|
|
walk(root)
|
|
nodeCount := len(nodes)
|
|
// dados
|
|
enc := &mmEnc{strs: map[string]int{}, ptrs: usePointers}
|
|
offs := make([]int, len(entries))
|
|
for i, en := range entries {
|
|
offs[i] = enc.buf.Len()
|
|
enc.value(en.val)
|
|
}
|
|
rec := func(c *trieNode) int {
|
|
switch {
|
|
case c == nil:
|
|
return nodeCount
|
|
case c.child[0] != nil || c.child[1] != nil:
|
|
return idx[c]
|
|
default:
|
|
return nodeCount + 16 + offs[c.data-1]
|
|
}
|
|
}
|
|
var out bytes.Buffer
|
|
for _, n := range nodes {
|
|
l, r := rec(n.child[0]), rec(n.child[1])
|
|
switch recSize {
|
|
case 24:
|
|
out.Write([]byte{byte(l >> 16), byte(l >> 8), byte(l), byte(r >> 16), byte(r >> 8), byte(r)})
|
|
case 28:
|
|
out.Write([]byte{byte(l >> 16), byte(l >> 8), byte(l), byte((l>>24)<<4) | byte(r>>24), byte(r >> 16), byte(r >> 8), byte(r)})
|
|
case 32:
|
|
var b [8]byte
|
|
binary.BigEndian.PutUint32(b[:], uint32(l))
|
|
binary.BigEndian.PutUint32(b[4:], uint32(r))
|
|
out.Write(b[:])
|
|
}
|
|
}
|
|
out.Write(make([]byte, 16))
|
|
out.Write(enc.buf.Bytes())
|
|
out.WriteString("\xab\xcd\xefMaxMind.com")
|
|
meta := &mmEnc{}
|
|
meta.value(map[string]any{
|
|
"binary_format_major_version": u16(2),
|
|
"binary_format_minor_version": u16(0),
|
|
"build_epoch": u64(buildEpoch),
|
|
"database_type": dbType,
|
|
"ip_version": u16(ipVersion),
|
|
"node_count": u32(nodeCount),
|
|
"record_size": u16(recSize),
|
|
"languages": []any{"en", "pt-BR"},
|
|
"description": map[string]any{"en": "teste"},
|
|
})
|
|
out.Write(meta.buf.Bytes())
|
|
return out.Bytes()
|
|
}
|