2017-02-10 22:35:02 +01:00
|
|
|
"""Python APIs for STIX 2."""
|
2017-01-17 21:37:47 +01:00
|
|
|
|
2017-03-22 14:05:59 +01:00
|
|
|
# flake8: noqa
|
|
|
|
|
2017-05-09 21:10:53 +02:00
|
|
|
from . import exceptions
|
2017-02-10 22:35:02 +01:00
|
|
|
from .bundle import Bundle
|
2017-05-15 19:48:41 +02:00
|
|
|
from .observables import (URL, AlternateDataStream, ArchiveExt, Artifact,
|
|
|
|
AutonomousSystem, Directory, DomainName,
|
|
|
|
EmailAddress, EmailMessage, EmailMIMEComponent, File,
|
|
|
|
HTTPRequestExt, ICMPExt, IPv4Address, IPv6Address,
|
|
|
|
MACAddress, Mutex, NetworkTraffic, NTFSExt, PDFExt,
|
|
|
|
Process, RasterImageExt, SocketExt, Software, TCPExt,
|
|
|
|
UNIXAccountExt, UserAccount, WindowsPEBinaryExt,
|
|
|
|
WindowsPEOptionalHeaderType, WindowsPESection,
|
|
|
|
WindowsProcessExt, WindowsRegistryKey,
|
|
|
|
WindowsRegistryValueType, WindowsServiceExt,
|
|
|
|
X509Certificate, X509V3ExtenstionsType)
|
2017-05-09 21:10:53 +02:00
|
|
|
from .other import (ExternalReference, GranularMarking, KillChainPhase,
|
|
|
|
MarkingDefinition, StatementMarking, TLPMarking)
|
|
|
|
from .sdo import (AttackPattern, Campaign, CourseOfAction, Identity, Indicator,
|
|
|
|
IntrusionSet, Malware, ObservedData, Report, ThreatActor,
|
|
|
|
Tool, Vulnerability)
|
2017-03-31 21:52:27 +02:00
|
|
|
from .sro import Relationship, Sighting
|
2017-04-19 20:32:56 +02:00
|
|
|
from .utils import get_dict
|
2017-04-05 23:12:44 +02:00
|
|
|
|
2017-04-24 22:33:59 +02:00
|
|
|
OBJ_MAP = {
|
|
|
|
'attack-pattern': AttackPattern,
|
|
|
|
'campaign': Campaign,
|
|
|
|
'course-of-action': CourseOfAction,
|
|
|
|
'identity': Identity,
|
|
|
|
'indicator': Indicator,
|
|
|
|
'intrusion-set': IntrusionSet,
|
|
|
|
'malware': Malware,
|
|
|
|
'marking-definition': MarkingDefinition,
|
|
|
|
'observed-data': ObservedData,
|
|
|
|
'report': Report,
|
|
|
|
'relationship': Relationship,
|
|
|
|
'threat-actor': ThreatActor,
|
|
|
|
'tool': Tool,
|
|
|
|
'sighting': Sighting,
|
|
|
|
'vulnerability': Vulnerability,
|
|
|
|
}
|
|
|
|
|
2017-05-03 23:35:33 +02:00
|
|
|
OBJ_MAP_OBSERVABLE = {
|
|
|
|
'artifact': Artifact,
|
2017-05-04 00:19:30 +02:00
|
|
|
'autonomous-system': AutonomousSystem,
|
2017-05-09 03:03:15 +02:00
|
|
|
'directory': Directory,
|
|
|
|
'domain-name': DomainName,
|
2017-05-05 18:32:02 +02:00
|
|
|
'email-address': EmailAddress,
|
2017-05-09 17:03:19 +02:00
|
|
|
'email-message': EmailMessage,
|
2017-05-03 23:35:33 +02:00
|
|
|
'file': File,
|
2017-05-09 03:03:15 +02:00
|
|
|
'ipv4-addr': IPv4Address,
|
|
|
|
'ipv6-addr': IPv6Address,
|
|
|
|
'mac-addr': MACAddress,
|
|
|
|
'mutex': Mutex,
|
|
|
|
'network-traffic': NetworkTraffic,
|
|
|
|
'process': Process,
|
|
|
|
'software': Software,
|
|
|
|
'url': URL,
|
|
|
|
'user-account': UserAccount,
|
|
|
|
'windows-registry-key': WindowsRegistryKey,
|
|
|
|
'x509-certificate': X509Certificate,
|
2017-05-03 23:35:33 +02:00
|
|
|
}
|
|
|
|
|
2017-05-12 17:22:23 +02:00
|
|
|
EXT_MAP_FILE = {
|
|
|
|
'archive-ext': ArchiveExt,
|
2017-05-15 19:48:41 +02:00
|
|
|
'ntfs-ext': NTFSExt,
|
|
|
|
'pdf-ext': PDFExt,
|
|
|
|
'raster-image-ext': RasterImageExt,
|
|
|
|
'windows-pebinary-ext': WindowsPEBinaryExt
|
|
|
|
}
|
|
|
|
|
|
|
|
EXT_MAP_NETWORK_TRAFFIC = {
|
|
|
|
'http-request-ext': HTTPRequestExt,
|
|
|
|
'icmp-ext': ICMPExt,
|
|
|
|
'socket-ext': SocketExt,
|
|
|
|
'tcp-ext': TCPExt,
|
|
|
|
}
|
|
|
|
|
|
|
|
EXT_MAP_PROCESS = {
|
|
|
|
'windows-process-ext': WindowsProcessExt,
|
|
|
|
'windows-service-ext': WindowsServiceExt,
|
|
|
|
}
|
|
|
|
|
|
|
|
EXT_MAP_USER_ACCOUNT = {
|
|
|
|
'unix-account-ext': UNIXAccountExt,
|
2017-05-12 17:22:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EXT_MAP = {
|
|
|
|
'file': EXT_MAP_FILE,
|
2017-05-15 19:48:41 +02:00
|
|
|
'network-traffic': EXT_MAP_NETWORK_TRAFFIC,
|
|
|
|
'process': EXT_MAP_PROCESS,
|
|
|
|
'user-account': EXT_MAP_USER_ACCOUNT,
|
|
|
|
|
2017-05-12 17:22:23 +02:00
|
|
|
}
|
|
|
|
|
2017-04-24 22:33:59 +02:00
|
|
|
|
2017-06-12 18:54:05 +02:00
|
|
|
def parse(data, allow_custom=False):
|
|
|
|
"""Deserialize a string or file-like object into a STIX object.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data: The STIX 2 string to be parsed.
|
|
|
|
allow_custom (bool): Whether to allow custom properties or not. Default: False.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
An instantiated Python STIX object.
|
|
|
|
"""
|
2017-04-05 23:12:44 +02:00
|
|
|
|
2017-04-19 20:32:56 +02:00
|
|
|
obj = get_dict(data)
|
2017-04-05 23:12:44 +02:00
|
|
|
|
|
|
|
if 'type' not in obj:
|
2017-06-12 18:54:05 +02:00
|
|
|
raise exceptions.ParseError("Can't parse object with no 'type' property: %s" % str(obj))
|
|
|
|
|
|
|
|
try:
|
|
|
|
obj_class = OBJ_MAP[obj['type']]
|
|
|
|
except KeyError:
|
|
|
|
# TODO handle custom objects
|
|
|
|
raise exceptions.ParseError("Can't parse unknown object type '%s'!" % obj['type'])
|
|
|
|
return obj_class(allow_custom=allow_custom, **obj)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_observable(data, _valid_refs, allow_custom=False):
|
|
|
|
"""Deserialize a string or file-like object into a STIX Cyber Observable object.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data: The STIX 2 string to be parsed.
|
|
|
|
_valid_refs: A list of object references valid for the scope of the object being parsed.
|
|
|
|
allow_custom: Whether to allow custom properties or not. Default: False.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
An instantiated Python STIX Cyber Observable object.
|
2017-05-05 18:32:02 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
obj = get_dict(data)
|
|
|
|
obj['_valid_refs'] = _valid_refs
|
|
|
|
|
|
|
|
if 'type' not in obj:
|
2017-06-12 18:54:05 +02:00
|
|
|
raise exceptions.ParseError("Can't parse object with no 'type' property: %s" % str(obj))
|
2017-05-05 18:32:02 +02:00
|
|
|
try:
|
|
|
|
obj_class = OBJ_MAP_OBSERVABLE[obj['type']]
|
|
|
|
except KeyError:
|
2017-05-12 17:22:23 +02:00
|
|
|
# TODO handle custom observable objects
|
2017-06-12 18:54:05 +02:00
|
|
|
raise exceptions.ParseError("Can't parse unknown object type '%s'!" % obj['type'])
|
2017-05-12 17:22:23 +02:00
|
|
|
|
|
|
|
if 'extensions' in obj and obj['type'] in EXT_MAP:
|
|
|
|
for name, ext in obj['extensions'].items():
|
|
|
|
if name not in EXT_MAP[obj['type']]:
|
2017-06-12 18:54:05 +02:00
|
|
|
raise exceptions.ParseError("Can't parse Unknown extension type '%s' for object type '%s'!" % (name, obj['type']))
|
2017-05-12 17:22:23 +02:00
|
|
|
ext_class = EXT_MAP[obj['type']][name]
|
2017-06-12 18:54:05 +02:00
|
|
|
obj['extensions'][name] = ext_class(allow_custom=allow_custom, **obj['extensions'][name])
|
2017-05-12 17:22:23 +02:00
|
|
|
|
2017-06-12 18:54:05 +02:00
|
|
|
return obj_class(allow_custom=allow_custom, **obj)
|
2017-06-12 22:15:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
def _register_type(new_type):
|
|
|
|
"""Register a custom STIX Object type.
|
|
|
|
"""
|
|
|
|
|
|
|
|
OBJ_MAP[new_type._type] = new_type
|