2018-07-03 13:00:18 +02:00
|
|
|
import datetime as dt
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import pytz
|
|
|
|
|
|
|
|
import stix2
|
2020-05-20 21:06:53 +02:00
|
|
|
import stix2.exceptions
|
2018-07-03 13:00:18 +02:00
|
|
|
|
2019-01-22 18:42:47 +01:00
|
|
|
from .constants import ATTACK_PATTERN_ID
|
|
|
|
|
2018-07-03 13:00:18 +02:00
|
|
|
EXPECTED = """{
|
|
|
|
"type": "attack-pattern",
|
|
|
|
"spec_version": "2.1",
|
|
|
|
"id": "attack-pattern--0c7b5b88-8ff7-4a4d-aa9d-feb398cd0061",
|
|
|
|
"created": "2016-05-12T08:17:27.000Z",
|
|
|
|
"modified": "2016-05-12T08:17:27.000Z",
|
|
|
|
"name": "Spear Phishing",
|
|
|
|
"description": "...",
|
|
|
|
"external_references": [
|
|
|
|
{
|
|
|
|
"source_name": "capec",
|
|
|
|
"external_id": "CAPEC-163"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}"""
|
|
|
|
|
|
|
|
|
|
|
|
def test_attack_pattern_example():
|
2018-07-03 15:40:51 +02:00
|
|
|
ap = stix2.v21.AttackPattern(
|
2019-01-22 18:42:47 +01:00
|
|
|
id=ATTACK_PATTERN_ID,
|
2018-07-03 13:00:18 +02:00
|
|
|
created="2016-05-12T08:17:27.000Z",
|
|
|
|
modified="2016-05-12T08:17:27.000Z",
|
|
|
|
name="Spear Phishing",
|
|
|
|
external_references=[{
|
|
|
|
"source_name": "capec",
|
2018-07-13 17:10:05 +02:00
|
|
|
"external_id": "CAPEC-163",
|
2018-07-03 13:00:18 +02:00
|
|
|
}],
|
|
|
|
description="...",
|
|
|
|
)
|
|
|
|
|
|
|
|
assert str(ap) == EXPECTED
|
|
|
|
|
|
|
|
|
2018-07-13 17:10:05 +02:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"data", [
|
|
|
|
EXPECTED,
|
|
|
|
{
|
|
|
|
"type": "attack-pattern",
|
|
|
|
"spec_version": "2.1",
|
2019-01-22 18:42:47 +01:00
|
|
|
"id": ATTACK_PATTERN_ID,
|
2018-07-13 17:10:05 +02:00
|
|
|
"created": "2016-05-12T08:17:27.000Z",
|
|
|
|
"modified": "2016-05-12T08:17:27.000Z",
|
|
|
|
"description": "...",
|
|
|
|
"external_references": [
|
|
|
|
{
|
|
|
|
"external_id": "CAPEC-163",
|
|
|
|
"source_name": "capec",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
"name": "Spear Phishing",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
2018-07-03 13:00:18 +02:00
|
|
|
def test_parse_attack_pattern(data):
|
2018-07-03 15:40:51 +02:00
|
|
|
ap = stix2.parse(data, version="2.1")
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
assert ap.type == 'attack-pattern'
|
2018-07-03 15:40:51 +02:00
|
|
|
assert ap.spec_version == '2.1'
|
2019-01-22 18:42:47 +01:00
|
|
|
assert ap.id == ATTACK_PATTERN_ID
|
2018-07-03 13:00:18 +02:00
|
|
|
assert ap.created == dt.datetime(2016, 5, 12, 8, 17, 27, tzinfo=pytz.utc)
|
|
|
|
assert ap.modified == dt.datetime(2016, 5, 12, 8, 17, 27, tzinfo=pytz.utc)
|
|
|
|
assert ap.description == "..."
|
|
|
|
assert ap.external_references[0].external_id == 'CAPEC-163'
|
|
|
|
assert ap.external_references[0].source_name == 'capec'
|
|
|
|
assert ap.name == "Spear Phishing"
|
|
|
|
|
|
|
|
|
|
|
|
def test_attack_pattern_invalid_labels():
|
|
|
|
with pytest.raises(stix2.exceptions.InvalidValueError):
|
2018-07-03 15:40:51 +02:00
|
|
|
stix2.v21.AttackPattern(
|
2019-01-22 18:42:47 +01:00
|
|
|
id=ATTACK_PATTERN_ID,
|
2018-07-03 13:00:18 +02:00
|
|
|
created="2016-05-12T08:17:27Z",
|
|
|
|
modified="2016-05-12T08:17:27Z",
|
|
|
|
name="Spear Phishing",
|
2018-07-13 17:10:05 +02:00
|
|
|
labels=1,
|
2018-07-03 13:00:18 +02:00
|
|
|
)
|
|
|
|
|
2019-01-22 16:05:22 +01:00
|
|
|
|
|
|
|
def test_overly_precise_timestamps():
|
2020-05-20 21:06:53 +02:00
|
|
|
with pytest.raises(stix2.exceptions.InvalidValueError):
|
|
|
|
stix2.v21.AttackPattern(
|
|
|
|
id=ATTACK_PATTERN_ID,
|
|
|
|
created="2016-05-12T08:17:27.000000342Z",
|
|
|
|
modified="2016-05-12T08:17:27.000000287Z",
|
|
|
|
name="Spear Phishing",
|
|
|
|
external_references=[{
|
|
|
|
"source_name": "capec",
|
|
|
|
"external_id": "CAPEC-163",
|
|
|
|
}],
|
|
|
|
description="...",
|
|
|
|
)
|
2019-01-22 16:05:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_less_precise_timestamps():
|
|
|
|
ap = stix2.v21.AttackPattern(
|
2019-01-22 18:42:47 +01:00
|
|
|
id=ATTACK_PATTERN_ID,
|
2019-01-22 16:05:22 +01:00
|
|
|
created="2016-05-12T08:17:27.00Z",
|
|
|
|
modified="2016-05-12T08:17:27.0Z",
|
|
|
|
name="Spear Phishing",
|
|
|
|
external_references=[{
|
|
|
|
"source_name": "capec",
|
|
|
|
"external_id": "CAPEC-163",
|
|
|
|
}],
|
|
|
|
description="...",
|
|
|
|
)
|
|
|
|
|
|
|
|
assert str(ap) == EXPECTED
|
|
|
|
|
|
|
|
|
2018-07-03 13:00:18 +02:00
|
|
|
# TODO: Add other examples
|