Skip to content

Commit 86d389a

Browse files
authored
Merge pull request #1 from thelok/get_software_batches
Adding proxies and verify parameters to the attack_api.py. Update the get_software_used_by_group() method to have a batch_size
2 parents 80abb22 + 9c3b725 commit 86d389a

1 file changed

Lines changed: 26 additions & 11 deletions

File tree

attackcti/attack_api.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ class attack_client(object):
4040
TC_ICS_SOURCE = None
4141
COMPOSITE_DS = None
4242

43-
def __init__(self, local_path=None, include_pre_attack=False):
43+
def __init__(self, local_path=None, include_pre_attack=False, proxies=None, verify=True):
44+
"""
45+
Args:
46+
proxies - See https://requests.readthedocs.io/en/latest/user/advanced/#proxies
47+
verify - See https://requests.readthedocs.io/en/latest/user/advanced/#ssl-cert-verification
48+
"""
49+
4450
if local_path is not None and os.path.isdir(os.path.join(local_path, ENTERPRISE_ATTACK_LOCAL_DIR)) \
4551
and os.path.isdir(os.path.join(local_path, PRE_ATTACK_LOCAL_DIR)) \
4652
and os.path.isdir(os.path.join(local_path, MOBILE_ATTACK_LOCAL_DIR)) \
@@ -50,10 +56,10 @@ def __init__(self, local_path=None, include_pre_attack=False):
5056
self.TC_MOBILE_SOURCE = FileSystemSource(os.path.join(local_path, MOBILE_ATTACK_LOCAL_DIR))
5157
self.TC_ICS_SOURCE = FileSystemSource(os.path.join(local_path, ICS_ATTACK_LOCAL_DIR))
5258
else:
53-
ENTERPRISE_COLLECTION = Collection(ATTACK_STIX_COLLECTIONS + ENTERPRISE_ATTACK + "/")
54-
PRE_COLLECTION = Collection(ATTACK_STIX_COLLECTIONS + PRE_ATTACK + "/")
55-
MOBILE_COLLECTION = Collection(ATTACK_STIX_COLLECTIONS + MOBILE_ATTACK + "/")
56-
ICS_COLLECTION = Collection(ATTACK_STIX_COLLECTIONS + ICS_ATTACK + "/")
59+
ENTERPRISE_COLLECTION = Collection(ATTACK_STIX_COLLECTIONS + ENTERPRISE_ATTACK + "/", verify=verify, proxies=proxies)
60+
PRE_COLLECTION = Collection(ATTACK_STIX_COLLECTIONS + PRE_ATTACK + "/", verify=verify, proxies=proxies)
61+
MOBILE_COLLECTION = Collection(ATTACK_STIX_COLLECTIONS + MOBILE_ATTACK + "/", verify=verify, proxies=proxies)
62+
ICS_COLLECTION = Collection(ATTACK_STIX_COLLECTIONS + ICS_ATTACK + "/", verify=verify, proxies=proxies)
5763

5864
self.TC_ENTERPRISE_SOURCE = TAXIICollectionSource(ENTERPRISE_COLLECTION)
5965
self.TC_PRE_SOURCE = TAXIICollectionSource(PRE_COLLECTION)
@@ -1715,12 +1721,14 @@ def get_techniques_used_by_all_groups(self, stix_format=True):
17151721
groups_use_techniques = self.translate_stix_objects(groups_use_techniques)
17161722
return groups_use_techniques
17171723

1718-
def get_software_used_by_group(self, stix_object, stix_format=True):
1724+
def get_software_used_by_group(self, stix_object, stix_format=True, batch_size=10):
17191725
""" Extracts software STIX objects used by one group accross all ATT&CK matrices
17201726
17211727
Args:
17221728
stix_object (stix object) : STIX Object group to extract software from
17231729
stix_format (bool): Returns results in original STIX format or friendly syntax (e.g. 'attack-pattern' or 'technique')
1730+
batch_size (int): The batch size to use when querying the TAXII datastore. Use a lower batch size if the
1731+
URI becomes too long and you get HTTP 414 errors.
17241732
17251733
Returns:
17261734
List of STIX objects
@@ -1733,11 +1741,17 @@ def get_software_used_by_group(self, stix_object, stix_format=True):
17331741
software_relationships.append(relation)
17341742
if len(software_relationships) == 0:
17351743
return software_relationships
1736-
filter_objects = [
1737-
Filter('type', 'in', ['malware', 'tool']),
1738-
Filter('id', '=', [r.target_ref for r in software_relationships])
1739-
]
1740-
all_software = self.COMPOSITE_DS.query(filter_objects)
1744+
1745+
all_software = []
1746+
1747+
for software_relation_batch in [software_relationships[i:i+batch_size] for i in range(0, len(software_relationships), batch_size)]:
1748+
filter_objects = [
1749+
Filter('type', 'in', ['malware', 'tool']),
1750+
Filter('id', '=', [r.target_ref for r in software_relation_batch])
1751+
]
1752+
1753+
search_results = self.COMPOSITE_DS.query(filter_objects)
1754+
all_software.extend(search_results)
17411755

17421756
if not stix_format:
17431757
all_software = self.translate_stix_objects(all_software)
@@ -2028,3 +2042,4 @@ def enrich_techniques_data_sources(self, stix_object):
20282042
new_data_sources = [ v for v in technique_ds.values()]
20292043
stix_object[i] = stix_object[i].new_version(x_mitre_data_sources = new_data_sources)
20302044
return stix_object
2045+

0 commit comments

Comments
 (0)