Repository navigation
Expand file tree
/
Copy pathtest_stack_emit.py
More file actions
206 lines (184 loc) · 8.46 KB
/
Copy pathtest_stack_emit.py
File metadata and controls
206 lines (184 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.
"""Tests for stack emit epochs and version-lock channel resolution."""
import unittest
from typing import Any
from unittest import mock
from semver import Version
from detection_rules.stack_emit import (
MITRE_ATTACK_TRANSFORM_ID,
EmitContext,
EmitTransform,
apply_emit_transforms,
emit_epoch_key,
resolve_stack_emit_entry,
rewrite_tactic_tags,
transforms_for_stack,
)
from detection_rules.version_lock import VersionLockFile
class TestStackEmitHelpers(unittest.TestCase):
"""Unit tests for emit epoch helpers."""
def test_epoch_follows_newest_applicable_transform(self) -> None:
"""9.5 is the ATT&CK promotion epoch; 9.6 is a new epoch once mitre_atlas is registered."""
self.assertEqual(emit_epoch_key(Version(9, 5, 0)), "9.5")
self.assertEqual(emit_epoch_key(Version(9, 6, 0)), "9.6")
self.assertIsNone(emit_epoch_key(Version(9, 4, 0)))
def test_transforms_empty_below_min_stack(self) -> None:
"""No transforms apply below 9.5."""
self.assertEqual(transforms_for_stack(Version(9, 4, 0)), [])
self.assertGreater(len(transforms_for_stack(Version(9, 5, 0))), 0)
ids_9_5 = {t.id for t in transforms_for_stack(Version(9, 5, 0))}
ids_9_6 = {t.id for t in transforms_for_stack(Version(9, 6, 0))}
self.assertNotIn("mitre_atlas", ids_9_5)
self.assertIn("mitre_atlas", ids_9_6)
def test_resolve_inherits_highest_applicable_epoch(self) -> None:
"""Package 9.6 reuses 9.5 until a 9.6 epoch is locked; then prefers 9.6."""
stack_emit = {
"9.5": {"sha256": "a" * 64, "version": 13, "transforms": [MITRE_ATTACK_TRANSFORM_ID]},
}
entry = resolve_stack_emit_entry(stack_emit, Version(9, 6, 0))
assert entry is not None
self.assertEqual(entry["version"], 13)
self.assertIsNone(resolve_stack_emit_entry(stack_emit, Version(9, 4, 0)))
stack_emit["9.6"] = {
"sha256": "b" * 64,
"version": 14,
"transforms": [MITRE_ATTACK_TRANSFORM_ID, "related_integrations_gte", "mitre_atlas"],
}
entry_96 = resolve_stack_emit_entry(stack_emit, Version(9, 6, 0))
assert entry_96 is not None
self.assertEqual(entry_96["version"], 14)
entry_95 = resolve_stack_emit_entry(stack_emit, Version(9, 5, 0))
assert entry_95 is not None
self.assertEqual(entry_95["version"], 13)
def test_rewrite_tactic_tags_swaps_defense_evasion(self) -> None:
"""Obsolete baseline tactic tags are dropped; emitted tactic tags are added."""
baseline = [
{
"tactic": {
"id": "TA0005",
"name": "Defense Evasion",
"reference": "https://attack.mitre.org/tactics/TA0005/",
}
}
]
emitted = [
{
"tactic": {
"id": "TA0040",
"name": "Impact",
"reference": "https://attack.mitre.org/tactics/TA0040/",
}
}
]
tags = ["Domain: Endpoint", "Tactic: Defense Evasion", "Data Source: foo"]
rewritten = rewrite_tactic_tags(tags, baseline, emitted)
self.assertIn("Domain: Endpoint", rewritten)
self.assertIn("Data Source: foo", rewritten)
self.assertNotIn("Tactic: Defense Evasion", rewritten)
self.assertIn("Tactic: Impact", rewritten)
def test_atlas_threat_stripped_below_9_6(self) -> None:
"""ATLAS threat entries must not ship on 8.19 / 9.4 / 9.5 release stacks."""
atlas_entry = {
"framework": "MITRE ATLAS",
"tactic": {
"id": "AML.TA0009",
"name": "Collection",
"reference": "https://atlas.mitre.org/tactics/AML.TA0009/",
},
"technique": [],
}
attack_entry = {
"framework": "MITRE ATT&CK",
"tactic": {
"id": "TA0009",
"name": "Collection",
"reference": "https://attack.mitre.org/tactics/TA0009/",
},
"technique": [],
}
mappings = [{"framework": "MITRE ATLAS", "version": "2026.08", "threat": [atlas_entry]}]
for stack in ("8.19.0", "9.4.0", "9.5.0"):
obj: dict[str, Any] = {"threat": [attack_entry, atlas_entry]}
apply_emit_transforms(obj, stack=stack, context=EmitContext(threat_mappings=mappings))
frameworks = [e["framework"] for e in obj["threat"]]
self.assertIn("MITRE ATT&CK", frameworks, stack)
self.assertNotIn("MITRE ATLAS", frameworks, stack)
def test_missing_threat_stays_absent_below_9_6(self) -> None:
"""A payload that omitted threat must not gain an empty threat array."""
obj: dict[str, Any] = {"name": "no-threat"}
apply_emit_transforms(obj, stack="9.5.0")
self.assertNotIn("threat", obj)
def test_atlas_threat_mappings_appended_on_9_6(self) -> None:
"""ATLAS threat_mappings are appended to threat on 9.6+."""
attack_entry = {
"framework": "MITRE ATT&CK",
"tactic": {
"id": "TA0009",
"name": "Collection",
"reference": "https://attack.mitre.org/tactics/TA0009/",
},
"technique": [],
}
atlas_entry = {
"framework": "MITRE ATLAS",
"tactic": {
"id": "AML.TA0009",
"name": "Collection",
"reference": "https://atlas.mitre.org/tactics/AML.TA0009/",
},
"technique": [],
}
mappings = [{"framework": "MITRE ATLAS", "version": "2026.08", "threat": [atlas_entry]}]
obj: dict[str, Any] = {"threat": [attack_entry]}
apply_emit_transforms(obj, stack="9.5.0", context=EmitContext(threat_mappings=mappings))
self.assertEqual([e["framework"] for e in obj["threat"]], ["MITRE ATT&CK"])
obj = {"threat": [attack_entry]}
apply_emit_transforms(obj, stack="9.6.0", context=EmitContext(threat_mappings=mappings))
self.assertEqual([e["framework"] for e in obj["threat"]], ["MITRE ATT&CK", "MITRE ATLAS"])
def test_apply_emit_transforms_runs_registry_hooks(self) -> None:
"""apply_emit_transforms invokes each applicable transform's apply fn."""
calls: list[str] = []
def _mark(obj: dict[str, Any], stack: Version, context: EmitContext) -> None:
_ = stack, context
calls.append("ran")
obj["marked"] = True
synthetic = EmitTransform(
id="test_marker",
min_stack=Version(9, 9, 0),
affects=("marked",),
apply=_mark,
)
with mock.patch("detection_rules.stack_emit.EMIT_TRANSFORMS", (synthetic,)):
obj: dict[str, Any] = {}
apply_emit_transforms(obj, stack="9.8.0")
self.assertEqual(calls, [])
self.assertNotIn("marked", obj)
apply_emit_transforms(obj, stack="9.9.0")
self.assertEqual(calls, ["ran"])
self.assertTrue(obj["marked"])
class TestVersionLockStackEmitSchema(unittest.TestCase):
"""Validate version.lock entries that include stack_emit."""
def test_stack_emit_round_trip(self) -> None:
"""stack_emit is accepted and preserved on VersionLockFile entries."""
contents: dict[str, Any] = {
"33f306e8-417c-411b-965c-c2812d6d3f4d": {
"rule_name": "Remote File Download via PowerShell",
"sha256": "8679cd72bf85b67dde3dcfdaba749ed1fa6560bca5efd03ed41c76a500ce31d6",
"type": "eql",
"version": 4,
"stack_emit": {
"9.5": {
"sha256": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"version": 5,
"transforms": [MITRE_ATTACK_TRANSFORM_ID, "related_integrations_gte"],
}
},
}
}
lock = VersionLockFile.from_dict({"data": contents})
entry = lock["33f306e8-417c-411b-965c-c2812d6d3f4d"]
self.assertIsNotNone(entry.stack_emit)
self.assertEqual(entry.to_dict()["stack_emit"]["9.5"]["version"], 5)