diff options
author | W. Kosior <koszko@koszko.org> | 2024-12-16 19:41:24 +0100 |
---|---|---|
committer | W. Kosior <koszko@koszko.org> | 2024-12-16 19:41:24 +0100 |
commit | 973d4b6fc1232ae36b865f91204e8198233d5484 (patch) | |
tree | 98ddc1e2592ce0dafb9520bf4ca7d0aed3c15cdb /threats_by_sector_table.py | |
download | AGH-threat-intel-course-973d4b6fc1232ae36b865f91204e8198233d5484.tar.gz AGH-threat-intel-course-973d4b6fc1232ae36b865f91204e8198233d5484.zip |
Initial commit.
Diffstat (limited to 'threats_by_sector_table.py')
-rwxr-xr-x | threats_by_sector_table.py | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/threats_by_sector_table.py b/threats_by_sector_table.py new file mode 100755 index 0000000..597cf12 --- /dev/null +++ b/threats_by_sector_table.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python3 + +# SPDX-License-Identifier: CC0-1.0 +# +# Copyright (C) 2024 Wojtek Kosior <koszko@koszko.org> + +import yaml +import sys + +def read_APT_data(yaml_path): + if yaml_path: + with open(yaml_path) as inp: + return yaml.safe_load(inp) + else: + return yaml.safe_load(sys.stdin) + +def group_has_sector(group, sector): + group_sectors = group.get("sectors", []) + + if sector is None: + return not group_sectors + + for sector_obj in group_sectors: + if sector_obj["sector"] == sector: + return True + +def group_has_goal(group, goal): + group_goals = group.get("goals", []) + + if goal is None: + return not group_goals + + for goal_obj in group_goals: + if goal_obj["goal"] == goal: + return True + +dashes = ''.join('-' for _ in range(24)) + +def print_tables(APT_data, out=sys.stdout): + all_groups = APT_data["groups"] + all_goals = sorted({goal["goal"] + for group in all_groups + for goal in group.get("goals", [])}) + all_origins = sorted({group["origin"] for group in all_groups}) + all_sectors = sorted({sector["sector"] + for group in all_groups + for sector in group.get("sectors", [])}) + + separator_line = f"|{'|'.join(dashes for _ in [None] + all_origins)}|" + + first = True + + for sector in all_sectors: + if first: + first = False + else: + print("\\newpage") + + groups = [group for group in all_groups + if group_has_sector(group, sector)] + + def make_group_listing(origin, goal): + return ", ".join(group["name"] for group in groups + if (group["origin"] == origin and + group_has_goal(group, goal))) + + print(f"### {sector}") + print() + print(f"|**goal \ origin**|**{'**|**'.join(all_origins)}**|") + + for goal in all_goals: + group_listings = [make_group_listing(origin, goal) or ' ' + for origin in all_origins] + print(separator_line) + print(f"|**{goal}**|{'|'.join(group_listings)}|") + + # Groups with no documented goal (if there are such ones) + group_listings = [make_group_listing(origin, None) + for origin in all_origins] + + if any(group_listings): + print(separator_line) + print(f"|*not documented*|{'|'.join(group_listings)}|") + + print() + +if __name__ == "__main__": + print_tables(read_APT_data(None if len(sys.argv) < 2 else sys.argv[1])) |