2018-07-03 13:00:18 +02:00
|
|
|
import datetime as dt
|
2020-02-26 02:07:47 +01:00
|
|
|
import json
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
import pytz
|
|
|
|
|
|
|
|
import stix2
|
|
|
|
|
2019-07-19 21:40:03 +02:00
|
|
|
from ...exceptions import InvalidValueError, PropertyPresenceError
|
2018-07-03 13:00:18 +02:00
|
|
|
from .constants import FAKE_TIME, MALWARE_ID, MALWARE_KWARGS
|
|
|
|
|
|
|
|
EXPECTED_MALWARE = """{
|
|
|
|
"type": "malware",
|
|
|
|
"spec_version": "2.1",
|
2018-07-11 15:43:37 +02:00
|
|
|
"id": "malware--9c4638ec-f1de-4ddb-abf4-1b760417654e",
|
2018-07-03 13:00:18 +02:00
|
|
|
"created": "2016-05-12T08:17:27.000Z",
|
|
|
|
"modified": "2016-05-12T08:17:27.000Z",
|
|
|
|
"name": "Cryptolocker",
|
2019-07-02 19:17:43 +02:00
|
|
|
"is_family": false
|
2018-07-03 13:00:18 +02:00
|
|
|
}"""
|
|
|
|
|
|
|
|
|
|
|
|
def test_malware_with_all_required_properties():
|
|
|
|
now = dt.datetime(2016, 5, 12, 8, 17, 27, tzinfo=pytz.utc)
|
|
|
|
|
2018-07-03 15:40:51 +02:00
|
|
|
mal = stix2.v21.Malware(
|
2018-07-03 13:00:18 +02:00
|
|
|
type="malware",
|
|
|
|
id=MALWARE_ID,
|
|
|
|
created=now,
|
|
|
|
modified=now,
|
|
|
|
name="Cryptolocker",
|
2019-07-01 21:26:30 +02:00
|
|
|
is_family=False,
|
2018-07-03 13:00:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
assert str(mal) == EXPECTED_MALWARE
|
|
|
|
|
|
|
|
|
|
|
|
def test_malware_autogenerated_properties(malware):
|
|
|
|
assert malware.type == 'malware'
|
2018-07-11 15:43:37 +02:00
|
|
|
assert malware.id == 'malware--00000000-0000-4000-8000-000000000001'
|
2018-07-03 13:00:18 +02:00
|
|
|
assert malware.created == FAKE_TIME
|
|
|
|
assert malware.modified == FAKE_TIME
|
2018-07-12 20:33:00 +02:00
|
|
|
assert malware.malware_types == ['ransomware']
|
2018-07-03 13:00:18 +02:00
|
|
|
assert malware.name == "Cryptolocker"
|
|
|
|
|
|
|
|
assert malware['type'] == 'malware'
|
2018-07-11 15:43:37 +02:00
|
|
|
assert malware['id'] == 'malware--00000000-0000-4000-8000-000000000001'
|
2018-07-03 13:00:18 +02:00
|
|
|
assert malware['created'] == FAKE_TIME
|
|
|
|
assert malware['modified'] == FAKE_TIME
|
2018-07-12 20:33:00 +02:00
|
|
|
assert malware['malware_types'] == ['ransomware']
|
2018-07-03 13:00:18 +02:00
|
|
|
assert malware['name'] == "Cryptolocker"
|
|
|
|
|
|
|
|
|
|
|
|
def test_malware_type_must_be_malware():
|
|
|
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
2018-07-03 15:40:51 +02:00
|
|
|
stix2.v21.Malware(type='xxx', **MALWARE_KWARGS)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
2018-07-03 15:40:51 +02:00
|
|
|
assert excinfo.value.cls == stix2.v21.Malware
|
2018-07-03 13:00:18 +02:00
|
|
|
assert excinfo.value.prop_name == "type"
|
|
|
|
assert excinfo.value.reason == "must equal 'malware'."
|
|
|
|
assert str(excinfo.value) == "Invalid value for Malware 'type': must equal 'malware'."
|
|
|
|
|
|
|
|
|
|
|
|
def test_malware_id_must_start_with_malware():
|
|
|
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
2018-07-03 15:40:51 +02:00
|
|
|
stix2.v21.Malware(id='my-prefix--', **MALWARE_KWARGS)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
2018-07-03 15:40:51 +02:00
|
|
|
assert excinfo.value.cls == stix2.v21.Malware
|
2018-07-03 13:00:18 +02:00
|
|
|
assert excinfo.value.prop_name == "id"
|
|
|
|
assert excinfo.value.reason == "must start with 'malware--'."
|
|
|
|
assert str(excinfo.value) == "Invalid value for Malware 'id': must start with 'malware--'."
|
|
|
|
|
|
|
|
|
|
|
|
def test_malware_required_properties():
|
|
|
|
with pytest.raises(stix2.exceptions.MissingPropertiesError) as excinfo:
|
2018-07-03 15:40:51 +02:00
|
|
|
stix2.v21.Malware()
|
2018-07-03 13:00:18 +02:00
|
|
|
|
2018-07-03 15:40:51 +02:00
|
|
|
assert excinfo.value.cls == stix2.v21.Malware
|
2020-02-26 02:07:47 +01:00
|
|
|
assert excinfo.value.properties == ["is_family"]
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_malware_required_property_name():
|
|
|
|
with pytest.raises(stix2.exceptions.MissingPropertiesError) as excinfo:
|
2018-07-25 19:34:56 +02:00
|
|
|
stix2.v21.Malware(malware_types=['ransomware'])
|
2018-07-03 13:00:18 +02:00
|
|
|
|
2018-07-03 15:40:51 +02:00
|
|
|
assert excinfo.value.cls == stix2.v21.Malware
|
2019-07-16 22:10:25 +02:00
|
|
|
assert excinfo.value.properties == ["is_family"]
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_cannot_assign_to_malware_attributes(malware):
|
|
|
|
with pytest.raises(stix2.exceptions.ImmutableError) as excinfo:
|
|
|
|
malware.name = "Cryptolocker II"
|
|
|
|
|
|
|
|
assert str(excinfo.value) == "Cannot modify 'name' property in 'Malware' after creation."
|
|
|
|
|
|
|
|
|
|
|
|
def test_invalid_kwarg_to_malware():
|
|
|
|
with pytest.raises(stix2.exceptions.ExtraPropertiesError) as excinfo:
|
2018-07-03 15:40:51 +02:00
|
|
|
stix2.v21.Malware(my_custom_property="foo", **MALWARE_KWARGS)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
2018-07-03 15:40:51 +02:00
|
|
|
assert excinfo.value.cls == stix2.v21.Malware
|
2018-07-03 13:00:18 +02:00
|
|
|
assert excinfo.value.properties == ['my_custom_property']
|
|
|
|
assert str(excinfo.value) == "Unexpected properties for Malware: (my_custom_property)."
|
|
|
|
|
|
|
|
|
2018-07-13 17:10:05 +02:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"data", [
|
|
|
|
EXPECTED_MALWARE,
|
|
|
|
{
|
|
|
|
"type": "malware",
|
|
|
|
"spec_version": "2.1",
|
2019-01-23 16:56:20 +01:00
|
|
|
"id": MALWARE_ID,
|
2018-07-13 17:10:05 +02:00
|
|
|
"created": "2016-05-12T08:17:27.000Z",
|
|
|
|
"modified": "2016-05-12T08:17:27.000Z",
|
|
|
|
"name": "Cryptolocker",
|
2019-07-01 21:26:30 +02:00
|
|
|
"is_family": False,
|
2018-07-13 17:10:05 +02:00
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
2018-07-03 13:00:18 +02:00
|
|
|
def test_parse_malware(data):
|
2018-07-12 20:33:00 +02:00
|
|
|
mal = stix2.parse(data)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
assert mal.type == 'malware'
|
2018-07-03 15:40:51 +02:00
|
|
|
assert mal.spec_version == '2.1'
|
2018-07-03 13:00:18 +02:00
|
|
|
assert mal.id == MALWARE_ID
|
|
|
|
assert mal.created == dt.datetime(2016, 5, 12, 8, 17, 27, tzinfo=pytz.utc)
|
|
|
|
assert mal.modified == dt.datetime(2016, 5, 12, 8, 17, 27, tzinfo=pytz.utc)
|
2018-07-12 20:33:00 +02:00
|
|
|
assert mal.name == 'Cryptolocker'
|
2019-07-01 21:26:30 +02:00
|
|
|
assert not mal.is_family
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
|
2020-02-26 02:07:47 +01:00
|
|
|
def test_parse_malware_invalid_types():
|
|
|
|
data = json.loads(EXPECTED_MALWARE)
|
|
|
|
data["malware_types"] = 1 # Oops, not a list
|
|
|
|
data = json.dumps(data)
|
Improved the exception class hierarchy:
- Removed all plain python base classes (e.g. ValueError, TypeError)
- Renamed InvalidPropertyConfigurationError -> PropertyPresenceError,
since incorrect values could be considered a property config error, and
I really just wanted this class to apply to presence (co-)constraint
violations.
- Added ObjectConfigurationError as a superclass of InvalidValueError,
PropertyPresenceError, and any other exception that could be raised
during _STIXBase object init, which is when the spec compliance
checks happen. This class is intended to represent general spec
violations.
- Did some class reordering in exceptions.py, so all the
ObjectConfigurationError subclasses were together.
Changed how property "cleaning" errors were handled:
- Previous docs said they should all be ValueErrors, but that would require
extra exception check-and-replace complexity in the property
implementations, so that requirement is removed. Doc is changed to just
say that cleaning problems should cause exceptions to be raised.
_STIXBase._check_property() now handles most exception types, not just
ValueError.
- Decided to try chaining the original clean error to the InvalidValueError,
in case the extra diagnostics would be helpful in the future. This is
done via 'six' adapter function and only works on python3.
- A small amount of testing was removed, since it was looking at custom
exception properties which became unavailable once the exception was
replaced with InvalidValueError.
Did another pass through unit tests to fix breakage caused by the changed
exception class hierarchy.
Removed unnecessary observable extension handling code from
parse_observable(), since it was all duplicated in ExtensionsProperty.
The redundant code in parse_observable() had different exception behavior
than ExtensionsProperty, which makes the API inconsistent and unit tests
more complicated. (Problems in ExtensionsProperty get replaced with
InvalidValueError, but extensions problems handled directly in
parse_observable() don't get the same replacement, and so the exception
type is different.)
Redid the workbench monkeypatching. The old way was impossible to make
work, and had caused ugly ripple effect hackage in other parts of the
codebase. Now, it replaces the global object maps with factory functions
which behave the same way when called, as real classes. Had to fix up a
few unit tests to get them all passing with this monkeypatching in place.
Also remove all the xfail markings in the workbench test suite, since all
tests now pass.
Since workbench monkeypatching isn't currently affecting any unit tests,
tox.ini was simplified to remove the special-casing for running the
workbench tests.
Removed the v20 workbench test suite, since the workbench currently only
works with the latest stix object version.
2019-07-19 20:50:11 +02:00
|
|
|
with pytest.raises(InvalidValueError) as excinfo:
|
2018-07-12 20:33:00 +02:00
|
|
|
stix2.parse(data)
|
|
|
|
assert "Invalid value for Malware 'malware_types'" in str(excinfo.value)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_parse_malware_kill_chain_phases():
|
|
|
|
kill_chain = """
|
|
|
|
"kill_chain_phases": [
|
|
|
|
{
|
|
|
|
"kill_chain_name": "lockheed-martin-cyber-kill-chain",
|
|
|
|
"phase_name": "reconnaissance"
|
|
|
|
}
|
|
|
|
]"""
|
|
|
|
data = EXPECTED_MALWARE.replace('malware"', 'malware",%s' % kill_chain)
|
2018-07-03 15:40:51 +02:00
|
|
|
mal = stix2.parse(data, version="2.1")
|
2018-07-03 13:00:18 +02:00
|
|
|
assert mal.kill_chain_phases[0].kill_chain_name == "lockheed-martin-cyber-kill-chain"
|
|
|
|
assert mal.kill_chain_phases[0].phase_name == "reconnaissance"
|
|
|
|
assert mal['kill_chain_phases'][0]['kill_chain_name'] == "lockheed-martin-cyber-kill-chain"
|
|
|
|
assert mal['kill_chain_phases'][0]['phase_name'] == "reconnaissance"
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_malware_clean_kill_chain_phases():
|
|
|
|
kill_chain = """
|
|
|
|
"kill_chain_phases": [
|
|
|
|
{
|
|
|
|
"kill_chain_name": "lockheed-martin-cyber-kill-chain",
|
|
|
|
"phase_name": 1
|
|
|
|
}
|
|
|
|
]"""
|
|
|
|
data = EXPECTED_MALWARE.replace('2.1"', '2.1",%s' % kill_chain)
|
2018-07-03 15:40:51 +02:00
|
|
|
mal = stix2.parse(data, version="2.1")
|
2018-07-03 13:00:18 +02:00
|
|
|
assert mal['kill_chain_phases'][0]['phase_name'] == "1"
|
2019-07-02 19:17:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_malware_invalid_last_before_first():
|
|
|
|
with pytest.raises(ValueError) as excinfo:
|
|
|
|
stix2.v21.Malware(first_seen="2017-01-01T12:34:56.000Z", last_seen="2017-01-01T12:33:56.000Z", **MALWARE_KWARGS)
|
|
|
|
|
|
|
|
assert "'last_seen' must be greater than or equal to 'first_seen'" in str(excinfo.value)
|
2019-07-19 21:40:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_malware_family_no_name():
|
|
|
|
with pytest.raises(PropertyPresenceError):
|
|
|
|
stix2.parse({
|
|
|
|
"type": "malware",
|
|
|
|
"id": MALWARE_ID,
|
|
|
|
"spec_version": "2.1",
|
|
|
|
"is_family": True,
|
|
|
|
"malware_types": ["a type"],
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
def test_malware_non_family_no_name():
|
|
|
|
stix2.parse({
|
|
|
|
"type": "malware",
|
|
|
|
"id": MALWARE_ID,
|
|
|
|
"spec_version": "2.1",
|
|
|
|
"is_family": False,
|
|
|
|
"malware_types": ["something"],
|
|
|
|
})
|
2020-03-07 00:48:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_malware_with_os_refs():
|
|
|
|
software = stix2.parse({
|
|
|
|
"type": "software",
|
|
|
|
"name": "SuperOS",
|
|
|
|
"spec_version": "2.1",
|
|
|
|
})
|
|
|
|
|
|
|
|
malware = stix2.parse({
|
|
|
|
"type": "malware",
|
|
|
|
"id": MALWARE_ID,
|
|
|
|
"spec_version": "2.1",
|
|
|
|
"is_family": False,
|
|
|
|
"malware_types": ["something"],
|
|
|
|
"operating_system_refs": [software],
|
|
|
|
})
|
|
|
|
|
|
|
|
assert malware["operating_system_refs"][0] == software["id"]
|