2017-04-25 00:29:56 +02:00
|
|
|
import datetime as dt
|
|
|
|
|
2017-03-31 21:52:27 +02:00
|
|
|
import pytest
|
2017-04-19 20:32:56 +02:00
|
|
|
import pytz
|
2017-05-09 21:10:53 +02:00
|
|
|
|
2017-04-25 00:29:56 +02:00
|
|
|
import stix2
|
2018-07-05 21:23:25 +02:00
|
|
|
from stix2.v20 import TLP_WHITE
|
2017-04-19 20:32:56 +02:00
|
|
|
|
2019-01-29 16:52:59 +01:00
|
|
|
from .constants import CAMPAIGN_ID, IDENTITY_ID, MARKING_DEFINITION_ID
|
2017-03-31 21:52:27 +02:00
|
|
|
|
|
|
|
EXPECTED_TLP_MARKING_DEFINITION = """{
|
2017-08-15 19:41:51 +02:00
|
|
|
"type": "marking-definition",
|
|
|
|
"id": "marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9",
|
2018-10-25 19:58:21 +02:00
|
|
|
"created": "2017-01-20T00:00:00.000Z",
|
2017-08-15 19:41:51 +02:00
|
|
|
"definition_type": "tlp",
|
2017-03-31 21:52:27 +02:00
|
|
|
"definition": {
|
|
|
|
"tlp": "white"
|
2017-08-15 19:41:51 +02:00
|
|
|
}
|
2017-03-31 21:52:27 +02:00
|
|
|
}"""
|
|
|
|
|
|
|
|
EXPECTED_STATEMENT_MARKING_DEFINITION = """{
|
2017-08-15 19:41:51 +02:00
|
|
|
"type": "marking-definition",
|
|
|
|
"id": "marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9",
|
2017-04-17 16:48:13 +02:00
|
|
|
"created": "2017-01-20T00:00:00Z",
|
2017-08-15 19:41:51 +02:00
|
|
|
"definition_type": "statement",
|
2017-03-31 21:52:27 +02:00
|
|
|
"definition": {
|
|
|
|
"statement": "Copyright 2016, Example Corp"
|
2017-08-15 19:41:51 +02:00
|
|
|
}
|
2017-03-31 21:52:27 +02:00
|
|
|
}"""
|
|
|
|
|
2017-07-12 20:44:52 +02:00
|
|
|
EXPECTED_CAMPAIGN_WITH_OBJECT_MARKING = """{
|
2017-08-15 19:41:51 +02:00
|
|
|
"type": "campaign",
|
2017-07-12 20:44:52 +02:00
|
|
|
"id": "campaign--8e2e2d2b-17d4-4cbf-938f-98ee46b3cd3f",
|
2019-01-29 16:52:59 +01:00
|
|
|
"created_by_ref": "identity--311b2d2d-f010-4473-83ec-1edf84858f4c",
|
2017-08-15 19:41:51 +02:00
|
|
|
"created": "2016-04-06T20:03:00.000Z",
|
2017-07-12 20:44:52 +02:00
|
|
|
"modified": "2016-04-06T20:03:00.000Z",
|
|
|
|
"name": "Green Group Attacks Against Finance",
|
2017-08-15 19:41:51 +02:00
|
|
|
"description": "Campaign by Green Group against a series of targets in the financial services sector.",
|
2017-07-12 20:44:52 +02:00
|
|
|
"object_marking_refs": [
|
|
|
|
"marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9"
|
2017-08-15 19:41:51 +02:00
|
|
|
]
|
2017-07-12 20:44:52 +02:00
|
|
|
}"""
|
|
|
|
|
2017-03-31 21:52:27 +02:00
|
|
|
EXPECTED_GRANULAR_MARKING = """{
|
|
|
|
"marking_ref": "marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9",
|
|
|
|
"selectors": [
|
|
|
|
"abc",
|
|
|
|
"abc.[23]",
|
|
|
|
"abc.def",
|
|
|
|
"abc.[2].efg"
|
|
|
|
]
|
|
|
|
}"""
|
|
|
|
|
|
|
|
EXPECTED_CAMPAIGN_WITH_GRANULAR_MARKINGS = """{
|
2017-08-15 19:41:51 +02:00
|
|
|
"type": "campaign",
|
|
|
|
"id": "campaign--8e2e2d2b-17d4-4cbf-938f-98ee46b3cd3f",
|
2019-01-29 16:52:59 +01:00
|
|
|
"created_by_ref": "identity--311b2d2d-f010-4473-83ec-1edf84858f4c",
|
2017-08-15 19:41:51 +02:00
|
|
|
"created": "2016-04-06T20:03:00.000Z",
|
|
|
|
"modified": "2016-04-06T20:03:00.000Z",
|
|
|
|
"name": "Green Group Attacks Against Finance",
|
2017-03-31 21:52:27 +02:00
|
|
|
"description": "Campaign by Green Group against a series of targets in the financial services sector.",
|
|
|
|
"granular_markings": [
|
|
|
|
{
|
|
|
|
"marking_ref": "marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9",
|
|
|
|
"selectors": [
|
|
|
|
"description"
|
|
|
|
]
|
|
|
|
}
|
2017-08-15 19:41:51 +02:00
|
|
|
]
|
2017-03-31 21:52:27 +02:00
|
|
|
}"""
|
|
|
|
|
|
|
|
|
|
|
|
def test_marking_def_example_with_tlp():
|
|
|
|
assert str(TLP_WHITE) == EXPECTED_TLP_MARKING_DEFINITION
|
|
|
|
|
|
|
|
|
2017-08-28 20:32:05 +02:00
|
|
|
def test_marking_def_example_with_statement_positional_argument():
|
2018-07-05 21:23:25 +02:00
|
|
|
marking_definition = stix2.v20.MarkingDefinition(
|
2019-01-23 05:07:20 +01:00
|
|
|
id=MARKING_DEFINITION_ID,
|
2017-03-31 21:52:27 +02:00
|
|
|
created="2017-01-20T00:00:00.000Z",
|
|
|
|
definition_type="statement",
|
2018-07-13 17:10:05 +02:00
|
|
|
definition=stix2.v20.StatementMarking(statement="Copyright 2016, Example Corp"),
|
2017-03-31 21:52:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
assert str(marking_definition) == EXPECTED_STATEMENT_MARKING_DEFINITION
|
|
|
|
|
|
|
|
|
2017-08-28 20:32:05 +02:00
|
|
|
def test_marking_def_example_with_kwargs_statement():
|
|
|
|
kwargs = dict(statement="Copyright 2016, Example Corp")
|
2018-07-05 21:23:25 +02:00
|
|
|
marking_definition = stix2.v20.MarkingDefinition(
|
2019-01-23 05:07:20 +01:00
|
|
|
id=MARKING_DEFINITION_ID,
|
2017-03-31 21:52:27 +02:00
|
|
|
created="2017-01-20T00:00:00.000Z",
|
|
|
|
definition_type="statement",
|
2018-07-13 17:10:05 +02:00
|
|
|
definition=stix2.v20.StatementMarking(**kwargs),
|
2017-03-31 21:52:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
assert str(marking_definition) == EXPECTED_STATEMENT_MARKING_DEFINITION
|
|
|
|
|
|
|
|
|
2017-07-12 20:44:52 +02:00
|
|
|
def test_marking_def_invalid_type():
|
|
|
|
with pytest.raises(ValueError):
|
2018-07-05 21:23:25 +02:00
|
|
|
stix2.v20.MarkingDefinition(
|
2019-01-23 05:07:20 +01:00
|
|
|
id=MARKING_DEFINITION_ID,
|
2017-07-12 20:44:52 +02:00
|
|
|
created="2017-01-20T00:00:00.000Z",
|
2017-08-24 18:47:14 +02:00
|
|
|
definition_type="my-definition-type",
|
2018-07-13 17:10:05 +02:00
|
|
|
definition=stix2.v20.StatementMarking("Copyright 2016, Example Corp"),
|
2017-07-12 20:44:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_campaign_with_markings_example():
|
2018-07-05 21:23:25 +02:00
|
|
|
campaign = stix2.v20.Campaign(
|
2019-01-23 05:07:20 +01:00
|
|
|
type='campaign',
|
|
|
|
id=CAMPAIGN_ID,
|
2019-01-29 16:52:59 +01:00
|
|
|
created_by_ref=IDENTITY_ID,
|
2019-01-23 05:07:20 +01:00
|
|
|
created="2016-04-06T20:03:00.000Z",
|
|
|
|
modified="2016-04-06T20:03:00.000Z",
|
2017-07-12 20:44:52 +02:00
|
|
|
name="Green Group Attacks Against Finance",
|
|
|
|
description="Campaign by Green Group against a series of targets in the financial services sector.",
|
2018-07-13 17:10:05 +02:00
|
|
|
object_marking_refs=TLP_WHITE,
|
2017-07-12 20:44:52 +02:00
|
|
|
)
|
|
|
|
assert str(campaign) == EXPECTED_CAMPAIGN_WITH_OBJECT_MARKING
|
|
|
|
|
|
|
|
|
2017-03-31 21:52:27 +02:00
|
|
|
def test_granular_example():
|
2018-07-05 21:23:25 +02:00
|
|
|
granular_marking = stix2.v20.GranularMarking(
|
2019-01-23 05:07:20 +01:00
|
|
|
marking_ref=MARKING_DEFINITION_ID,
|
2018-07-13 17:10:05 +02:00
|
|
|
selectors=["abc", "abc.[23]", "abc.def", "abc.[2].efg"],
|
2017-03-31 21:52:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
assert str(granular_marking) == EXPECTED_GRANULAR_MARKING
|
|
|
|
|
|
|
|
|
|
|
|
def test_granular_example_with_bad_selector():
|
2017-04-18 21:42:59 +02:00
|
|
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
2018-07-05 21:23:25 +02:00
|
|
|
stix2.v20.GranularMarking(
|
2019-01-23 05:07:20 +01:00
|
|
|
marking_ref=MARKING_DEFINITION_ID,
|
2018-07-13 17:10:05 +02:00
|
|
|
selectors=["abc[0]"], # missing "."
|
2017-03-31 21:52:27 +02:00
|
|
|
)
|
|
|
|
|
2018-07-05 21:23:25 +02:00
|
|
|
assert excinfo.value.cls == stix2.v20.GranularMarking
|
2017-04-18 21:19:16 +02:00
|
|
|
assert excinfo.value.prop_name == "selectors"
|
|
|
|
assert excinfo.value.reason == "must adhere to selector syntax."
|
|
|
|
assert str(excinfo.value) == "Invalid value for GranularMarking 'selectors': must adhere to selector syntax."
|
2017-03-31 21:52:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_campaign_with_granular_markings_example():
|
2018-07-05 21:23:25 +02:00
|
|
|
campaign = stix2.v20.Campaign(
|
2019-01-23 05:07:20 +01:00
|
|
|
type='campaign',
|
|
|
|
id=CAMPAIGN_ID,
|
2019-01-29 16:52:59 +01:00
|
|
|
created_by_ref=IDENTITY_ID,
|
2019-01-23 05:07:20 +01:00
|
|
|
created="2016-04-06T20:03:00.000Z",
|
|
|
|
modified="2016-04-06T20:03:00.000Z",
|
2017-03-31 21:52:27 +02:00
|
|
|
name="Green Group Attacks Against Finance",
|
|
|
|
description="Campaign by Green Group against a series of targets in the financial services sector.",
|
|
|
|
granular_markings=[
|
2018-07-05 21:23:25 +02:00
|
|
|
stix2.v20.GranularMarking(
|
2019-01-23 05:07:20 +01:00
|
|
|
marking_ref=MARKING_DEFINITION_ID,
|
2018-07-13 17:10:05 +02:00
|
|
|
selectors=["description"],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2017-03-31 21:52:27 +02:00
|
|
|
assert str(campaign) == EXPECTED_CAMPAIGN_WITH_GRANULAR_MARKINGS
|
|
|
|
|
2017-04-19 20:32:56 +02:00
|
|
|
|
2018-07-13 17:10:05 +02:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"data", [
|
|
|
|
EXPECTED_TLP_MARKING_DEFINITION,
|
|
|
|
{
|
2019-01-23 05:07:20 +01:00
|
|
|
"id": MARKING_DEFINITION_ID,
|
2018-07-13 17:10:05 +02:00
|
|
|
"type": "marking-definition",
|
|
|
|
"created": "2017-01-20T00:00:00Z",
|
|
|
|
"definition": {
|
|
|
|
"tlp": "white",
|
|
|
|
},
|
|
|
|
"definition_type": "tlp",
|
2017-04-19 20:32:56 +02:00
|
|
|
},
|
2018-07-13 17:10:05 +02:00
|
|
|
],
|
|
|
|
)
|
2017-04-19 20:32:56 +02:00
|
|
|
def test_parse_marking_definition(data):
|
2018-07-05 21:23:25 +02:00
|
|
|
gm = stix2.parse(data, version="2.0")
|
2017-04-19 20:32:56 +02:00
|
|
|
|
|
|
|
assert gm.type == 'marking-definition'
|
|
|
|
assert gm.id == MARKING_DEFINITION_ID
|
|
|
|
assert gm.created == dt.datetime(2017, 1, 20, 0, 0, 0, tzinfo=pytz.utc)
|
|
|
|
assert gm.definition.tlp == "white"
|
|
|
|
assert gm.definition_type == "tlp"
|
|
|
|
|
|
|
|
|
2018-07-13 17:10:05 +02:00
|
|
|
@stix2.v20.CustomMarking(
|
|
|
|
'x-new-marking-type', [
|
|
|
|
('property1', stix2.properties.StringProperty(required=True)),
|
|
|
|
('property2', stix2.properties.IntegerProperty()),
|
|
|
|
],
|
|
|
|
)
|
2017-08-28 20:32:05 +02:00
|
|
|
class NewMarking(object):
|
|
|
|
def __init__(self, property2=None, **kwargs):
|
2017-11-17 14:50:40 +01:00
|
|
|
if "property3" in kwargs and not isinstance(kwargs.get("property3"), int):
|
|
|
|
raise TypeError("Must be integer!")
|
2017-08-28 20:32:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_registered_custom_marking():
|
|
|
|
nm = NewMarking(property1='something', property2=55)
|
|
|
|
|
2018-07-05 21:23:25 +02:00
|
|
|
marking_def = stix2.v20.MarkingDefinition(
|
2018-07-11 15:43:37 +02:00
|
|
|
id="marking-definition--00000000-0000-4000-8000-000000000012",
|
2017-08-28 20:32:05 +02:00
|
|
|
created="2017-01-22T00:00:00.000Z",
|
|
|
|
definition_type="x-new-marking-type",
|
2018-07-13 17:10:05 +02:00
|
|
|
definition=nm,
|
2017-08-28 20:32:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
assert marking_def.type == "marking-definition"
|
2018-07-11 15:43:37 +02:00
|
|
|
assert marking_def.id == "marking-definition--00000000-0000-4000-8000-000000000012"
|
2017-08-28 20:32:05 +02:00
|
|
|
assert marking_def.created == dt.datetime(2017, 1, 22, 0, 0, 0, tzinfo=pytz.utc)
|
|
|
|
assert marking_def.definition.property1 == "something"
|
|
|
|
assert marking_def.definition.property2 == 55
|
|
|
|
assert marking_def.definition_type == "x-new-marking-type"
|
|
|
|
|
|
|
|
|
2017-11-17 14:50:40 +01:00
|
|
|
def test_registered_custom_marking_raises_exception():
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
NewMarking(property1='something', property3='something', allow_custom=True)
|
|
|
|
|
|
|
|
assert str(excinfo.value) == "Must be integer!"
|
|
|
|
|
|
|
|
|
2017-08-28 20:32:05 +02:00
|
|
|
def test_not_registered_marking_raises_exception():
|
2017-08-29 21:15:32 +02:00
|
|
|
with pytest.raises(ValueError) as excinfo:
|
|
|
|
# Used custom object on purpose to demonstrate a not-registered marking
|
2018-07-13 17:10:05 +02:00
|
|
|
@stix2.v20.CustomObject(
|
|
|
|
'x-new-marking-type2', [
|
|
|
|
('property1', stix2.properties.StringProperty(required=True)),
|
|
|
|
('property2', stix2.properties.IntegerProperty()),
|
|
|
|
],
|
|
|
|
)
|
2017-08-28 20:32:05 +02:00
|
|
|
class NewObject2(object):
|
|
|
|
def __init__(self, property2=None, **kwargs):
|
2017-08-28 21:19:55 +02:00
|
|
|
return
|
2017-08-28 20:32:05 +02:00
|
|
|
|
|
|
|
no = NewObject2(property1='something', property2=55)
|
|
|
|
|
2018-07-05 21:23:25 +02:00
|
|
|
stix2.v20.MarkingDefinition(
|
2018-07-11 15:43:37 +02:00
|
|
|
id="marking-definition--00000000-0000-4000-8000-000000000012",
|
2017-08-28 20:32:05 +02:00
|
|
|
created="2017-01-22T00:00:00.000Z",
|
|
|
|
definition_type="x-new-marking-type2",
|
2018-07-13 17:10:05 +02:00
|
|
|
definition=no,
|
2017-08-28 20:32:05 +02:00
|
|
|
)
|
|
|
|
|
2017-08-29 21:15:32 +02:00
|
|
|
assert str(excinfo.value) == "definition_type must be a valid marking type"
|
2017-08-28 20:32:05 +02:00
|
|
|
|
2017-08-29 21:15:32 +02:00
|
|
|
|
|
|
|
def test_marking_wrong_type_construction():
|
2020-04-02 20:15:45 +02:00
|
|
|
with pytest.raises(ValueError):
|
2017-08-29 21:15:32 +02:00
|
|
|
# Test passing wrong type for properties.
|
2018-07-05 21:23:25 +02:00
|
|
|
@stix2.v20.CustomMarking('x-new-marking-type2', ("a", "b"))
|
2017-08-28 21:19:55 +02:00
|
|
|
class NewObject3(object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-10-03 16:28:58 +02:00
|
|
|
def test_campaign_add_markings():
|
2018-07-05 21:23:25 +02:00
|
|
|
campaign = stix2.v20.Campaign(
|
2019-01-23 05:07:20 +01:00
|
|
|
id=CAMPAIGN_ID,
|
2019-01-29 16:52:59 +01:00
|
|
|
created_by_ref=IDENTITY_ID,
|
2017-10-03 16:28:58 +02:00
|
|
|
created="2016-04-06T20:03:00Z",
|
|
|
|
modified="2016-04-06T20:03:00Z",
|
|
|
|
name="Green Group Attacks Against Finance",
|
|
|
|
description="Campaign by Green Group against a series of targets in the financial services sector.",
|
|
|
|
)
|
|
|
|
campaign = campaign.add_markings(TLP_WHITE)
|
|
|
|
assert campaign.object_marking_refs[0] == TLP_WHITE.id
|