Skip to content

Commit

Permalink
ensure navigator layer comments are easy to read (#89)
Browse files Browse the repository at this point in the history
* ensure navigator layer comments are easy to read

* refactor code

* change capability array to set

---------

Co-authored-by: Eva <[email protected]>
  • Loading branch information
emeth1 and Eva authored Feb 28, 2024
1 parent 7ece2dd commit e7a4592
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
46 changes: 26 additions & 20 deletions src/mapex/write_parsed_mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def load_attack_json(parsed_mappings):
def get_techniques_dict(mapping_objects):
techniques_dict = {}
for mapping in mapping_objects:
tehchnique_id = mapping["attack_object_id"]
technique_id = mapping["attack_object_id"]
capability_id = mapping["capability_id"]

# define metadata
Expand All @@ -380,25 +380,30 @@ def get_techniques_dict(mapping_objects):
if mapping.get("comments"):
metadata.append({"name": "comment", "value": mapping["comments"]})

if techniques_dict.get(tehchnique_id):
# add capability information to technique it is mapped to
techniques_dict[tehchnique_id]["capability_ids"].append(capability_id)
metadata_info = [{"name": "control", "value": mapping["capability_id"]}]
if techniques_dict.get(technique_id) is None:
techniques_dict[technique_id] = {
"capability_ids": {capability_id},
"metadata": [],
}

technique = techniques_dict[technique_id]

# Add Capability ID
technique["capability_ids"].add(capability_id)

# Add Metadata
metadata_info = []
if len(metadata) > 0:
metadata_info.extend(
[
{"divider": True},
{"name": "control", "value": mapping["capability_id"]},
]
)
metadata_info.extend(metadata)
metadata_info.append({"divider": True})

if "metadata" in techniques_dict[tehchnique_id]:
techniques_dict[tehchnique_id]["metadata"].extend(metadata_info)
else:
techniques_dict[tehchnique_id]["metadata"] = metadata_info
else:
# add capability information to technique it is mapped to
techniques_dict[tehchnique_id] = {"capability_ids": [capability_id]}
techniques_dict[tehchnique_id]["metadata"] = [
{"name": "control", "value": mapping["capability_id"]}
]
techniques_dict[tehchnique_id]["metadata"].extend(metadata)
techniques_dict[tehchnique_id]["metadata"].append({"divider": True})
# No need to check if metadata_info is empty
technique["metadata"].extend(metadata_info)

return techniques_dict

Expand Down Expand Up @@ -434,13 +439,14 @@ def create_layer(techniques_dict, layer_title, domain, attack_version):

related_controls_string = ""
if len(capability_ids):
related_controls_string = ", ".join(capability_ids)
# formats ids in a bulleted list
related_controls_string = "\u2022" + "\n\u2022".join(capability_ids)

layer["techniques"].append(
{
"techniqueID": technique,
"score": len(techniques_dict[technique]["capability_ids"]),
"comment": f"Related to {related_controls_string}",
"comment": f" Related to: \n {related_controls_string}",
"metadata": techniques_dict[technique].get("metadata", []),
}
)
Expand Down
8 changes: 4 additions & 4 deletions tests/expected_results/expected_results_navigator_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
{
"techniqueID": "T1137",
"score": 1,
"comment": "Related to AC-10",
"metadata": [{"name": "control", "value": "AC-10"}, {"divider": True}],
"comment": " Related to: \nAC-10",
"metadata": [],
},
{
"techniqueID": "T1137.002",
"score": 1,
"comment": "Related to AC-10",
"metadata": [{"name": "control", "value": "AC-10"}, {"divider": True}],
"comment": " Related to: \nAC-10",
"metadata": [],
},
],
"gradient": {"colors": ["#ffe766", "#ffaf66"], "minValue": 1, "maxValue": 1},
Expand Down

0 comments on commit e7a4592

Please sign in to comment.