2018-11-28 22:51:00 +01:00
|
|
|
"""Python STIX2 Environment API."""
|
2017-07-18 18:05:19 +02:00
|
|
|
import copy
|
|
|
|
|
2018-03-01 17:27:37 +01:00
|
|
|
from .datastore import CompositeDataSource, DataStoreMixin
|
2021-02-16 06:58:33 +01:00
|
|
|
from .equivalence.graph import graph_equivalence, graph_similarity
|
2021-02-18 16:20:42 +01:00
|
|
|
from .equivalence.object import object_equivalence, object_similarity
|
2020-03-27 10:53:39 +01:00
|
|
|
from .parsing import parse as _parse
|
2017-07-12 17:36:15 +02:00
|
|
|
|
2017-07-17 21:21:49 +02:00
|
|
|
|
2017-09-06 22:20:16 +02:00
|
|
|
class ObjectFactory(object):
|
|
|
|
"""Easily create STIX objects with default values for certain properties.
|
2017-07-17 21:21:49 +02:00
|
|
|
|
|
|
|
Args:
|
2017-09-08 17:15:10 +02:00
|
|
|
created_by_ref (optional): Default created_by_ref value to apply to all
|
2017-07-17 21:21:49 +02:00
|
|
|
objects created by this factory.
|
2017-09-08 17:15:10 +02:00
|
|
|
created (optional): Default created value to apply to all
|
2017-07-17 21:21:49 +02:00
|
|
|
objects created by this factory.
|
2017-09-08 17:15:10 +02:00
|
|
|
external_references (optional): Default `external_references` value to apply
|
2017-07-17 21:21:49 +02:00
|
|
|
to all objects created by this factory.
|
2017-09-08 17:15:10 +02:00
|
|
|
object_marking_refs (optional): Default `object_marking_refs` value to apply
|
2017-07-17 21:21:49 +02:00
|
|
|
to all objects created by this factory.
|
2017-09-08 17:15:10 +02:00
|
|
|
list_append (bool, optional): When a default is set for a list property like
|
2017-07-17 21:21:49 +02:00
|
|
|
`external_references` or `object_marking_refs` and a value for
|
|
|
|
that property is passed into `create()`, if this is set to True,
|
|
|
|
that value will be added to the list alongside the default. If
|
|
|
|
this is set to False, the passed in value will replace the
|
|
|
|
default. Defaults to True.
|
|
|
|
"""
|
2017-07-12 17:36:15 +02:00
|
|
|
|
2018-07-13 17:10:05 +02:00
|
|
|
def __init__(
|
|
|
|
self, created_by_ref=None, created=None,
|
|
|
|
external_references=None, object_marking_refs=None,
|
|
|
|
list_append=True,
|
|
|
|
):
|
2017-07-13 17:03:31 +02:00
|
|
|
|
|
|
|
self._defaults = {}
|
|
|
|
if created_by_ref:
|
2018-03-19 18:32:02 +01:00
|
|
|
self.set_default_creator(created_by_ref)
|
2017-07-13 17:03:31 +02:00
|
|
|
if created:
|
2018-03-19 18:32:02 +01:00
|
|
|
self.set_default_created(created)
|
2017-07-13 17:03:31 +02:00
|
|
|
if external_references:
|
2018-03-19 18:32:02 +01:00
|
|
|
self.set_default_external_refs(external_references)
|
2017-07-13 17:03:31 +02:00
|
|
|
if object_marking_refs:
|
2018-03-19 18:32:02 +01:00
|
|
|
self.set_default_object_marking_refs(object_marking_refs)
|
2017-07-17 20:56:13 +02:00
|
|
|
self._list_append = list_append
|
|
|
|
self._list_properties = ['external_references', 'object_marking_refs']
|
2017-07-12 17:36:15 +02:00
|
|
|
|
2018-03-19 18:32:02 +01:00
|
|
|
def set_default_creator(self, creator=None):
|
|
|
|
"""Set default value for the `created_by_ref` property.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self._defaults['created_by_ref'] = creator
|
|
|
|
|
|
|
|
def set_default_created(self, created=None):
|
|
|
|
"""Set default value for the `created` property.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self._defaults['created'] = created
|
|
|
|
# If the user provides a default "created" time, we also want to use
|
|
|
|
# that as the modified time.
|
|
|
|
self._defaults['modified'] = created
|
|
|
|
|
|
|
|
def set_default_external_refs(self, external_references=None):
|
|
|
|
"""Set default external references.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self._defaults['external_references'] = external_references
|
|
|
|
|
|
|
|
def set_default_object_marking_refs(self, object_marking_refs=None):
|
|
|
|
"""Set default object markings.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self._defaults['object_marking_refs'] = object_marking_refs
|
|
|
|
|
2017-07-12 21:22:50 +02:00
|
|
|
def create(self, cls, **kwargs):
|
2017-09-08 17:15:10 +02:00
|
|
|
"""Create a STIX object using object factory defaults.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
cls: the python-stix2 class of the object to be created (eg. Indicator)
|
|
|
|
**kwargs: The property/value pairs of the STIX object to be created
|
|
|
|
"""
|
|
|
|
|
2017-07-13 17:03:31 +02:00
|
|
|
# Use self.defaults as the base, but update with any explicit args
|
|
|
|
# provided by the user.
|
2017-07-18 18:05:19 +02:00
|
|
|
properties = copy.deepcopy(self._defaults)
|
2017-07-13 17:03:31 +02:00
|
|
|
if kwargs:
|
2017-07-17 20:56:13 +02:00
|
|
|
if self._list_append:
|
|
|
|
# Append provided items to list properties instead of replacing them
|
|
|
|
for list_prop in set(self._list_properties).intersection(kwargs.keys(), properties.keys()):
|
|
|
|
kwarg_prop = kwargs.pop(list_prop)
|
|
|
|
if kwarg_prop is None:
|
|
|
|
del properties[list_prop]
|
|
|
|
continue
|
|
|
|
if not isinstance(properties[list_prop], list):
|
|
|
|
properties[list_prop] = [properties[list_prop]]
|
2017-07-18 18:05:19 +02:00
|
|
|
|
|
|
|
if isinstance(kwarg_prop, list):
|
|
|
|
properties[list_prop].extend(kwarg_prop)
|
|
|
|
else:
|
|
|
|
properties[list_prop].append(kwarg_prop)
|
2017-07-17 20:56:13 +02:00
|
|
|
|
2017-07-13 17:03:31 +02:00
|
|
|
properties.update(**kwargs)
|
2017-07-12 17:36:15 +02:00
|
|
|
|
2017-07-13 17:03:31 +02:00
|
|
|
return cls(**properties)
|
2017-09-06 22:20:16 +02:00
|
|
|
|
|
|
|
|
2018-03-01 17:27:37 +01:00
|
|
|
class Environment(DataStoreMixin):
|
2017-11-09 21:42:59 +01:00
|
|
|
"""Abstract away some of the nasty details of working with STIX content.
|
2017-09-06 22:20:16 +02:00
|
|
|
|
|
|
|
Args:
|
2017-09-08 17:15:10 +02:00
|
|
|
factory (ObjectFactory, optional): Factory for creating objects with common
|
2017-09-06 22:20:16 +02:00
|
|
|
defaults for certain properties.
|
2017-09-08 17:15:10 +02:00
|
|
|
store (DataStore, optional): Data store providing the source and sink for the
|
2017-09-06 22:20:16 +02:00
|
|
|
environment.
|
2017-09-08 17:15:10 +02:00
|
|
|
source (DataSource, optional): Source for retrieving STIX objects.
|
|
|
|
sink (DataSink, optional): Destination for saving STIX objects.
|
2017-09-06 22:20:16 +02:00
|
|
|
Invalid if `store` is also provided.
|
2018-03-01 17:27:37 +01:00
|
|
|
|
|
|
|
.. automethod:: get
|
|
|
|
.. automethod:: all_versions
|
|
|
|
.. automethod:: query
|
|
|
|
.. automethod:: creator_of
|
|
|
|
.. automethod:: relationships
|
|
|
|
.. automethod:: related_to
|
|
|
|
.. automethod:: add
|
2018-03-16 20:41:08 +01:00
|
|
|
|
2017-09-06 22:20:16 +02:00
|
|
|
"""
|
|
|
|
|
2017-09-08 15:01:12 +02:00
|
|
|
def __init__(self, factory=ObjectFactory(), store=None, source=None, sink=None):
|
2017-09-06 22:20:16 +02:00
|
|
|
self.factory = factory
|
|
|
|
self.source = CompositeDataSource()
|
|
|
|
if store:
|
|
|
|
self.source.add_data_source(store.source)
|
|
|
|
self.sink = store.sink
|
|
|
|
if source:
|
|
|
|
self.source.add_data_source(source)
|
|
|
|
if sink:
|
|
|
|
if store:
|
|
|
|
raise ValueError("Data store already provided! Environment may only have one data sink.")
|
|
|
|
self.sink = sink
|
|
|
|
|
2017-09-08 15:01:12 +02:00
|
|
|
def create(self, *args, **kwargs):
|
|
|
|
return self.factory.create(*args, **kwargs)
|
2017-09-08 17:15:10 +02:00
|
|
|
create.__doc__ = ObjectFactory.create.__doc__
|
2017-09-06 22:20:16 +02:00
|
|
|
|
2018-03-19 18:32:02 +01:00
|
|
|
def set_default_creator(self, *args, **kwargs):
|
|
|
|
return self.factory.set_default_creator(*args, **kwargs)
|
|
|
|
set_default_creator.__doc__ = ObjectFactory.set_default_creator.__doc__
|
|
|
|
|
|
|
|
def set_default_created(self, *args, **kwargs):
|
|
|
|
return self.factory.set_default_created(*args, **kwargs)
|
|
|
|
set_default_created.__doc__ = ObjectFactory.set_default_created.__doc__
|
|
|
|
|
|
|
|
def set_default_external_refs(self, *args, **kwargs):
|
|
|
|
return self.factory.set_default_external_refs(*args, **kwargs)
|
|
|
|
set_default_external_refs.__doc__ = ObjectFactory.set_default_external_refs.__doc__
|
|
|
|
|
|
|
|
def set_default_object_marking_refs(self, *args, **kwargs):
|
|
|
|
return self.factory.set_default_object_marking_refs(*args, **kwargs)
|
|
|
|
set_default_object_marking_refs.__doc__ = ObjectFactory.set_default_object_marking_refs.__doc__
|
2017-09-08 15:01:12 +02:00
|
|
|
|
|
|
|
def add_filters(self, *args, **kwargs):
|
2018-04-12 18:03:07 +02:00
|
|
|
return self.source.filters.add(*args, **kwargs)
|
2017-09-29 17:24:19 +02:00
|
|
|
|
|
|
|
def add_filter(self, *args, **kwargs):
|
2018-03-19 22:41:16 +01:00
|
|
|
return self.source.filters.add(*args, **kwargs)
|
2017-09-08 15:01:12 +02:00
|
|
|
|
2017-09-08 18:39:36 +02:00
|
|
|
def parse(self, *args, **kwargs):
|
|
|
|
return _parse(*args, **kwargs)
|
|
|
|
parse.__doc__ = _parse.__doc__
|
2017-11-02 12:48:37 +01:00
|
|
|
|
|
|
|
def creator_of(self, obj):
|
|
|
|
"""Retrieve the Identity refered to by the object's `created_by_ref`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
obj: The STIX object whose `created_by_ref` property will be looked
|
|
|
|
up.
|
|
|
|
|
|
|
|
Returns:
|
2018-11-28 22:51:00 +01:00
|
|
|
str: The STIX object's creator, or None, if the object contains no
|
|
|
|
`created_by_ref` property or the object's creator cannot be
|
|
|
|
found.
|
2017-11-02 12:48:37 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
creator_id = obj.get('created_by_ref', '')
|
|
|
|
if creator_id:
|
|
|
|
return self.get(creator_id)
|
|
|
|
else:
|
|
|
|
return None
|
2019-07-26 22:01:45 +02:00
|
|
|
|
2019-09-10 21:04:07 +02:00
|
|
|
@staticmethod
|
2021-03-01 18:27:52 +01:00
|
|
|
def object_similarity(
|
|
|
|
obj1, obj2, prop_scores={}, ds1=None, ds2=None,
|
|
|
|
ignore_spec_version=False, versioning_checks=False,
|
|
|
|
max_depth=1, **weight_dict
|
|
|
|
):
|
2021-02-18 15:14:47 +01:00
|
|
|
"""This method returns a measure of how similar the two objects are.
|
2019-07-26 22:01:45 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
obj1: A stix2 object instance
|
|
|
|
obj2: A stix2 object instance
|
2020-07-01 04:32:04 +02:00
|
|
|
prop_scores: A dictionary that can hold individual property scores,
|
|
|
|
weights, contributing score, matching score and sum of weights.
|
2021-03-10 15:52:15 +01:00
|
|
|
ds1 (optional): A DataStore object instance from which to pull related objects
|
|
|
|
ds2 (optional): A DataStore object instance from which to pull related objects
|
2021-03-01 18:27:52 +01:00
|
|
|
ignore_spec_version: A boolean indicating whether to test object types
|
|
|
|
that belong to different spec versions (STIX 2.0 and STIX 2.1 for example).
|
|
|
|
If set to True this check will be skipped.
|
|
|
|
versioning_checks: A boolean indicating whether to test multiple revisions
|
|
|
|
of the same object (when present) to maximize similarity against a
|
|
|
|
particular version. If set to True the algorithm will perform this step.
|
|
|
|
max_depth: A positive integer indicating the maximum recursion depth the
|
|
|
|
algorithm can reach when de-referencing objects and performing the
|
|
|
|
object_similarity algorithm.
|
|
|
|
weight_dict: A dictionary that can be used to override what checks are done
|
|
|
|
to objects in the similarity process.
|
2019-07-26 22:01:45 +02:00
|
|
|
|
|
|
|
Returns:
|
2021-02-16 06:58:33 +01:00
|
|
|
float: A number between 0.0 and 100.0 as a measurement of similarity.
|
2019-07-26 22:01:45 +02:00
|
|
|
|
2019-09-23 15:44:09 +02:00
|
|
|
Warning:
|
2021-02-16 06:58:33 +01:00
|
|
|
Object types need to have property weights defined for the similarity process.
|
Graph Equivalence (#449)
* new packages for graph and object-based semantic equivalence
* new method graphically_equivalent for Environment, move equivalence methods out
* object equivalence function, methods used for object-based moved here.
* new graph_equivalence methods
* add notes
* add support for versioning checks (default disabled)
* new tests to cover graph equivalence and new methods
* added more imports to environment.py to prevent breaking changes
* variable changes, new fields for checks, reset depth check per call
* flexibility when object is not available on graph.
* refactor debug logging message
* new file stix2.equivalence.graph_equivalence.rst and stix2.equivalence.object_equivalence.rst for docs
* API documentation for new modules
* additional text required to build docs
* add more test methods for list_semantic_check an graphically_equivalent/versioning
* add logging debug messages, code clean-up
* include individual scoring on results dict, fix issue on list_semantic_check not keeping highest score
* include results as summary in prop_scores, minor tweaks
* Update __init__.py
doctrings update
* apply feedback from pull request
- rename semantic_check to reference_check
- rename modules to graph and object respectively to eliminate redundancy
- remove created_by_ref and object_marking_refs from graph WEIGHTS and rebalance
* update docs/ entries
* add more checks, make max score based on actual objects checked instead of the full list, only create entry when type is present in WEIGHTS dictionary
update tests to reflect changes
* rename package patterns -> pattern
* documentation, moving weights around
* more documentation moving
* rename WEIGHTS variable for graph_equivalence
2020-10-16 17:35:26 +02:00
|
|
|
Otherwise, those objects will not influence the final score. The WEIGHTS
|
|
|
|
dictionary under `stix2.equivalence.object` can give you an idea on how to add
|
|
|
|
new entries and pass them via the `weight_dict` argument. Similarly, the values
|
|
|
|
or methods can be fine tuned for a particular use case.
|
2019-07-26 22:01:45 +02:00
|
|
|
|
2019-12-11 19:13:36 +01:00
|
|
|
Note:
|
2020-10-19 03:09:07 +02:00
|
|
|
Default weight_dict:
|
2019-12-11 19:13:36 +01:00
|
|
|
|
2021-03-01 18:29:33 +01:00
|
|
|
.. include:: ../similarity_weights.rst
|
2019-12-11 19:13:36 +01:00
|
|
|
|
2019-09-23 15:44:09 +02:00
|
|
|
Note:
|
Graph Equivalence (#449)
* new packages for graph and object-based semantic equivalence
* new method graphically_equivalent for Environment, move equivalence methods out
* object equivalence function, methods used for object-based moved here.
* new graph_equivalence methods
* add notes
* add support for versioning checks (default disabled)
* new tests to cover graph equivalence and new methods
* added more imports to environment.py to prevent breaking changes
* variable changes, new fields for checks, reset depth check per call
* flexibility when object is not available on graph.
* refactor debug logging message
* new file stix2.equivalence.graph_equivalence.rst and stix2.equivalence.object_equivalence.rst for docs
* API documentation for new modules
* additional text required to build docs
* add more test methods for list_semantic_check an graphically_equivalent/versioning
* add logging debug messages, code clean-up
* include individual scoring on results dict, fix issue on list_semantic_check not keeping highest score
* include results as summary in prop_scores, minor tweaks
* Update __init__.py
doctrings update
* apply feedback from pull request
- rename semantic_check to reference_check
- rename modules to graph and object respectively to eliminate redundancy
- remove created_by_ref and object_marking_refs from graph WEIGHTS and rebalance
* update docs/ entries
* add more checks, make max score based on actual objects checked instead of the full list, only create entry when type is present in WEIGHTS dictionary
update tests to reflect changes
* rename package patterns -> pattern
* documentation, moving weights around
* more documentation moving
* rename WEIGHTS variable for graph_equivalence
2020-10-16 17:35:26 +02:00
|
|
|
This implementation follows the Semantic Equivalence Committee Note.
|
2019-07-26 22:01:45 +02:00
|
|
|
see `the Committee Note <link here>`__.
|
|
|
|
|
|
|
|
"""
|
2021-03-01 18:27:52 +01:00
|
|
|
return object_similarity(
|
|
|
|
obj1, obj2, prop_scores, ds1, ds2, ignore_spec_version,
|
|
|
|
versioning_checks, max_depth, **weight_dict
|
|
|
|
)
|
2019-07-26 22:01:45 +02:00
|
|
|
|
2021-02-16 06:58:33 +01:00
|
|
|
@staticmethod
|
2021-03-01 18:27:52 +01:00
|
|
|
def object_equivalence(
|
|
|
|
obj1, obj2, prop_scores={}, threshold=70, ds1=None, ds2=None,
|
|
|
|
ignore_spec_version=False, versioning_checks=False,
|
|
|
|
max_depth=1, **weight_dict
|
|
|
|
):
|
2021-02-16 06:58:33 +01:00
|
|
|
"""This method returns a true/false value if two objects are semantically equivalent.
|
|
|
|
Internally, it calls the object_similarity function and compares it against the given
|
|
|
|
threshold value.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
obj1: A stix2 object instance
|
|
|
|
obj2: A stix2 object instance
|
|
|
|
prop_scores: A dictionary that can hold individual property scores,
|
|
|
|
weights, contributing score, matching score and sum of weights.
|
|
|
|
threshold: A numerical value between 0 and 100 to determine the minimum
|
|
|
|
score to result in successfully calling both objects equivalent. This
|
|
|
|
value can be tuned.
|
2021-03-10 15:52:24 +01:00
|
|
|
ds1 (optional): A DataStore object instance from which to pull related objects
|
|
|
|
ds2 (optional): A DataStore object instance from which to pull related objects
|
2021-03-01 18:27:52 +01:00
|
|
|
ignore_spec_version: A boolean indicating whether to test object types
|
|
|
|
that belong to different spec versions (STIX 2.0 and STIX 2.1 for example).
|
|
|
|
If set to True this check will be skipped.
|
|
|
|
versioning_checks: A boolean indicating whether to test multiple revisions
|
|
|
|
of the same object (when present) to maximize similarity against a
|
|
|
|
particular version. If set to True the algorithm will perform this step.
|
|
|
|
max_depth: A positive integer indicating the maximum recursion depth the
|
|
|
|
algorithm can reach when de-referencing objects and performing the
|
|
|
|
object_similarity algorithm.
|
|
|
|
weight_dict: A dictionary that can be used to override what checks are done
|
|
|
|
to objects in the similarity process.
|
2021-02-16 06:58:33 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True if the result of the object similarity is greater than or equal to
|
|
|
|
the threshold value. False otherwise.
|
|
|
|
|
|
|
|
Warning:
|
|
|
|
Object types need to have property weights defined for the similarity process.
|
|
|
|
Otherwise, those objects will not influence the final score. The WEIGHTS
|
|
|
|
dictionary under `stix2.equivalence.object` can give you an idea on how to add
|
|
|
|
new entries and pass them via the `weight_dict` argument. Similarly, the values
|
|
|
|
or methods can be fine tuned for a particular use case.
|
|
|
|
|
|
|
|
Note:
|
|
|
|
Default weight_dict:
|
|
|
|
|
2021-03-01 18:29:33 +01:00
|
|
|
.. include:: ../similarity_weights.rst
|
2021-02-16 06:58:33 +01:00
|
|
|
|
|
|
|
Note:
|
|
|
|
This implementation follows the Semantic Equivalence Committee Note.
|
|
|
|
see `the Committee Note <link here>`__.
|
|
|
|
|
|
|
|
"""
|
2021-03-01 18:27:52 +01:00
|
|
|
return object_equivalence(
|
|
|
|
obj1, obj2, prop_scores, threshold, ds1, ds2,
|
|
|
|
ignore_spec_version, versioning_checks, max_depth, **weight_dict
|
|
|
|
)
|
2021-02-16 06:58:33 +01:00
|
|
|
|
Graph Equivalence (#449)
* new packages for graph and object-based semantic equivalence
* new method graphically_equivalent for Environment, move equivalence methods out
* object equivalence function, methods used for object-based moved here.
* new graph_equivalence methods
* add notes
* add support for versioning checks (default disabled)
* new tests to cover graph equivalence and new methods
* added more imports to environment.py to prevent breaking changes
* variable changes, new fields for checks, reset depth check per call
* flexibility when object is not available on graph.
* refactor debug logging message
* new file stix2.equivalence.graph_equivalence.rst and stix2.equivalence.object_equivalence.rst for docs
* API documentation for new modules
* additional text required to build docs
* add more test methods for list_semantic_check an graphically_equivalent/versioning
* add logging debug messages, code clean-up
* include individual scoring on results dict, fix issue on list_semantic_check not keeping highest score
* include results as summary in prop_scores, minor tweaks
* Update __init__.py
doctrings update
* apply feedback from pull request
- rename semantic_check to reference_check
- rename modules to graph and object respectively to eliminate redundancy
- remove created_by_ref and object_marking_refs from graph WEIGHTS and rebalance
* update docs/ entries
* add more checks, make max score based on actual objects checked instead of the full list, only create entry when type is present in WEIGHTS dictionary
update tests to reflect changes
* rename package patterns -> pattern
* documentation, moving weights around
* more documentation moving
* rename WEIGHTS variable for graph_equivalence
2020-10-16 17:35:26 +02:00
|
|
|
@staticmethod
|
2021-03-01 18:27:52 +01:00
|
|
|
def graph_similarity(
|
|
|
|
ds1, ds2, prop_scores={}, ignore_spec_version=False,
|
|
|
|
versioning_checks=False, max_depth=1, **weight_dict
|
|
|
|
):
|
2021-02-16 06:58:33 +01:00
|
|
|
"""This method returns a similarity score for two given graphs.
|
Graph Equivalence (#449)
* new packages for graph and object-based semantic equivalence
* new method graphically_equivalent for Environment, move equivalence methods out
* object equivalence function, methods used for object-based moved here.
* new graph_equivalence methods
* add notes
* add support for versioning checks (default disabled)
* new tests to cover graph equivalence and new methods
* added more imports to environment.py to prevent breaking changes
* variable changes, new fields for checks, reset depth check per call
* flexibility when object is not available on graph.
* refactor debug logging message
* new file stix2.equivalence.graph_equivalence.rst and stix2.equivalence.object_equivalence.rst for docs
* API documentation for new modules
* additional text required to build docs
* add more test methods for list_semantic_check an graphically_equivalent/versioning
* add logging debug messages, code clean-up
* include individual scoring on results dict, fix issue on list_semantic_check not keeping highest score
* include results as summary in prop_scores, minor tweaks
* Update __init__.py
doctrings update
* apply feedback from pull request
- rename semantic_check to reference_check
- rename modules to graph and object respectively to eliminate redundancy
- remove created_by_ref and object_marking_refs from graph WEIGHTS and rebalance
* update docs/ entries
* add more checks, make max score based on actual objects checked instead of the full list, only create entry when type is present in WEIGHTS dictionary
update tests to reflect changes
* rename package patterns -> pattern
* documentation, moving weights around
* more documentation moving
* rename WEIGHTS variable for graph_equivalence
2020-10-16 17:35:26 +02:00
|
|
|
Each DataStore can contain a connected or disconnected graph and the
|
|
|
|
final result is weighted over the amount of objects we managed to compare.
|
2021-02-16 06:58:33 +01:00
|
|
|
This approach builds on top of the object-based similarity process
|
Graph Equivalence (#449)
* new packages for graph and object-based semantic equivalence
* new method graphically_equivalent for Environment, move equivalence methods out
* object equivalence function, methods used for object-based moved here.
* new graph_equivalence methods
* add notes
* add support for versioning checks (default disabled)
* new tests to cover graph equivalence and new methods
* added more imports to environment.py to prevent breaking changes
* variable changes, new fields for checks, reset depth check per call
* flexibility when object is not available on graph.
* refactor debug logging message
* new file stix2.equivalence.graph_equivalence.rst and stix2.equivalence.object_equivalence.rst for docs
* API documentation for new modules
* additional text required to build docs
* add more test methods for list_semantic_check an graphically_equivalent/versioning
* add logging debug messages, code clean-up
* include individual scoring on results dict, fix issue on list_semantic_check not keeping highest score
* include results as summary in prop_scores, minor tweaks
* Update __init__.py
doctrings update
* apply feedback from pull request
- rename semantic_check to reference_check
- rename modules to graph and object respectively to eliminate redundancy
- remove created_by_ref and object_marking_refs from graph WEIGHTS and rebalance
* update docs/ entries
* add more checks, make max score based on actual objects checked instead of the full list, only create entry when type is present in WEIGHTS dictionary
update tests to reflect changes
* rename package patterns -> pattern
* documentation, moving weights around
* more documentation moving
* rename WEIGHTS variable for graph_equivalence
2020-10-16 17:35:26 +02:00
|
|
|
and each comparison can return a value between 0 and 100.
|
2019-07-26 22:01:45 +02:00
|
|
|
|
Graph Equivalence (#449)
* new packages for graph and object-based semantic equivalence
* new method graphically_equivalent for Environment, move equivalence methods out
* object equivalence function, methods used for object-based moved here.
* new graph_equivalence methods
* add notes
* add support for versioning checks (default disabled)
* new tests to cover graph equivalence and new methods
* added more imports to environment.py to prevent breaking changes
* variable changes, new fields for checks, reset depth check per call
* flexibility when object is not available on graph.
* refactor debug logging message
* new file stix2.equivalence.graph_equivalence.rst and stix2.equivalence.object_equivalence.rst for docs
* API documentation for new modules
* additional text required to build docs
* add more test methods for list_semantic_check an graphically_equivalent/versioning
* add logging debug messages, code clean-up
* include individual scoring on results dict, fix issue on list_semantic_check not keeping highest score
* include results as summary in prop_scores, minor tweaks
* Update __init__.py
doctrings update
* apply feedback from pull request
- rename semantic_check to reference_check
- rename modules to graph and object respectively to eliminate redundancy
- remove created_by_ref and object_marking_refs from graph WEIGHTS and rebalance
* update docs/ entries
* add more checks, make max score based on actual objects checked instead of the full list, only create entry when type is present in WEIGHTS dictionary
update tests to reflect changes
* rename package patterns -> pattern
* documentation, moving weights around
* more documentation moving
* rename WEIGHTS variable for graph_equivalence
2020-10-16 17:35:26 +02:00
|
|
|
Args:
|
|
|
|
ds1: A DataStore object instance representing your graph
|
|
|
|
ds2: A DataStore object instance representing your graph
|
|
|
|
prop_scores: A dictionary that can hold individual property scores,
|
|
|
|
weights, contributing score, matching score and sum of weights.
|
2021-03-01 18:27:52 +01:00
|
|
|
ignore_spec_version: A boolean indicating whether to test object types
|
|
|
|
that belong to different spec versions (STIX 2.0 and STIX 2.1 for example).
|
|
|
|
If set to True this check will be skipped.
|
|
|
|
versioning_checks: A boolean indicating whether to test multiple revisions
|
|
|
|
of the same object (when present) to maximize similarity against a
|
|
|
|
particular version. If set to True the algorithm will perform this step.
|
|
|
|
max_depth: A positive integer indicating the maximum recursion depth the
|
|
|
|
algorithm can reach when de-referencing objects and performing the
|
|
|
|
object_similarity algorithm.
|
|
|
|
weight_dict: A dictionary that can be used to override what checks are done
|
|
|
|
to objects in the similarity process.
|
2019-09-24 05:13:50 +02:00
|
|
|
|
Graph Equivalence (#449)
* new packages for graph and object-based semantic equivalence
* new method graphically_equivalent for Environment, move equivalence methods out
* object equivalence function, methods used for object-based moved here.
* new graph_equivalence methods
* add notes
* add support for versioning checks (default disabled)
* new tests to cover graph equivalence and new methods
* added more imports to environment.py to prevent breaking changes
* variable changes, new fields for checks, reset depth check per call
* flexibility when object is not available on graph.
* refactor debug logging message
* new file stix2.equivalence.graph_equivalence.rst and stix2.equivalence.object_equivalence.rst for docs
* API documentation for new modules
* additional text required to build docs
* add more test methods for list_semantic_check an graphically_equivalent/versioning
* add logging debug messages, code clean-up
* include individual scoring on results dict, fix issue on list_semantic_check not keeping highest score
* include results as summary in prop_scores, minor tweaks
* Update __init__.py
doctrings update
* apply feedback from pull request
- rename semantic_check to reference_check
- rename modules to graph and object respectively to eliminate redundancy
- remove created_by_ref and object_marking_refs from graph WEIGHTS and rebalance
* update docs/ entries
* add more checks, make max score based on actual objects checked instead of the full list, only create entry when type is present in WEIGHTS dictionary
update tests to reflect changes
* rename package patterns -> pattern
* documentation, moving weights around
* more documentation moving
* rename WEIGHTS variable for graph_equivalence
2020-10-16 17:35:26 +02:00
|
|
|
Returns:
|
2021-02-16 06:58:33 +01:00
|
|
|
float: A number between 0.0 and 100.0 as a measurement of similarity.
|
2019-09-24 05:13:50 +02:00
|
|
|
|
Graph Equivalence (#449)
* new packages for graph and object-based semantic equivalence
* new method graphically_equivalent for Environment, move equivalence methods out
* object equivalence function, methods used for object-based moved here.
* new graph_equivalence methods
* add notes
* add support for versioning checks (default disabled)
* new tests to cover graph equivalence and new methods
* added more imports to environment.py to prevent breaking changes
* variable changes, new fields for checks, reset depth check per call
* flexibility when object is not available on graph.
* refactor debug logging message
* new file stix2.equivalence.graph_equivalence.rst and stix2.equivalence.object_equivalence.rst for docs
* API documentation for new modules
* additional text required to build docs
* add more test methods for list_semantic_check an graphically_equivalent/versioning
* add logging debug messages, code clean-up
* include individual scoring on results dict, fix issue on list_semantic_check not keeping highest score
* include results as summary in prop_scores, minor tweaks
* Update __init__.py
doctrings update
* apply feedback from pull request
- rename semantic_check to reference_check
- rename modules to graph and object respectively to eliminate redundancy
- remove created_by_ref and object_marking_refs from graph WEIGHTS and rebalance
* update docs/ entries
* add more checks, make max score based on actual objects checked instead of the full list, only create entry when type is present in WEIGHTS dictionary
update tests to reflect changes
* rename package patterns -> pattern
* documentation, moving weights around
* more documentation moving
* rename WEIGHTS variable for graph_equivalence
2020-10-16 17:35:26 +02:00
|
|
|
Warning:
|
2021-02-16 06:58:33 +01:00
|
|
|
Object types need to have property weights defined for the similarity process.
|
Graph Equivalence (#449)
* new packages for graph and object-based semantic equivalence
* new method graphically_equivalent for Environment, move equivalence methods out
* object equivalence function, methods used for object-based moved here.
* new graph_equivalence methods
* add notes
* add support for versioning checks (default disabled)
* new tests to cover graph equivalence and new methods
* added more imports to environment.py to prevent breaking changes
* variable changes, new fields for checks, reset depth check per call
* flexibility when object is not available on graph.
* refactor debug logging message
* new file stix2.equivalence.graph_equivalence.rst and stix2.equivalence.object_equivalence.rst for docs
* API documentation for new modules
* additional text required to build docs
* add more test methods for list_semantic_check an graphically_equivalent/versioning
* add logging debug messages, code clean-up
* include individual scoring on results dict, fix issue on list_semantic_check not keeping highest score
* include results as summary in prop_scores, minor tweaks
* Update __init__.py
doctrings update
* apply feedback from pull request
- rename semantic_check to reference_check
- rename modules to graph and object respectively to eliminate redundancy
- remove created_by_ref and object_marking_refs from graph WEIGHTS and rebalance
* update docs/ entries
* add more checks, make max score based on actual objects checked instead of the full list, only create entry when type is present in WEIGHTS dictionary
update tests to reflect changes
* rename package patterns -> pattern
* documentation, moving weights around
* more documentation moving
* rename WEIGHTS variable for graph_equivalence
2020-10-16 17:35:26 +02:00
|
|
|
Otherwise, those objects will not influence the final score. The WEIGHTS
|
|
|
|
dictionary under `stix2.equivalence.graph` can give you an idea on how to add
|
|
|
|
new entries and pass them via the `weight_dict` argument. Similarly, the values
|
|
|
|
or methods can be fine tuned for a particular use case.
|
2019-09-24 05:13:50 +02:00
|
|
|
|
Graph Equivalence (#449)
* new packages for graph and object-based semantic equivalence
* new method graphically_equivalent for Environment, move equivalence methods out
* object equivalence function, methods used for object-based moved here.
* new graph_equivalence methods
* add notes
* add support for versioning checks (default disabled)
* new tests to cover graph equivalence and new methods
* added more imports to environment.py to prevent breaking changes
* variable changes, new fields for checks, reset depth check per call
* flexibility when object is not available on graph.
* refactor debug logging message
* new file stix2.equivalence.graph_equivalence.rst and stix2.equivalence.object_equivalence.rst for docs
* API documentation for new modules
* additional text required to build docs
* add more test methods for list_semantic_check an graphically_equivalent/versioning
* add logging debug messages, code clean-up
* include individual scoring on results dict, fix issue on list_semantic_check not keeping highest score
* include results as summary in prop_scores, minor tweaks
* Update __init__.py
doctrings update
* apply feedback from pull request
- rename semantic_check to reference_check
- rename modules to graph and object respectively to eliminate redundancy
- remove created_by_ref and object_marking_refs from graph WEIGHTS and rebalance
* update docs/ entries
* add more checks, make max score based on actual objects checked instead of the full list, only create entry when type is present in WEIGHTS dictionary
update tests to reflect changes
* rename package patterns -> pattern
* documentation, moving weights around
* more documentation moving
* rename WEIGHTS variable for graph_equivalence
2020-10-16 17:35:26 +02:00
|
|
|
Note:
|
2020-10-19 03:09:07 +02:00
|
|
|
Default weight_dict:
|
2019-09-24 05:13:50 +02:00
|
|
|
|
2021-03-01 18:27:52 +01:00
|
|
|
.. include:: ../similarity_weights.rst
|
2019-09-24 05:13:50 +02:00
|
|
|
|
Graph Equivalence (#449)
* new packages for graph and object-based semantic equivalence
* new method graphically_equivalent for Environment, move equivalence methods out
* object equivalence function, methods used for object-based moved here.
* new graph_equivalence methods
* add notes
* add support for versioning checks (default disabled)
* new tests to cover graph equivalence and new methods
* added more imports to environment.py to prevent breaking changes
* variable changes, new fields for checks, reset depth check per call
* flexibility when object is not available on graph.
* refactor debug logging message
* new file stix2.equivalence.graph_equivalence.rst and stix2.equivalence.object_equivalence.rst for docs
* API documentation for new modules
* additional text required to build docs
* add more test methods for list_semantic_check an graphically_equivalent/versioning
* add logging debug messages, code clean-up
* include individual scoring on results dict, fix issue on list_semantic_check not keeping highest score
* include results as summary in prop_scores, minor tweaks
* Update __init__.py
doctrings update
* apply feedback from pull request
- rename semantic_check to reference_check
- rename modules to graph and object respectively to eliminate redundancy
- remove created_by_ref and object_marking_refs from graph WEIGHTS and rebalance
* update docs/ entries
* add more checks, make max score based on actual objects checked instead of the full list, only create entry when type is present in WEIGHTS dictionary
update tests to reflect changes
* rename package patterns -> pattern
* documentation, moving weights around
* more documentation moving
* rename WEIGHTS variable for graph_equivalence
2020-10-16 17:35:26 +02:00
|
|
|
Note:
|
|
|
|
This implementation follows the Semantic Equivalence Committee Note.
|
|
|
|
see `the Committee Note <link here>`__.
|
2019-09-24 05:13:50 +02:00
|
|
|
|
Graph Equivalence (#449)
* new packages for graph and object-based semantic equivalence
* new method graphically_equivalent for Environment, move equivalence methods out
* object equivalence function, methods used for object-based moved here.
* new graph_equivalence methods
* add notes
* add support for versioning checks (default disabled)
* new tests to cover graph equivalence and new methods
* added more imports to environment.py to prevent breaking changes
* variable changes, new fields for checks, reset depth check per call
* flexibility when object is not available on graph.
* refactor debug logging message
* new file stix2.equivalence.graph_equivalence.rst and stix2.equivalence.object_equivalence.rst for docs
* API documentation for new modules
* additional text required to build docs
* add more test methods for list_semantic_check an graphically_equivalent/versioning
* add logging debug messages, code clean-up
* include individual scoring on results dict, fix issue on list_semantic_check not keeping highest score
* include results as summary in prop_scores, minor tweaks
* Update __init__.py
doctrings update
* apply feedback from pull request
- rename semantic_check to reference_check
- rename modules to graph and object respectively to eliminate redundancy
- remove created_by_ref and object_marking_refs from graph WEIGHTS and rebalance
* update docs/ entries
* add more checks, make max score based on actual objects checked instead of the full list, only create entry when type is present in WEIGHTS dictionary
update tests to reflect changes
* rename package patterns -> pattern
* documentation, moving weights around
* more documentation moving
* rename WEIGHTS variable for graph_equivalence
2020-10-16 17:35:26 +02:00
|
|
|
"""
|
2021-03-01 18:27:52 +01:00
|
|
|
return graph_similarity(
|
|
|
|
ds1, ds2, prop_scores, ignore_spec_version,
|
|
|
|
versioning_checks, max_depth, **weight_dict
|
|
|
|
)
|
2021-02-16 06:58:33 +01:00
|
|
|
|
|
|
|
@staticmethod
|
2021-03-01 18:27:52 +01:00
|
|
|
def graph_equivalence(
|
|
|
|
ds1, ds2, prop_scores={}, threshold=70,
|
|
|
|
ignore_spec_version=False, versioning_checks=False,
|
|
|
|
max_depth=1, **weight_dict
|
|
|
|
):
|
2021-02-16 06:58:33 +01:00
|
|
|
"""This method returns a true/false value if two graphs are semantically equivalent.
|
|
|
|
Internally, it calls the graph_similarity function and compares it against the given
|
|
|
|
threshold value.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ds1: A DataStore object instance representing your graph
|
|
|
|
ds2: A DataStore object instance representing your graph
|
|
|
|
prop_scores: A dictionary that can hold individual property scores,
|
|
|
|
weights, contributing score, matching score and sum of weights.
|
|
|
|
threshold: A numerical value between 0 and 100 to determine the minimum
|
|
|
|
score to result in successfully calling both graphs equivalent. This
|
|
|
|
value can be tuned.
|
2021-03-01 18:27:52 +01:00
|
|
|
ignore_spec_version: A boolean indicating whether to test object types
|
|
|
|
that belong to different spec versions (STIX 2.0 and STIX 2.1 for example).
|
|
|
|
If set to True this check will be skipped.
|
|
|
|
versioning_checks: A boolean indicating whether to test multiple revisions
|
|
|
|
of the same object (when present) to maximize similarity against a
|
|
|
|
particular version. If set to True the algorithm will perform this step.
|
|
|
|
max_depth: A positive integer indicating the maximum recursion depth the
|
|
|
|
algorithm can reach when de-referencing objects and performing the
|
|
|
|
object_similarity algorithm.
|
|
|
|
weight_dict: A dictionary that can be used to override what checks are done
|
|
|
|
to objects in the similarity process.
|
2021-02-16 06:58:33 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True if the result of the graph similarity is greater than or equal to
|
|
|
|
the threshold value. False otherwise.
|
|
|
|
|
|
|
|
Warning:
|
|
|
|
Object types need to have property weights defined for the similarity process.
|
|
|
|
Otherwise, those objects will not influence the final score. The WEIGHTS
|
|
|
|
dictionary under `stix2.equivalence.graph` can give you an idea on how to add
|
|
|
|
new entries and pass them via the `weight_dict` argument. Similarly, the values
|
|
|
|
or methods can be fine tuned for a particular use case.
|
|
|
|
|
|
|
|
Note:
|
|
|
|
Default weight_dict:
|
|
|
|
|
2021-03-01 18:27:52 +01:00
|
|
|
.. include:: ../similarity_weights.rst
|
2021-02-16 06:58:33 +01:00
|
|
|
|
|
|
|
Note:
|
|
|
|
This implementation follows the Semantic Equivalence Committee Note.
|
|
|
|
see `the Committee Note <link here>`__.
|
|
|
|
|
|
|
|
"""
|
2021-03-01 18:27:52 +01:00
|
|
|
return graph_equivalence(
|
|
|
|
ds1, ds2, prop_scores, threshold, ignore_spec_version,
|
|
|
|
versioning_checks, max_depth, **weight_dict
|
|
|
|
)
|