2020-06-18 16:23:08 +02:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
2018-07-26 02:53:53 +02:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
import stix2
|
2020-03-27 10:53:39 +01:00
|
|
|
from stix2 import exceptions, parsing
|
2018-07-26 02:53:53 +02:00
|
|
|
|
|
|
|
BUNDLE = {
|
|
|
|
"type": "bundle",
|
|
|
|
"id": "bundle--00000000-0000-4000-8000-000000000007",
|
|
|
|
"objects": [
|
|
|
|
{
|
|
|
|
"type": "indicator",
|
|
|
|
"spec_version": "2.1",
|
|
|
|
"id": "indicator--00000000-0000-4000-8000-000000000001",
|
|
|
|
"created": "2017-01-01T12:34:56.000Z",
|
|
|
|
"modified": "2017-01-01T12:34:56.000Z",
|
|
|
|
"pattern": "[file:hashes.MD5 = 'd41d8cd98f00b204e9800998ecf8427e']",
|
2019-07-16 22:10:25 +02:00
|
|
|
"pattern_type": "stix",
|
2018-07-26 02:53:53 +02:00
|
|
|
"valid_from": "2017-01-01T12:34:56Z",
|
|
|
|
"indicator_types": [
|
|
|
|
"malicious-activity",
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "malware",
|
|
|
|
"spec_version": "2.1",
|
|
|
|
"id": "malware--00000000-0000-4000-8000-000000000003",
|
|
|
|
"created": "2017-01-01T12:34:56.000Z",
|
|
|
|
"modified": "2017-01-01T12:34:56.000Z",
|
|
|
|
"name": "Cryptolocker",
|
|
|
|
"malware_types": [
|
|
|
|
"ransomware",
|
|
|
|
],
|
2019-07-01 21:26:30 +02:00
|
|
|
"is_family": False,
|
2018-07-26 02:53:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "relationship",
|
|
|
|
"spec_version": "2.1",
|
|
|
|
"id": "relationship--00000000-0000-4000-8000-000000000005",
|
|
|
|
"created": "2017-01-01T12:34:56.000Z",
|
|
|
|
"modified": "2017-01-01T12:34:56.000Z",
|
|
|
|
"relationship_type": "indicates",
|
|
|
|
"source_ref": "indicator--a740531e-63ff-4e49-a9e1-a0a3eed0e3e7",
|
|
|
|
"target_ref": "malware--9c4638ec-f1de-4ddb-abf4-1b760417654e",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def test_dict_to_stix2_bundle_with_version():
|
|
|
|
with pytest.raises(exceptions.InvalidValueError) as excinfo:
|
2020-03-27 10:53:39 +01:00
|
|
|
parsing.dict_to_stix2(BUNDLE, version='2.0')
|
2018-07-26 02:53:53 +02:00
|
|
|
|
|
|
|
msg = "Invalid value for Bundle 'objects': Spec version 2.0 bundles don't yet support containing objects of a different spec version."
|
|
|
|
assert str(excinfo.value) == msg
|
|
|
|
|
|
|
|
|
2018-11-01 14:21:02 +01:00
|
|
|
def test_parse_observable_with_version():
|
2018-07-26 02:53:53 +02:00
|
|
|
observable = {"type": "file", "name": "foo.exe"}
|
2020-03-27 10:53:39 +01:00
|
|
|
obs_obj = parsing.parse_observable(observable, version='2.1')
|
2018-11-01 14:21:02 +01:00
|
|
|
v = 'v21'
|
2018-07-26 02:53:53 +02:00
|
|
|
|
2018-11-01 14:21:02 +01:00
|
|
|
assert v in str(obs_obj.__class__)
|
2018-07-26 02:53:53 +02:00
|
|
|
|
|
|
|
|
2018-11-29 20:41:57 +01:00
|
|
|
@pytest.mark.xfail(reason="The default version is not 2.1", condition=stix2.DEFAULT_VERSION != "2.1")
|
|
|
|
def test_parse_observable_with_no_version():
|
2020-06-18 16:23:08 +02:00
|
|
|
observable = {"type": "file", "name": "foo.exe", "spec_version": "2.1"}
|
2020-03-27 10:53:39 +01:00
|
|
|
obs_obj = parsing.parse_observable(observable)
|
2018-11-29 20:41:57 +01:00
|
|
|
v = 'v21'
|
|
|
|
|
|
|
|
assert v in str(obs_obj.__class__)
|
|
|
|
|
|
|
|
|
2018-11-01 14:21:02 +01:00
|
|
|
def test_register_marking_with_version():
|
2020-06-18 16:23:08 +02:00
|
|
|
class NewMarking1:
|
|
|
|
_type = 'x-new-marking1'
|
|
|
|
_properties = OrderedDict()
|
|
|
|
|
|
|
|
parsing._register_marking(NewMarking1, version='2.1')
|
2018-11-01 14:21:02 +01:00
|
|
|
v = 'v21'
|
2018-07-26 02:53:53 +02:00
|
|
|
|
2020-06-18 16:23:08 +02:00
|
|
|
assert NewMarking1._type in parsing.STIX2_OBJ_MAPS[v]['markings']
|
|
|
|
assert v in str(parsing.STIX2_OBJ_MAPS[v]['markings'][NewMarking1._type])
|
2018-07-26 02:53:53 +02:00
|
|
|
|
|
|
|
|
2018-11-29 20:41:57 +01:00
|
|
|
@pytest.mark.xfail(reason="The default version is not 2.1", condition=stix2.DEFAULT_VERSION != "2.1")
|
|
|
|
def test_register_marking_with_no_version():
|
2020-06-18 16:23:08 +02:00
|
|
|
# Uses default version (2.1 in this case)
|
|
|
|
class NewMarking2:
|
|
|
|
_type = 'x-new-marking2'
|
|
|
|
_properties = OrderedDict()
|
|
|
|
|
|
|
|
parsing._register_marking(NewMarking2)
|
2018-11-29 20:41:57 +01:00
|
|
|
v = 'v21'
|
|
|
|
|
2020-06-18 16:23:08 +02:00
|
|
|
assert NewMarking2._type in parsing.STIX2_OBJ_MAPS[v]['markings']
|
|
|
|
assert v in str(parsing.STIX2_OBJ_MAPS[v]['markings'][NewMarking2._type])
|