Documentation
¶
Overview ¶
Package rules implements the malicious-file detection engine for the enterprise agent. It evaluates backend-authored, purely declarative detection rules against the directories the agent already walks and reports matches on the existing telemetry payload as the additive `rule_scan` field.
Trust & privacy model:
- Rules are pure DATA, never code. No rule field can carry a command, script, executable path, or URL. A condition can only ask "does this path exist, and does its content match this regex / its hash equal this SHA-256?" — so a compromised backend can at worst cause a false finding or a yes/no probe, never code execution or content exfiltration.
- A condition only ever yields a boolean. File content is never read out, captured, logged, or sent — only the path, matched glob, whole-file SHA-256, the per-group/per-condition booleans, and Stat-only metadata.
- Evaluation is hard-bounded: a per-file size guard, a global file cap, a per-rule match cap, and an overall time budget (see Caps).
There is NO embedded rule pack: rules live only in the backend and are fetched at run start. On any fetch/parse/validation failure the engine simply scans nothing this run — it never fails the run and never falls back to a local pack.
The engine is drivable with zero backend and zero network: hardcode a RuleSet, point Scan at a temp dir of fixtures, and assert on the returned model.RuleScan. Prepare() is likewise pure.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Caps ¶
type Caps struct {
MaxFileSize int64 // hard cap 8 MiB; a rule's max_file_size is clamped to this
MaxFiles int // total candidate files evaluated across all rules
MaxMatchesPerRule int // per-rule file-match cap; overflow ⇒ truncated + incomplete
PerRunBudget time.Duration // overall scan deadline (also honors the passed ctx)
}
Caps bounds the cost of a scan. Zero/negative fields are replaced with the defaults by NewEngine.
type Condition ¶
type Condition struct {
ID string `json:"id"`
Kind string `json:"kind"` // "regex" | "sha256"
Pattern string `json:"pattern"` // RE2 source (regex) | 64-char hex SHA-256 (sha256)
Negate bool `json:"negate,omitempty"`
Mandatory bool `json:"mandatory,omitempty"`
// contains filtered or unexported fields
}
Condition is a single boolean test against a file. Negate inverts it. Mandatory conditions gate whether the file is reported at all: a group is "satisfied" only when all its mandatory conditions match, and a file with groups is reported only if at least one group is satisfied. Optional conditions (the default) only influence confidence. Use mandatory conditions for rules that target files which legitimately exist (e.g. injected config), and all-optional conditions for rules whose target file should never exist.
type ConditionGroup ¶
ConditionGroup is one signature set (e.g. one attack). A rule can screen one file for several attacks via multiple groups.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine evaluates a RuleSet against a machine. It does no I/O of its own beyond the executor it is handed and the directory walk, which makes Scan fully drivable from a temp dir of fixtures with no backend or network.
func NewEngine ¶
func NewEngine(exec executor.Executor, skipper *tcc.Skipper, caps Caps, log *progress.Logger) *Engine
NewEngine builds an Engine, clamping caps to safe bounds.
func (*Engine) Scan ¶
Scan evaluates rs against searchDirs and returns the result object. It never fails: unreadable/vanished files are skipped; hitting a global file or time budget sets ScanComplete=false (suppressing backend auto-resolution), and a rule hitting its per-rule cap is marked truncated/incomplete.
type Fetcher ¶
Fetcher returns the RuleSet for one device. Implemented by HTTPFetcher and stubbed in tests.
type HTTPFetcher ¶
type HTTPFetcher struct {
// contains filtered or unexported fields
}
HTTPFetcher fetches the run-config bundle over TLS + bearer auth. It is a near-verbatim clone of internal/aiagents/state/fetcher.go, differing only in the URL path and decode target. Safe for concurrent use.
func NewHTTPFetcher ¶
func NewHTTPFetcher(endpoint, apiKey string, h *http.Client) (*HTTPFetcher, bool)
NewHTTPFetcher constructs an HTTPFetcher from the enterprise config. Returns ok=false when endpoint/apiKey are missing — the caller treats that as "no rules" (scan nothing).
type Rule ¶
type Rule struct {
ID string `json:"id"`
Revision string `json:"revision,omitempty"`
FileGlobs []string `json:"file_globs"`
MaxFileSize int64 `json:"max_file_size"`
Groups []ConditionGroup `json:"groups,omitempty"`
// contains filtered or unexported fields
}
Rule is one detection rule: where to look (FileGlobs + a MaxFileSize cost guard) and what to match (Groups of boolean conditions). Revision is the opaque, backend-assigned rule revision that the agent echoes back but never interprets. No severity/category/title/campaign/description lives on a rule — that is backend-only metadata.
type RuleSet ¶
type RuleSet struct {
Rules []Rule `json:"rules"`
}
RuleSet is the agent-facing rule bundle (the fixed wire contract, verbatim, with no schema version field). It is produced by the backend's run-config endpoint and consumed by the engine after Prepare().
func FetchOrEmpty ¶
func FetchOrEmpty(ctx context.Context, f Fetcher, customerID, deviceID string, log *progress.Logger) RuleSet
FetchOrEmpty fetches and validates the RuleSet, returning an empty RuleSet on ANY failure (nil fetcher, transport, non-200, oversized body, decode, or Prepare rejection). It logs the reason and never returns an error — a failing rules API must never fail the run. An empty RuleSet makes Scan a no-op.
func LoadFileOrEmpty ¶
LoadFileOrEmpty loads a RuleSet from a local JSON file (the dev-only --rules-file flag), accepting either a bare RuleSet or a run-config envelope. Returns an empty RuleSet on any error. Mirrors FetchOrEmpty's fail-safe contract so an offline run degrades exactly like a failed backend fetch.
func (*RuleSet) Prepare ¶
Prepare validates and compiles the ruleset in place. Any error means the whole bundle is rejected (the caller scans nothing this run). It is pure: no I/O, no network. Enforces non-empty unique rule ids, ≥1 file glob, unique group ids per rule and condition ids per group, glob validity, RE2-compilable regexes, 64-char-hex sha256 patterns, and clamps max_file_size to the hard cap.