Skip to content

[critical] fix: [export_mod/yara_export] escape attribute values in generated YARA string/meta literals - #896

Open
elhoim wants to merge 1 commit into
MISP:mainfrom
elhoim:fix/084-yara-export-escape-strings
Open

[critical] fix: [export_mod/yara_export] escape attribute values in generated YARA string/meta literals#896
elhoim wants to merge 1 commit into
MISP:mainfrom
elhoim:fix/084-yara-export-escape-strings

Conversation

@elhoim

@elhoim elhoim commented Aug 31, 2026

Copy link
Copy Markdown
Member

BLUF — yara_export splices attribute values into YARA literals unescaped; this escapes and compiles them.

  • ProblemYaraRule.__str__ in misp_modules/modules/export_mod/yara_export.py interpolates MISP attribute values and meta fields (event info, org name) into double-quoted YARA string literals without escaping backslashes or quotes, so a value containing a double quote breaks out of its literal and attacker-supplied text is spliced into the rule body as YARA syntax; unlike handle_yara, this path never validated the result.
  • Fix — Escapes backslash and double quote at both interpolation sites and compiles the assembled ruleset with yara.compile() before returning it.
  • Effect — Such values stay inert data in exports, and a ruleset that still fails to compile returns a misperrors response instead of a silently broken or manipulated rule.

YaraRule.__str__ in misp_modules/modules/export_mod/yara_export.py interpolated MISP attribute values and meta values (event info, org name, link attributes) directly into double-quoted YARA string literals without escaping backslash or embedded double-quote characters:

for key, values in self.meta.items():
    ...
    result.append(f'        {key} = "{values[0]}"')
    ...
for key, values in self.strings.items():
    ...
    result.append(f'        ${key}_{i} = "{value}"')

Unlike the handle_yara path in the same module, the ruleset built by this class was also never passed through yara.compile() before being returned.

Impact: an attacker-controlled MISP attribute value (or event info / org name) containing a " or \ breaks out of the YARA string literal it was placed in. Depending on the surrounding content this either produces a ruleset that fails to compile in the analyst's YARA engine (an unusable export with no warning from the module) or, more seriously, lets attacker-supplied text be spliced into the rule body as YARA syntax rather than staying inert data — an analyst exporting attributes to a YARA rule has no indication the export is malformed or manipulated until it fails, or misbehaves, downstream.

Fix

Escape \ and " in both the strings-section and meta-section interpolation sites, and compile the fully generated ruleset with yara.compile() before returning it, returning a misperrors response instead of silently emitting an invalid/unsafe ruleset if compilation fails.

Verification

  • flake8 on the changed file: clean, no output.
  • python -m pytest tests/ against a local misp-modules instance: 161 passed, 4 skipped, 5 subtests passed.

Found during a review of the repository; other findings are being submitted as separate PRs.

🤖 Generated with Claude Code

…RA string/meta literals

The dynamically-generated rule (YaraRule.__str__) interpolates attribute
values and event/org metadata directly into double-quoted YARA string
literals without escaping backslashes or embedded double quotes. An
attacker-influenced attribute value (or event info/org name) containing
a `"` breaks out of the literal and corrupts the generated rule; unlike
the handle_yara path, which validates the raw YARA text supplied by a
user via yara.compile(), this generated path was never validated at all,
so a broken rule was silently exported as if it were valid YARA.

Both string and meta values are now backslash/quote-escaped before being
quoted, and the full generated ruleset is compiled with yara.compile()
before being returned; if it fails to compile, the module now reports an
error instead of silently emitting an invalid ruleset.

Verified with flake8 (clean) and the full test suite against a locally
started misp-modules server on port 6784: 161 passed, 4 skipped, 5
subtests passed, matching baseline, including the existing
test_yara_export case (which exercises the handle_yara path and is
unaffected).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018dfYpyaSZd1nxSRLr8suj8
@elhoim elhoim changed the title fix: [export_mod/yara_export] escape attribute values in generated YARA string/meta literals [critical] fix: [export_mod/yara_export] escape attribute values in generated YARA string/meta literals Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant