2018-11-28 22:51:00 +01:00
|
|
|
"""Utility functions and classes for the STIX2 library."""
|
|
|
|
|
2021-01-20 01:57:05 +01:00
|
|
|
import collections.abc
|
2017-02-10 22:35:02 +01:00
|
|
|
import datetime as dt
|
2020-03-17 01:25:38 +01:00
|
|
|
import enum
|
2017-05-05 16:53:28 +02:00
|
|
|
import json
|
2020-03-19 15:40:35 +01:00
|
|
|
import re
|
2017-05-05 16:53:28 +02:00
|
|
|
|
2017-05-10 00:03:46 +02:00
|
|
|
import pytz
|
2017-04-25 00:29:56 +02:00
|
|
|
|
2021-01-29 01:21:57 +01:00
|
|
|
import stix2.registry as mappings
|
2021-01-29 01:32:27 +01:00
|
|
|
import stix2.version
|
2018-10-26 03:03:27 +02:00
|
|
|
|
2017-05-19 19:51:59 +02:00
|
|
|
# Sentinel value for properties that should be set to the current time.
|
2017-02-10 22:35:02 +01:00
|
|
|
# We can't use the standard 'default' approach, since if there are multiple
|
|
|
|
# timestamps in a single object, the timestamps will vary by a few microseconds.
|
|
|
|
NOW = object()
|
|
|
|
|
2020-03-19 15:40:35 +01:00
|
|
|
PREFIX_21_REGEX = re.compile(r'^[a-z].*')
|
2018-04-13 20:52:00 +02:00
|
|
|
|
2020-05-20 21:06:53 +02:00
|
|
|
_TIMESTAMP_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
|
|
|
|
_TIMESTAMP_FORMAT_FRAC = "%Y-%m-%dT%H:%M:%S.%fZ"
|
|
|
|
|
2017-02-10 22:35:02 +01:00
|
|
|
|
2020-03-17 01:25:38 +01:00
|
|
|
class Precision(enum.Enum):
|
|
|
|
"""
|
|
|
|
Timestamp format precisions.
|
|
|
|
"""
|
|
|
|
# auto() wasn't introduced until Python 3.6.
|
|
|
|
ANY = 1
|
|
|
|
SECOND = 2
|
|
|
|
MILLISECOND = 3
|
|
|
|
|
|
|
|
|
|
|
|
class PrecisionConstraint(enum.Enum):
|
|
|
|
"""
|
|
|
|
Timestamp precision constraints. These affect how the Precision
|
|
|
|
values are applied when formatting a timestamp.
|
|
|
|
|
|
|
|
These constraints don't really make sense with the ANY precision, so they
|
|
|
|
have no effect in that case.
|
|
|
|
"""
|
|
|
|
EXACT = 1 # format must have exactly the given precision
|
|
|
|
MIN = 2 # format must have at least the given precision
|
|
|
|
# no need for a MAX constraint yet
|
|
|
|
|
|
|
|
|
2021-04-01 02:01:27 +02:00
|
|
|
def to_enum(value, enum_type, enum_default=None):
|
2020-03-17 01:25:38 +01:00
|
|
|
"""
|
|
|
|
Detect and convert strings to enums and None to a default enum. This
|
|
|
|
allows use of strings and None in APIs, while enforcing the enum type: if
|
|
|
|
you use a string, it must name a valid enum value. This implementation is
|
|
|
|
case-insensitive.
|
|
|
|
|
|
|
|
:param value: A value to be interpreted as an enum (string, Enum instance,
|
|
|
|
or None). If an Enum instance, it must be an instance of enum_type.
|
|
|
|
:param enum_type: The enum type which strings will be interpreted against
|
|
|
|
:param enum_default: The default enum to use if value is None. Must be
|
|
|
|
an instance of enum_type, or None. If None, you are disallowing a
|
|
|
|
default and requiring that value be non-None.
|
|
|
|
:return: An instance of enum_type
|
|
|
|
:raises TypeError: If value was neither an instance of enum_type, None, nor
|
|
|
|
a string
|
|
|
|
:raises KeyError: If value was a string which couldn't be interpreted as an
|
|
|
|
enum value from enum_type
|
|
|
|
"""
|
|
|
|
assert enum_default is None or isinstance(enum_default, enum_type)
|
|
|
|
|
|
|
|
if not isinstance(value, enum_type):
|
|
|
|
if value is None and enum_default is not None:
|
|
|
|
value = enum_default
|
2021-02-18 18:26:54 +01:00
|
|
|
elif isinstance(value, str):
|
2020-03-17 01:25:38 +01:00
|
|
|
value = enum_type[value.upper()]
|
|
|
|
else:
|
2021-01-13 23:52:15 +01:00
|
|
|
raise TypeError(
|
|
|
|
"Not a valid {}: {}".format(
|
|
|
|
enum_type.__name__, value,
|
|
|
|
),
|
|
|
|
)
|
2020-03-17 01:25:38 +01:00
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2017-06-23 00:47:35 +02:00
|
|
|
class STIXdatetime(dt.datetime):
|
2020-03-17 01:25:38 +01:00
|
|
|
"""
|
|
|
|
Bundle a datetime with some format-related metadata, so that JSON
|
|
|
|
serialization has the info it needs to produce compliant timestamps.
|
|
|
|
"""
|
|
|
|
|
2017-06-23 00:47:35 +02:00
|
|
|
def __new__(cls, *args, **kwargs):
|
2021-04-01 02:01:27 +02:00
|
|
|
precision = to_enum(
|
2020-03-17 01:25:38 +01:00
|
|
|
kwargs.pop("precision", Precision.ANY),
|
2020-03-17 23:26:57 +01:00
|
|
|
Precision,
|
2020-03-17 01:25:38 +01:00
|
|
|
)
|
2021-04-01 02:01:27 +02:00
|
|
|
precision_constraint = to_enum(
|
2020-03-17 01:25:38 +01:00
|
|
|
kwargs.pop("precision_constraint", PrecisionConstraint.EXACT),
|
2020-03-17 23:26:57 +01:00
|
|
|
PrecisionConstraint,
|
2020-03-17 01:25:38 +01:00
|
|
|
)
|
|
|
|
|
2017-06-23 00:47:35 +02:00
|
|
|
if isinstance(args[0], dt.datetime): # Allow passing in a datetime object
|
|
|
|
dttm = args[0]
|
2018-07-13 17:10:05 +02:00
|
|
|
args = (
|
|
|
|
dttm.year, dttm.month, dttm.day, dttm.hour, dttm.minute,
|
|
|
|
dttm.second, dttm.microsecond, dttm.tzinfo,
|
|
|
|
)
|
2017-06-28 21:55:23 +02:00
|
|
|
# self will be an instance of STIXdatetime, not dt.datetime
|
2017-06-23 00:47:35 +02:00
|
|
|
self = dt.datetime.__new__(cls, *args, **kwargs)
|
|
|
|
self.precision = precision
|
2020-03-17 01:25:38 +01:00
|
|
|
self.precision_constraint = precision_constraint
|
2017-06-23 00:47:35 +02:00
|
|
|
return self
|
|
|
|
|
2017-08-11 21:04:58 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return "'%s'" % format_datetime(self)
|
|
|
|
|
2017-06-23 00:47:35 +02:00
|
|
|
|
2017-09-29 17:24:19 +02:00
|
|
|
def deduplicate(stix_obj_list):
|
2017-11-02 12:48:37 +01:00
|
|
|
"""Deduplicate a list of STIX objects to a unique set.
|
2017-09-29 17:24:19 +02:00
|
|
|
|
|
|
|
Reduces a set of STIX objects to unique set by looking
|
|
|
|
at 'id' and 'modified' fields - as a unique object version
|
|
|
|
is determined by the combination of those fields
|
|
|
|
|
|
|
|
Note: Be aware, as can be seen in the implementation
|
|
|
|
of deduplicate(),that if the "stix_obj_list" argument has
|
|
|
|
multiple STIX objects of the same version, the last object
|
|
|
|
version found in the list will be the one that is returned.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
stix_obj_list (list): list of STIX objects (dicts)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list with a unique set of the passed list of STIX objects.
|
|
|
|
|
|
|
|
"""
|
|
|
|
unique_objs = {}
|
|
|
|
|
|
|
|
for obj in stix_obj_list:
|
2020-08-18 00:38:29 +02:00
|
|
|
ver = obj.get("modified") or obj.get("created")
|
|
|
|
|
|
|
|
if ver is None:
|
|
|
|
unique_objs[obj["id"]] = obj
|
|
|
|
else:
|
|
|
|
unique_objs[(obj['id'], ver)] = obj
|
2017-09-29 17:24:19 +02:00
|
|
|
|
|
|
|
return list(unique_objs.values())
|
|
|
|
|
|
|
|
|
2017-02-10 22:35:02 +01:00
|
|
|
def get_timestamp():
|
2017-09-22 20:54:21 +02:00
|
|
|
"""Return a STIX timestamp of the current date and time."""
|
2017-06-23 00:47:35 +02:00
|
|
|
return STIXdatetime.now(tz=pytz.UTC)
|
2017-02-10 22:35:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
def format_datetime(dttm):
|
2017-09-22 20:54:21 +02:00
|
|
|
"""Convert a datetime object into a valid STIX timestamp string.
|
|
|
|
|
|
|
|
1. Convert to timezone-aware
|
|
|
|
2. Convert to UTC
|
|
|
|
3. Format in ISO format
|
|
|
|
4. Ensure correct precision
|
2020-03-17 01:25:38 +01:00
|
|
|
a. Add subsecond value if warranted, according to precision settings
|
2017-09-22 20:54:21 +02:00
|
|
|
5. Add "Z"
|
|
|
|
|
|
|
|
"""
|
2017-04-17 16:48:13 +02:00
|
|
|
|
2017-05-22 17:11:42 +02:00
|
|
|
if dttm.tzinfo is None or dttm.tzinfo.utcoffset(dttm) is None:
|
2017-04-17 19:16:14 +02:00
|
|
|
# dttm is timezone-naive; assume UTC
|
2017-05-22 17:11:42 +02:00
|
|
|
zoned = pytz.utc.localize(dttm)
|
|
|
|
else:
|
|
|
|
zoned = dttm.astimezone(pytz.utc)
|
2018-06-30 00:38:04 +02:00
|
|
|
ts = zoned.strftime('%Y-%m-%dT%H:%M:%S')
|
2020-03-17 01:25:38 +01:00
|
|
|
precision = getattr(dttm, 'precision', Precision.ANY)
|
|
|
|
precision_constraint = getattr(
|
2020-03-17 23:26:57 +01:00
|
|
|
dttm, 'precision_constraint', PrecisionConstraint.EXACT,
|
2020-03-17 01:25:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
frac_seconds_str = ""
|
|
|
|
if precision == Precision.ANY:
|
|
|
|
# No need to truncate; ignore constraint
|
|
|
|
if zoned.microsecond:
|
|
|
|
frac_seconds_str = "{:06d}".format(zoned.microsecond).rstrip("0")
|
|
|
|
|
|
|
|
elif precision == Precision.SECOND:
|
|
|
|
if precision_constraint == PrecisionConstraint.MIN:
|
|
|
|
# second precision, or better. Winds up being the same as ANY:
|
|
|
|
# just use all our digits
|
|
|
|
if zoned.microsecond:
|
|
|
|
frac_seconds_str = "{:06d}".format(zoned.microsecond)\
|
|
|
|
.rstrip("0")
|
|
|
|
# exact: ignore microseconds entirely
|
|
|
|
|
|
|
|
else:
|
|
|
|
# precision == millisecond
|
|
|
|
if precision_constraint == PrecisionConstraint.EXACT:
|
|
|
|
# can't rstrip() here or we may lose precision
|
|
|
|
frac_seconds_str = "{:06d}".format(zoned.microsecond)[:3]
|
|
|
|
|
|
|
|
else:
|
|
|
|
# millisecond precision, or better. So we can rstrip() zeros, but
|
|
|
|
# only to a length of at least 3 digits (ljust() adds zeros back,
|
|
|
|
# if it stripped too far.)
|
|
|
|
frac_seconds_str = "{:06d}"\
|
|
|
|
.format(zoned.microsecond)\
|
|
|
|
.rstrip("0")\
|
|
|
|
.ljust(3, "0")
|
|
|
|
|
|
|
|
ts = "{}{}{}Z".format(
|
|
|
|
ts,
|
|
|
|
"." if frac_seconds_str else "",
|
2020-03-17 23:26:57 +01:00
|
|
|
frac_seconds_str,
|
2020-03-17 01:25:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return ts
|
|
|
|
|
|
|
|
|
|
|
|
def parse_into_datetime(
|
|
|
|
value, precision=Precision.ANY,
|
2020-03-17 23:26:57 +01:00
|
|
|
precision_constraint=PrecisionConstraint.EXACT,
|
2020-03-17 01:25:38 +01:00
|
|
|
):
|
2017-09-22 20:54:21 +02:00
|
|
|
"""
|
2020-03-17 01:25:38 +01:00
|
|
|
Parse a value into a valid STIX timestamp object. Also, optionally adjust
|
|
|
|
precision of fractional seconds. This allows alignment with JSON
|
|
|
|
serialization requirements, and helps ensure we're not using extra
|
|
|
|
precision which would be lost upon JSON serialization. The precision
|
|
|
|
info will be embedded in the returned object, so that JSON serialization
|
|
|
|
will format it correctly.
|
|
|
|
|
|
|
|
:param value: A datetime.datetime or datetime.date instance, or a string
|
|
|
|
:param precision: A precision value: either an instance of the Precision
|
|
|
|
enum, or a string naming one of the enum values (case-insensitive)
|
|
|
|
:param precision_constraint: A precision constraint value: either an
|
|
|
|
instance of the PrecisionConstraint enum, or a string naming one of
|
|
|
|
the enum values (case-insensitive)
|
|
|
|
:return: A STIXdatetime instance, which is a datetime but also carries the
|
|
|
|
precision info necessary to properly JSON-serialize it.
|
|
|
|
"""
|
2021-04-01 02:01:27 +02:00
|
|
|
precision = to_enum(precision, Precision)
|
|
|
|
precision_constraint = to_enum(precision_constraint, PrecisionConstraint)
|
2020-03-17 01:25:38 +01:00
|
|
|
|
2017-05-04 22:34:08 +02:00
|
|
|
if isinstance(value, dt.date):
|
|
|
|
if hasattr(value, 'hour'):
|
2017-06-23 00:47:35 +02:00
|
|
|
ts = value
|
2017-05-04 22:34:08 +02:00
|
|
|
else:
|
|
|
|
# Add a time component
|
2017-06-23 00:47:35 +02:00
|
|
|
ts = dt.datetime.combine(value, dt.time(0, 0, tzinfo=pytz.utc))
|
2017-05-04 22:34:08 +02:00
|
|
|
else:
|
2017-06-23 00:47:35 +02:00
|
|
|
# value isn't a date or datetime object so assume it's a string
|
2020-05-20 21:06:53 +02:00
|
|
|
fmt = _TIMESTAMP_FORMAT_FRAC if "." in value else _TIMESTAMP_FORMAT
|
2017-06-23 00:47:35 +02:00
|
|
|
try:
|
2020-05-20 21:06:53 +02:00
|
|
|
parsed = dt.datetime.strptime(value, fmt)
|
2017-06-23 00:47:35 +02:00
|
|
|
except (TypeError, ValueError):
|
|
|
|
# Unknown format
|
2018-07-13 17:10:05 +02:00
|
|
|
raise ValueError(
|
|
|
|
"must be a datetime object, date object, or "
|
|
|
|
"timestamp string in a recognizable format.",
|
|
|
|
)
|
2017-06-23 00:47:35 +02:00
|
|
|
if parsed.tzinfo:
|
|
|
|
ts = parsed.astimezone(pytz.utc)
|
|
|
|
else:
|
|
|
|
# Doesn't have timezone info in the string; assume UTC
|
|
|
|
ts = pytz.utc.localize(parsed)
|
|
|
|
|
|
|
|
# Ensure correct precision
|
2020-03-17 01:25:38 +01:00
|
|
|
if precision == Precision.SECOND:
|
|
|
|
if precision_constraint == PrecisionConstraint.EXACT:
|
2017-06-28 21:55:23 +02:00
|
|
|
ts = ts.replace(microsecond=0)
|
2020-03-17 01:25:38 +01:00
|
|
|
# else, no need to modify fractional seconds
|
|
|
|
|
|
|
|
elif precision == Precision.MILLISECOND:
|
|
|
|
if precision_constraint == PrecisionConstraint.EXACT:
|
|
|
|
us = (ts.microsecond // 1000) * 1000
|
|
|
|
ts = ts.replace(microsecond=us)
|
|
|
|
# else: at least millisecond precision: the constraint will affect JSON
|
|
|
|
# formatting, but there's nothing we need to do here.
|
|
|
|
|
|
|
|
# else, precision == Precision.ANY: nothing for us to do.
|
|
|
|
|
|
|
|
return STIXdatetime(
|
2020-03-17 23:26:57 +01:00
|
|
|
ts, precision=precision, precision_constraint=precision_constraint,
|
2020-03-17 01:25:38 +01:00
|
|
|
)
|
2017-05-04 22:34:08 +02:00
|
|
|
|
|
|
|
|
2018-04-13 17:08:03 +02:00
|
|
|
def _get_dict(data):
|
2017-04-19 20:32:56 +02:00
|
|
|
"""Return data as a dictionary.
|
2017-09-22 20:54:21 +02:00
|
|
|
|
2017-04-19 20:32:56 +02:00
|
|
|
Input can be a dictionary, string, or file-like object.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if type(data) is dict:
|
2017-05-16 15:25:08 +02:00
|
|
|
return data
|
2017-04-19 20:32:56 +02:00
|
|
|
else:
|
|
|
|
try:
|
2017-05-16 15:25:08 +02:00
|
|
|
return json.loads(data)
|
2017-04-19 20:32:56 +02:00
|
|
|
except TypeError:
|
2017-05-16 15:25:08 +02:00
|
|
|
pass
|
|
|
|
try:
|
|
|
|
return json.load(data)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
return dict(data)
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
raise ValueError("Cannot convert '%s' to dictionary." % str(data))
|
2017-08-31 18:28:07 +02:00
|
|
|
|
|
|
|
|
2017-11-02 12:48:37 +01:00
|
|
|
def get_class_hierarchy_names(obj):
|
|
|
|
"""Given an object, return the names of the class hierarchy."""
|
|
|
|
names = []
|
|
|
|
for cls in obj.__class__.__mro__:
|
|
|
|
names.append(cls.__name__)
|
|
|
|
return names
|
2017-11-20 04:11:44 +01:00
|
|
|
|
2017-11-20 05:00:42 +01:00
|
|
|
|
2017-11-20 04:11:44 +01:00
|
|
|
def get_type_from_id(stix_id):
|
|
|
|
return stix_id.split('--', 1)[0]
|
2018-10-26 03:03:27 +02:00
|
|
|
|
|
|
|
|
2021-01-20 01:57:05 +01:00
|
|
|
def detect_spec_version(stix_dict):
|
|
|
|
"""
|
|
|
|
Given a dict representing a STIX object, try to detect what spec version
|
|
|
|
it is likely to comply with.
|
|
|
|
|
|
|
|
:param stix_dict: A dict with some STIX content. Must at least have a
|
|
|
|
"type" property.
|
|
|
|
:return: A STIX version in "X.Y" format
|
|
|
|
"""
|
|
|
|
|
|
|
|
obj_type = stix_dict["type"]
|
|
|
|
|
|
|
|
if 'spec_version' in stix_dict:
|
|
|
|
# For STIX 2.0, applies to bundles only.
|
|
|
|
# For STIX 2.1+, applies to SCOs, SDOs, SROs, and markings only.
|
|
|
|
v = stix_dict['spec_version']
|
|
|
|
elif "id" not in stix_dict:
|
|
|
|
# Only 2.0 SCOs don't have ID properties
|
|
|
|
v = "2.0"
|
|
|
|
elif obj_type == 'bundle':
|
|
|
|
# Bundle without a spec_version property: must be 2.1. But to
|
|
|
|
# future-proof, use max version over all contained SCOs, with 2.1
|
|
|
|
# minimum.
|
|
|
|
v = max(
|
|
|
|
"2.1",
|
|
|
|
max(
|
|
|
|
detect_spec_version(obj) for obj in stix_dict["objects"]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
elif obj_type in mappings.STIX2_OBJ_MAPS["2.1"]["observables"]:
|
|
|
|
# Non-bundle object with an ID and without spec_version. Could be a
|
|
|
|
# 2.1 SCO or 2.0 SDO/SRO/marking. Check for 2.1 SCO...
|
|
|
|
v = "2.1"
|
|
|
|
else:
|
|
|
|
# Not a 2.1 SCO; must be a 2.0 object.
|
|
|
|
v = "2.0"
|
|
|
|
|
|
|
|
return v
|
|
|
|
|
|
|
|
|
2021-01-07 00:59:48 +01:00
|
|
|
def _stix_type_of(value):
|
|
|
|
"""
|
|
|
|
Get a STIX type from the given value: if a STIX ID is passed, the type
|
|
|
|
prefix is extracted; if string which is not a STIX ID is passed, it is
|
|
|
|
assumed to be a STIX type and is returned; otherwise it is assumed to be a
|
|
|
|
mapping with a "type" property, and the value of that property is returned.
|
|
|
|
|
|
|
|
:param value: A mapping with a "type" property, or a STIX ID or type
|
|
|
|
as a string
|
|
|
|
:return: A STIX type
|
|
|
|
"""
|
|
|
|
if isinstance(value, str):
|
|
|
|
if "--" in value:
|
|
|
|
type_ = get_type_from_id(value)
|
|
|
|
else:
|
|
|
|
type_ = value
|
|
|
|
else:
|
|
|
|
type_ = value["type"]
|
|
|
|
|
|
|
|
return type_
|
|
|
|
|
|
|
|
|
2021-01-16 01:12:14 +01:00
|
|
|
def is_sdo(value, stix_version=stix2.version.DEFAULT_VERSION):
|
2021-01-07 00:59:48 +01:00
|
|
|
"""
|
2021-01-20 01:57:05 +01:00
|
|
|
Determine whether the given object, type, or ID is/is for an SDO of the
|
|
|
|
given STIX version. If value is a type or ID, this just checks whether
|
|
|
|
the type was registered as an SDO in the given STIX version. If a mapping,
|
|
|
|
*simple* STIX version inference is additionally done on the value, and the
|
|
|
|
result is checked against stix_version. It does not attempt to fully
|
|
|
|
validate the value.
|
2021-01-07 00:59:48 +01:00
|
|
|
|
|
|
|
:param value: A mapping with a "type" property, or a STIX ID or type
|
|
|
|
as a string
|
|
|
|
:param stix_version: A STIX version as a string
|
2021-01-20 01:57:05 +01:00
|
|
|
:return: True if the type of the given value is an SDO type of the given
|
|
|
|
version; False if not
|
2021-01-07 00:59:48 +01:00
|
|
|
"""
|
2021-01-20 01:57:05 +01:00
|
|
|
|
|
|
|
result = True
|
|
|
|
if isinstance(value, collections.abc.Mapping):
|
|
|
|
value_stix_version = detect_spec_version(value)
|
|
|
|
if value_stix_version != stix_version:
|
|
|
|
result = False
|
|
|
|
|
|
|
|
if result:
|
|
|
|
cls_maps = mappings.STIX2_OBJ_MAPS[stix_version]
|
|
|
|
type_ = _stix_type_of(value)
|
|
|
|
result = type_ in cls_maps["objects"] and type_ not in {
|
|
|
|
"relationship", "sighting", "marking-definition", "bundle",
|
2021-01-21 02:49:01 +01:00
|
|
|
"language-content",
|
2021-01-20 01:57:05 +01:00
|
|
|
}
|
2021-01-07 00:59:48 +01:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2021-01-16 01:12:14 +01:00
|
|
|
def is_sco(value, stix_version=stix2.version.DEFAULT_VERSION):
|
2021-01-07 00:59:48 +01:00
|
|
|
"""
|
2021-01-20 01:57:05 +01:00
|
|
|
Determine whether the given object, type, or ID is/is for an SCO of the
|
|
|
|
given STIX version. If value is a type or ID, this just checks whether
|
|
|
|
the type was registered as an SCO in the given STIX version. If a mapping,
|
|
|
|
*simple* STIX version inference is additionally done on the value, and the
|
|
|
|
result is checked against stix_version. It does not attempt to fully
|
|
|
|
validate the value.
|
2021-01-07 00:59:48 +01:00
|
|
|
|
|
|
|
:param value: A mapping with a "type" property, or a STIX ID or type
|
|
|
|
as a string
|
|
|
|
:param stix_version: A STIX version as a string
|
2021-01-20 01:57:05 +01:00
|
|
|
:return: True if the type of the given value is an SCO type of the given
|
|
|
|
version; False if not
|
2021-01-07 00:59:48 +01:00
|
|
|
"""
|
2021-01-20 01:57:05 +01:00
|
|
|
|
|
|
|
result = True
|
|
|
|
if isinstance(value, collections.abc.Mapping):
|
|
|
|
value_stix_version = detect_spec_version(value)
|
|
|
|
if value_stix_version != stix_version:
|
|
|
|
result = False
|
|
|
|
|
|
|
|
if result:
|
|
|
|
cls_maps = mappings.STIX2_OBJ_MAPS[stix_version]
|
|
|
|
type_ = _stix_type_of(value)
|
|
|
|
result = type_ in cls_maps["observables"]
|
2021-01-07 00:59:48 +01:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2021-01-16 01:12:14 +01:00
|
|
|
def is_sro(value, stix_version=stix2.version.DEFAULT_VERSION):
|
2021-01-07 00:59:48 +01:00
|
|
|
"""
|
2021-01-20 01:57:05 +01:00
|
|
|
Determine whether the given object, type, or ID is/is for an SRO of the
|
|
|
|
given STIX version. If value is a type or ID, this just checks whether
|
2021-01-29 04:54:52 +01:00
|
|
|
the type is "sighting" or "relationship". If a mapping, *simple* STIX
|
2021-01-28 04:25:41 +01:00
|
|
|
version inference is additionally done on the value, and the result is
|
|
|
|
checked against stix_version. It does not attempt to fully validate the
|
|
|
|
value.
|
2021-01-07 00:59:48 +01:00
|
|
|
|
|
|
|
:param value: A mapping with a "type" property, or a STIX ID or type
|
|
|
|
as a string
|
|
|
|
:param stix_version: A STIX version as a string
|
2021-01-20 01:57:05 +01:00
|
|
|
:return: True if the type of the given value is an SRO type of the given
|
|
|
|
version; False if not
|
2021-01-07 00:59:48 +01:00
|
|
|
"""
|
|
|
|
|
2021-01-20 01:57:05 +01:00
|
|
|
result = True
|
|
|
|
if isinstance(value, collections.abc.Mapping):
|
|
|
|
value_stix_version = detect_spec_version(value)
|
|
|
|
if value_stix_version != stix_version:
|
|
|
|
result = False
|
|
|
|
|
|
|
|
if result:
|
|
|
|
# No need to check registration in this case
|
|
|
|
type_ = _stix_type_of(value)
|
|
|
|
result = type_ in ("sighting", "relationship")
|
2021-01-07 00:59:48 +01:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2021-01-16 01:12:14 +01:00
|
|
|
def is_object(value, stix_version=stix2.version.DEFAULT_VERSION):
|
2021-01-07 00:59:48 +01:00
|
|
|
"""
|
|
|
|
Determine whether an object, type, or ID is/is for any STIX object. This
|
2021-01-20 01:57:05 +01:00
|
|
|
includes all SDOs, SCOs, meta-objects, and bundle. If value is a type or
|
|
|
|
ID, this just checks whether the type was registered in the given STIX
|
|
|
|
version. If a mapping, *simple* STIX version inference is additionally
|
|
|
|
done on the value, and the result is checked against stix_version. It does
|
|
|
|
not attempt to fully validate the value.
|
2021-01-07 00:59:48 +01:00
|
|
|
|
|
|
|
:param value: A mapping with a "type" property, or a STIX ID or type
|
|
|
|
as a string
|
|
|
|
:param stix_version: A STIX version as a string
|
|
|
|
:return: True if the type of the given value is a valid STIX type with
|
|
|
|
respect to the given STIX version; False if not
|
|
|
|
"""
|
2021-01-20 01:57:05 +01:00
|
|
|
|
|
|
|
result = True
|
|
|
|
if isinstance(value, collections.abc.Mapping):
|
|
|
|
value_stix_version = detect_spec_version(value)
|
|
|
|
if value_stix_version != stix_version:
|
|
|
|
result = False
|
|
|
|
|
|
|
|
if result:
|
|
|
|
cls_maps = mappings.STIX2_OBJ_MAPS[stix_version]
|
|
|
|
type_ = _stix_type_of(value)
|
|
|
|
result = type_ in cls_maps["observables"] \
|
2021-01-21 02:49:01 +01:00
|
|
|
or type_ in cls_maps["objects"]
|
2021-01-07 00:59:48 +01:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2021-01-16 01:12:14 +01:00
|
|
|
def is_marking(value, stix_version=stix2.version.DEFAULT_VERSION):
|
2021-01-20 01:57:05 +01:00
|
|
|
"""
|
|
|
|
Determine whether the given object, type, or ID is/is for an marking
|
|
|
|
definition of the given STIX version. If value is a type or ID, this just
|
2021-01-28 04:25:41 +01:00
|
|
|
checks whether the type is "marking-definition". If a mapping, *simple*
|
|
|
|
STIX version inference is additionally done on the value, and the result
|
|
|
|
is checked against stix_version. It does not attempt to fully validate the
|
|
|
|
value.
|
2018-10-26 03:03:27 +02:00
|
|
|
|
2021-01-07 00:59:48 +01:00
|
|
|
:param value: A STIX object, object ID, or type as a string.
|
|
|
|
:param stix_version: A STIX version as a string
|
|
|
|
:return: True if the value is/is for a marking definition, False otherwise.
|
2018-10-26 03:03:27 +02:00
|
|
|
"""
|
|
|
|
|
2021-01-20 01:57:05 +01:00
|
|
|
result = True
|
|
|
|
if isinstance(value, collections.abc.Mapping):
|
|
|
|
value_stix_version = detect_spec_version(value)
|
|
|
|
if value_stix_version != stix_version:
|
|
|
|
result = False
|
|
|
|
|
|
|
|
if result:
|
|
|
|
# No need to check registration in this case
|
|
|
|
type_ = _stix_type_of(value)
|
|
|
|
result = type_ == "marking-definition"
|
2021-01-07 00:59:48 +01:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
class STIXTypeClass(enum.Enum):
|
|
|
|
"""
|
|
|
|
Represents different classes of STIX type.
|
|
|
|
"""
|
|
|
|
SDO = 0
|
|
|
|
SCO = 1
|
|
|
|
SRO = 2
|
|
|
|
|
|
|
|
|
2021-01-16 01:12:14 +01:00
|
|
|
def is_stix_type(value, stix_version=stix2.version.DEFAULT_VERSION, *types):
|
2021-01-07 00:59:48 +01:00
|
|
|
"""
|
|
|
|
Determine whether the type of the given value satisfies the given
|
|
|
|
constraints. 'types' must contain STIX types as strings, and/or the
|
|
|
|
STIXTypeClass enum values. STIX types imply an exact match constraint;
|
|
|
|
STIXTypeClass enum values imply a more general constraint, that the object
|
|
|
|
or type be in that class of STIX type. These constraints are implicitly
|
|
|
|
OR'd together.
|
|
|
|
|
|
|
|
:param value: A mapping with a "type" property, or a STIX ID or type
|
|
|
|
as a string
|
|
|
|
:param stix_version: A STIX version as a string
|
|
|
|
:param types: A sequence of STIX type strings or STIXTypeClass enum values
|
|
|
|
:return: True if the object or type satisfies the constraints; False if not
|
|
|
|
"""
|
|
|
|
|
|
|
|
for type_ in types:
|
|
|
|
if type_ is STIXTypeClass.SDO:
|
|
|
|
result = is_sdo(value, stix_version)
|
|
|
|
elif type_ is STIXTypeClass.SCO:
|
|
|
|
result = is_sco(value, stix_version)
|
|
|
|
elif type_ is STIXTypeClass.SRO:
|
|
|
|
result = is_sro(value, stix_version)
|
|
|
|
else:
|
|
|
|
# Assume a string STIX type is given instead of a class enum,
|
|
|
|
# and just check for exact match.
|
|
|
|
obj_type = _stix_type_of(value)
|
2021-01-20 01:57:05 +01:00
|
|
|
result = obj_type == type_ and is_object(value, stix_version)
|
2021-01-07 00:59:48 +01:00
|
|
|
|
|
|
|
if result:
|
|
|
|
break
|
|
|
|
|
2018-10-26 03:03:27 +02:00
|
|
|
else:
|
2021-01-07 00:59:48 +01:00
|
|
|
result = False
|
2018-10-26 03:03:27 +02:00
|
|
|
|
|
|
|
return result
|