scan

package
v0.50.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 36 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package scan is the scan stage. It runs VirusTotal's YARA-X, as the yr program, over an item's parsed text with Agent Threat Rules converted to YARA and ingestion's own rules, and stores each finding: the rule, its severity and the text it matched. A finding never drops an item.

The rules are built once per image by build-rules, which keeps each converted rule only where yr compiles it and it passes its own examples. The findings carry the rules' version, so a new rule set replays beside the old.

Index

Constants

View Source
const (
	DroppedSkippedByATR   = "ATR's engine skips a draft or deprecated rule"
	DroppedNeedsEvaluator = "it needs an evaluator other than patterns"
	DroppedOtherFields    = "it is written for fields the scan does not read"
	DroppedNotRegex       = "a condition is not a regular expression"
	DroppedUnreadable     = "its file is not a rule"
	DroppedNoSeverity     = "it names no severity the scan knows"
	DroppedNoExample      = "it has no example on the fields it keeps"
	DroppedMissesPositive = "it misses one of its own true positives"
	DroppedFiresNegative  = "it fires on one of its own true negatives"
)

Why a rule is dropped. A decision's Detail says more, such as the fields it reads or the refusal under yr's caret.

View Source
const (
	SourceATR       = "atr"
	SourceIngestion = "ingestion"
)

Rule sources.

View Source
const (
	ManifestFile = "rules.json"
	CompiledFile = "rules.yarc"
	SourceFile   = "rules.yar"
)

Files in a built rules directory.

View Source
const (
	// Capability is the capability of every scan task.
	Capability = store.StageScan
	// Timeout bounds one scan of one parse, and a scan past it runs again. A
	// 61 MiB parse, about the most a parse report carries, scanned in five
	// and a half minutes, and in under nine where its text held no line break.
	Timeout = 10 * time.Minute
	// Attempts bounds the claims of one scan task. A scan whose parse read
	// or yr run fails is queued again until its task's claims reach it, and
	// then fails.
	Attempts = 3
)
View Source
const (
	// RulesDir is where the worker image installs the built rules.
	RulesDir = "/usr/local/share/ingestion/scan-rules"
	// Yr is where the worker image installs yr.
	Yr = "/usr/local/bin/yr"
	// Producer names each scan output. Its version is the rules' version and
	// the parse it read, as store.ScanVersion writes them.
	Producer = "rules"
	// SpanKept bounds the bytes of the span that a finding keeps. The
	// finding's length still counts the whole match.
	SpanKept = 1024
	// MatchesPerRule bounds the findings that one rule makes on one text.
	MatchesPerRule = 100
)

Variables

View Source
var Fields = []string{"content", "user_input", "tool_response"}

Fields are the fields of an agent's event whose conditions the scan keeps. An item's text is outsider-written content that a model reads, as a tool's response or a user's input is. A rule written only for a tool's arguments, its name or description, or an agent's own output is dropped, because an item's text plays none of those parts.

View Source
var Severities = severities()

Severities are what a rule's severity may be, from least to most severe, as the $defs/severity of schemas/ingestion-record.schema.json lists them.

Functions

func NewHandler

func NewHandler(scanner *Scanner, client *worker.Client, log *slog.Logger) worker.Handler

NewHandler is the worker handler that scans with scanner, reading each task's parse through client.

func QueueAfterParse

func QueueAfterParse(ctx context.Context, tx *store.Tx, task store.Task) error

QueueAfterParse queues a scan of the item in the write that ends its parse task done. So every parsed item is scanned. A parse that failed ends its task failed, so it waits in review unscanned.

func Replayable

func Replayable(extractors *extract.Registry) server.Replayable

Replayable is the scan as a replay runs it again, such as over a new rule set: a task for each item whose media type one of extractors reads. An item whose parse is not done yet ends its scan with nothing scanned.

func Suggests added in v0.25.0

func Suggests(rule string) (string, bool)

Suggests is the kind of thing that a finding of rule lists, as the rule's file declares it, and false for a rule whose match flags an attack.

Types

type BuildConfig

type BuildConfig struct {
	// ATRDir is a checkout of Agent Threat Rules at one commit. Its rules/
	// directory holds the rules.
	ATRDir string
	// ATRCommit names the commit checked out in ATRDir. Build reads it with
	// git where it is empty.
	ATRCommit string
	// IngestionRulesDir holds ingestion's own rules.
	IngestionRulesDir string
	Yr                string
	// OutDir receives rules.yar, rules.yarc and rules.json.
	OutDir string
}

BuildConfig is one build of the scan's rules.

type Condition

type Condition struct {
	Field    string `yaml:"field"`
	Operator string `yaml:"operator"`
	Value    string `yaml:"value"`
}

Condition is one pattern and the field of an agent's event it reads.

type Decision

type Decision struct {
	ID       string `json:"id"`
	Source   string `json:"source"`
	File     string `json:"file"`
	Severity string `json:"severity"`
	Kept     bool   `json:"kept"`
	Reason   string `json:"reason,omitempty"`
	Detail   string `json:"detail,omitempty"`
}

Decision is whether one rule was kept, and why it was dropped where not.

type Detection

type Detection struct {
	// Method is pattern where empty. ATR also writes semantic, trace and
	// behavioral, each needing an evaluator other than patterns.
	Method     string      `yaml:"method"`
	Condition  string      `yaml:"condition"`
	Conditions []Condition `yaml:"conditions"`
	Semantic   struct {
		FallbackMethod string `yaml:"fallback_method"`
	} `yaml:"semantic"`
}

Detection is how a rule matches: Condition is any or all of Conditions.

type Manifest

type Manifest struct {
	// Version names the rules: the ATR commit, the yr that compiled them,
	// and the SHA-256 of their YARA source, so any change to the converter,
	// ATR or ingestion's rules names a new version.
	Version   string         `json:"version"`
	ATRCommit string         `json:"atr_commit"`
	YaraX     string         `json:"yara_x"`
	Fields    []string       `json:"fields"`
	Kept      int            `json:"kept"`
	Dropped   map[string]int `json:"dropped"`
	Decisions []Decision     `json:"decisions"`
}

Manifest is what a rules build wrote: the version every finding carries, and the decision on every rule it read. It is rules.json beside rules.yarc.

func Build

func Build(ctx context.Context, c BuildConfig) (Manifest, error)

Build converts ATR's rules and ingestion's own to YARA, keeps each one that yr compiles and that passes its own examples, and writes the kept rules, compiled, with a manifest of every decision. A rule is never patched by hand: one that yr refuses or that fails an example is dropped and counted by why.

type Rule

type Rule struct {
	ID       string `yaml:"id"`
	Title    string `yaml:"title"`
	Status   string `yaml:"status"`
	Severity string `yaml:"severity"`
	// Suggests is the kind of thing that a match lists for a reader to run,
	// open or paste: command, link or code. It is empty for a rule whose
	// match flags an attack.
	Suggests  string    `yaml:"suggests"`
	Tags      Tags      `yaml:"tags"`
	Detection Detection `yaml:"detection"`
	TestCases TestCases `yaml:"test_cases"`
	// File is the rule's path below the directory it was read from.
	File string `yaml:"-"`
}

Rule is one rule in Agent Threat Rules' YAML shape. ATR's rules and ingestion's own are both read this way, and both carry their own examples.

type Scanner

type Scanner struct {
	// contains filtered or unexported fields
}

Scanner runs yr over text with one built rule set.

func Installed

func Installed(log *slog.Logger) *Scanner

Installed is the scanner over the yr and the built rules that the image installs, and nil where either is missing here. A worker with no scanner claims no scan task and no model read's task, because a model read's suggestion is scanned before it is kept.

func Open

func Open(yr, dir string) (*Scanner, error)

Open is a scanner that runs yr with the rules that build-rules wrote into dir.

func (*Scanner) Manifest

func (s *Scanner) Manifest() Manifest

Manifest is what the rules' build decided.

func (*Scanner) Scan

func (s *Scanner) Scan(ctx context.Context, text string) ([]store.Finding, []store.Span, error)

Scan scans one text that belongs to no parse, and answers each finding and its span in the order they sit in the text. No finding names a unit, and each offset counts from the text's start.

func (*Scanner) ScanUnits

func (s *Scanner) ScanUnits(ctx context.Context, units []store.Unit) ([]store.Finding, []store.Span, error)

ScanUnits scans a parse's units as the one text that store.JoinUnits makes of them, so a rule reads across them as it would read a page. Each finding names the unit it starts in, and its offset counts from that unit's start.

func (*Scanner) Version

func (s *Scanner) Version() string

Version is the rules' version, which each finding's output carries.

type Tags

type Tags struct {
	// SuppressInCodeBlocks drops a match inside markdown code: a fenced
	// block, inline code, or a quoted string on a table's row.
	SuppressInCodeBlocks bool `yaml:"suppress_in_code_blocks"`
}

Tags are what ATR's engine reads of a rule's tags.

type TestCases

type TestCases struct {
	TruePositives []map[string]any `yaml:"true_positives"`
	TrueNegatives []map[string]any `yaml:"true_negatives"`
}

TestCases are a rule's examples. Each case names its text under input or under the field it is written for, and says whether the rule fires.

Directories

Path Synopsis
Command build-rules converts Agent Threat Rules and ingestion's own rules to YARA, keeps each rule that yr compiles and that passes its own examples, and writes the compiled rules with a manifest of every decision.
Command build-rules converts Agent Threat Rules and ingestion's own rules to YARA, keeps each rule that yr compiles and that passes its own examples, and writes the compiled rules with a manifest of every decision.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL