|
| 1 | +# Standard library imports |
| 2 | +import json |
1 | 3 | import pathlib
|
2 |
| -from typing import List, Union |
| 4 | +from datetime import datetime |
| 5 | +from typing import Any, TypedDict |
3 | 6 |
|
| 7 | +# Third-party imports |
4 | 8 | from contentctl.objects.detection import Detection
|
5 |
| -from contentctl.output.attack_nav_writer import AttackNavWriter |
| 9 | + |
| 10 | + |
| 11 | +class TechniqueData(TypedDict): |
| 12 | + score: int |
| 13 | + file_paths: list[str] |
| 14 | + links: list[dict[str, str]] |
| 15 | + |
| 16 | + |
| 17 | +class LayerData(TypedDict): |
| 18 | + name: str |
| 19 | + versions: dict[str, str] |
| 20 | + domain: str |
| 21 | + description: str |
| 22 | + filters: dict[str, list[str]] |
| 23 | + sorting: int |
| 24 | + layout: dict[str, str | bool] |
| 25 | + hideDisabled: bool |
| 26 | + techniques: list[dict[str, Any]] |
| 27 | + gradient: dict[str, list[str] | int] |
| 28 | + legendItems: list[dict[str, str]] |
| 29 | + showTacticRowBackground: bool |
| 30 | + tacticRowBackground: str |
| 31 | + selectTechniquesAcrossTactics: bool |
| 32 | + selectSubtechniquesWithParent: bool |
| 33 | + selectVisibleTechniques: bool |
| 34 | + metadata: list[dict[str, str]] |
6 | 35 |
|
7 | 36 |
|
8 | 37 | class AttackNavOutput:
|
| 38 | + def __init__( |
| 39 | + self, |
| 40 | + layer_name: str = "Splunk Detection Coverage", |
| 41 | + layer_description: str = "MITRE ATT&CK coverage for Splunk detections", |
| 42 | + layer_domain: str = "enterprise-attack", |
| 43 | + ): |
| 44 | + self.layer_name = layer_name |
| 45 | + self.layer_description = layer_description |
| 46 | + self.layer_domain = layer_domain |
| 47 | + |
9 | 48 | def writeObjects(
|
10 |
| - self, detections: List[Detection], output_path: pathlib.Path |
| 49 | + self, detections: list[Detection], output_path: pathlib.Path |
11 | 50 | ) -> None:
|
12 |
| - techniques: dict[str, dict[str, Union[List[str], int]]] = {} |
| 51 | + """ |
| 52 | + Generate MITRE ATT&CK Navigator layer file from detections |
| 53 | + Args: |
| 54 | + detections: List of Detection objects |
| 55 | + output_path: Path to write the layer file |
| 56 | + """ |
| 57 | + techniques: dict[str, TechniqueData] = {} |
| 58 | + tactic_coverage: dict[str, set[str]] = {} |
13 | 59 |
|
| 60 | + # Process each detection |
14 | 61 | for detection in detections:
|
| 62 | + if not hasattr(detection.tags, "mitre_attack_id"): |
| 63 | + continue |
| 64 | + |
15 | 65 | for tactic in detection.tags.mitre_attack_id:
|
16 | 66 | if tactic not in techniques:
|
17 |
| - techniques[tactic] = {"score": 0, "file_paths": []} |
| 67 | + techniques[tactic] = {"score": 0, "file_paths": [], "links": []} |
| 68 | + tactic_coverage[tactic] = set() |
18 | 69 |
|
19 | 70 | detection_type = detection.source
|
20 |
| - detection_id = detection.id |
| 71 | + detection_id = str(detection.id) # Convert UUID to string |
| 72 | + detection_url = ( |
| 73 | + f"https://research.splunk.com/{detection_type}/{detection_id}/" |
| 74 | + ) |
| 75 | + detection_name = detection.name.replace( |
| 76 | + "_", " " |
| 77 | + ).title() # Convert to Title Case |
| 78 | + detection_info = f"{detection_name}" |
21 | 79 |
|
22 |
| - # Store all three pieces of information separately |
23 |
| - detection_info = f"{detection_type}|{detection_id}|{detection.name}" |
| 80 | + techniques[tactic]["score"] += 1 |
| 81 | + techniques[tactic]["file_paths"].append(detection_info) |
| 82 | + techniques[tactic]["links"].append( |
| 83 | + {"label": detection_name, "url": detection_url} |
| 84 | + ) |
| 85 | + tactic_coverage[tactic].add(detection_id) |
24 | 86 |
|
25 |
| - techniques[tactic]["score"] = techniques[tactic].get("score", 0) + 1 |
26 |
| - if isinstance(techniques[tactic]["file_paths"], list): |
27 |
| - techniques[tactic]["file_paths"].append(detection_info) |
| 87 | + # Create the layer file |
| 88 | + layer: LayerData = { |
| 89 | + "name": self.layer_name, |
| 90 | + "versions": { |
| 91 | + "attack": "14", # Update as needed |
| 92 | + "navigator": "5.1.0", |
| 93 | + "layer": "4.5", |
| 94 | + }, |
| 95 | + "domain": self.layer_domain, |
| 96 | + "description": self.layer_description, |
| 97 | + "filters": { |
| 98 | + "platforms": [ |
| 99 | + "Windows", |
| 100 | + "Linux", |
| 101 | + "macOS", |
| 102 | + "AWS", |
| 103 | + "GCP", |
| 104 | + "Azure", |
| 105 | + "Office 365", |
| 106 | + "SaaS", |
| 107 | + ] |
| 108 | + }, |
| 109 | + "sorting": 0, |
| 110 | + "layout": { |
| 111 | + "layout": "flat", |
| 112 | + "showName": True, |
| 113 | + "showID": False, |
| 114 | + "showAggregateScores": True, |
| 115 | + "countUnscored": True, |
| 116 | + "aggregateFunction": "average", |
| 117 | + "expandedSubtechniques": "none", |
| 118 | + }, |
| 119 | + "hideDisabled": False, |
| 120 | + "techniques": [ |
| 121 | + { |
| 122 | + "techniqueID": tid, |
| 123 | + "score": data["score"], |
| 124 | + "metadata": [ |
| 125 | + {"name": "Detection", "value": name, "divider": False} |
| 126 | + for name in data["file_paths"] |
| 127 | + ] |
| 128 | + + [ |
| 129 | + { |
| 130 | + "name": "Link", |
| 131 | + "value": f"[View Detection]({link['url']})", |
| 132 | + "divider": False, |
| 133 | + } |
| 134 | + for link in data["links"] |
| 135 | + ], |
| 136 | + "links": [ |
| 137 | + {"label": link["label"], "url": link["url"]} |
| 138 | + for link in data["links"] |
| 139 | + ], |
| 140 | + } |
| 141 | + for tid, data in techniques.items() |
| 142 | + ], |
| 143 | + "gradient": { |
| 144 | + "colors": [ |
| 145 | + "#1a365d", # Dark blue |
| 146 | + "#2c5282", # Medium blue |
| 147 | + "#4299e1", # Light blue |
| 148 | + "#48bb78", # Light green |
| 149 | + "#38a169", # Medium green |
| 150 | + "#276749", # Dark green |
| 151 | + ], |
| 152 | + "minValue": 0, |
| 153 | + "maxValue": 5, # Adjust based on your max detections per technique |
| 154 | + }, |
| 155 | + "legendItems": [ |
| 156 | + {"label": "1 Detection", "color": "#1a365d"}, |
| 157 | + {"label": "2 Detections", "color": "#4299e1"}, |
| 158 | + {"label": "3 Detections", "color": "#48bb78"}, |
| 159 | + {"label": "4+ Detections", "color": "#276749"}, |
| 160 | + ], |
| 161 | + "showTacticRowBackground": True, |
| 162 | + "tacticRowBackground": "#dddddd", |
| 163 | + "selectTechniquesAcrossTactics": True, |
| 164 | + "selectSubtechniquesWithParent": True, |
| 165 | + "selectVisibleTechniques": False, |
| 166 | + "metadata": [ |
| 167 | + {"name": "Generated", "value": datetime.now().isoformat()}, |
| 168 | + {"name": "Total Detections", "value": str(len(detections))}, |
| 169 | + {"name": "Covered Techniques", "value": str(len(techniques))}, |
| 170 | + ], |
| 171 | + } |
28 | 172 |
|
29 |
| - """ |
30 |
| - for detection in objects: |
31 |
| - if detection.tags.mitre_attack_enrichments: |
32 |
| - for mitre_attack_enrichment in detection.tags.mitre_attack_enrichments: |
33 |
| - if not mitre_attack_enrichment.mitre_attack_id in techniques: |
34 |
| - techniques[mitre_attack_enrichment.mitre_attack_id] = { |
35 |
| - 'score': 1, |
36 |
| - 'file_paths': ['https://github.com/splunk/security_content/blob/develop/detections/' + detection.getSource() + '/' + self.convertNameToFileName(detection.name)] |
37 |
| - } |
38 |
| - else: |
39 |
| - techniques[mitre_attack_enrichment.mitre_attack_id]['score'] = techniques[mitre_attack_enrichment.mitre_attack_id]['score'] + 1 |
40 |
| - techniques[mitre_attack_enrichment.mitre_attack_id]['file_paths'].append('https://github.com/splunk/security_content/blob/develop/detections/' + detection.getSource() + '/' + self.convertNameToFileName(detection.name)) |
41 |
| - """ |
42 |
| - AttackNavWriter.writeAttackNavFile(techniques, output_path / "coverage.json") |
| 173 | + # Write the layer file |
| 174 | + output_file = output_path / "coverage.json" |
| 175 | + with open(output_file, "w") as f: |
| 176 | + json.dump(layer, f, indent=2) |
| 177 | + |
| 178 | + print(f"\n✅ MITRE ATT&CK Navigator layer file written to: {output_file}") |
| 179 | + print("📊 Coverage Summary:") |
| 180 | + print(f" Total Detections: {len(detections)}") |
| 181 | + print(f" Covered Techniques: {len(techniques)}") |
| 182 | + print(f" Tactics with Coverage: {len(tactic_coverage)}") |
| 183 | + print("\n🗺️ To view the layer:") |
| 184 | + print(" 1. Go to https://mitre-attack.github.io/attack-navigator/") |
| 185 | + print(" 2. Click 'Open Existing Layer'") |
| 186 | + print(f" 3. Select the file: {output_file}") |
43 | 187 |
|
44 |
| - def convertNameToFileName(self, name: str): |
| 188 | + def convertNameToFileName(self, name: str) -> str: |
| 189 | + """Convert a detection name to a valid filename""" |
45 | 190 | file_name = (
|
46 | 191 | name.replace(" ", "_")
|
47 | 192 | .replace("-", "_")
|
48 | 193 | .replace(".", "_")
|
49 | 194 | .replace("/", "_")
|
50 | 195 | .lower()
|
51 | 196 | )
|
52 |
| - file_name = file_name + ".yml" |
53 |
| - return file_name |
| 197 | + return f"{file_name}.yml" |
0 commit comments