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>
256 lines
8.6 KiB
Go
256 lines
8.6 KiB
Go
package intel
|
|
|
|
import (
|
|
"crypto/sha1" //nolint:gosec // UUIDv5 is defined over SHA-1
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/netip"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// stixNamespace is the fixed UUIDv5 namespace of every Urutau STIX id.
|
|
var stixNamespace = [16]byte{0x5a, 0x1f, 0x7c, 0x3e, 0x9d, 0x42, 0x4b, 0x6a, 0x8e, 0x01, 0xc4, 0xf7, 0x2b, 0x90, 0xd3, 0x6e}
|
|
|
|
func uuidV5(key string) string {
|
|
h := sha1.New() //nolint:gosec
|
|
h.Write(stixNamespace[:])
|
|
h.Write([]byte(key))
|
|
b := h.Sum(nil)[:16]
|
|
b[6] = b[6]&0x0f | 0x50
|
|
b[8] = b[8]&0x3f | 0x80
|
|
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:16])
|
|
}
|
|
|
|
func stixID(typ, key string) string { return typ + "--" + uuidV5(typ+":"+key) }
|
|
|
|
func stixTime(t time.Time) string { return t.UTC().Format("2006-01-02T15:04:05.000Z") }
|
|
|
|
type extRef struct {
|
|
SourceName string `json:"source_name"`
|
|
ExternalID string `json:"external_id,omitempty"`
|
|
URL string `json:"url,omitempty"`
|
|
}
|
|
|
|
type sdo struct {
|
|
Type string `json:"type"`
|
|
SpecVersion string `json:"spec_version"`
|
|
ID string `json:"id"`
|
|
Created string `json:"created"`
|
|
Modified string `json:"modified"`
|
|
Name string `json:"name,omitempty"`
|
|
Description string `json:"description,omitempty"`
|
|
IdentityClass string `json:"identity_class,omitempty"`
|
|
Pattern string `json:"pattern,omitempty"`
|
|
PatternType string `json:"pattern_type,omitempty"`
|
|
ValidFrom string `json:"valid_from,omitempty"`
|
|
ValidUntil string `json:"valid_until,omitempty"`
|
|
IndicatorTypes []string `json:"indicator_types,omitempty"`
|
|
Labels []string `json:"labels,omitempty"`
|
|
CreatedByRef string `json:"created_by_ref,omitempty"`
|
|
ExternalRefs []extRef `json:"external_references,omitempty"`
|
|
RelationshipTyp string `json:"relationship_type,omitempty"`
|
|
SourceRef string `json:"source_ref,omitempty"`
|
|
TargetRef string `json:"target_ref,omitempty"`
|
|
FirstSeen string `json:"first_seen,omitempty"`
|
|
LastSeen string `json:"last_seen,omitempty"`
|
|
Count int `json:"count,omitempty"`
|
|
SightingOfRef string `json:"sighting_of_ref,omitempty"`
|
|
WhereSighted []string `json:"where_sighted_refs,omitempty"`
|
|
}
|
|
|
|
type sco struct {
|
|
Type string `json:"type"`
|
|
SpecVersion string `json:"spec_version"`
|
|
ID string `json:"id"`
|
|
Value string `json:"value"`
|
|
}
|
|
|
|
type bundle struct {
|
|
Type string `json:"type"`
|
|
ID string `json:"id"`
|
|
Objects []any `json:"objects"`
|
|
}
|
|
|
|
var techIDRe = regexp.MustCompile(`^T\d{4}(\.\d{3})?$`)
|
|
|
|
const maxSTIXItems = 20
|
|
|
|
// STIXBundle builds a STIX 2.1 bundle describing one malicious IP: the Urutau
|
|
// identity, an indicator, the ipv4/ipv6 observable, a sighting, one
|
|
// attack-pattern per MITRE technique and "indicates"/"based-on" relationships.
|
|
// Ids are deterministic (UUIDv5 over fixed namespace and keys); created and
|
|
// modified are now. sightingOf optionally overrides the sighting's
|
|
// sighting_of_ref when it is a STIX id ("indicator--..."); otherwise the
|
|
// indicator is used.
|
|
func STIXBundle(ip string, first, last time.Time, count int, fams []string, techs []string, sightingOf string, now time.Time) ([]byte, error) {
|
|
ip = strings.TrimSpace(ip)
|
|
if strings.Contains(ip, "/") {
|
|
p, err := netip.ParsePrefix(ip)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("stix: invalid prefix: %w", err)
|
|
}
|
|
return STIXBundlePrefix(p, first, last, count, fams, techs, sightingOf, now)
|
|
}
|
|
addr, err := netip.ParseAddr(ip)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("stix: invalid ip: %w", err)
|
|
}
|
|
addr = addr.Unmap().WithZone("")
|
|
return stixBundle(addr.String(), addr.Is6(), first, last, count, fams, techs, sightingOf, now)
|
|
}
|
|
|
|
// STIXBundlePrefix is STIXBundle for a network prefix (for instance the /64
|
|
// key used for IPv6 sources): the indicator pattern and the observable carry
|
|
// the CIDR ("[ipv6-addr:value = '2001:db8::/64']"). A single-address prefix
|
|
// is treated as that address.
|
|
func STIXBundlePrefix(p netip.Prefix, first, last time.Time, count int, fams []string, techs []string, sightingOf string, now time.Time) ([]byte, error) {
|
|
if !p.IsValid() {
|
|
return nil, errors.New("stix: invalid prefix")
|
|
}
|
|
a := p.Addr().WithZone("")
|
|
if a.Is4In6() {
|
|
a = a.Unmap()
|
|
bits := p.Bits() - 96
|
|
if bits < 0 {
|
|
return nil, errors.New("stix: invalid prefix")
|
|
}
|
|
p = netip.PrefixFrom(a, bits)
|
|
}
|
|
p = p.Masked()
|
|
if p.Bits() == p.Addr().BitLen() {
|
|
return stixBundle(p.Addr().String(), p.Addr().Is6(), first, last, count, fams, techs, sightingOf, now)
|
|
}
|
|
return stixBundle(p.String(), p.Addr().Is6(), first, last, count, fams, techs, sightingOf, now)
|
|
}
|
|
|
|
func stixBundle(ipStr string, v6 bool, first, last time.Time, count int, fams []string, techs []string, sightingOf string, now time.Time) ([]byte, error) {
|
|
scoType := "ipv4-addr"
|
|
if v6 {
|
|
scoType = "ipv6-addr"
|
|
}
|
|
if last.Before(first) {
|
|
first, last = last, first
|
|
}
|
|
if count < 1 {
|
|
count = 1
|
|
}
|
|
ts := stixTime(now)
|
|
base := func(typ, key string) sdo {
|
|
return sdo{Type: typ, SpecVersion: "2.1", ID: stixID(typ, key), Created: ts, Modified: ts}
|
|
}
|
|
|
|
ident := base("identity", "urutau")
|
|
ident.Name, ident.IdentityClass = "Urutau", "system"
|
|
|
|
lset := map[string]bool{}
|
|
for _, f := range fams {
|
|
if f != "" {
|
|
lset[strings.ToLower(f)] = true
|
|
}
|
|
}
|
|
ind := base("indicator", ipStr)
|
|
ind.Name = "Malicious IP " + ipStr
|
|
ind.Pattern = fmt.Sprintf("[%s:value = '%s']", scoType, ipStr)
|
|
ind.PatternType = "stix"
|
|
ind.ValidFrom = stixTime(first)
|
|
ind.ValidUntil = stixTime(last.Add(30 * 24 * time.Hour))
|
|
ind.IndicatorTypes = []string{"malicious-activity"}
|
|
ind.Labels = sortedKeys(lset, maxSTIXItems)
|
|
ind.CreatedByRef = ident.ID
|
|
|
|
obs := sco{Type: scoType, SpecVersion: "2.1", ID: stixID(scoType, ipStr), Value: ipStr}
|
|
|
|
sight := base("sighting", ipStr)
|
|
sight.FirstSeen, sight.LastSeen, sight.Count = stixTime(first), stixTime(last), count
|
|
sight.SightingOfRef = ind.ID
|
|
if strings.HasPrefix(sightingOf, "indicator--") {
|
|
sight.SightingOfRef = sightingOf
|
|
}
|
|
sight.WhereSighted = []string{ident.ID}
|
|
sight.CreatedByRef = ident.ID
|
|
|
|
objs := []any{ident, ind, obs, sight}
|
|
rel := func(src, typ, dst string) sdo {
|
|
r := base("relationship", src+"|"+typ+"|"+dst)
|
|
r.RelationshipTyp, r.SourceRef, r.TargetRef = typ, src, dst
|
|
return r
|
|
}
|
|
objs = append(objs, rel(ind.ID, "based-on", obs.ID))
|
|
|
|
tset := map[string]bool{}
|
|
for _, t := range techs {
|
|
if techIDRe.MatchString(t) {
|
|
tset[t] = true
|
|
}
|
|
}
|
|
for _, t := range sortedKeys(tset, maxSTIXItems) {
|
|
ap := base("attack-pattern", t)
|
|
ap.Name = TechniqueName(t)
|
|
if ap.Name == "" {
|
|
ap.Name = t
|
|
}
|
|
ap.ExternalRefs = []extRef{{SourceName: "mitre-attack", ExternalID: t,
|
|
URL: "https://attack.mitre.org/techniques/" + strings.ReplaceAll(t, ".", "/") + "/"}}
|
|
objs = append(objs, ap, rel(ind.ID, "indicates", ap.ID))
|
|
}
|
|
|
|
b := bundle{Type: "bundle", ID: stixID("bundle", ipStr), Objects: objs}
|
|
return json.MarshalIndent(b, "", " ")
|
|
}
|
|
|
|
var stixIDRe = regexp.MustCompile(`^[a-z][a-z0-9-]*--[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`)
|
|
|
|
// ValidateSTIX checks the structural minimum of a STIX 2.1 bundle: bundle
|
|
// shape, and for every SDO/SRO the fields type, spec_version "2.1", id,
|
|
// created and modified; SCOs need type, spec_version and id. Ids must match
|
|
// their type and reference ids must be well formed.
|
|
func ValidateSTIX(data []byte) error {
|
|
var b struct {
|
|
Type string `json:"type"`
|
|
ID string `json:"id"`
|
|
Objects []map[string]any `json:"objects"`
|
|
}
|
|
if err := json.Unmarshal(data, &b); err != nil {
|
|
return err
|
|
}
|
|
if b.Type != "bundle" || !stixIDRe.MatchString(b.ID) || !strings.HasPrefix(b.ID, "bundle--") {
|
|
return errors.New("stix: bad bundle header")
|
|
}
|
|
if len(b.Objects) == 0 {
|
|
return errors.New("stix: empty bundle")
|
|
}
|
|
ids := map[string]bool{}
|
|
for _, o := range b.Objects {
|
|
typ, _ := o["type"].(string)
|
|
id, _ := o["id"].(string)
|
|
if typ == "" || !strings.HasPrefix(id, typ+"--") || !stixIDRe.MatchString(id) {
|
|
return fmt.Errorf("stix: bad type/id %q %q", typ, id)
|
|
}
|
|
if o["spec_version"] != "2.1" {
|
|
return fmt.Errorf("stix: %s missing spec_version 2.1", id)
|
|
}
|
|
if !strings.HasSuffix(typ, "-addr") {
|
|
for _, f := range []string{"created", "modified"} {
|
|
if s, _ := o[f].(string); s == "" {
|
|
return fmt.Errorf("stix: %s missing %s", id, f)
|
|
}
|
|
}
|
|
}
|
|
if ids[id] {
|
|
return fmt.Errorf("stix: duplicate id %s", id)
|
|
}
|
|
ids[id] = true
|
|
}
|
|
for _, o := range b.Objects {
|
|
for _, f := range []string{"source_ref", "target_ref", "sighting_of_ref", "created_by_ref"} {
|
|
if r, ok := o[f].(string); ok && !ids[r] && !strings.HasPrefix(r, "indicator--") {
|
|
return fmt.Errorf("stix: dangling %s %s", f, r)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|