architect exporters

Build byte patterns from a reference range and a static mask.

class PatternGenerator[source]

Bases: object

Generate wildcard byte patterns from a region and its static mask.

Static bytes become exact match values; volatile bytes become wildcards (??). The result can be exported as YARA or JSON.

static generate(reference_bytes, static_mask, name='unnamed_pattern', min_static_ratio=0.3)[source]

Return a byte pattern and its static-byte counts.

Supply one mask value per reference byte. A true value keeps the byte literal in wildcard_pattern. A false value writes ??. The returned hex_pattern contains all reference bytes. Empty inputs or a static ratio below min_static_ratio return None.

Parameters:
Return type:

dict | None

static find_anchors(static_mask, min_anchor_length=4)[source]

Find contiguous runs of static bytes that can serve as anchors.

Parameters:
  • static_mask (List[bool]) – Per-byte static flags.

  • min_anchor_length (int) – Minimum consecutive static bytes for an anchor.

Returns:

List of (start_offset, length) tuples for anchor regions.

Return type:

List[Tuple[int, int]]

static anchor_distinctiveness(reference_bytes, static_mask, min_anchor_length=4)[source]

Measure the byte diversity of a pattern’s static anchors.

min_static_ratio in generate() answers “is there enough static material to build a rule from?”. It cannot answer the question that decides whether the rule is useful: “do those static bytes say anything?” A window whose anchors are 128 zero bytes passes any static ratio and then matches thousands of positions in the same dump it came from.

This is not hypothetical. On the real 8-dump OpenSSL TLS 1.2 corpus the 48-byte master secret at offset 370,672 sits inside a run of zeros: at the default 64 bytes of context the resulting mask has 128 static bytes carrying exactly ONE distinct value, and that rule fires 825,779 times in its own 11 MB source dump under libyara – or 5,311 times under Volatility3’s RegExScanner, which walks with non-overlapping re.finditer and so reports one hit per ~176-byte stride (tests/test_vol3_verify.py pins the 5,311 for that engine; tests/test_detector_loop_corpus.py pins the 825,779 for this one). Both numbers describe the SAME rule on the SAME dump, 155x apart: a selectivity figure is meaningless without naming the engine that produced it, so a caller should measure its own rule – memdiver scan-yara --count-only --no-max-matches – rather than reuse either.

Only bytes inside a static RUN of at least min_anchor_length count — the same anchors find_anchors() reports, because a lone static byte between two wildcards anchors nothing.

Returns:

distinct_bytes (how many different values the anchors carry), shannon_bits (their per-byte Shannon entropy, 0.0 for a single repeated value), longest_constant_run (the longest run of ONE repeated value within a single anchor — never spanning two anchors, which are not adjacent in the matched bytes) and static_bytes (how many bytes were measured). All zero when there are no anchors.

Parameters:
Return type:

Dict[str, int | float]

static infer_fields(variance, key_offset, key_length, threshold=2000.0)[source]

Segment variance into structural fields and dynamic regions.

Group consecutive bytes with variance at or below threshold as static. Group bytes above the threshold as dynamic. The known key region is labeled key_material regardless of individual byte variance.

Returns:

List of field dicts with offset, length, type ('static', 'dynamic', or 'key_material'), mean_variance, and label.

Parameters:
Return type:

List[dict]

Export byte patterns as YARA rules.

class YaraExporter[source]

Bases: object

Export byte patterns as YARA detection rules.

static export(pattern, rule_name=None, description=None, tags=None, key_offset=None, key_length=None)[source]

Export the byte pattern as a YARA rule.

Supply hex byte pairs and ?? wildcards in wildcard_pattern. An invalid token raises ValueError. An all-wildcard pattern logs a warning and still produces a rule.

The exporter sanitizes the rule name and tags, escapes the description, and converts numeric metadata to YARA integers. A missing or nonpositive pattern length omits pattern_length. Supply key_offset relative to the pattern start and key_length as a byte count to include those metadata fields.

The exporter returns rule text. The caller can compile it to check limits that depend on the installed YARA version.

Parameters:
  • pattern (dict)

  • rule_name (str | None)

  • description (str | None)

  • tags (list | None)

  • key_offset (int | None)

  • key_length (int | None)

Return type:

str

key_locator_from_pattern(pattern)[source]

Read the key locator a pattern dict may already carry.

engine.vol3_emit enriches the dict produced by PatternGenerator.generate() with key_offset (relative to the pattern window start, exactly as YaraExporter.export() wants it) and key_length. Callers that hold only such a dict – the HTTP /architect/export endpoint and Volatility3Exporter.export()’s own YARA fallback – recover the locator here rather than re-deriving or inventing it.

A missing, None, or non-integral value yields None so the meta line is omitted instead of emitted wrong: the dict can arrive verbatim from an HTTP request body, where any JSON value is possible.

Returns:

(key_offset, key_length), either element possibly None.

Parameters:

pattern (dict)

Return type:

Tuple[int | None, int | None]

Export byte patterns as JSON signature files.

class JsonExporter[source]

Bases: object

Export byte patterns as JSON signatures compatible with pattern_loader.

static export(pattern, library='', tls_version='', description='', structural_rules=None)[source]

Build a signature dictionary from the pattern and optional rules.

Use library and TLS version values to constrain applicability. The before and after rules go in the signature’s pattern fields. Record the wildcard string and static-byte ratio as metadata.

Parameters:
Return type:

dict

static save(signature, output_path)[source]

Write an indented JSON signature, replacing any existing output file.

Parameters:
Return type:

None

static to_string(signature)[source]

Convert a signature to a formatted JSON string.

Parameters:

signature (dict)

Return type:

str

Generate v1 Volatility 3 plugins from byte patterns and artifact models.

DEPRECATED (v1 generator): pattern plugins are now recipe-v2 plugins (architect.vol3_recipe.recipe_from_pattern + render). Kept for artifact-model STRUCTURE plugins (app.tools_semantic), which v2 cannot express yet, and for the explicit emit_plugin_for_hit(template="v1"). The public entry points emit a DeprecationWarning so remaining callers surface.

class Volatility3Exporter[source]

Bases: object

Export byte patterns as self-contained Volatility3 Python plugins.

static export(pattern, plugin_name=None, description=None, yara_rule=None)[source]

Export a pattern dict as a Volatility3 plugin Python source.

Parameters:
  • pattern (dict) – Pattern dict from PatternGenerator.generate(), optionally enriched with key_offset, key_length, and vtypes by vol3_emit.

  • plugin_name (str | None) – Plugin class name. Defaults to MemDiverScan followed by the normalized pattern name.

  • description (str | None) – Human-readable description.

  • yara_rule (str | None) – Pre-built YARA rule string. Generated if not provided, in which case the pattern’s own key_offset/key_length (when present) become YARA metas on the generated rule.

Returns:

Complete Python source code for a Volatility3 plugin.

Return type:

str

static export_model(model, plugin_name=None, description=None)[source]

Export an artifact model as a Volatility3 plugin Python source.

byte_pattern models go through export() unchanged. A structure model becomes a plugin that decodes the model’s fields around each prefilter hit (see _STRUCTURE_TEMPLATE). An inconsistent model raises ValueError listing every problem.

Parameters:
  • model (ArtifactModel)

  • plugin_name (str | None)

  • description (str | None)

Return type:

str

static save(content, output_path)[source]

Write plugin source to a file.

Parameters:
Return type:

None