22import os
33import ssl
44import urllib .request
5+ from json import JSONDecodeError
56from urllib .error import HTTPError
67
78from app .translator .tools .singleton_meta import SingletonMeta
@@ -12,9 +13,11 @@ class MitreConfig(metaclass=SingletonMeta):
1213config_url :str = "https://raw.githubusercontent.com/mitre/cti/master/enterprise-attack/enterprise-attack.json"
1314mitre_source_types :tuple = ("mitre-attack" ,)
1415
15- def __init__ (self ):
16+ def __init__ (self , server : bool = False ):
1617self .tactics = {}
1718self .techniques = {}
19+ if not server :
20+ self .__load_mitre_configs_from_files ()
1821
1922@staticmethod
2023def __revoked_or_deprecated (entry :dict )-> bool :
@@ -88,20 +91,30 @@ def update_mitre_config(self) -> None: # noqa: PLR0912
8891sub_technique_id = ref ["external_id" ]
8992sub_technique_name = entry ["name" ]
9093parent_technique_name = technique_map [sub_technique_id .split ("." )[0 ]]
94+ parent_tactics = self .techniques .get (sub_technique_id .split ("." )[0 ].lower (), {}).get (
95+ "tactic" , []
96+ )
9197sub_technique_name = f"{ parent_technique_name } :{ sub_technique_name } "
9298self .techniques [ref ["external_id" ].lower ()]= {
9399"technique_id" :ref ["external_id" ],
94100"technique" :sub_technique_name ,
95101"url" :ref ["url" ],
102+ "tactic" :parent_tactics
96103 }
97104break
98105
99106def __load_mitre_configs_from_files (self )-> None :
100- with open (os .path .join (ROOT_PROJECT_PATH ,"app/dictionaries/tactics.json" ))as file :
101- self .tactics = json .load (file )
107+ try :
108+ with open (os .path .join (ROOT_PROJECT_PATH ,"app/dictionaries/tactics.json" ))as file :
109+ self .tactics = json .load (file )
110+ except JSONDecodeError :
111+ self .tactics = {}
102112
103- with open (os .path .join (ROOT_PROJECT_PATH ,"app/dictionaries/techniques.json" ))as file :
104- self .techniques = json .load (file )
113+ try :
114+ with open (os .path .join (ROOT_PROJECT_PATH ,"app/dictionaries/techniques.json" ))as file :
115+ self .techniques = json .load (file )
116+ except JSONDecodeError :
117+ self .techniques = {}
105118
106119def get_tactic (self ,tactic :str )-> dict :
107120tactic = tactic .replace ("." ,"_" )