|
1 | 1 | import copy |
2 | 2 |
|
3 | 3 | import pandas as pd |
4 | | -from stix2 import Filter |
| 4 | +from stix2 import Filter, MemoryStore |
5 | 5 | from itertools import chain |
6 | 6 | from tqdm import tqdm |
7 | 7 | import datetime |
@@ -112,13 +112,17 @@ def techniquesToDf(src, domain): |
112 | 112 | techniques = remove_revoked_deprecated(techniques) |
113 | 113 | technique_rows = [] |
114 | 114 |
|
| 115 | + all_sub_techniques = src.query([ |
| 116 | + Filter("type", "=", "relationship"), |
| 117 | + Filter("relationship_type", "=", "subtechnique-of"), |
| 118 | + ]) |
| 119 | + all_sub_techniques = MemoryStore(stix_data=all_sub_techniques) |
| 120 | + |
115 | 121 | for technique in tqdm(techniques, desc="parsing techniques"): |
116 | 122 | # get parent technique if sub-technique |
117 | 123 | subtechnique = "x_mitre_is_subtechnique" in technique and technique["x_mitre_is_subtechnique"] |
118 | 124 | if subtechnique: |
119 | | - subtechnique_of = src.query([ |
120 | | - Filter("type", "=", "relationship"), |
121 | | - Filter("relationship_type", "=", "subtechnique-of"), |
| 125 | + subtechnique_of = all_sub_techniques.query([ |
122 | 126 | Filter("source_ref", "=", technique["id"]) |
123 | 127 | ])[0] |
124 | 128 | parent = src.get(subtechnique_of["target_ref"]) |
@@ -461,12 +465,16 @@ def build_technique_and_sub_columns(src, techniques, columns, merge_data_handle, |
461 | 465 | techniques_column = [] |
462 | 466 | subtechniques_column = [] |
463 | 467 |
|
| 468 | + all_sub_techniques = src.query([ |
| 469 | + Filter("type", "=", "relationship"), |
| 470 | + Filter("relationship_type", "=", "subtechnique-of"), |
| 471 | + ]) |
| 472 | + all_sub_techniques = MemoryStore(stix_data=all_sub_techniques) |
| 473 | + |
464 | 474 | for technique in techniques: |
465 | 475 | techniques_column.append(technique["name"]) |
466 | 476 | # sub-technique relationships |
467 | | - subtechnique_ofs = src.query([ |
468 | | - Filter("type", "=", "relationship"), |
469 | | - Filter("relationship_type", "=", "subtechnique-of"), |
| 477 | + subtechnique_ofs = all_sub_techniques.query([ |
470 | 478 | Filter("target_ref", "=", technique["id"]) |
471 | 479 | ]) |
472 | 480 | if len(subtechnique_ofs) > 0: # if there are sub-techniques on the tactic |
|
0 commit comments