diff --git a/src/skillspector/nodes/analyzers/static_yara.py b/src/skillspector/nodes/analyzers/static_yara.py index 5de066b1a..4a949170b 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,37 @@ _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_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 +# 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"(?:^|(?<=[\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( + 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 +669,74 @@ def _has_local_destructive_autonomy_evidence( ) +def _payment_is_directly_negated(data: bytes, payment_offset: int) -> bool: + """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) + if _DIRECT_RANSOMWARE_NEGATION.search(data, prefix_start, payment_offset) is None: + return False + return _RANSOMWARE_MINIMUM_PAYMENT.match(data, payment_offset) is None + + +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 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 + ): + 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 +857,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 84d2402fc..2763fda3e 100644 --- a/src/skillspector/yara_rules/malware.yar.b64 +++ b/src/skillspector/yara_rules/malware.yar.b64 @@ -79,38 +79,65 @@ DQogICAgICAgIGRlc2NyaXB0aW9uID0gIlJhbnNvbXdhcmUtbGlrZSBwYXR0ZXJucyAobWFzcyBl bmNyeXB0aW9uLCByYW5zb20gbm90ZXMpIg0KICAgICAgICBjYXRlZ29yeSA9ICJtYWx3YXJlIg0K ICAgICAgICBzZXZlcml0eSA9ICJDUklUSUNBTCINCiAgICAg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b3JtYXRpb24gc3RlYWxlciBwYXR0 +ZXJucyAoY3JlZGVudGlhbCBoYXJ2ZXN0aW5nLCBicm93c2VyIGRhdGEgdGhlZnQpIg0KICAgICAg +ICBjYXRlZ29yeSA9ICJtYWx3YXJlIg0KICAgICAgICBzZXZlcml0eSA9ICJISUdIIg0KICAgICAg +ICBjb25maWRlbmNlID0gIjAuNzUiDQogICAgICAgIHJlZmVyZW5jZSA9ICJodHRwczovL2dpdGh1 +Yi5jb20vTmVvMjN4MC9zaWduYXR1cmUtYmFzZSINCiAgICBzdHJpbmdzOg0KICAgICAgICAkY2hy +b21lX2xvZ2luICAgICA9IC9DaHJvbWUuKkxvZ2luXHMqRGF0YS8gbm9jYXNlDQogICAgICAgICRm +aXJlZm94X2xvZ2lucyAgID0gL2xvZ2luc1wuanNvbi4qZmlyZWZveC8gbm9jYXNlDQogICAgICAg +ICRicm93c2VyX2Nvb2tpZXMgID0gL0Nvb2tpZXMuKihjaHJvbWV8ZmlyZWZveHxlZGdlfG9wZXJh +KS8gbm9jYXNlDQogICAgICAgICR3YWxsZXRfc3RlYWwgICAgID0gL3dhbGxldFwuZGF0LyBub2Nh +c2UNCiAgICAgICAgJG1pbWlrYXR6ICAgICAgICAgPSAibWltaWthdHoiIG5vY2FzZQ0KICAgICAg +ICAkbGF6YWduZSAgICAgICAgICA9ICJsYXphZ25lIiBub2Nhc2UNCiAgICAgICAgJGNyZWRfZHVt +cF9zYW0gICAgPSAvcmVnXHMrc2F2ZVxzKy4qXFxzYW0vIG5vY2FzZQ0KICAgICAgICAkbnRkc19k +dW1wICAgICAgICA9IC9udGRzXC5kaXQvIG5vY2FzZQ0KICAgIGNvbmRpdGlvbjoNCiAgICAgICAg +YW55IG9mIHRoZW0NCn0NCg== diff --git a/tests/integration/test_ransomware_rule.py b/tests/integration/test_ransomware_rule.py new file mode 100644 index 000000000..af7ed2bc0 --- /dev/null +++ b/tests/integration/test_ransomware_rule.py @@ -0,0 +1,259 @@ +# 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 markdown_it import MarkdownIt + +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", + True, + 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( + "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( + "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( + "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"] + 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 + 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: + body = result["report_body"] + if output_format == "markdown": + body = MarkdownIt().enable("table").render(body) + assert "ransomware_behavior" in body diff --git a/tests/nodes/analyzers/test_static_yara.py b/tests/nodes/analyzers/test_static_yara.py index 9c8f6c5e3..57b7d79dc 100644 --- a/tests/nodes/analyzers/test_static_yara.py +++ b/tests/nodes/analyzers/test_static_yara.py @@ -1568,6 +1568,367 @@ def test_extra_rules_still_match_with_builtin_malware_representation(self, tmp_p assert _has_rule(findings, "reverse_shell") +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.", + "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 " + "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): + 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", + "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): + 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.", + "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): + 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("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( + "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( + "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 = ( + "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", + [ + "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_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, + "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 cryptominer rules ───────────────────────────────────────