2018-04-09 19:29:53 +02:00
|
|
|
"""STIX 2.0 Domain Objects.
|
|
|
|
"""
|
2017-02-10 22:35:02 +01:00
|
|
|
|
2017-09-01 22:37:49 +02:00
|
|
|
from collections import OrderedDict
|
2018-04-13 20:52:00 +02:00
|
|
|
import re
|
2017-08-11 21:12:45 +02:00
|
|
|
|
2017-06-12 22:15:12 +02:00
|
|
|
import stix2
|
|
|
|
|
2017-10-26 17:39:45 +02:00
|
|
|
from ..base import _STIXBase
|
|
|
|
from ..markings import _MarkingsMixin
|
|
|
|
from ..properties import (BooleanProperty, IDProperty, IntegerProperty,
|
|
|
|
ListProperty, PatternProperty, ReferenceProperty,
|
|
|
|
StringProperty, TimestampProperty, TypeProperty)
|
2018-04-13 20:52:00 +02:00
|
|
|
from ..utils import NOW, TYPE_REGEX
|
2017-08-14 21:21:58 +02:00
|
|
|
from .common import ExternalReference, GranularMarking, KillChainPhase
|
2017-07-14 20:55:57 +02:00
|
|
|
from .observables import ObservableProperty
|
2017-02-10 22:35:02 +01:00
|
|
|
|
|
|
|
|
2017-10-06 02:50:54 +02:00
|
|
|
class STIXDomainObject(_STIXBase, _MarkingsMixin):
|
2017-10-03 21:01:55 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class AttackPattern(STIXDomainObject):
|
2018-02-21 22:42:25 +01:00
|
|
|
"""For more detailed information on this object's properties, see
|
|
|
|
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714302>`__.
|
|
|
|
"""
|
2017-02-22 16:06:35 +01:00
|
|
|
|
|
|
|
_type = 'attack-pattern'
|
2017-08-11 21:12:45 +02:00
|
|
|
_properties = OrderedDict()
|
|
|
|
_properties.update([
|
|
|
|
('type', TypeProperty(_type)),
|
|
|
|
('id', IDProperty(_type)),
|
|
|
|
('created_by_ref', ReferenceProperty(type="identity")),
|
|
|
|
('created', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('modified', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('name', StringProperty(required=True)),
|
|
|
|
('description', StringProperty()),
|
|
|
|
('kill_chain_phases', ListProperty(KillChainPhase)),
|
2018-04-16 20:37:07 +02:00
|
|
|
('revoked', BooleanProperty(default=lambda: False)),
|
2017-08-11 21:12:45 +02:00
|
|
|
('labels', ListProperty(StringProperty)),
|
|
|
|
('external_references', ListProperty(ExternalReference)),
|
|
|
|
('object_marking_refs', ListProperty(ReferenceProperty(type="marking-definition"))),
|
|
|
|
('granular_markings', ListProperty(GranularMarking)),
|
|
|
|
])
|
2017-02-22 16:06:35 +01:00
|
|
|
|
|
|
|
|
2017-10-03 21:01:55 +02:00
|
|
|
class Campaign(STIXDomainObject):
|
2018-02-21 22:42:25 +01:00
|
|
|
"""For more detailed information on this object's properties, see
|
|
|
|
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714305>`__.
|
|
|
|
"""
|
2017-02-23 16:11:56 +01:00
|
|
|
|
|
|
|
_type = 'campaign'
|
2017-08-11 21:12:45 +02:00
|
|
|
_properties = OrderedDict()
|
|
|
|
_properties.update([
|
|
|
|
('type', TypeProperty(_type)),
|
|
|
|
('id', IDProperty(_type)),
|
|
|
|
('created_by_ref', ReferenceProperty(type="identity")),
|
|
|
|
('created', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('modified', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('name', StringProperty(required=True)),
|
|
|
|
('description', StringProperty()),
|
|
|
|
('aliases', ListProperty(StringProperty)),
|
|
|
|
('first_seen', TimestampProperty()),
|
|
|
|
('last_seen', TimestampProperty()),
|
|
|
|
('objective', StringProperty()),
|
2018-04-16 20:37:07 +02:00
|
|
|
('revoked', BooleanProperty(default=lambda: False)),
|
2017-08-11 21:12:45 +02:00
|
|
|
('labels', ListProperty(StringProperty)),
|
|
|
|
('external_references', ListProperty(ExternalReference)),
|
|
|
|
('object_marking_refs', ListProperty(ReferenceProperty(type="marking-definition"))),
|
|
|
|
('granular_markings', ListProperty(GranularMarking)),
|
|
|
|
])
|
2017-02-23 16:11:56 +01:00
|
|
|
|
|
|
|
|
2017-10-03 21:01:55 +02:00
|
|
|
class CourseOfAction(STIXDomainObject):
|
2018-02-21 22:42:25 +01:00
|
|
|
"""For more detailed information on this object's properties, see
|
|
|
|
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714308>`__.
|
|
|
|
"""
|
2017-02-23 16:11:56 +01:00
|
|
|
|
|
|
|
_type = 'course-of-action'
|
2017-08-11 21:12:45 +02:00
|
|
|
_properties = OrderedDict()
|
|
|
|
_properties.update([
|
|
|
|
('type', TypeProperty(_type)),
|
|
|
|
('id', IDProperty(_type)),
|
|
|
|
('created_by_ref', ReferenceProperty(type="identity")),
|
|
|
|
('created', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('modified', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('name', StringProperty(required=True)),
|
|
|
|
('description', StringProperty()),
|
2018-04-16 20:37:07 +02:00
|
|
|
('revoked', BooleanProperty(default=lambda: False)),
|
2017-08-11 21:12:45 +02:00
|
|
|
('labels', ListProperty(StringProperty)),
|
|
|
|
('external_references', ListProperty(ExternalReference)),
|
|
|
|
('object_marking_refs', ListProperty(ReferenceProperty(type="marking-definition"))),
|
|
|
|
('granular_markings', ListProperty(GranularMarking)),
|
|
|
|
])
|
2017-02-23 16:11:56 +01:00
|
|
|
|
|
|
|
|
2017-10-03 21:01:55 +02:00
|
|
|
class Identity(STIXDomainObject):
|
2018-02-21 22:42:25 +01:00
|
|
|
"""For more detailed information on this object's properties, see
|
|
|
|
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714311>`__.
|
|
|
|
"""
|
2017-02-23 16:11:56 +01:00
|
|
|
|
|
|
|
_type = 'identity'
|
2017-08-11 21:12:45 +02:00
|
|
|
_properties = OrderedDict()
|
|
|
|
_properties.update([
|
|
|
|
('type', TypeProperty(_type)),
|
|
|
|
('id', IDProperty(_type)),
|
|
|
|
('created_by_ref', ReferenceProperty(type="identity")),
|
|
|
|
('created', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('modified', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('name', StringProperty(required=True)),
|
|
|
|
('description', StringProperty()),
|
|
|
|
('identity_class', StringProperty(required=True)),
|
|
|
|
('sectors', ListProperty(StringProperty)),
|
|
|
|
('contact_information', StringProperty()),
|
2018-04-16 20:37:07 +02:00
|
|
|
('revoked', BooleanProperty(default=lambda: False)),
|
2017-08-11 21:12:45 +02:00
|
|
|
('labels', ListProperty(StringProperty)),
|
|
|
|
('external_references', ListProperty(ExternalReference)),
|
|
|
|
('object_marking_refs', ListProperty(ReferenceProperty(type="marking-definition"))),
|
|
|
|
('granular_markings', ListProperty(GranularMarking)),
|
|
|
|
])
|
2017-02-23 16:11:56 +01:00
|
|
|
|
|
|
|
|
2017-10-03 21:01:55 +02:00
|
|
|
class Indicator(STIXDomainObject):
|
2018-02-21 22:42:25 +01:00
|
|
|
"""For more detailed information on this object's properties, see
|
|
|
|
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714314>`__.
|
|
|
|
"""
|
2017-02-10 22:35:02 +01:00
|
|
|
|
|
|
|
_type = 'indicator'
|
2017-08-11 21:12:45 +02:00
|
|
|
_properties = OrderedDict()
|
|
|
|
_properties.update([
|
|
|
|
('type', TypeProperty(_type)),
|
|
|
|
('id', IDProperty(_type)),
|
|
|
|
('created_by_ref', ReferenceProperty(type="identity")),
|
|
|
|
('created', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('modified', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('name', StringProperty()),
|
|
|
|
('description', StringProperty()),
|
2017-08-22 00:40:07 +02:00
|
|
|
('pattern', PatternProperty(required=True)),
|
2017-08-11 21:12:45 +02:00
|
|
|
('valid_from', TimestampProperty(default=lambda: NOW)),
|
|
|
|
('valid_until', TimestampProperty()),
|
|
|
|
('kill_chain_phases', ListProperty(KillChainPhase)),
|
2018-04-16 20:37:07 +02:00
|
|
|
('revoked', BooleanProperty(default=lambda: False)),
|
2017-10-06 16:29:30 +02:00
|
|
|
('labels', ListProperty(StringProperty, required=True)),
|
2017-08-11 21:12:45 +02:00
|
|
|
('external_references', ListProperty(ExternalReference)),
|
|
|
|
('object_marking_refs', ListProperty(ReferenceProperty(type="marking-definition"))),
|
|
|
|
('granular_markings', ListProperty(GranularMarking)),
|
|
|
|
])
|
2017-02-10 22:35:02 +01:00
|
|
|
|
|
|
|
|
2017-10-03 21:01:55 +02:00
|
|
|
class IntrusionSet(STIXDomainObject):
|
2018-02-21 22:42:25 +01:00
|
|
|
"""For more detailed information on this object's properties, see
|
|
|
|
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714317>`__.
|
|
|
|
"""
|
2017-02-22 16:06:35 +01:00
|
|
|
|
|
|
|
_type = 'intrusion-set'
|
2017-08-11 21:12:45 +02:00
|
|
|
_properties = OrderedDict()
|
|
|
|
_properties.update([
|
|
|
|
('type', TypeProperty(_type)),
|
|
|
|
('id', IDProperty(_type)),
|
|
|
|
('created_by_ref', ReferenceProperty(type="identity")),
|
|
|
|
('created', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('modified', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('name', StringProperty(required=True)),
|
|
|
|
('description', StringProperty()),
|
|
|
|
('aliases', ListProperty(StringProperty)),
|
|
|
|
('first_seen', TimestampProperty()),
|
2018-01-16 15:49:15 +01:00
|
|
|
('last_seen', TimestampProperty()),
|
2017-08-11 21:12:45 +02:00
|
|
|
('goals', ListProperty(StringProperty)),
|
|
|
|
('resource_level', StringProperty()),
|
|
|
|
('primary_motivation', StringProperty()),
|
|
|
|
('secondary_motivations', ListProperty(StringProperty)),
|
2018-04-16 20:37:07 +02:00
|
|
|
('revoked', BooleanProperty(default=lambda: False)),
|
2017-08-11 21:12:45 +02:00
|
|
|
('labels', ListProperty(StringProperty)),
|
|
|
|
('external_references', ListProperty(ExternalReference)),
|
|
|
|
('object_marking_refs', ListProperty(ReferenceProperty(type="marking-definition"))),
|
|
|
|
('granular_markings', ListProperty(GranularMarking)),
|
|
|
|
])
|
2017-02-22 16:06:35 +01:00
|
|
|
|
|
|
|
|
2017-10-03 21:01:55 +02:00
|
|
|
class Malware(STIXDomainObject):
|
2018-02-21 22:42:25 +01:00
|
|
|
"""For more detailed information on this object's properties, see
|
|
|
|
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714320>`__.
|
|
|
|
"""
|
2017-02-10 22:35:02 +01:00
|
|
|
|
|
|
|
_type = 'malware'
|
2017-08-11 21:12:45 +02:00
|
|
|
_properties = OrderedDict()
|
|
|
|
_properties.update([
|
|
|
|
('type', TypeProperty(_type)),
|
|
|
|
('id', IDProperty(_type)),
|
|
|
|
('created_by_ref', ReferenceProperty(type="identity")),
|
|
|
|
('created', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('modified', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('name', StringProperty(required=True)),
|
|
|
|
('description', StringProperty()),
|
|
|
|
('kill_chain_phases', ListProperty(KillChainPhase)),
|
2018-04-16 20:37:07 +02:00
|
|
|
('revoked', BooleanProperty(default=lambda: False)),
|
2017-08-11 21:12:45 +02:00
|
|
|
('labels', ListProperty(StringProperty, required=True)),
|
|
|
|
('external_references', ListProperty(ExternalReference)),
|
|
|
|
('object_marking_refs', ListProperty(ReferenceProperty(type="marking-definition"))),
|
|
|
|
('granular_markings', ListProperty(GranularMarking)),
|
|
|
|
])
|
2017-02-10 22:35:02 +01:00
|
|
|
|
2017-02-22 16:06:35 +01:00
|
|
|
|
2017-10-03 21:01:55 +02:00
|
|
|
class ObservedData(STIXDomainObject):
|
2018-02-21 22:42:25 +01:00
|
|
|
"""For more detailed information on this object's properties, see
|
|
|
|
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714323>`__.
|
|
|
|
"""
|
2017-02-23 16:11:56 +01:00
|
|
|
|
|
|
|
_type = 'observed-data'
|
2017-08-11 21:12:45 +02:00
|
|
|
_properties = OrderedDict()
|
|
|
|
_properties.update([
|
|
|
|
('type', TypeProperty(_type)),
|
|
|
|
('id', IDProperty(_type)),
|
|
|
|
('created_by_ref', ReferenceProperty(type="identity")),
|
|
|
|
('created', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('modified', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('first_observed', TimestampProperty(required=True)),
|
|
|
|
('last_observed', TimestampProperty(required=True)),
|
|
|
|
('number_observed', IntegerProperty(required=True)),
|
2017-10-06 16:29:30 +02:00
|
|
|
('objects', ObservableProperty(required=True)),
|
2018-04-16 20:37:07 +02:00
|
|
|
('revoked', BooleanProperty(default=lambda: False)),
|
2017-08-11 21:12:45 +02:00
|
|
|
('labels', ListProperty(StringProperty)),
|
|
|
|
('external_references', ListProperty(ExternalReference)),
|
|
|
|
('object_marking_refs', ListProperty(ReferenceProperty(type="marking-definition"))),
|
|
|
|
('granular_markings', ListProperty(GranularMarking)),
|
|
|
|
])
|
2017-02-23 16:11:56 +01:00
|
|
|
|
2018-05-16 18:14:33 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.__allow_custom = kwargs.get('allow_custom', False)
|
|
|
|
self._properties['objects'].allow_custom = kwargs.get('allow_custom', False)
|
2018-05-11 23:28:55 +02:00
|
|
|
|
|
|
|
super(ObservedData, self).__init__(*args, **kwargs)
|
|
|
|
|
2017-02-23 16:11:56 +01:00
|
|
|
|
2017-10-03 21:01:55 +02:00
|
|
|
class Report(STIXDomainObject):
|
2018-02-21 22:42:25 +01:00
|
|
|
"""For more detailed information on this object's properties, see
|
|
|
|
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714326>`__.
|
|
|
|
"""
|
2017-02-23 16:11:56 +01:00
|
|
|
|
|
|
|
_type = 'report'
|
2017-08-11 21:12:45 +02:00
|
|
|
_properties = OrderedDict()
|
|
|
|
_properties.update([
|
|
|
|
('type', TypeProperty(_type)),
|
|
|
|
('id', IDProperty(_type)),
|
|
|
|
('created_by_ref', ReferenceProperty(type="identity")),
|
|
|
|
('created', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('modified', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('name', StringProperty(required=True)),
|
|
|
|
('description', StringProperty()),
|
2017-10-06 16:29:30 +02:00
|
|
|
('published', TimestampProperty(required=True)),
|
|
|
|
('object_refs', ListProperty(ReferenceProperty, required=True)),
|
2018-04-16 20:37:07 +02:00
|
|
|
('revoked', BooleanProperty(default=lambda: False)),
|
2017-08-11 21:12:45 +02:00
|
|
|
('labels', ListProperty(StringProperty, required=True)),
|
|
|
|
('external_references', ListProperty(ExternalReference)),
|
|
|
|
('object_marking_refs', ListProperty(ReferenceProperty(type="marking-definition"))),
|
|
|
|
('granular_markings', ListProperty(GranularMarking)),
|
|
|
|
])
|
2017-02-23 16:11:56 +01:00
|
|
|
|
|
|
|
|
2017-10-03 21:01:55 +02:00
|
|
|
class ThreatActor(STIXDomainObject):
|
2018-02-21 22:42:25 +01:00
|
|
|
"""For more detailed information on this object's properties, see
|
|
|
|
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714329>`__.
|
|
|
|
"""
|
2017-02-23 16:11:56 +01:00
|
|
|
|
|
|
|
_type = 'threat-actor'
|
2017-08-11 21:12:45 +02:00
|
|
|
_properties = OrderedDict()
|
|
|
|
_properties.update([
|
|
|
|
('type', TypeProperty(_type)),
|
|
|
|
('id', IDProperty(_type)),
|
|
|
|
('created_by_ref', ReferenceProperty(type="identity")),
|
|
|
|
('created', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('modified', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('name', StringProperty(required=True)),
|
|
|
|
('description', StringProperty()),
|
|
|
|
('aliases', ListProperty(StringProperty)),
|
|
|
|
('roles', ListProperty(StringProperty)),
|
|
|
|
('goals', ListProperty(StringProperty)),
|
|
|
|
('sophistication', StringProperty()),
|
|
|
|
('resource_level', StringProperty()),
|
|
|
|
('primary_motivation', StringProperty()),
|
|
|
|
('secondary_motivations', ListProperty(StringProperty)),
|
|
|
|
('personal_motivations', ListProperty(StringProperty)),
|
2018-04-16 20:37:07 +02:00
|
|
|
('revoked', BooleanProperty(default=lambda: False)),
|
2017-08-11 21:12:45 +02:00
|
|
|
('labels', ListProperty(StringProperty, required=True)),
|
|
|
|
('external_references', ListProperty(ExternalReference)),
|
|
|
|
('object_marking_refs', ListProperty(ReferenceProperty(type="marking-definition"))),
|
|
|
|
('granular_markings', ListProperty(GranularMarking)),
|
|
|
|
])
|
2017-02-23 16:11:56 +01:00
|
|
|
|
|
|
|
|
2017-10-03 21:01:55 +02:00
|
|
|
class Tool(STIXDomainObject):
|
2018-02-21 22:42:25 +01:00
|
|
|
"""For more detailed information on this object's properties, see
|
|
|
|
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714332>`__.
|
|
|
|
"""
|
2017-02-22 16:06:35 +01:00
|
|
|
|
|
|
|
_type = 'tool'
|
2017-08-11 21:12:45 +02:00
|
|
|
_properties = OrderedDict()
|
|
|
|
_properties.update([
|
|
|
|
('type', TypeProperty(_type)),
|
|
|
|
('id', IDProperty(_type)),
|
|
|
|
('created_by_ref', ReferenceProperty(type="identity")),
|
|
|
|
('created', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('modified', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('name', StringProperty(required=True)),
|
|
|
|
('description', StringProperty()),
|
|
|
|
('kill_chain_phases', ListProperty(KillChainPhase)),
|
|
|
|
('tool_version', StringProperty()),
|
2018-04-16 20:37:07 +02:00
|
|
|
('revoked', BooleanProperty(default=lambda: False)),
|
2017-08-11 21:12:45 +02:00
|
|
|
('labels', ListProperty(StringProperty, required=True)),
|
|
|
|
('external_references', ListProperty(ExternalReference)),
|
|
|
|
('object_marking_refs', ListProperty(ReferenceProperty(type="marking-definition"))),
|
|
|
|
('granular_markings', ListProperty(GranularMarking)),
|
|
|
|
])
|
2017-02-22 16:06:35 +01:00
|
|
|
|
2017-02-23 16:11:56 +01:00
|
|
|
|
2017-10-03 21:01:55 +02:00
|
|
|
class Vulnerability(STIXDomainObject):
|
2018-02-21 22:42:25 +01:00
|
|
|
"""For more detailed information on this object's properties, see
|
|
|
|
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part2-stix-objects/stix-v2.0-cs01-part2-stix-objects.html#_Toc496714335>`__.
|
|
|
|
"""
|
2017-02-23 16:11:56 +01:00
|
|
|
|
|
|
|
_type = 'vulnerability'
|
2017-08-11 21:12:45 +02:00
|
|
|
_properties = OrderedDict()
|
|
|
|
_properties.update([
|
|
|
|
('type', TypeProperty(_type)),
|
|
|
|
('id', IDProperty(_type)),
|
|
|
|
('created_by_ref', ReferenceProperty(type="identity")),
|
|
|
|
('created', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('modified', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('name', StringProperty(required=True)),
|
|
|
|
('description', StringProperty()),
|
2018-04-16 20:37:07 +02:00
|
|
|
('revoked', BooleanProperty(default=lambda: False)),
|
2017-08-11 21:12:45 +02:00
|
|
|
('labels', ListProperty(StringProperty)),
|
|
|
|
('external_references', ListProperty(ExternalReference)),
|
|
|
|
('object_marking_refs', ListProperty(ReferenceProperty(type="marking-definition"))),
|
|
|
|
('granular_markings', ListProperty(GranularMarking)),
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
def CustomObject(type='x-custom-type', properties=None):
|
2017-09-22 16:01:00 +02:00
|
|
|
"""Custom STIX Object type decorator.
|
2017-06-13 16:26:43 +02:00
|
|
|
|
2017-09-22 16:01:00 +02:00
|
|
|
Example:
|
|
|
|
>>> @CustomObject('x-type-name', [
|
|
|
|
... ('property1', StringProperty(required=True)),
|
|
|
|
... ('property2', IntegerProperty()),
|
|
|
|
... ])
|
|
|
|
... class MyNewObjectType():
|
|
|
|
... pass
|
2017-06-13 16:26:43 +02:00
|
|
|
|
2017-09-22 17:03:25 +02:00
|
|
|
Supply an ``__init__()`` function to add any special validations to the custom
|
|
|
|
type. Don't call ``super().__init__()`` though - doing so will cause an error.
|
2017-06-13 16:26:43 +02:00
|
|
|
|
2017-09-22 16:01:00 +02:00
|
|
|
Example:
|
|
|
|
>>> @CustomObject('x-type-name', [
|
|
|
|
... ('property1', StringProperty(required=True)),
|
|
|
|
... ('property2', IntegerProperty()),
|
|
|
|
... ])
|
|
|
|
... class MyNewObjectType():
|
|
|
|
... def __init__(self, property2=None, **kwargs):
|
|
|
|
... if property2 and property2 < 10:
|
|
|
|
... raise ValueError("'property2' is too small.")
|
2017-06-13 16:26:43 +02:00
|
|
|
"""
|
2017-06-12 22:15:12 +02:00
|
|
|
|
|
|
|
def custom_builder(cls):
|
|
|
|
|
2017-10-03 21:01:55 +02:00
|
|
|
class _Custom(cls, STIXDomainObject):
|
2018-04-13 20:52:00 +02:00
|
|
|
|
|
|
|
if not re.match(TYPE_REGEX, type):
|
|
|
|
raise ValueError("Invalid type name '%s': must only contain the "
|
|
|
|
"characters a-z (lowercase ASCII), 0-9, and hyphen (-)." % type)
|
|
|
|
elif len(type) < 3 or len(type) > 250:
|
|
|
|
raise ValueError("Invalid type name '%s': must be between 3 and 250 characters." % type)
|
|
|
|
|
2017-06-12 22:15:12 +02:00
|
|
|
_type = type
|
2017-08-11 21:12:45 +02:00
|
|
|
_properties = OrderedDict()
|
|
|
|
_properties.update([
|
|
|
|
('type', TypeProperty(_type)),
|
|
|
|
('id', IDProperty(_type)),
|
|
|
|
('created_by_ref', ReferenceProperty(type="identity")),
|
|
|
|
('created', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
('modified', TimestampProperty(default=lambda: NOW, precision='millisecond')),
|
|
|
|
])
|
|
|
|
|
2017-08-28 20:30:53 +02:00
|
|
|
if not properties or not isinstance(properties, list):
|
2017-08-11 21:12:45 +02:00
|
|
|
raise ValueError("Must supply a list, containing tuples. For example, [('property1', IntegerProperty())]")
|
|
|
|
|
2017-08-28 20:30:53 +02:00
|
|
|
_properties.update([x for x in properties if not x[0].startswith("x_")])
|
2017-06-13 16:26:43 +02:00
|
|
|
|
2017-08-11 21:12:45 +02:00
|
|
|
# This is to follow the general properties structure.
|
|
|
|
_properties.update([
|
2018-04-16 20:37:07 +02:00
|
|
|
('revoked', BooleanProperty(default=lambda: False)),
|
2017-08-11 21:12:45 +02:00
|
|
|
('labels', ListProperty(StringProperty)),
|
|
|
|
('external_references', ListProperty(ExternalReference)),
|
|
|
|
('object_marking_refs', ListProperty(ReferenceProperty(type="marking-definition"))),
|
|
|
|
('granular_markings', ListProperty(GranularMarking)),
|
|
|
|
])
|
|
|
|
|
2017-08-14 15:06:20 +02:00
|
|
|
# Put all custom properties at the bottom, sorted alphabetically.
|
2017-08-28 20:30:53 +02:00
|
|
|
_properties.update(sorted([x for x in properties if x[0].startswith("x_")], key=lambda x: x[0]))
|
2017-08-14 15:06:20 +02:00
|
|
|
|
2017-06-13 16:26:43 +02:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
_STIXBase.__init__(self, **kwargs)
|
2017-09-20 23:13:51 +02:00
|
|
|
try:
|
|
|
|
cls.__init__(self, **kwargs)
|
|
|
|
except (AttributeError, TypeError) as e:
|
|
|
|
# Don't accidentally catch errors raised in a custom __init__()
|
|
|
|
if ("has no attribute '__init__'" in str(e) or
|
|
|
|
str(e) == "object.__init__() takes no parameters"):
|
|
|
|
return
|
|
|
|
raise e
|
2017-06-13 16:26:43 +02:00
|
|
|
|
2017-10-26 17:39:45 +02:00
|
|
|
stix2._register_type(_Custom, version="2.0")
|
2017-06-12 22:15:12 +02:00
|
|
|
return _Custom
|
|
|
|
|
|
|
|
return custom_builder
|