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

Commit 8d36620

Browse files
author
Samuel Hassine
committed
[client] Handle creation of relation between observables, handle avoid spliting if needed
1 parent 9403892 commit 8d36620

File tree

6 files changed

+37
-34
lines changed

6 files changed

+37
-34
lines changed

examples/link_hashes_together.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# coding: utf-8
22

3-
import datetime
43
from pycti import OpenCTIApiClient
54

65
# Variables

pycti/connector/opencti_connector_helper.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,25 @@ def date_now(self):
166166
return datetime.datetime.utcnow().replace(microsecond=0, tzinfo=datetime.timezone.utc).isoformat()
167167

168168
# Push Stix2 helper
169-
def send_stix2_bundle(self, bundle, entities_types=None, update=False, confidence_level=1):
169+
def send_stix2_bundle(self, bundle, entities_types=None, update=False, split=True):
170170
if entities_types is None:
171171
entities_types = []
172-
bundles = self.split_stix2_bundle(bundle)
173-
if len(bundles) == 0:
174-
raise ValueError('Nothing to import')
175-
pika_connection = pika.BlockingConnection(pika.URLParameters(self.config['uri']))
176-
channel = pika_connection.channel()
177-
for bundle in bundles:
178-
self._send_bundle(channel, bundle, entities_types, update, confidence_level)
172+
if split:
173+
bundles = self.split_stix2_bundle(bundle)
174+
if len(bundles) == 0:
175+
raise ValueError('Nothing to import')
176+
pika_connection = pika.BlockingConnection(pika.URLParameters(self.config['uri']))
177+
channel = pika_connection.channel()
178+
for bundle in bundles:
179+
self._send_bundle(channel, bundle, entities_types, update)
180+
else:
181+
pika_connection = pika.BlockingConnection(pika.URLParameters(self.config['uri']))
182+
channel = pika_connection.channel()
183+
self._send_bundle(channel, bundle, entities_types, update)
179184
channel.close()
180-
return bundles
185+
return True
181186

182-
def _send_bundle(self, channel, bundle, entities_types=None, update=False, confidence_level=1):
187+
def _send_bundle(self, channel, bundle, entities_types=None, update=False):
183188
"""
184189
This method send a STIX2 bundle to RabbitMQ to be consumed by workers
185190
:param bundle: A valid STIX2 bundle
@@ -206,7 +211,6 @@ def _send_bundle(self, channel, bundle, entities_types=None, update=False, confi
206211
'job_id': job_id,
207212
'entities_types': entities_types,
208213
'update': update,
209-
'confidence_level': confidence_level,
210214
'content': base64.b64encode(bundle.encode('utf-8')).decode('utf-8')
211215
}
212216

pycti/entities/opencti_stix_observable_relation.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@ def list(self, **kwargs):
121121
after = kwargs.get('after', None)
122122
order_by = kwargs.get('orderBy', None)
123123
order_mode = kwargs.get('orderMode', None)
124-
self.opencti.log('info', 'Listing stix_observable_relations with {from_id: ' + str(from_id) + ', to_id: ' + str(
125-
to_id) + '}')
124+
self.opencti.log('info',
125+
'Listing stix_observable_relations with {type: ' + relation_type + ', from_id: ' + str(
126+
from_id) + ', to_id: ' + str(to_id) + '}')
126127
query = """
127128
query StixObservableRelations($fromId: String, $fromTypes: [String], $toId: String, $toTypes: [String], $relationType: String, $firstSeenStart: DateTime, $firstSeenStop: DateTime, $lastSeenStart: DateTime, $lastSeenStop: DateTime, $inferred: Boolean, $first: Int, $after: ID, $orderBy: StixObservableRelationsOrdering, $orderMode: OrderingMode) {
128129
stixObservableRelations(fromId: $fromId, fromTypes: $fromTypes, toId: $toId, toTypes: $toTypes, relationType: $relationType, firstSeenStart: $firstSeenStart, firstSeenStop: $firstSeenStop, lastSeenStart: $lastSeenStart, lastSeenStop: $lastSeenStop, inferred: $inferred, first: $first, after: $after, orderBy: $orderBy, orderMode: $orderMode) {

pycti/entities/opencti_stix_relation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ def list(self, **kwargs):
145145
order_by = kwargs.get('orderBy', None)
146146
order_mode = kwargs.get('orderMode', None)
147147
self.opencti.log('info',
148-
'Listing stix_relations with {from_id: ' + str(from_id) + ', to_id: ' + str(to_id) + '}')
148+
'Listing stix_relations with {type: ' + relation_type + ', from_id: ' + str(
149+
from_id) + ', to_id: ' + str(to_id) + '}')
149150
query = """
150151
query StixRelations($fromId: String, $fromTypes: [String], $toId: String, $toTypes: [String], $relationType: String, $firstSeenStart: DateTime, $firstSeenStop: DateTime, $lastSeenStart: DateTime, $lastSeenStop: DateTime, $inferred: Boolean, $first: Int, $after: ID, $orderBy: StixRelationsOrdering, $orderMode: OrderingMode) {
151152
stixRelations(fromId: $fromId, fromTypes: $fromTypes, toId: $toId, toTypes: $toTypes, relationType: $relationType, firstSeenStart: $firstSeenStart, firstSeenStop: $firstSeenStop, lastSeenStart: $lastSeenStart, lastSeenStop: $lastSeenStop, inferred: $inferred, first: $first, after: $after, orderBy: $orderBy, orderMode: $orderMode) {

pycti/utils/opencti_stix2.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,7 @@ def import_object(self, stix_object, update=False, types=None):
332332

333333
# Add embedded relationships
334334
if stix_object_result is not None:
335-
if stix_object['type'] == 'indicator':
336-
stix_object_result_type = 'observable'
337-
else:
338-
stix_object_result_type = stix_object_result['entity_type']
339-
self.mapping_cache[stix_object['id']] = {'id': stix_object_result['id'], 'type': stix_object_result_type}
335+
self.mapping_cache[stix_object['id']] = {'id': stix_object_result['id'], 'type': stix_object_result['entity_type']}
340336

341337
# Update created by ref
342338
if created_by_ref_id is not None and stix_object['type'] != 'marking-definition':
@@ -1205,20 +1201,22 @@ def create_course_of_action(self, stix_object, update=False):
12051201
)
12061202

12071203
def create_report(self, stix_object, update=False):
1208-
return self.opencti.create_report_if_not_exists(
1209-
stix_object['name'],
1210-
self.convert_markdown(stix_object['description']) if 'description' in stix_object else '',
1211-
stix_object['published'] if 'published' in stix_object else '',
1212-
stix_object[
1204+
return self.opencti.report.create(
1205+
name=stix_object['name'],
1206+
description=self.convert_markdown(stix_object['description']) if 'description' in stix_object else '',
1207+
published=stix_object['published'] if 'published' in stix_object else '',
1208+
report_class=stix_object[
12131209
CustomProperties.REPORT_CLASS] if CustomProperties.REPORT_CLASS in stix_object else 'Threat Report',
1214-
stix_object[CustomProperties.OBJECT_STATUS] if CustomProperties.OBJECT_STATUS in stix_object else 0,
1215-
stix_object[CustomProperties.SRC_CONF_LEVEL] if CustomProperties.SRC_CONF_LEVEL in stix_object else 3,
1216-
stix_object[CustomProperties.GRAPH_DATA] if CustomProperties.GRAPH_DATA in stix_object else '',
1217-
stix_object[CustomProperties.ID] if CustomProperties.ID in stix_object else None,
1218-
stix_object['id'] if 'id' in stix_object else None,
1219-
stix_object['created'] if 'created' in stix_object else None,
1220-
stix_object['modified'] if 'modified' in stix_object else None,
1221-
update
1210+
object_status=stix_object[
1211+
CustomProperties.OBJECT_STATUS] if CustomProperties.OBJECT_STATUS in stix_object else 0,
1212+
source_confidence_level=stix_object[
1213+
CustomProperties.SRC_CONF_LEVEL] if CustomProperties.SRC_CONF_LEVEL in stix_object else 1,
1214+
graph_data=stix_object[CustomProperties.GRAPH_DATA] if CustomProperties.GRAPH_DATA in stix_object else '',
1215+
id=stix_object[CustomProperties.ID] if CustomProperties.ID in stix_object else None,
1216+
stix_id_key=stix_object['id'] if 'id' in stix_object else None,
1217+
created=stix_object['created'] if 'created' in stix_object else None,
1218+
modified=stix_object['modified'] if 'modified' in stix_object else None,
1219+
update=update
12221220
)
12231221

12241222
def export_stix_observable(self, entity):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
print("warning: pypandoc module not found, could not convert Markdown to RST")
1414
read_md = lambda f: open(f, 'r').read()
1515

16-
VERSION = "2.1.2"
16+
VERSION = "2.1.3"
1717

1818

1919
class VerifyVersionCommand(install):

0 commit comments

Comments
 (0)