Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit cce6ffa

Browse files
author
Samuel Hassine
committed
Add check report before inserting by name and date, fix depedency to STIX2
1 parent 7e8c66e commit cce6ffa

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

examples/stix2/import.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
config = yaml.load(open(os.path.dirname(__file__) + '/../config.yml'))
1010

1111
# File to import
12-
file_to_import = config['mitre']['repository_path_cti'] + '/enterprise-attack/enterprise-attack.json'
12+
file_to_import = config['mitre']['repository_path_cti'] + '/apt1.json'
1313

1414
# OpenCTI initialization
1515
opencti = OpenCTI(config['opencti']['api_url'], config['opencti']['api_key'], config['opencti']['log_file'], config['opencti']['verbose'])

pycti/opencti.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ def check_existing_stix_domain_entity(self, stix_id=None, name=None, type=None):
9191
object_result = self.search_stix_domain_entity_by_name(name, type)
9292
return object_result
9393

94+
def check_existing_report(self, stix_id=None, name=None, published=None):
95+
object_result = None
96+
if stix_id is not None:
97+
object_result = self.get_stix_domain_entity_by_stix_id(stix_id)
98+
if object_result is None and name is not None and published is not None:
99+
object_result = self.search_report_by_name_and_date(name, published)
100+
return object_result
101+
94102
def update_settings_field(self, id, key, value):
95103
self.log('Updating settings field ' + key + ' of ' + id + '...')
96104
query = """
@@ -259,6 +267,29 @@ def search_stix_domain_entity_by_name(self, name_or_alias, type='Stix-Domain-Ent
259267
else:
260268
return None
261269

270+
def search_reports_by_name_and_date(self, name, published):
271+
query = """
272+
query Reports($name: String, $published: DateTime) {
273+
reports(name: $name, published: $published) {
274+
edges {
275+
node {
276+
id
277+
entity_type
278+
}
279+
}
280+
}
281+
}
282+
"""
283+
result = self.query(query, {'name': name, 'published': published})
284+
return self.parse_multiple(result['data']['reports'])
285+
286+
def search_report_by_name_and_date(self, name, published):
287+
result = self.search_reports_by_name_and_date(name, published)
288+
if len(result) > 0:
289+
return result[0]
290+
else:
291+
return None
292+
262293
def update_stix_domain_entity_field(self, id, key, value):
263294
self.log('Updating field ' + key + ' of ' + id + '...')
264295
query = """
@@ -2731,7 +2762,7 @@ def create_report_if_not_exists(self,
27312762
modified=None
27322763
):
27332764
if stix_id is not None:
2734-
object_result = self.get_stix_domain_entity_by_stix_id(stix_id)
2765+
object_result = self.check_existing_report(stix_id, name, published)
27352766
else:
27362767
object_result = None
27372768
if object_result is not None:

pycti/opencti_stix2.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ def import_relationship(self, stix_relation, update=False):
949949
if date is None:
950950
date = datetime.datetime.utcnow().replace(microsecond=0, tzinfo=datetime.timezone.utc).isoformat()
951951

952-
stix_relation = self.opencti.create_relation_if_not_exists(
952+
stix_relation_result = self.opencti.create_relation_if_not_exists(
953953
source_id,
954954
source_type,
955955
target_id,
@@ -967,8 +967,9 @@ def import_relationship(self, stix_relation, update=False):
967967
stix_relation['created'] if 'created' in stix_relation else None,
968968
stix_relation['modified'] if 'modified' in stix_relation else None,
969969
)
970-
if stix_relation is not None:
971-
stix_relation_id = stix_relation['id']
970+
if stix_relation_result is not None:
971+
stix_relation_result_id = stix_relation_result['id']
972+
self.mapping_cache[stix_relation['id']] = stix_relation_result_id
972973
else:
973974
return None
974975

@@ -1040,7 +1041,7 @@ def import_relationship(self, stix_relation, update=False):
10401041
# Add refs to report
10411042
self.opencti.add_object_ref_to_report_if_not_exists(report_id, source_id)
10421043
self.opencti.add_object_ref_to_report_if_not_exists(report_id, target_id)
1043-
self.opencti.add_object_ref_to_report_if_not_exists(report_id, stix_relation_id)
1044+
self.opencti.add_object_ref_to_report_if_not_exists(report_id, stix_relation_result_id)
10441045

10451046
def resolve_author(self, title):
10461047
if 'fireeye' in title.lower() or 'mandiant' in title.lower():

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
print("warning: pypandoc module not found, could not convert Markdown to RST")
1313
read_md = lambda f: open(f, 'r').read()
1414

15-
VERSION = "1.2.4"
15+
VERSION = "1.2.5"
1616

1717
class VerifyVersionCommand(install):
1818
description = 'verify that the git tag matches our version'
@@ -50,7 +50,7 @@ def run(self):
5050
'Topic :: Software Development :: Libraries :: Python Modules'
5151
],
5252
include_package_data=True,
53-
install_requires=['requests', 'PyYAML', 'python-dateutil', 'datefinder'],
53+
install_requires=['requests', 'PyYAML', 'python-dateutil', 'datefinder', 'stix2', 'pytz'],
5454
cmdclass={
5555
'verify': VerifyVersionCommand,
5656
}

0 commit comments

Comments
 (0)