Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backend] observables values implemented (#8312) #735

Closed
wants to merge 10 commits into from
2 changes: 1 addition & 1 deletion pycti/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
__version__ = "6.3.1"
__version__ = "6.3.5"

from .api.opencti_api_client import OpenCTIApiClient
from .api.opencti_api_connector import OpenCTIApiConnector
Expand Down
25 changes: 13 additions & 12 deletions pycti/connector/opencti_connector_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,8 @@ def send_stix2_bundle(self, bundle: str, **kwargs) -> list:
:type entities_types: list, optional
:param update: whether to updated data in the database, defaults to False
:type update: bool, optional
:param bypass_split: use to prevent splitting of the bundle. This option has been removed since 6.3 and is no longer used.
:type bypass_split: bool, optional
:raises ValueError: if the bundle is empty
:return: list of bundles
:rtype: list
Expand All @@ -1564,11 +1566,11 @@ def send_stix2_bundle(self, bundle: str, **kwargs) -> list:
entities_types = kwargs.get("entities_types", None)
update = kwargs.get("update", False)
event_version = kwargs.get("event_version", None)
bypass_split = kwargs.get("bypass_split", False)
bypass_validation = kwargs.get("bypass_validation", False)
entity_id = kwargs.get("entity_id", None)
file_name = kwargs.get("file_name", None)
bundle_send_to_queue = kwargs.get("send_to_queue", self.bundle_send_to_queue)
cleanup_inconsistent_bundle = kwargs.get("cleanup_inconsistent_bundle", False)
bundle_send_to_directory = kwargs.get(
"send_to_directory", self.bundle_send_to_directory
)
Expand Down Expand Up @@ -1690,17 +1692,16 @@ def send_stix2_bundle(self, bundle: str, **kwargs) -> list:
final_write_file = os.path.join(bundle_send_to_directory_path, bundle_file)
os.rename(write_file, final_write_file)

if bypass_split:
bundles = [bundle]
expectations_number = len(json.loads(bundle)["objects"])
else:
stix2_splitter = OpenCTIStix2Splitter()
(
expectations_number,
bundles,
) = stix2_splitter.split_bundle_with_expectations(
bundle, True, event_version
)
stix2_splitter = OpenCTIStix2Splitter()
(
expectations_number,
bundles,
) = stix2_splitter.split_bundle_with_expectations(
bundle=bundle,
use_json=True,
event_version=event_version,
cleanup_inconsistent_bundle=cleanup_inconsistent_bundle,
)

if len(bundles) == 0:
self.metric.inc("error_count")
Expand Down
8 changes: 8 additions & 0 deletions pycti/entities/indicator/opencti_indicator_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@
x_opencti_score
x_opencti_detection
x_opencti_main_observable_type
x_opencti_observables_values {
type
value
}
x_mitre_platforms
observables {
edges {
Expand Down Expand Up @@ -220,6 +224,10 @@
x_opencti_score
x_opencti_detection
x_opencti_main_observable_type
x_opencti_observables_values {
type
value
}
x_mitre_platforms
observables {
edges {
Expand Down
10 changes: 4 additions & 6 deletions pycti/entities/opencti_kill_chain_phase.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# coding: utf-8

import json
import uuid

from stix2.canonicalization.Canonicalize import canonicalize
from pycti.utils.opencti_stix2_identifier import kill_chain_phase_generate_id


class KillChainPhase:
Expand All @@ -25,10 +24,9 @@ def __init__(self, opencti):

@staticmethod
def generate_id(phase_name, kill_chain_name):
data = {"phase_name": phase_name, "kill_chain_name": kill_chain_name}
data = canonicalize(data, utf8=False)
id = str(uuid.uuid5(uuid.UUID("00abedb4-aa42-466c-9c01-fed23315a9b7"), data))
return "kill-chain-phase--" + id
return kill_chain_phase_generate_id(
phase_name=phase_name, kill_chain_name=kill_chain_name
)

"""
List Kill-Chain-Phase objects
Expand Down
13 changes: 9 additions & 4 deletions pycti/utils/opencti_stix2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2280,6 +2280,12 @@ def export_entities_list(
do_list = lister.get(
entity_type, lambda **kwargs: self.unknown_type({"type": entity_type})
)

if getAll and (orderBy is None or orderBy == "_score"):
orderBy = "created_at"
if orderMode is None:
orderMode = "desc"

# noinspection PyTypeChecker
return do_list(
search=search,
Expand Down Expand Up @@ -2619,10 +2625,9 @@ def import_bundle(
else None
)
stix2_splitter = OpenCTIStix2Splitter()
try:
bundles = stix2_splitter.split_bundle(stix_bundle, False, event_version)
except RecursionError:
bundles = [stix_bundle]
_, bundles = stix2_splitter.split_bundle_with_expectations(
stix_bundle, False, event_version
)
# Import every element in a specific order
imported_elements = []
for bundle in bundles:
Expand Down
22 changes: 22 additions & 0 deletions pycti/utils/opencti_stix2_identifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import uuid

from stix2.canonicalization.Canonicalize import canonicalize


def external_reference_generate_id(url=None, source_name=None, external_id=None):
if url is not None:
data = {"url": url}
elif source_name is not None and external_id is not None:
data = {"source_name": source_name, "external_id": external_id}
else:
return None
data = canonicalize(data, utf8=False)
id = str(uuid.uuid5(uuid.UUID("00abedb4-aa42-466c-9c01-fed23315a9b7"), data))
return "external-reference--" + id


def kill_chain_phase_generate_id(phase_name, kill_chain_name):
data = {"phase_name": phase_name, "kill_chain_name": kill_chain_name}
data = canonicalize(data, utf8=False)
id = str(uuid.uuid5(uuid.UUID("00abedb4-aa42-466c-9c01-fed23315a9b7"), data))
return "kill-chain-phase--" + id
Loading