From 2c1b2fbdd8caac878c651d14182e1719d127eb95 Mon Sep 17 00:00:00 2001 From: Christopher Kevin Date: Tue, 22 Sep 2026 11:56:35 -0700 Subject: [PATCH 1/6] fix(yara): avoid ransomware findings for Bitcoin payment prose Signed-off-by: Christopher Kevin --- src/skillspector/yara_rules/malware.yar.b64 | 91 ++++++++----- tests/integration/test_ransomware_rule.py | 137 ++++++++++++++++++++ tests/nodes/analyzers/test_static_yara.py | 108 +++++++++++++++ 3 files changed, 301 insertions(+), 35 deletions(-) create mode 100644 tests/integration/test_ransomware_rule.py diff --git a/src/skillspector/yara_rules/malware.yar.b64 b/src/skillspector/yara_rules/malware.yar.b64 index 84d2402fc..a4f2f9f41 100644 --- a/src/skillspector/yara_rules/malware.yar.b64 +++ b/src/skillspector/yara_rules/malware.yar.b64 @@ -79,38 +79,59 @@ DQogICAgICAgIGRlc2NyaXB0aW9uID0gIlJhbnNvbXdhcmUtbGlrZSBwYXR0ZXJucyAobWFzcyBl bmNyeXB0aW9uLCByYW5zb20gbm90ZXMpIg0KICAgICAgICBjYXRlZ29yeSA9ICJtYWx3YXJlIg0K ICAgICAgICBzZXZlcml0eSA9ICJDUklUSUNBTCINCiAgICAgICAgY29uZmlkZW5jZSA9ICIwLjgi DQogICAgc3RyaW5nczoNCiAgICAgICAgJHdhbGtfZW5jcnlwdCAgID0gL29zXC53YWxrXHMqXCgu -KlwuKGVuY3J5cHR8Y2lwaGVyKS8NCiAgICAgICAgJHJhbnNvbV9ub3RlICAgID0gLyh5b3VyXHMr -ZmlsZXNccysoaGF2ZVxzK2JlZW58YXJlKVxzK2VuY3J5cHRlZHxwYXlccysuKmJpdGNvaW58c2Vu -ZFxzKy4qYnRjKS8gbm9jYXNlDQogICAgICAgICRleHRfcmVuYW1lICAgICA9IC9vc1wucmVuYW1l -XHMqXCguKlwrXHMqWyciXVwuKGxvY2tlZHxlbmNyeXB0ZWR8Y3J5cHR8ZW5jKVsnIl1ccypcKS8N -CiAgICAgICAgJG1hc3N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ft +LyBub2Nhc2UNCiAgICAgICAgJG50ZHNfZHVtcCAgICAgICAgPSAvbnRkc1wuZGl0LyBub2Nhc2UN +CiAgICBjb25kaXRpb246DQogICAgICAgIGFueSBvZiB0aGVtDQp9DQo= diff --git a/tests/integration/test_ransomware_rule.py b/tests/integration/test_ransomware_rule.py new file mode 100644 index 000000000..5deed6dce --- /dev/null +++ b/tests/integration/test_ransomware_rule.py @@ -0,0 +1,137 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Real-YARA regression coverage through the complete static analysis graph.""" + +import json +from pathlib import Path + +import pytest + +from skillspector.graph import graph + + +@pytest.mark.parametrize("output_format", ["json", "markdown", "sarif", "terminal"]) +@pytest.mark.parametrize( + ("content", "expected_match"), + [ + pytest.param( + "Use this wallet to send BTC to a recipient.\n" + "Pay an invoice with bitcoin after reviewing the amount.\n", + False, + id="wallet-documentation", + ), + pytest.param( + "Never pay the ransom in Bitcoin; report the demand to your security team.\n", + False, + id="security-advice", + ), + pytest.param( + "Your data has been encrypted. Never pay the ransom in Bitcoin; report it.\n", + False, + id="encrypted-data-security-advice", + ), + pytest.param( + "Your data has been encrypted.\nPay the ransom in Bitcoin.\n", + False, + id="multiline-bare-imperative", + ), + pytest.param("Your files are encrypted.\n", True, id="encrypted-files-are"), + pytest.param("Your files have been encrypted.\n", True, id="encrypted-files-have-been"), + pytest.param( + "Send BTC or we will publish your data.\n", + True, + id="payment-then-extortion", + ), + pytest.param( + "We will delete your files unless you pay Bitcoin.\n", + True, + id="extortion-then-payment", + ), + pytest.param( + "You must pay the ransom in Bitcoin.\n", + True, + id="explicit-ransom-payment", + ), + pytest.param( + "Your data has been encrypted. Pay the ransom in Bitcoin to recover your files.\n", + True, + id="bare-imperative-ransom-payment", + ), + ], +) +def test_graph_reports_only_specific_ransomware_indicators( + tmp_path: Path, output_format: str, content: str, expected_match: bool +) -> None: + """Payment prose stays clear while encrypted-files notices retain accurate reports.""" + (tmp_path / "SKILL.md").write_text( + "---\n" + "name: ransomware-regression\n" + "description: Text-only detector regression fixture.\n" + "---\n\n" + "# Text fixture\n\n" + content, + encoding="utf-8", + ) + + result = graph.invoke( + { + "skill_path": str(tmp_path), + "output_format": output_format, + "use_llm": False, + } + ) + + rule_name = "ransomware_behavior" + findings = [finding for finding in result["findings"] if rule_name in finding.message] + assert bool(findings) is expected_match + assert result["analysis_completeness"]["execution_successful"] is True + if expected_match: + assert len(findings) == 1 + finding = findings[0] + assert finding.rule_id == "YR1" + assert finding.severity == "CRITICAL" + assert finding.confidence == 0.8 + assert finding.file == "SKILL.md" + assert finding.start_line == 8 + + if output_format == "json": + report = json.loads(result["report_body"]) + issues = [issue for issue in report["issues"] if rule_name in (issue["pattern"] or "")] + assert bool(issues) is expected_match + if expected_match: + assert issues[0]["id"] == "YR1" + assert issues[0]["severity"] == "CRITICAL" + assert issues[0]["location"]["file"] == "SKILL.md" + assert issues[0]["location"]["start_line"] == 8 + assert report["risk_assessment"]["max_issue_severity"] == "CRITICAL" + elif output_format == "sarif": + report = result["sarif_report"] + issues = [ + issue for issue in report["runs"][0]["results"] if rule_name in issue["message"]["text"] + ] + assert bool(issues) is expected_match + if expected_match: + assert issues[0]["ruleId"] == "YR1" + assert issues[0]["level"] == "error" + location = issues[0]["locations"][0]["physicalLocation"] + assert location["artifactLocation"]["uri"] == "SKILL.md" + assert location["region"]["startLine"] == 8 + else: + body = result["report_body"] + assert (rule_name in body) is expected_match + if expected_match: + assert "YR1" in body + assert "CRITICAL" in body + assert "SKILL.md" in body diff --git a/tests/nodes/analyzers/test_static_yara.py b/tests/nodes/analyzers/test_static_yara.py index 9c8f6c5e3..83f02d53c 100644 --- a/tests/nodes/analyzers/test_static_yara.py +++ b/tests/nodes/analyzers/test_static_yara.py @@ -1649,6 +1649,114 @@ def test_wasm_instantiate_with_randomx_calculate_hash_is_coinjacking(self): ) findings = _run_builtin(content, "loader.js") assert _has_rule(findings, "crypto_coinjacking") +class TestBuiltInRansomwareRule: + @pytest.mark.parametrize("filename", ["api.md", "wallet.txt", "wallet.py"]) + @pytest.mark.parametrize( + "content", + [ + "Send BTC on the returned network from an external wallet you control.", + "SEND BTC to fund your own account.", + "send 0.01 btc to your wallet on the correct network.", + "Pay with Bitcoin at checkout.", + "PAY the invoice using BITCOIN.", + "Send BTC to your wallet.\nBackups use encryption to protect account data.", + "Never pay Bitcoin ransomware demands; report them to your security team.", + "Never pay the ransom in Bitcoin; report the demand to your security team.", + "Do not pay a ransom with BTC. Restore from backups instead.", + "Your data has been encrypted. Never pay the ransom in Bitcoin; report it.", + "Your data has been encrypted. Do not pay the ransom in Bitcoin.", + "Your data has been encrypted.\nPay the ransom in Bitcoin.", + "Send BTC after you recover your wallet backup.", + "Pay Bitcoin to unlock premium account features.", + "Send BTC for encrypted cloud backup storage.", + "Data recovery service: pay with Bitcoin at checkout.", + "Send BTC, then publish your transaction receipt.", + "Send BTC\nTo receive the decryption key, contact support.", + "Send BTC " + "x" * 161 + " to receive the decryption key.", + ], + ) + def test_wallet_payment_text_is_not_ransomware(self, content, filename): + findings = _run_builtin(content, filename) + + assert not _has_rule(findings, "ransomware_behavior") + + @pytest.mark.parametrize( + "content", + [ + "Your files are encrypted.", + "Your files have been encrypted.", + "YOUR FILES ARE ENCRYPTED. Send BTC for recovery.", + "your\tfiles\thave\tbeen\tencrypted", + "your\nfiles\nare\nencrypted", + ], + ) + def test_encrypted_files_notice_remains_critical(self, content): + findings = _run_builtin("# Notice\n\n" + content, "notice.md") + ransomware = [f for f in findings if "ransomware_behavior" in f.message] + + assert len(ransomware) == 1 + finding = ransomware[0] + assert finding.rule_id == "YR1" + assert finding.severity == "CRITICAL" + assert finding.confidence == 0.8 + assert finding.file == "notice.md" + assert finding.start_line == 3 + assert finding.matched_text + assert "encrypted" in finding.matched_text.lower() + + @pytest.mark.parametrize( + "content", + [ + "Your files are locked. Send BTC to receive the decryption key.", + "Your data has been encrypted. Pay the ransom in Bitcoin to recover your files.", + "You must pay the ransom in Bitcoin to recover your files.", + "Send 0.5 BTC to obtain the decryption key.", + "To receive the decryption key, send 1 BTC.", + "We will delete your files unless you pay 0.5 Bitcoin.", + "Send BTC or we will publish your data.", + "Pay Bitcoin or your files will be destroyed.", + ], + ) + def test_payment_with_explicit_extortion_context_remains_critical(self, content): + findings = _run_builtin(content, "notice.md") + ransomware = [f for f in findings if "ransomware_behavior" in f.message] + + assert len(ransomware) == 1 + assert ransomware[0].rule_id == "YR1" + assert ransomware[0].severity == "CRITICAL" + assert ransomware[0].confidence == 0.8 + + @pytest.mark.parametrize( + "content", + [ + "os.walk(root) ... .encrypt(data)", + "os.walk(root) ... .cipher(data)", + "os.rename(path, path + '.locked')", + "os.rename(path, path + '.encrypted')", + "os.rename(path, path + '.crypt')", + "os.rename(path, path + '.enc')", + "os.walk(root) ... open(path, 'wb')", + ], + ) + def test_behavior_indicators_remain_detected(self, content): + # Inert source fragments are scanned as text, never executed. + findings = _run_builtin(content, "sample.txt") + + assert _has_rule(findings, "ransomware_behavior") + + def test_wallet_text_still_reaches_custom_rules(self, tmp_path): + _write_rule( + tmp_path, + "wallet_policy", + category="hack_tool", + severity="MEDIUM", + strings={"payment": "Send BTC"}, + ) + + findings = _run("Send BTC to your own wallet.", "api.md", str(tmp_path)) + + assert _has_rule(findings, "wallet_policy") + assert not _has_rule(findings, "ransomware_behavior") # ── Built-in agent skill rules ──────────────────────────────────────── From 6e4153720cf68b372076bd12fd663d26db66a41c Mon Sep 17 00:00:00 2001 From: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com> Date: Sun, 4 Oct 2026 23:48:44 -0700 Subject: [PATCH 2/6] fix(yara): restore ransom notice and affirmative payment recall Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com> --- .../nodes/analyzers/static_yara.py | 84 +++++ src/skillspector/yara_rules/malware.yar.b64 | 117 +++---- tests/integration/test_ransomware_rule.py | 91 ++++++ tests/nodes/analyzers/test_static_yara.py | 308 +++++++++++++----- 4 files changed, 462 insertions(+), 138 deletions(-) diff --git a/src/skillspector/nodes/analyzers/static_yara.py b/src/skillspector/nodes/analyzers/static_yara.py index 5de066b1a..3b4dd73b5 100644 --- a/src/skillspector/nodes/analyzers/static_yara.py +++ b/src/skillspector/nodes/analyzers/static_yara.py @@ -27,6 +27,7 @@ import hashlib import math import os +import re import stat import time from collections.abc import Callable @@ -86,6 +87,20 @@ _DESTRUCTIVE_AUTONOMY_NAMESPACE = "agent_skills" _DESTRUCTIVE_AUTONOMY_RULE = "agent_skill_destructive_autonomous_actions" _MAX_DESTRUCTIVE_AUTONOMY_LINE_DISTANCE = 3 +_BUILTIN_RANSOMWARE_NAMESPACE = "malware" +_BUILTIN_RANSOMWARE_RULE = "ransomware_behavior" +_RANSOMWARE_PAYMENT_IDENTIFIERS = frozenset( + {"$payment_then_context", "$context_then_payment", "$explicit_ransom_payment"} +) +_RANSOMWARE_PAYMENT_VERB = re.compile(rb"\b(?:pay|send)\b", re.IGNORECASE) +_DIRECT_RANSOMWARE_NEGATION = re.compile( + rb"\b(?:never|do[ \t]+not|don't|must[ \t]+not|should[ \t]+not|avoid)" + rb"(?:[ \t]+ever)?[ \t]+$", + re.IGNORECASE, +) +_RANSOMWARE_STORAGE_CONTINUATION = re.compile( + rb"[ \t]+(?:at[ \t]+rest|in[ \t]+transit)\b", re.IGNORECASE +) MAX_YARA_MATCH_INSTANCES_PER_RULE = 4_096 MAX_YARA_RULE_FILES = 1_024 MAX_YARA_RULE_DIRECTORY_ENTRIES = 10_000 @@ -637,6 +652,66 @@ def _has_local_destructive_autonomy_evidence( ) +def _payment_is_directly_negated(data: bytes, payment_offset: int) -> bool: + """Check the immediate payment prefix without inventing a clipped word boundary.""" + prefix_start = max(0, payment_offset - 80) + negation = _DIRECT_RANSOMWARE_NEGATION.search(data[prefix_start:payment_offset]) + if negation is None: + return False + start = prefix_start + negation.start() + return start == 0 or not (data[start - 1 : start].isalnum() or data[start - 1 : start] == b"_") + + +def _accepted_builtin_ransomware_instances( + match: yara.Match, + instances: list[tuple[str, object]], + data: bytes, +) -> list[tuple[str, object]]: + """Drop direct payment negation and storage prose from the bundled ransomware rule. + + The bounded YARA expressions establish local payment and extortion context, + while this per-instance check distinguishes an affirmative demand from advice + such as ``Never pay the ransom in Bitcoin``. Encryption notices retain their + recall except when immediately continued with storage-protection wording. + Custom rules are never filtered. + """ + if match.namespace != _BUILTIN_RANSOMWARE_NAMESPACE or match.rule != _BUILTIN_RANSOMWARE_RULE: + return instances + + accepted: list[tuple[str, object]] = [] + for identifier, instance in instances: + if identifier == "$ransom_note": + offset = max(0, int(getattr(instance, "offset", 0))) + matched_length = max(0, int(getattr(instance, "matched_length", 0))) + end = offset + matched_length + continuation = _RANSOMWARE_STORAGE_CONTINUATION.match(data[end : end + 81]) + # The extra byte proves a real word boundary at the bounded edge. + if continuation is not None and continuation.end() <= 80: + continue + if identifier not in _RANSOMWARE_PAYMENT_IDENTIFIERS: + accepted.append((identifier, instance)) + continue + + offset = max(0, int(getattr(instance, "offset", 0))) + matched_length = max(0, int(getattr(instance, "matched_length", 0))) + local_start = max(0, offset - 80) + local_end = min(len(data), offset + matched_length) + prefix = data[local_start:offset] + local_match = prefix + data[offset:local_end] + match_start = len(prefix) + payment_offsets = [ + payment.start() + for payment in _RANSOMWARE_PAYMENT_VERB.finditer(local_match) + if payment.start() >= match_start + ] + if payment_offsets and all( + _payment_is_directly_negated(data, local_start + payment) for payment in payment_offsets + ): + continue + accepted.append((identifier, instance)) + return accepted + + def _parse_meta(match: yara.Match) -> tuple[str, Severity, float, str | None]: """Extract rule_id, severity, confidence, and description from a YARA match's meta.""" meta: dict[str, object] = match.meta or {} @@ -757,6 +832,15 @@ def _match_callback(_match_data: dict[str, object]) -> int: file_path, ) continue + if not limited: + instances = _accepted_builtin_ransomware_instances(match, instances, data) + if not instances: + logger.debug( + "%s: ignored non-extortion ransomware evidence in %s", + ANALYZER_ID, + file_path, + ) + continue rule_id, severity, confidence, description = _parse_meta(match) first_offset, matched_text = _extract_match_strings(instances) fingerprint_limit: _YaraFingerprintLimitError | None = None diff --git a/src/skillspector/yara_rules/malware.yar.b64 b/src/skillspector/yara_rules/malware.yar.b64 index a4f2f9f41..76c9543e5 100644 --- a/src/skillspector/yara_rules/malware.yar.b64 +++ b/src/skillspector/yara_rules/malware.yar.b64 @@ -80,58 +80,65 @@ bmNyeXB0aW9uLCByYW5zb20gbm90ZXMpIg0KICAgICAgICBjYXRlZ29yeSA9ICJtYWx3YXJlIg0K ICAgICAgICBzZXZlcml0eSA9ICJDUklUSUNBTCINCiAgICAgICAgY29uZmlkZW5jZSA9ICIwLjgi DQogICAgc3RyaW5nczoNCiAgICAgICAgJHdhbGtfZW5jcnlwdCAgID0gL29zXC53YWxrXHMqXCgu KlwuKGVuY3J5cHR8Y2lwaGVyKS8NCiAgICAgICAgJHJhbnNvbV9ub3RlICAgID0gL3lvdXJccytm -aWxlc1xzKyhoYXZlXHMrYmVlbnxhcmUpXHMrZW5jcnlwdGVkLyBub2Nhc2UNCiAgICAgICAgJHBh -eW1lbnRfdGhlbl9leHRvcnRpb24gPSAvXGIocGF5fHNlbmQpXGJbXlxyXG5dezEsODB9XGIoYnRj -fGJpdGNvaW4pXGJbXlxyXG5dezAsMTYwfVxiKHRvWyBcdF17MSw4fShyZWNlaXZlfGdldHxvYnRh -aW4pWyBcdF17MSw4fSh0aGV8eW91cilbIFx0XXsxLDh9ZGVjcnlwdGlvblsgXHRdezEsOH1rZXl8 -d2VbIFx0XXsxLDh9d2lsbFsgXHRdezEsOH0ocHVibGlzaHxsZ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fc3RlYWxlcg0Kew0KICAgIG1l +dGE6DQogICAgICAgIGRlc2NyaXB0aW9uID0gIkluZm9ybWF0aW9uIHN0ZWFsZXIgcGF0dGVybnMg +KGNyZWRlbnRpYWwgaGFydmVzdGluZywgYnJvd3NlciBkYXRhIHRoZWZ0KSINCiAgICAgICAgY2F0 +ZWdvcnkgPSAibWFsd2FyZSINCiAgICAgICAgc2V2ZXJpdHkgPSAiSElHSCINCiAgICAgICAgY29u +ZmlkZW5jZSA9ICIwLjc1Ig0KICAgICAgICByZWZlcmVuY2UgPSAiaHR0cHM6Ly9naXRodWIuY29t +L05lbzIzeDAvc2lnbmF0dXJlLWJhc2UiDQogICAgc3RyaW5nczoNCiAgICAgICAgJGNocm9tZV9s +b2dpbiAgICAgPSAvQ2hyb21lLipMb2dpblxzKkRhdGEvIG5vY2FzZQ0KICAgICAgICAkZmlyZWZv +eF9sb2dpbnMgICA9IC9sb2dpbnNcLmpzb24uKmZpcmVmb3gvIG5vY2FzZQ0KICAgICAgICAkYnJv +d3Nlcl9jb29raWVzICA9IC9Db29raWVzLiooY2hyb21lfGZpcmVmb3h8ZWRnZXxvcGVyYSkvIG5v +Y2FzZQ0KICAgICAgICAkd2FsbGV0X3N0ZWFsICAgICA9IC93YWxsZXRcLmRhdC8gbm9jYXNlDQog +ICAgICAgICRtaW1pa2F0eiAgICAgICAgID0gIm1pbWlrYXR6IiBub2Nhc2UNCiAgICAgICAgJGxh +emFnbmUgICAgICAgICAgPSAibGF6YWduZSIgbm9jYXNlDQogICAgICAgICRjcmVkX2R1bXBfc2Ft +ICAgID0gL3JlZ1xzK3NhdmVccysuKlxcc2FtLyBub2Nhc2UNCiAgICAgICAgJG50ZHNfZHVtcCAg +ICAgICAgPSAvbnRkc1wuZGl0LyBub2Nhc2UNCiAgICBjb25kaXRpb246DQogICAgICAgIGFueSBv +ZiB0aGVtDQp9DQo= diff --git a/tests/integration/test_ransomware_rule.py b/tests/integration/test_ransomware_rule.py index 5deed6dce..0786b516d 100644 --- a/tests/integration/test_ransomware_rule.py +++ b/tests/integration/test_ransomware_rule.py @@ -48,6 +48,66 @@ False, id="multiline-bare-imperative", ), + pytest.param( + "Send 0.5 BTC to .\nTo receive the decryption key, email us your personal ID.\n", + True, + id="multiline-payment-key", + ), + pytest.param( + "Don't forget to send 0.5 BTC to example.invalid to decrypt your files.\n", + True, + id="dont-forget-payment", + ), + pytest.param( + "Do not hesitate to pay 1 bitcoin to restore your files.\n", + True, + id="dont-hesitate-payment", + ), + pytest.param( + "All of your files are encrypted with RSA-2048 and AES-128 ciphers.\n", + True, + id="encryption-with-ciphers", + ), + pytest.param( + "All your files have been encrypted due to a security problem with your PC.\n", + True, + id="encryption-due-to", + ), + pytest.param( + "Your files have been encrypted by LockBit 3.0\n", + True, + id="encryption-by", + ), + pytest.param( + "Your files have been encrypted, and the only way to get them back is to pay us.\n", + True, + id="encryption-comma", + ), + pytest.param( + "Your files have been encrypted with AES. Send XMR for recovery.\n", + True, + id="notice-non-bitcoin-demand", + ), + pytest.param( + "You have to pay for decryption in Bitcoins to recover your files.\n", + True, + id="plural-bitcoins", + ), + pytest.param( + "Your files are encrypted at rest and in transit with AES-256.\n", + False, + id="encrypted-at-rest-prose", + ), + pytest.param( + "Your files have been encrypted in transit using TLS.\n", + False, + id="encrypted-in-transit-prose", + ), + pytest.param( + "To recover your files, never ever send 0.5 Bitcoins.\n", + False, + id="direct-negation-with-ever", + ), pytest.param("Your files are encrypted.\n", True, id="encrypted-files-are"), pytest.param("Your files have been encrypted.\n", True, id="encrypted-files-have-been"), pytest.param( @@ -135,3 +195,34 @@ def test_graph_reports_only_specific_ransomware_indicators( assert "YR1" in body assert "CRITICAL" in body assert "SKILL.md" in body + + +@pytest.mark.parametrize("output_format", ["json", "markdown", "sarif", "terminal"]) +def test_graph_mixed_notices_retains_only_actionable_evidence(tmp_path, output_format): + (tmp_path / "SKILL.md").write_text( + "---\nname: mixed-ransomware-fixture\ndescription: Inert text detector regression.\n---\n" + "Your files are encrypted at rest and in transit." + " " * 161 + "\n" + "Never ever send BTC to decrypt your files." + " " * 161 + "\n" + "Don't forget to send 0.5 Bitcoins to decrypt your files.\n", + encoding="utf-8", + ) + result = graph.invoke( + {"skill_path": str(tmp_path), "output_format": output_format, "use_llm": False} + ) + findings = [f for f in result["findings"] if "ransomware_behavior" in f.message] + + assert len(findings) == 1 + assert findings[0].start_line == 7 + assert findings[0].matched_text.startswith("send 0.5 Bitcoins") + assert findings[0].match_fingerprint + assert result["analysis_completeness"]["execution_successful"] is True + if output_format == "json": + issues = json.loads(result["report_body"])["issues"] + reported = [issue for issue in issues if "ransomware_behavior" in (issue["pattern"] or "")] + assert reported[0]["location"]["start_line"] == 7 + elif output_format == "sarif": + issues = result["sarif_report"]["runs"][0]["results"] + reported = [issue for issue in issues if "ransomware_behavior" in issue["message"]["text"]] + assert reported[0]["locations"][0]["physicalLocation"]["region"]["startLine"] == 7 + else: + assert "ransomware_behavior" in result["report_body"] diff --git a/tests/nodes/analyzers/test_static_yara.py b/tests/nodes/analyzers/test_static_yara.py index 83f02d53c..d914dbdcb 100644 --- a/tests/nodes/analyzers/test_static_yara.py +++ b/tests/nodes/analyzers/test_static_yara.py @@ -1568,87 +1568,6 @@ def test_extra_rules_still_match_with_builtin_malware_representation(self, tmp_p assert _has_rule(findings, "reverse_shell") -# ── Built-in cryptominer rules ─────────────────────────────────────── - - -class TestBuiltInCryptominerRules: - """Regression coverage for crypto_coinjacking's $wasm_miner string. - - Unbounded ``(mine|hash|crypto)`` matched inside unrelated identifiers - (``deteRMINE``) and against common, benign Web APIs/module names - (``crypto.getRandomValues``, ``hashmap``) that routinely appear near any - ``WebAssembly.instantiate`` call, firing a CRITICAL cryptojacking finding - on ordinary code. - """ - - def test_wasm_instantiate_with_unrelated_hash_call_is_not_coinjacking(self): - content = "WebAssembly.instantiate(bytes).then(r=>{ hashmap.set(r,1) })\n" - findings = _run_builtin(content, "loader.js") - assert not _has_rule(findings, "crypto_coinjacking") - - def test_wasm_instantiate_with_web_crypto_api_is_not_coinjacking(self): - content = "WebAssembly.instantiate(bytes).then(r=>{ crypto.getRandomValues(buf) })\n" - findings = _run_builtin(content, "loader.js") - assert not _has_rule(findings, "crypto_coinjacking") - - def test_wasm_instantiate_with_mid_word_mine_is_not_coinjacking(self): - content = "WebAssembly.instantiate(bytes).then(r=>{ return determine(r) })\n" - findings = _run_builtin(content, "loader.js") - assert not _has_rule(findings, "crypto_coinjacking") - - def test_wasm_instantiate_with_text_mining_prose_is_not_coinjacking(self): - """`mining` as English prose must not fire; only a mining call does.""" - content = ( - "WebAssembly.instantiate(bytes).then(m=>runAnalytics(m)); // helpers for text mining\n" - ) - findings = _run_builtin(content, "loader.js") - assert not _has_rule(findings, "crypto_coinjacking") - - def test_wasm_instantiate_with_start_mining_call_is_coinjacking(self): - """`startMining(` is a mining call even though `Mining` is mid-identifier.""" - content = "WebAssembly.instantiate(w).then(m=>{ m.exports.startMining(pool) })\n" - findings = _run_builtin(content, "loader.js") - assert _has_rule(findings, "crypto_coinjacking") - - def test_wasm_instantiate_with_capitalised_miner_is_coinjacking(self): - """Case must not matter: `new Miner(` is the common CoinHive-era shape.""" - content = "WebAssembly.instantiate(w).then(m=>{ var x = new Miner(siteKey) })\n" - findings = _run_builtin(content, "loader.js") - assert _has_rule(findings, "crypto_coinjacking") - - def test_wasm_instantiate_with_literal_miner_call_is_coinjacking(self): - content = "WebAssembly.instantiate(minerWasm).then(function(m){ m.exports.mine(); })\n" - findings = _run_builtin(content, "loader.js") - assert _has_rule(findings, "crypto_coinjacking") - - def test_wasm_instantiate_with_cryptonight_glue_is_coinjacking(self): - content = ( - "WebAssembly.instantiate(wasmBinary,info);" - "var _cryptonight_hash=Module._cryptonight_hash=function(){};\n" - ) - findings = _run_builtin(content, "loader.js") - assert _has_rule(findings, "crypto_coinjacking") - - def test_wasm_instantiate_with_hash_cn_cwrap_is_coinjacking(self): - content = 'WebAssembly.instantiate(x).then(()=>{ Module.cwrap("hash_cn", "number", ["number"]) })\n' - findings = _run_builtin(content, "loader.js") - assert _has_rule(findings, "crypto_coinjacking") - - def test_wasm_instantiate_with_cryptonight_wasm_fetch_is_coinjacking(self): - content = ( - 'WebAssembly.instantiateStreaming(fetch("cryptonight.wasm"))' - ".then(o=>{ exports.cn_hash(blob,nonce++) })\n" - ) - findings = _run_builtin(content, "loader.js") - assert _has_rule(findings, "crypto_coinjacking") - - def test_wasm_instantiate_with_randomx_calculate_hash_is_coinjacking(self): - content = ( - "WebAssembly.instantiate(randomxWasm)" - ".then(m=>m.instance.exports.randomx_calculate_hash(blob))\n" - ) - findings = _run_builtin(content, "loader.js") - assert _has_rule(findings, "crypto_coinjacking") class TestBuiltInRansomwareRule: @pytest.mark.parametrize("filename", ["api.md", "wallet.txt", "wallet.py"]) @pytest.mark.parametrize( @@ -1665,14 +1584,16 @@ class TestBuiltInRansomwareRule: "Do not pay a ransom with BTC. Restore from backups instead.", "Your data has been encrypted. Never pay the ransom in Bitcoin; report it.", "Your data has been encrypted. Do not pay the ransom in Bitcoin.", - "Your data has been encrypted.\nPay the ransom in Bitcoin.", "Send BTC after you recover your wallet backup.", "Pay Bitcoin to unlock premium account features.", "Send BTC for encrypted cloud backup storage.", "Data recovery service: pay with Bitcoin at checkout.", "Send BTC, then publish your transaction receipt.", - "Send BTC\nTo receive the decryption key, contact support.", "Send BTC " + "x" * 161 + " to receive the decryption key.", + "Your files are encrypted at rest and in transit with AES-256.", + "Your files have been encrypted at rest with AES-256.", + "Your files are encrypted in transit using TLS.", + "YOUR FILES HAVE BEEN ENCRYPTED\tAT\tREST.", ], ) def test_wallet_payment_text_is_not_ransomware(self, content, filename): @@ -1688,6 +1609,11 @@ def test_wallet_payment_text_is_not_ransomware(self, content, filename): "YOUR FILES ARE ENCRYPTED. Send BTC for recovery.", "your\tfiles\thave\tbeen\tencrypted", "your\nfiles\nare\nencrypted", + "All of your files are encrypted with RSA-2048 and AES-128 ciphers.", + "All your files have been encrypted due to a security problem with your PC.", + "Your files have been encrypted by LockBit 3.0", + "Your files have been encrypted, and the only way to get them back is to pay us.", + "Your files have been encrypted with AES. Send XMR for recovery.", ], ) def test_encrypted_files_notice_remains_critical(self, content): @@ -1711,10 +1637,30 @@ def test_encrypted_files_notice_remains_critical(self, content): "Your data has been encrypted. Pay the ransom in Bitcoin to recover your files.", "You must pay the ransom in Bitcoin to recover your files.", "Send 0.5 BTC to obtain the decryption key.", + "Don't forget to send 0.5 BTC to example.invalid to decrypt your files.", + "Do not hesitate to pay 1 bitcoin to restore your files.", + "You have to pay for decryption in Bitcoins to recover your files.", + "Send 0.5 Bitcoins to decrypt your files.", + "To restore your files, pay 1 Bitcoins.", + "Pay the ransom in Bitcoins.", "To receive the decryption key, send 1 BTC.", "We will delete your files unless you pay 0.5 Bitcoin.", "Send BTC or we will publish your data.", "Pay Bitcoin or your files will be destroyed.", + "Your data has been encrypted.\nPay the ransom in Bitcoin.", + "Send 0.5 BTC to to decrypt your files.", + "Pay 1 bitcoin within 72 hours or your files will be lost forever.", + "To recover your files, pay 0.3 Bitcoin to .", + "Send BTC\nTo receive the decryption key, contact support.", + ( + "Your documents, photos and databases have been encrypted.\n" + "To decrypt them, send 0.5 BTC to ." + ), + ( + "Send 0.5 BTC to .\n" + "After payment you will get the decryption key.\n" + "If payment is not received, your files will be deleted." + ), ], ) def test_payment_with_explicit_extortion_context_remains_critical(self, content): @@ -1726,6 +1672,99 @@ def test_payment_with_explicit_extortion_context_remains_critical(self, content) assert ransomware[0].severity == "CRITICAL" assert ransomware[0].confidence == 0.8 + @pytest.mark.parametrize("negator", ["Never", "Do not", "Don't", "Must not", "Should not"]) + @pytest.mark.parametrize("modifier", ["", " ever"]) + @pytest.mark.parametrize("payment", ["pay", "send"]) + @pytest.mark.parametrize("context_first", [False, True]) + def test_only_directly_negated_payments_are_filtered( + self, negator, modifier, payment, context_first + ): + demand = f"{negator}{modifier} {payment} 0.5 Bitcoins" + content = ( + f"To recover your files, {demand}." + if context_first + else f"{demand} to decrypt your files." + ) + + assert not _has_rule(_run_builtin(content), "ransomware_behavior") + + @pytest.mark.parametrize("separator", ["\n", "\r\n"]) + def test_storage_notice_does_not_hide_later_encryption_notice(self, separator): + content = ( + "Your files are encrypted at rest with AES-256." + + separator + + "Your files have been encrypted by a ransomware operator." + ) + ransomware = [ + finding + for finding in _run_builtin(content, "notice.md") + if "ransomware_behavior" in finding.message + ] + + assert len(ransomware) == 1 + assert ransomware[0].start_line == 2 + assert ransomware[0].matched_text == "Your files have been encrypted" + assert ransomware[0].match_fingerprint + + def test_storage_notice_does_not_hide_independent_payment_demand(self): + content = ( + "Your files are encrypted at rest with AES-256." + " " * 161 + "\n" + "Don't forget to send 0.5 BTC to decrypt your files." + ) + ransomware = [ + finding + for finding in _run_builtin(content, "notice.md") + if "ransomware_behavior" in finding.message + ] + + assert len(ransomware) == 1 + assert ransomware[0].start_line == 2 + assert ransomware[0].matched_text.startswith("send 0.5 BTC") + + def test_clipped_prefix_cannot_manufacture_a_negator_word(self): + content = "xnever" + " " * 75 + "send BTC to decrypt your files." + + assert _has_rule(_run_builtin(content), "ransomware_behavior") + + def test_storage_filter_does_not_change_custom_rule_evidence(self, tmp_path): + _write_rule( + tmp_path, + "ransomware_behavior", + category="malware", + severity="CRITICAL", + strings={"ransom_note": "Your files are encrypted at rest"}, + ) + + findings = _run("Your files are encrypted at rest.", "storage.md", str(tmp_path)) + + assert len(findings) == 1 + assert findings[0].matched_text == "Your files are encrypted at rest" + assert "[malware]" not in findings[0].message + + @pytest.mark.parametrize("spacing, expected", [(160, True), (161, False)]) + @pytest.mark.parametrize("context_first", [False, True]) + def test_cross_line_extortion_window_remains_bounded(self, spacing, expected, context_first): + gap = "\n" + "x" * (spacing - 2) + " " + content = ( + "to decrypt your files" + gap + "send BTC" + if context_first + else "send BTC" + gap + "to decrypt your files" + ) + + assert _has_rule(_run_builtin(content), "ransomware_behavior") is expected + + def test_real_demand_after_negated_advice_supplies_evidence_and_location(self): + content = ( + "Never pay the ransom in Bitcoin; report the demand.\nPay the ransom in Bitcoin.\n" + ) + + findings = _run_builtin(content, "notice.md") + ransomware = [finding for finding in findings if "ransomware_behavior" in finding.message] + + assert len(ransomware) == 1 + assert ransomware[0].start_line == 2 + assert ransomware[0].matched_text == "Pay the ransom in Bitcoin" + @pytest.mark.parametrize( "content", [ @@ -1744,6 +1783,28 @@ def test_behavior_indicators_remain_detected(self, content): assert _has_rule(findings, "ransomware_behavior") + def test_multiline_encrypt_and_drop_note_script_is_detected(self): + content = """\ +from cryptography.fernet import Fernet +import os + +for root, _dirs, files in os.walk(os.path.expanduser("~")): + for filename in files: + path = os.path.join(root, filename) + with open(path, "rb") as source: + encrypted = Fernet(key).encrypt(source.read()) + with open(path, "wb") as destination: + destination.write(encrypted) + +with open("README_RESTORE_FILES.txt", "w") as note: + note.write("Your documents have been encrypted.\\n") + note.write("Send 0.5 BTC to to decrypt your files.\\n") +""" + + findings = _run_builtin(content, "encrypt.py") + + assert _has_rule(findings, "ransomware_behavior") + def test_wallet_text_still_reaches_custom_rules(self, tmp_path): _write_rule( tmp_path, @@ -1759,6 +1820,87 @@ def test_wallet_text_still_reaches_custom_rules(self, tmp_path): assert not _has_rule(findings, "ransomware_behavior") +# ── Built-in cryptominer rules ─────────────────────────────────────── + + +class TestBuiltInCryptominerRules: + """Regression coverage for crypto_coinjacking's $wasm_miner string. + + Unbounded ``(mine|hash|crypto)`` matched inside unrelated identifiers + (``deteRMINE``) and against common, benign Web APIs/module names + (``crypto.getRandomValues``, ``hashmap``) that routinely appear near any + ``WebAssembly.instantiate`` call, firing a CRITICAL cryptojacking finding + on ordinary code. + """ + + def test_wasm_instantiate_with_unrelated_hash_call_is_not_coinjacking(self): + content = "WebAssembly.instantiate(bytes).then(r=>{ hashmap.set(r,1) })\n" + findings = _run_builtin(content, "loader.js") + assert not _has_rule(findings, "crypto_coinjacking") + + def test_wasm_instantiate_with_web_crypto_api_is_not_coinjacking(self): + content = "WebAssembly.instantiate(bytes).then(r=>{ crypto.getRandomValues(buf) })\n" + findings = _run_builtin(content, "loader.js") + assert not _has_rule(findings, "crypto_coinjacking") + + def test_wasm_instantiate_with_mid_word_mine_is_not_coinjacking(self): + content = "WebAssembly.instantiate(bytes).then(r=>{ return determine(r) })\n" + findings = _run_builtin(content, "loader.js") + assert not _has_rule(findings, "crypto_coinjacking") + + def test_wasm_instantiate_with_text_mining_prose_is_not_coinjacking(self): + """`mining` as English prose must not fire; only a mining call does.""" + content = ( + "WebAssembly.instantiate(bytes).then(m=>runAnalytics(m)); // helpers for text mining\n" + ) + findings = _run_builtin(content, "loader.js") + assert not _has_rule(findings, "crypto_coinjacking") + + def test_wasm_instantiate_with_start_mining_call_is_coinjacking(self): + """`startMining(` is a mining call even though `Mining` is mid-identifier.""" + content = "WebAssembly.instantiate(w).then(m=>{ m.exports.startMining(pool) })\n" + findings = _run_builtin(content, "loader.js") + assert _has_rule(findings, "crypto_coinjacking") + + def test_wasm_instantiate_with_capitalised_miner_is_coinjacking(self): + """Case must not matter: `new Miner(` is the common CoinHive-era shape.""" + content = "WebAssembly.instantiate(w).then(m=>{ var x = new Miner(siteKey) })\n" + findings = _run_builtin(content, "loader.js") + assert _has_rule(findings, "crypto_coinjacking") + + def test_wasm_instantiate_with_literal_miner_call_is_coinjacking(self): + content = "WebAssembly.instantiate(minerWasm).then(function(m){ m.exports.mine(); })\n" + findings = _run_builtin(content, "loader.js") + assert _has_rule(findings, "crypto_coinjacking") + + def test_wasm_instantiate_with_cryptonight_glue_is_coinjacking(self): + content = ( + "WebAssembly.instantiate(wasmBinary,info);" + "var _cryptonight_hash=Module._cryptonight_hash=function(){};\n" + ) + findings = _run_builtin(content, "loader.js") + assert _has_rule(findings, "crypto_coinjacking") + + def test_wasm_instantiate_with_hash_cn_cwrap_is_coinjacking(self): + content = 'WebAssembly.instantiate(x).then(()=>{ Module.cwrap("hash_cn", "number", ["number"]) })\n' + findings = _run_builtin(content, "loader.js") + assert _has_rule(findings, "crypto_coinjacking") + + def test_wasm_instantiate_with_cryptonight_wasm_fetch_is_coinjacking(self): + content = ( + 'WebAssembly.instantiateStreaming(fetch("cryptonight.wasm"))' + ".then(o=>{ exports.cn_hash(blob,nonce++) })\n" + ) + findings = _run_builtin(content, "loader.js") + assert _has_rule(findings, "crypto_coinjacking") + + def test_wasm_instantiate_with_randomx_calculate_hash_is_coinjacking(self): + content = ( + "WebAssembly.instantiate(randomxWasm)" + ".then(m=>m.instance.exports.randomx_calculate_hash(blob))\n" + ) + findings = _run_builtin(content, "loader.js") + assert _has_rule(findings, "crypto_coinjacking") # ── Built-in agent skill rules ──────────────────────────────────────── From b19c8fc7caff5e417d88c4be222798a5e2a12081 Mon Sep 17 00:00:00 2001 From: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com> Date: Mon, 5 Oct 2026 13:48:53 -0700 Subject: [PATCH 3/6] test(yara): retain ransom payment expectations after upstream integration Signed-off-by: Christopher Kevin <256191862+chrisknvidia@users.noreply.github.com> --- tests/integration/test_ransomware_rule.py | 2 +- tests/nodes/analyzers/test_static_yara.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/integration/test_ransomware_rule.py b/tests/integration/test_ransomware_rule.py index 0786b516d..ae98136c6 100644 --- a/tests/integration/test_ransomware_rule.py +++ b/tests/integration/test_ransomware_rule.py @@ -45,7 +45,7 @@ ), pytest.param( "Your data has been encrypted.\nPay the ransom in Bitcoin.\n", - False, + True, id="multiline-bare-imperative", ), pytest.param( diff --git a/tests/nodes/analyzers/test_static_yara.py b/tests/nodes/analyzers/test_static_yara.py index d914dbdcb..3e0133e7c 100644 --- a/tests/nodes/analyzers/test_static_yara.py +++ b/tests/nodes/analyzers/test_static_yara.py @@ -1901,6 +1901,8 @@ def test_wasm_instantiate_with_randomx_calculate_hash_is_coinjacking(self): ) findings = _run_builtin(content, "loader.js") assert _has_rule(findings, "crypto_coinjacking") + + # ── Built-in agent skill rules ──────────────────────────────────────── From 136c99dac68ba3f6ec7cdb3f48c482c74b9fe48c Mon Sep 17 00:00:00 2001 From: Christopher Kevin Date: Tue, 6 Oct 2026 21:51:50 -0700 Subject: [PATCH 4/6] test(yara): verify rendered Markdown ransomware reports Signed-off-by: Christopher Kevin --- tests/integration/test_ransomware_rule.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/integration/test_ransomware_rule.py b/tests/integration/test_ransomware_rule.py index ae98136c6..67202c91b 100644 --- a/tests/integration/test_ransomware_rule.py +++ b/tests/integration/test_ransomware_rule.py @@ -19,6 +19,7 @@ from pathlib import Path import pytest +from markdown_it import MarkdownIt from skillspector.graph import graph @@ -190,6 +191,8 @@ def test_graph_reports_only_specific_ransomware_indicators( assert location["region"]["startLine"] == 8 else: body = result["report_body"] + if output_format == "markdown": + body = MarkdownIt().enable("table").render(body) assert (rule_name in body) is expected_match if expected_match: assert "YR1" in body @@ -225,4 +228,7 @@ def test_graph_mixed_notices_retains_only_actionable_evidence(tmp_path, output_f reported = [issue for issue in issues if "ransomware_behavior" in issue["message"]["text"]] assert reported[0]["locations"][0]["physicalLocation"]["region"]["startLine"] == 7 else: - assert "ransomware_behavior" in result["report_body"] + body = result["report_body"] + if output_format == "markdown": + body = MarkdownIt().enable("table").render(body) + assert "ransomware_behavior" in body From 9a0fe9dac757589289450a6620c0ad01c1266496 Mon Sep 17 00:00:00 2001 From: Narendran Raghavan Date: Thu, 8 Oct 2026 13:53:17 -0700 Subject: [PATCH 5/6] fix(yara): keep conditional ransom threats out of the payment-advice filter The ransomware negation filter dropped any pay/send verb directly after "don't", "do not" or "never". Conditional threats such as "If you don't pay 0.5 bitcoin within 48 hours, your files will be deleted" and minimum-amount demands such as "Never send less than 0.5 BTC to decrypt your files" were therefore suppressed even though main reports them. Treat the negation as advice only when it opens a clause (start of input or after a line break or .;:!?, punctuation, allowing list/quote/emphasis markup and a "you should/must" subject), and never when the verb continues with "less than"/"fewer than". The search runs on the original buffer so the clause boundary is never inferred from a clipped window edge. A typographic apostrophe in "Don't" is recognised as well. Signed-off-by: Narendran Raghavan Co-Authored-By: Claude Opus 5.5 --- .../nodes/analyzers/static_yara.py | 32 ++++++++--- tests/integration/test_ransomware_rule.py | 15 ++++++ tests/nodes/analyzers/test_static_yara.py | 53 +++++++++++++++++++ 3 files changed, 93 insertions(+), 7 deletions(-) diff --git a/src/skillspector/nodes/analyzers/static_yara.py b/src/skillspector/nodes/analyzers/static_yara.py index 3b4dd73b5..a507dc55e 100644 --- a/src/skillspector/nodes/analyzers/static_yara.py +++ b/src/skillspector/nodes/analyzers/static_yara.py @@ -93,9 +93,23 @@ {"$payment_then_context", "$context_then_payment", "$explicit_ransom_payment"} ) _RANSOMWARE_PAYMENT_VERB = re.compile(rb"\b(?:pay|send)\b", re.IGNORECASE) +# Advice such as "Never pay the ransom" opens its clause with the negator (after +# optional list/quote markup or a "you should" subject). A negator that follows +# other words, as in "If you don't pay ... your files will be deleted", is part of +# a conditional threat and must not suppress the demand. _DIRECT_RANSOMWARE_NEGATION = re.compile( - rb"\b(?:never|do[ \t]+not|don't|must[ \t]+not|should[ \t]+not|avoid)" - rb"(?:[ \t]+ever)?[ \t]+$", + rb"(?:^|(?<=[\r\n.;:!?,]))[ \t]*" + rb"(?:(?:[-*+>]|\d{1,3}[.)])[ \t]+){0,3}" + rb"(?:[\"'(\[]|\xe2\x80[\x98\x9c])?[*_]{0,2}" + rb"(?:you[ \t]+(?:(?:should|must)[ \t]+)?)?" + rb"(?:never|do[ \t]+not|don(?:'|\xe2\x80\x99)t|must[ \t]+not|should[ \t]+not|avoid)" + rb"[*_]{0,2}(?:[ \t]+ever)?[ \t]+$", + re.IGNORECASE, +) +# "Never pay less than 1 BTC" sets a minimum amount; it is a demand, not advice. +_RANSOMWARE_MINIMUM_PAYMENT = re.compile( + rb"(?:pay|send)[ \t]{1,8}(?:(?:anything|any[ \t]{1,8}amount)[ \t]{1,8})?" + rb"(?:less|fewer)[ \t]{1,8}than\b", re.IGNORECASE, ) _RANSOMWARE_STORAGE_CONTINUATION = re.compile( @@ -653,13 +667,17 @@ def _has_local_destructive_autonomy_evidence( def _payment_is_directly_negated(data: bytes, payment_offset: int) -> bool: - """Check the immediate payment prefix without inventing a clipped word boundary.""" + """Return whether the payment verb is clause-opening advice such as ``Never pay``. + + The search runs over the original buffer (``pos``/``endpos``), so the clause + boundary lookbehind sees the real preceding byte instead of a clipped window + edge. Conditional threats (``If you don't pay``) and minimum-amount demands + (``Never send less than 0.5 BTC``) are not advice. + """ prefix_start = max(0, payment_offset - 80) - negation = _DIRECT_RANSOMWARE_NEGATION.search(data[prefix_start:payment_offset]) - if negation is None: + if _DIRECT_RANSOMWARE_NEGATION.search(data, prefix_start, payment_offset) is None: return False - start = prefix_start + negation.start() - return start == 0 or not (data[start - 1 : start].isalnum() or data[start - 1 : start] == b"_") + return _RANSOMWARE_MINIMUM_PAYMENT.match(data, payment_offset) is None def _accepted_builtin_ransomware_instances( diff --git a/tests/integration/test_ransomware_rule.py b/tests/integration/test_ransomware_rule.py index 67202c91b..98ea306c3 100644 --- a/tests/integration/test_ransomware_rule.py +++ b/tests/integration/test_ransomware_rule.py @@ -109,6 +109,21 @@ False, id="direct-negation-with-ever", ), + pytest.param( + "Never send BTC to anyone who promises to decrypt your files.\n", + False, + id="clause-opening-advice", + ), + pytest.param( + "If you don't pay 0.5 bitcoin within 48 hours, your files will be deleted.\n", + True, + id="conditional-negated-threat", + ), + pytest.param( + "Never send less than 0.5 BTC to decrypt your files.\n", + True, + id="minimum-amount-demand", + ), pytest.param("Your files are encrypted.\n", True, id="encrypted-files-are"), pytest.param("Your files have been encrypted.\n", True, id="encrypted-files-have-been"), pytest.param( diff --git a/tests/nodes/analyzers/test_static_yara.py b/tests/nodes/analyzers/test_static_yara.py index 3e0133e7c..2b87101e4 100644 --- a/tests/nodes/analyzers/test_static_yara.py +++ b/tests/nodes/analyzers/test_static_yara.py @@ -1688,6 +1688,59 @@ def test_only_directly_negated_payments_are_filtered( assert not _has_rule(_run_builtin(content), "ransomware_behavior") + @pytest.mark.parametrize( + "content", + [ + "If you don't pay 0.5 bitcoin within 48 hours, your files will be deleted.", + "If you don’t pay 0.5 bitcoin within 48 hours, your files will be deleted.", + "If you do not pay 1 Bitcoin within 72 hours, all your files will be lost forever.", + "If you do not send 1 BTC to the address below, we will publish your stolen data.", + "Never pay less than 1 bitcoin or your files will be deleted.", + "Never send less than 0.5 BTC to decrypt your files.", + "Do not pay less than 1 BTC or your files will be destroyed.", + "Don't send anything less than 0.5 BTC to decrypt your files.", + ( + "We have downloaded your confidential data.\n" + "If you do not send 5 BTC to the address below within 72 hours, " + "we will publish your stolen data." + ), + ( + "All your important files are encrypted!\n" + "If you don't pay 0.5 bitcoin within 48 hours, your files will be deleted." + ), + ], + ) + def test_conditional_and_minimum_amount_demands_remain_critical(self, content): + ransomware = [ + finding + for finding in _run_builtin(content, "notice.md") + if "ransomware_behavior" in finding.message + ] + + assert len(ransomware) == 1 + assert ransomware[0].rule_id == "YR1" + assert ransomware[0].severity == "CRITICAL" + assert ransomware[0].start_line == content.count("\n") + 1 + + @pytest.mark.parametrize( + "content", + [ + "Never send BTC to strangers.", + "Never send BTC to anyone who promises to decrypt your files.", + "Never pay the ransom in Bitcoin; report it to your security team.", + "Do not pay the ransom in Bitcoin.", + "If you are attacked, do not pay the ransom in Bitcoin.", + "You should never pay the ransom in Bitcoin.", + "You must not send BTC to anyone who offers to decrypt your files.", + "- Never pay the ransom in Bitcoin.", + "> **Never** pay the ransom in Bitcoin.", + "1. Do not send BTC to anyone who offers to decrypt your files.", + "Don’t send BTC to anyone promising to decrypt your files.", + ], + ) + def test_clause_opening_payment_advice_is_not_ransomware(self, content): + assert not _has_rule(_run_builtin(content, "advice.md"), "ransomware_behavior") + @pytest.mark.parametrize("separator", ["\n", "\r\n"]) def test_storage_notice_does_not_hide_later_encryption_notice(self, separator): content = ( From 9ac516a90830f420565f09ac3f10ed9eb052c6e8 Mon Sep 17 00:00:00 2001 From: Narendran Raghavan Date: Thu, 8 Oct 2026 14:57:52 -0700 Subject: [PATCH 6/6] fix(yara): keep hard-wrapped and glued-amount ransom demands detectable Ransom notes are often hard-wrapped, but the payment-context strings required spaces or tabs inside each phrase and kept the pay/send verb and the currency on one line. Notes such as "To decrypt your files you must send\n0.5 BTC" or "To decrypt\nyour files you must send 0.5 BTC" were therefore missed, as were glued amounts such as "Send 0.5BTC". In $payment_then_context, $context_then_payment and $explicit_ransom_payment: - use \s instead of [ \t] inside the bounded phrase separators; - allow one line break (and up to 80 more characters) between the verb and the currency; - accept a digit immediately before btc/bitcoin. Every window stays bounded and the rule compiles with the same YARA warnings as before. Because a payment-first match can now run into a demand on the next line, the negation check for $payment_then_context and $explicit_ransom_payment judges only the leading verb; the later demand is reported as its own instance and location. Signed-off-by: Narendran Raghavan Co-Authored-By: Claude Opus 5.5 --- .../nodes/analyzers/static_yara.py | 7 + src/skillspector/yara_rules/malware.yar.b64 | 121 +++++++++--------- tests/integration/test_ransomware_rule.py | 10 ++ tests/nodes/analyzers/test_static_yara.py | 56 ++++++++ 4 files changed, 133 insertions(+), 61 deletions(-) diff --git a/src/skillspector/nodes/analyzers/static_yara.py b/src/skillspector/nodes/analyzers/static_yara.py index a507dc55e..4a949170b 100644 --- a/src/skillspector/nodes/analyzers/static_yara.py +++ b/src/skillspector/nodes/analyzers/static_yara.py @@ -92,6 +92,9 @@ _RANSOMWARE_PAYMENT_IDENTIFIERS = frozenset( {"$payment_then_context", "$context_then_payment", "$explicit_ransom_payment"} ) +_RANSOMWARE_PAYMENT_FIRST_IDENTIFIERS = frozenset( + {"$payment_then_context", "$explicit_ransom_payment"} +) _RANSOMWARE_PAYMENT_VERB = re.compile(rb"\b(?:pay|send)\b", re.IGNORECASE) # Advice such as "Never pay the ransom" opens its clause with the negator (after # optional list/quote markup or a "you should" subject). A negator that follows @@ -722,6 +725,10 @@ def _accepted_builtin_ransomware_instances( for payment in _RANSOMWARE_PAYMENT_VERB.finditer(local_match) if payment.start() >= match_start ] + if identifier in _RANSOMWARE_PAYMENT_FIRST_IDENTIFIERS: + # The leading verb owns the currency. A wrapped match can run into a + # later demand, which YARA reports as its own instance and location. + payment_offsets = payment_offsets[:1] if payment_offsets and all( _payment_is_directly_negated(data, local_start + payment) for payment in payment_offsets ): diff --git a/src/skillspector/yara_rules/malware.yar.b64 b/src/skillspector/yara_rules/malware.yar.b64 index 76c9543e5..2763fda3e 100644 --- a/src/skillspector/yara_rules/malware.yar.b64 +++ b/src/skillspector/yara_rules/malware.yar.b64 @@ -81,64 +81,63 @@ ICAgICAgICBzZXZlcml0eSA9ICJDUklUSUNBTCINCiAgICAgICAgY29uZmlkZW5jZSA9ICIwLjgi DQogICAgc3RyaW5nczoNCiAgICAgICAgJHdhbGtfZW5jcnlwdCAgID0gL29zXC53YWxrXHMqXCgu KlwuKGVuY3J5cHR8Y2lwaGVyKS8NCiAgICAgICAgJHJhbnNvbV9ub3RlICAgID0gL3lvdXJccytm aWxlc1xzKyhoYXZlXHMrYmVlbnxhcmUpXHMrZW5jcnlwdGVkXGIvIG5vY2FzZQ0KICAgICAgICAk -cGF5bWVudF90aGVuX2NvbnRleHQgPSAvXGIocGF5fHNlbmQpXGJbXlxyXG5dezAsODB9XGIoYnRj -fGJpdGNvaW5zPylcYltcc1xTXXswLDE2MH1cYigodG9bIFx0XXsxLDh9KT8oZGVjcnlwdHxyZXN0 -b3JlfHJlY292ZXJ8dW5sb2NrKVsgXHRdezEsOH0oeW91cnx0aGUpP1sgXHRdezAsOH0oZmlsZXN8 -ZGF0YXxkb2N1bWVudHN8cGhvdG9zfGRhdGFiYXNlc3x0aGVtKXx0b1sgXHRdezEsOH0ocmVjZWl2 -ZXxnZXR8b2J0YWluKVsgXHRdezEsOH0odGhlfHlvdXIpP1sgXHRdezAsOH1kZWNyeXB0aW9uWyBc -dF17MSw4fWtleXxhZnRlclsgXHRdezEsOH1wYXltZW50W1xzXFNdezAsODB9KHJlY2VpdmV8Z2V0 -fG9idGFpbnx3aWxsWyBcdF17MSw4fWdldClbIFx0XXsxLDh9KHRoZXx5b3VyK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0NCn0NCg== diff --git a/tests/integration/test_ransomware_rule.py b/tests/integration/test_ransomware_rule.py index 98ea306c3..af7ed2bc0 100644 --- a/tests/integration/test_ransomware_rule.py +++ b/tests/integration/test_ransomware_rule.py @@ -124,6 +124,16 @@ True, id="minimum-amount-demand", ), + pytest.param( + "To decrypt your files you must send\n0.5 BTC to the address below.\n", + True, + id="hard-wrapped-payment", + ), + pytest.param( + "Send 0.5BTC to example.invalid to decrypt your files.\n", + True, + id="glued-amount-payment", + ), pytest.param("Your files are encrypted.\n", True, id="encrypted-files-are"), pytest.param("Your files have been encrypted.\n", True, id="encrypted-files-have-been"), pytest.param( diff --git a/tests/nodes/analyzers/test_static_yara.py b/tests/nodes/analyzers/test_static_yara.py index 2b87101e4..57b7d79dc 100644 --- a/tests/nodes/analyzers/test_static_yara.py +++ b/tests/nodes/analyzers/test_static_yara.py @@ -1741,6 +1741,62 @@ def test_conditional_and_minimum_amount_demands_remain_critical(self, content): def test_clause_opening_payment_advice_is_not_ransomware(self, content): assert not _has_rule(_run_builtin(content, "advice.md"), "ransomware_behavior") + @pytest.mark.parametrize( + "content", + [ + "To decrypt your files you must send\n0.5 BTC to the address below.", + "To decrypt your files you must send\r\n0.5 BTC to the address below.", + "Send 1 BTC to the wallet below\nto decrypt\nyour files.", + ( + "Your network has been breached and your documents are locked. To decrypt\n" + "your files you must send 0.5 BTC to the address below." + ), + ( + "Your network has been breached and your documents are locked. To decrypt your\n" + "files you must pay 0.5 bitcoin to the address below." + ), + "Pay the\nransom in Bitcoin.", + "Pay the ransom\nin Bitcoin.", + "Send 0.5BTC to example.invalid to decrypt your files.", + "To restore your files, pay 1bitcoin.", + ], + ) + def test_hard_wrapped_and_glued_amount_demands_remain_critical(self, content): + ransomware = [ + finding + for finding in _run_builtin(content, "notice.txt") + if "ransomware_behavior" in finding.message + ] + + assert len(ransomware) == 1 + assert ransomware[0].rule_id == "YR1" + assert ransomware[0].severity == "CRITICAL" + assert ransomware[0].start_line == 1 + + @pytest.mark.parametrize("filler, expected", [(79, True), (80, False)]) + def test_wrapped_payment_currency_window_remains_bounded(self, filler, expected): + content = "To decrypt your files you must send\n" + "x" * filler + " BTC." + + assert _has_rule(_run_builtin(content, "notice.txt"), "ransomware_behavior") is expected + + def test_payment_currency_allows_only_one_line_break(self): + content = "To decrypt your files you must send\n\n0.5 BTC to the address below." + + assert not _has_rule(_run_builtin(content, "notice.txt"), "ransomware_behavior") + + def test_negated_advice_does_not_own_a_later_wrapped_demand(self): + content = "Never send BTC to strangers.\nSend 1 BTC to decrypt your files.\n" + + ransomware = [ + finding + for finding in _run_builtin(content, "notice.md") + if "ransomware_behavior" in finding.message + ] + + assert len(ransomware) == 1 + assert ransomware[0].start_line == 2 + assert ransomware[0].matched_text.startswith("Send 1 BTC") + @pytest.mark.parametrize("separator", ["\n", "\r\n"]) def test_storage_notice_does_not_hide_later_encryption_notice(self, separator): content = (