2018-12-06 21:11:30 +01:00
|
|
|
import datetime
|
|
|
|
import errno
|
2018-07-03 13:00:18 +02:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import shutil
|
2018-12-06 21:11:30 +01:00
|
|
|
import stat
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
import pytest
|
2018-12-06 21:11:30 +01:00
|
|
|
import pytz
|
2018-07-03 13:00:18 +02:00
|
|
|
|
2018-07-03 15:40:51 +02:00
|
|
|
import stix2
|
2018-12-06 21:19:50 +01:00
|
|
|
from stix2.datastore.filesystem import (
|
|
|
|
AuthSet, _find_search_optimizations, _get_matching_dir_entries,
|
|
|
|
_timestamp2filename,
|
|
|
|
)
|
2018-12-06 21:11:30 +01:00
|
|
|
from stix2.exceptions import STIXError
|
2018-11-29 19:50:05 +01:00
|
|
|
|
2018-11-29 19:49:06 +01:00
|
|
|
from .constants import (
|
2018-07-13 17:10:05 +02:00
|
|
|
CAMPAIGN_ID, CAMPAIGN_KWARGS, IDENTITY_ID, IDENTITY_KWARGS, INDICATOR_ID,
|
|
|
|
INDICATOR_KWARGS, MALWARE_ID, MALWARE_KWARGS, RELATIONSHIP_IDS,
|
|
|
|
)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
FS_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "stix2_data")
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fs_store():
|
|
|
|
# create
|
2018-07-03 15:40:51 +02:00
|
|
|
yield stix2.FileSystemStore(FS_PATH)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
# remove campaign dir
|
|
|
|
shutil.rmtree(os.path.join(FS_PATH, "campaign"), True)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fs_source():
|
|
|
|
# create
|
2018-07-03 15:40:51 +02:00
|
|
|
fs = stix2.FileSystemSource(FS_PATH)
|
2018-07-03 13:00:18 +02:00
|
|
|
assert fs.stix_dir == FS_PATH
|
|
|
|
yield fs
|
|
|
|
|
|
|
|
# remove campaign dir
|
|
|
|
shutil.rmtree(os.path.join(FS_PATH, "campaign"), True)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fs_sink():
|
|
|
|
# create
|
2018-07-03 15:40:51 +02:00
|
|
|
fs = stix2.FileSystemSink(FS_PATH)
|
2018-07-03 13:00:18 +02:00
|
|
|
assert fs.stix_dir == FS_PATH
|
|
|
|
yield fs
|
|
|
|
|
|
|
|
# remove campaign dir
|
|
|
|
shutil.rmtree(os.path.join(FS_PATH, "campaign"), True)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def bad_json_files():
|
|
|
|
# create erroneous JSON files for tests to make sure handled gracefully
|
|
|
|
|
|
|
|
with open(os.path.join(FS_PATH, "intrusion-set", "intrusion-set--test-non-json.txt"), "w+") as f:
|
|
|
|
f.write("Im not a JSON file")
|
|
|
|
|
|
|
|
with open(os.path.join(FS_PATH, "intrusion-set", "intrusion-set--test-bad-json.json"), "w+") as f:
|
|
|
|
f.write("Im not a JSON formatted file")
|
|
|
|
|
|
|
|
yield True # dummy yield so can have teardown
|
|
|
|
|
|
|
|
os.remove(os.path.join(FS_PATH, "intrusion-set", "intrusion-set--test-non-json.txt"))
|
|
|
|
os.remove(os.path.join(FS_PATH, "intrusion-set", "intrusion-set--test-bad-json.json"))
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def bad_stix_files():
|
|
|
|
# create erroneous STIX JSON files for tests to make sure handled correctly
|
|
|
|
|
|
|
|
# bad STIX object
|
|
|
|
stix_obj = {
|
|
|
|
"id": "intrusion-set--test-bad-stix",
|
2018-07-13 17:10:05 +02:00
|
|
|
"spec_version": "2.0",
|
2018-07-03 13:00:18 +02:00
|
|
|
# no "type" field
|
|
|
|
}
|
|
|
|
|
|
|
|
with open(os.path.join(FS_PATH, "intrusion-set", "intrusion-set--test-non-stix.json"), "w+") as f:
|
|
|
|
f.write(json.dumps(stix_obj))
|
|
|
|
|
|
|
|
yield True # dummy yield so can have teardown
|
|
|
|
|
|
|
|
os.remove(os.path.join(FS_PATH, "intrusion-set", "intrusion-set--test-non-stix.json"))
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def rel_fs_store():
|
2018-07-03 15:40:51 +02:00
|
|
|
cam = stix2.v21.Campaign(id=CAMPAIGN_ID, **CAMPAIGN_KWARGS)
|
|
|
|
idy = stix2.v21.Identity(id=IDENTITY_ID, **IDENTITY_KWARGS)
|
|
|
|
ind = stix2.v21.Indicator(id=INDICATOR_ID, **INDICATOR_KWARGS)
|
|
|
|
mal = stix2.v21.Malware(id=MALWARE_ID, **MALWARE_KWARGS)
|
|
|
|
rel1 = stix2.v21.Relationship(ind, 'indicates', mal, id=RELATIONSHIP_IDS[0])
|
|
|
|
rel2 = stix2.v21.Relationship(mal, 'targets', idy, id=RELATIONSHIP_IDS[1])
|
|
|
|
rel3 = stix2.v21.Relationship(cam, 'uses', mal, id=RELATIONSHIP_IDS[2])
|
2018-07-03 13:00:18 +02:00
|
|
|
stix_objs = [cam, idy, ind, mal, rel1, rel2, rel3]
|
2018-07-03 15:40:51 +02:00
|
|
|
fs = stix2.FileSystemStore(FS_PATH)
|
2018-07-03 13:00:18 +02:00
|
|
|
for o in stix_objs:
|
|
|
|
fs.add(o)
|
|
|
|
yield fs
|
|
|
|
|
|
|
|
for o in stix_objs:
|
2018-12-06 21:19:50 +01:00
|
|
|
filepath = os.path.join(
|
|
|
|
FS_PATH, o.type, o.id,
|
|
|
|
_timestamp2filename(o.modified) + '.json',
|
|
|
|
)
|
2018-12-06 21:11:30 +01:00
|
|
|
|
|
|
|
# Some test-scoped fixtures (e.g. fs_store) delete all campaigns, so by
|
|
|
|
# the time this module-scoped fixture tears itself down, it may find
|
|
|
|
# its campaigns already gone, which causes not-found errors.
|
|
|
|
try:
|
|
|
|
os.remove(filepath)
|
|
|
|
except OSError as e:
|
|
|
|
# 3 is the ERROR_PATH_NOT_FOUND windows error code. Which has an
|
|
|
|
# errno symbolic value, but not the windows meaning...
|
|
|
|
if e.errno in (errno.ENOENT, 3):
|
|
|
|
continue
|
|
|
|
raise
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_source_nonexistent_folder():
|
|
|
|
with pytest.raises(ValueError) as excinfo:
|
2018-07-03 15:40:51 +02:00
|
|
|
stix2.FileSystemSource('nonexistent-folder')
|
2018-07-03 13:00:18 +02:00
|
|
|
assert "for STIX data does not exist" in str(excinfo)
|
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_sink_nonexistent_folder():
|
|
|
|
with pytest.raises(ValueError) as excinfo:
|
2018-07-03 15:40:51 +02:00
|
|
|
stix2.FileSystemSink('nonexistent-folder')
|
2018-07-03 13:00:18 +02:00
|
|
|
assert "for STIX data does not exist" in str(excinfo)
|
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_source_bad_json_file(fs_source, bad_json_files):
|
|
|
|
# this tests the handling of two bad json files
|
|
|
|
# - one file should just be skipped (silently) as its a ".txt" extension
|
|
|
|
# - one file should be parsed and raise Exception bc its not JSON
|
|
|
|
try:
|
|
|
|
fs_source.get("intrusion-set--test-bad-json")
|
|
|
|
except TypeError as e:
|
|
|
|
assert "intrusion-set--test-bad-json" in str(e)
|
|
|
|
assert "could either not be parsed to JSON or was not valid STIX JSON" in str(e)
|
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_source_bad_stix_file(fs_source, bad_stix_files):
|
|
|
|
# this tests handling of bad STIX json object
|
|
|
|
try:
|
|
|
|
fs_source.get("intrusion-set--test-non-stix")
|
2018-12-06 21:11:30 +01:00
|
|
|
except STIXError as e:
|
|
|
|
assert "Can't parse object with no 'type' property" in str(e)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
|
2018-12-06 21:11:30 +01:00
|
|
|
def test_filesystem_source_get_object(fs_source):
|
|
|
|
# get (latest) object
|
2018-07-03 13:00:18 +02:00
|
|
|
mal = fs_source.get("malware--6b616fc1-1505-48e3-8b2c-0d19337bff38")
|
|
|
|
assert mal.id == "malware--6b616fc1-1505-48e3-8b2c-0d19337bff38"
|
|
|
|
assert mal.name == "Rover"
|
2018-12-06 21:19:50 +01:00
|
|
|
assert mal.modified == datetime.datetime(
|
|
|
|
2018, 11, 16, 22, 54, 20, 390000,
|
|
|
|
pytz.utc,
|
|
|
|
)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
|
2018-12-06 21:11:30 +01:00
|
|
|
def test_filesystem_source_get_nonexistent_object(fs_source):
|
2018-07-03 13:00:18 +02:00
|
|
|
ind = fs_source.get("indicator--6b616fc1-1505-48e3-8b2c-0d19337bff38")
|
|
|
|
assert ind is None
|
|
|
|
|
|
|
|
|
2018-12-06 21:11:30 +01:00
|
|
|
def test_filesystem_source_all_versions(fs_source):
|
|
|
|
ids = fs_source.all_versions(
|
2018-12-06 21:19:50 +01:00
|
|
|
"identity--c78cb6e5-0c4b-4611-8297-d1b8b55e40b5",
|
2018-12-06 21:11:30 +01:00
|
|
|
)
|
|
|
|
assert len(ids) == 2
|
2018-12-06 21:19:50 +01:00
|
|
|
assert all(
|
|
|
|
id_.id == "identity--c78cb6e5-0c4b-4611-8297-d1b8b55e40b5"
|
|
|
|
for id_ in ids
|
|
|
|
)
|
2018-12-06 21:11:30 +01:00
|
|
|
assert all(id_.name == "The MITRE Corporation" for id_ in ids)
|
|
|
|
assert all(id_.type == "identity" for id_ in ids)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
|
2018-12-06 21:11:30 +01:00
|
|
|
def test_filesystem_source_query_single(fs_source):
|
2018-07-03 13:00:18 +02:00
|
|
|
# query2
|
2018-07-03 15:40:51 +02:00
|
|
|
is_2 = fs_source.query([stix2.Filter("external_references.external_id", '=', "T1027")])
|
2018-07-03 13:00:18 +02:00
|
|
|
assert len(is_2) == 1
|
|
|
|
|
|
|
|
is_2 = is_2[0]
|
|
|
|
assert is_2.id == "attack-pattern--b3d682b6-98f2-4fb0-aa3b-b4df007ca70a"
|
|
|
|
assert is_2.type == "attack-pattern"
|
|
|
|
|
|
|
|
|
2018-12-06 21:11:30 +01:00
|
|
|
def test_filesystem_source_query_multiple(fs_source):
|
2018-07-03 13:00:18 +02:00
|
|
|
# query
|
2018-07-03 15:40:51 +02:00
|
|
|
intrusion_sets = fs_source.query([stix2.Filter("type", '=', "intrusion-set")])
|
2018-07-03 13:00:18 +02:00
|
|
|
assert len(intrusion_sets) == 2
|
|
|
|
assert "intrusion-set--a653431d-6a5e-4600-8ad3-609b5af57064" in [is_.id for is_ in intrusion_sets]
|
|
|
|
assert "intrusion-set--f3bdec95-3d62-42d9-a840-29630f6cdc1a" in [is_.id for is_ in intrusion_sets]
|
|
|
|
|
|
|
|
is_1 = [is_ for is_ in intrusion_sets if is_.id == "intrusion-set--f3bdec95-3d62-42d9-a840-29630f6cdc1a"][0]
|
|
|
|
assert "DragonOK" in is_1.aliases
|
|
|
|
assert len(is_1.external_references) == 4
|
|
|
|
|
|
|
|
|
2018-12-06 21:11:30 +01:00
|
|
|
def test_filesystem_source_backward_compatible(fs_source):
|
|
|
|
# this specific object is outside an "ID" directory; make sure we can get
|
|
|
|
# it.
|
|
|
|
modified = datetime.datetime(2018, 11, 16, 22, 54, 20, 390000, pytz.utc)
|
|
|
|
results = fs_source.query([
|
|
|
|
stix2.Filter("type", "=", "malware"),
|
|
|
|
stix2.Filter("id", "=", "malware--6b616fc1-1505-48e3-8b2c-0d19337bff38"),
|
2018-12-06 21:19:50 +01:00
|
|
|
stix2.Filter("modified", "=", modified),
|
2018-12-06 21:11:30 +01:00
|
|
|
])
|
|
|
|
|
|
|
|
assert len(results) == 1
|
|
|
|
result = results[0]
|
|
|
|
assert result.type == "malware"
|
|
|
|
assert result.id == "malware--6b616fc1-1505-48e3-8b2c-0d19337bff38"
|
|
|
|
assert result.modified == modified
|
|
|
|
assert result.malware_types == ["version four"]
|
|
|
|
|
|
|
|
|
2018-07-03 13:00:18 +02:00
|
|
|
def test_filesystem_sink_add_python_stix_object(fs_sink, fs_source):
|
|
|
|
# add python stix object
|
2018-07-13 17:10:05 +02:00
|
|
|
camp1 = stix2.v21.Campaign(
|
|
|
|
name="Hannibal",
|
|
|
|
objective="Targeting Italian and Spanish Diplomat internet accounts",
|
|
|
|
aliases=["War Elephant"],
|
|
|
|
)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
fs_sink.add(camp1)
|
|
|
|
|
2018-12-06 21:19:50 +01:00
|
|
|
filepath = os.path.join(
|
|
|
|
FS_PATH, "campaign", camp1.id,
|
|
|
|
_timestamp2filename(camp1.modified) + ".json",
|
|
|
|
)
|
2018-12-06 21:11:30 +01:00
|
|
|
assert os.path.exists(filepath)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
camp1_r = fs_source.get(camp1.id)
|
|
|
|
assert camp1_r.id == camp1.id
|
|
|
|
assert camp1_r.name == "Hannibal"
|
|
|
|
assert "War Elephant" in camp1_r.aliases
|
|
|
|
|
2018-12-06 21:11:30 +01:00
|
|
|
os.remove(filepath)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_sink_add_stix_object_dict(fs_sink, fs_source):
|
|
|
|
# add stix object dict
|
|
|
|
camp2 = {
|
|
|
|
"name": "Aurelius",
|
|
|
|
"type": "campaign",
|
|
|
|
"objective": "German and French Intelligence Services",
|
|
|
|
"aliases": ["Purple Robes"],
|
2018-07-11 15:43:37 +02:00
|
|
|
"id": "campaign--8e2e2d2b-17d4-4cbf-938f-98ee46b3cd3f",
|
2018-07-13 17:10:05 +02:00
|
|
|
"created": "2017-05-31T21:31:53.197755Z",
|
2018-12-06 21:19:50 +01:00
|
|
|
"modified": "2017-05-31T21:31:53.197755Z",
|
2018-07-03 13:00:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fs_sink.add(camp2)
|
|
|
|
|
2018-12-06 21:11:30 +01:00
|
|
|
# Need to get the exact "modified" timestamp which would have been
|
|
|
|
# in effect at the time the object was saved to the sink, which determines
|
|
|
|
# the filename it would have been saved as. It may not be exactly the same
|
|
|
|
# as what's in the dict, since the parsing process can enforce a precision
|
|
|
|
# constraint (e.g. truncate to milliseconds), which results in a slightly
|
|
|
|
# different name.
|
|
|
|
camp2obj = stix2.parse(camp2)
|
2018-12-06 21:19:50 +01:00
|
|
|
filepath = os.path.join(
|
|
|
|
FS_PATH, "campaign", camp2obj["id"],
|
|
|
|
_timestamp2filename(camp2obj["modified"]) + ".json",
|
|
|
|
)
|
2018-12-06 21:11:30 +01:00
|
|
|
|
|
|
|
assert os.path.exists(filepath)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
camp2_r = fs_source.get(camp2["id"])
|
|
|
|
assert camp2_r.id == camp2["id"]
|
|
|
|
assert camp2_r.name == camp2["name"]
|
|
|
|
assert "Purple Robes" in camp2_r.aliases
|
|
|
|
|
2018-12-06 21:11:30 +01:00
|
|
|
os.remove(filepath)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_sink_add_stix_bundle_dict(fs_sink, fs_source):
|
|
|
|
# add stix bundle dict
|
|
|
|
bund = {
|
|
|
|
"type": "bundle",
|
2018-07-11 15:43:37 +02:00
|
|
|
"id": "bundle--040ae5ec-2e91-4e94-b075-bc8b368e8ca3",
|
2018-07-03 13:00:18 +02:00
|
|
|
"objects": [
|
|
|
|
{
|
|
|
|
"name": "Atilla",
|
|
|
|
"type": "campaign",
|
|
|
|
"objective": "Bulgarian, Albanian and Romanian Intelligence Services",
|
|
|
|
"aliases": ["Huns"],
|
2018-07-11 15:43:37 +02:00
|
|
|
"id": "campaign--b8f86161-ccae-49de-973a-4ca320c62478",
|
2018-07-13 17:10:05 +02:00
|
|
|
"created": "2017-05-31T21:31:53.197755Z",
|
2018-12-06 21:19:50 +01:00
|
|
|
"modified": "2017-05-31T21:31:53.197755Z",
|
|
|
|
},
|
|
|
|
],
|
2018-07-03 13:00:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fs_sink.add(bund)
|
|
|
|
|
2018-12-06 21:11:30 +01:00
|
|
|
camp_obj = stix2.parse(bund["objects"][0])
|
2018-12-06 21:19:50 +01:00
|
|
|
filepath = os.path.join(
|
|
|
|
FS_PATH, "campaign", camp_obj["id"],
|
|
|
|
_timestamp2filename(camp_obj["modified"]) + ".json",
|
|
|
|
)
|
2018-12-06 21:11:30 +01:00
|
|
|
|
|
|
|
assert os.path.exists(filepath)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
camp3_r = fs_source.get(bund["objects"][0]["id"])
|
|
|
|
assert camp3_r.id == bund["objects"][0]["id"]
|
|
|
|
assert camp3_r.name == bund["objects"][0]["name"]
|
|
|
|
assert "Huns" in camp3_r.aliases
|
|
|
|
|
2018-12-06 21:11:30 +01:00
|
|
|
os.remove(filepath)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_sink_add_json_stix_object(fs_sink, fs_source):
|
|
|
|
# add json-encoded stix obj
|
2018-07-11 15:43:37 +02:00
|
|
|
camp4 = '{"type": "campaign", "id":"campaign--6a6ca372-ba07-42cc-81ef-9840fc1f963d",'\
|
2018-12-06 21:11:30 +01:00
|
|
|
' "created":"2017-05-31T21:31:53.197755Z",'\
|
|
|
|
' "modified":"2017-05-31T21:31:53.197755Z",'\
|
|
|
|
' "name": "Ghengis Khan", "objective": "China and Russian infrastructure"}'
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
fs_sink.add(camp4)
|
|
|
|
|
2018-12-06 21:11:30 +01:00
|
|
|
camp4obj = stix2.parse(camp4)
|
2018-12-06 21:19:50 +01:00
|
|
|
filepath = os.path.join(
|
|
|
|
FS_PATH, "campaign",
|
|
|
|
"campaign--6a6ca372-ba07-42cc-81ef-9840fc1f963d",
|
|
|
|
_timestamp2filename(camp4obj["modified"]) + ".json",
|
|
|
|
)
|
2018-12-06 21:11:30 +01:00
|
|
|
|
|
|
|
assert os.path.exists(filepath)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
2018-07-11 15:43:37 +02:00
|
|
|
camp4_r = fs_source.get("campaign--6a6ca372-ba07-42cc-81ef-9840fc1f963d")
|
|
|
|
assert camp4_r.id == "campaign--6a6ca372-ba07-42cc-81ef-9840fc1f963d"
|
2018-07-03 13:00:18 +02:00
|
|
|
assert camp4_r.name == "Ghengis Khan"
|
|
|
|
|
2018-12-06 21:11:30 +01:00
|
|
|
os.remove(filepath)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_sink_json_stix_bundle(fs_sink, fs_source):
|
|
|
|
# add json-encoded stix bundle
|
2018-07-11 15:43:37 +02:00
|
|
|
bund2 = '{"type": "bundle", "id": "bundle--3d267103-8475-4d8f-b321-35ec6eccfa37",' \
|
2018-12-06 21:11:30 +01:00
|
|
|
' "spec_version": "2.0", "objects": [{"type": "campaign", "id": "campaign--2c03b8bf-82ee-433e-9918-ca2cb6e9534b",' \
|
|
|
|
' "created":"2017-05-31T21:31:53.197755Z",'\
|
|
|
|
' "modified":"2017-05-31T21:31:53.197755Z",'\
|
|
|
|
' "name": "Spartacus", "objective": "Oppressive regimes of Africa and Middle East"}]}'
|
2018-07-03 13:00:18 +02:00
|
|
|
fs_sink.add(bund2)
|
|
|
|
|
2018-12-06 21:11:30 +01:00
|
|
|
bund2obj = stix2.parse(bund2)
|
|
|
|
camp_obj = bund2obj["objects"][0]
|
|
|
|
|
2018-12-06 21:19:50 +01:00
|
|
|
filepath = os.path.join(
|
|
|
|
FS_PATH, "campaign",
|
|
|
|
"campaign--2c03b8bf-82ee-433e-9918-ca2cb6e9534b",
|
|
|
|
_timestamp2filename(camp_obj["modified"]) + ".json",
|
|
|
|
)
|
2018-12-06 21:11:30 +01:00
|
|
|
|
|
|
|
assert os.path.exists(filepath)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
2018-07-11 15:43:37 +02:00
|
|
|
camp5_r = fs_source.get("campaign--2c03b8bf-82ee-433e-9918-ca2cb6e9534b")
|
|
|
|
assert camp5_r.id == "campaign--2c03b8bf-82ee-433e-9918-ca2cb6e9534b"
|
2018-07-03 13:00:18 +02:00
|
|
|
assert camp5_r.name == "Spartacus"
|
|
|
|
|
2018-12-06 21:11:30 +01:00
|
|
|
os.remove(filepath)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_sink_add_objects_list(fs_sink, fs_source):
|
|
|
|
# add list of objects
|
2018-07-13 17:10:05 +02:00
|
|
|
camp6 = stix2.v21.Campaign(
|
|
|
|
name="Comanche",
|
|
|
|
objective="US Midwest manufacturing firms, oil refineries, and businesses",
|
|
|
|
aliases=["Horse Warrior"],
|
|
|
|
)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
camp7 = {
|
|
|
|
"name": "Napolean",
|
|
|
|
"type": "campaign",
|
|
|
|
"objective": "Central and Eastern Europe military commands and departments",
|
|
|
|
"aliases": ["The Frenchmen"],
|
2018-07-11 15:43:37 +02:00
|
|
|
"id": "campaign--122818b6-1112-4fb0-b11b-b111107ca70a",
|
2018-07-13 17:10:05 +02:00
|
|
|
"created": "2017-05-31T21:31:53.197755Z",
|
2018-12-06 21:19:50 +01:00
|
|
|
"modified": "2017-05-31T21:31:53.197755Z",
|
2018-07-03 13:00:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fs_sink.add([camp6, camp7])
|
|
|
|
|
2018-12-06 21:11:30 +01:00
|
|
|
camp7obj = stix2.parse(camp7)
|
|
|
|
|
2018-12-06 21:19:50 +01:00
|
|
|
camp6filepath = os.path.join(
|
|
|
|
FS_PATH, "campaign", camp6.id,
|
|
|
|
_timestamp2filename(camp6["modified"]) +
|
|
|
|
".json",
|
|
|
|
)
|
2018-12-06 21:11:30 +01:00
|
|
|
camp7filepath = os.path.join(
|
|
|
|
FS_PATH, "campaign", "campaign--122818b6-1112-4fb0-b11b-b111107ca70a",
|
2018-12-06 21:19:50 +01:00
|
|
|
_timestamp2filename(camp7obj["modified"]) + ".json",
|
|
|
|
)
|
2018-12-06 21:11:30 +01:00
|
|
|
|
|
|
|
assert os.path.exists(camp6filepath)
|
|
|
|
assert os.path.exists(camp7filepath)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
camp6_r = fs_source.get(camp6.id)
|
|
|
|
assert camp6_r.id == camp6.id
|
|
|
|
assert "Horse Warrior" in camp6_r.aliases
|
|
|
|
|
|
|
|
camp7_r = fs_source.get(camp7["id"])
|
|
|
|
assert camp7_r.id == camp7["id"]
|
|
|
|
assert "The Frenchmen" in camp7_r.aliases
|
|
|
|
|
|
|
|
# remove all added objects
|
2018-12-06 21:11:30 +01:00
|
|
|
os.remove(camp6filepath)
|
|
|
|
os.remove(camp7filepath)
|
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_sink_marking(fs_sink):
|
|
|
|
marking = stix2.v21.MarkingDefinition(
|
2019-05-03 15:58:45 +02:00
|
|
|
id="marking-definition--34098fce-860f-48ae-8e50-ebd3cc5e41da",
|
|
|
|
created="2017-01-20T00:00:00.000Z",
|
2018-12-06 21:11:30 +01:00
|
|
|
definition_type="tlp",
|
2018-12-06 21:19:50 +01:00
|
|
|
definition=stix2.v21.TLPMarking(tlp="green"),
|
2018-12-06 21:11:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
fs_sink.add(marking)
|
|
|
|
marking_filepath = os.path.join(
|
2018-12-06 21:19:50 +01:00
|
|
|
FS_PATH, "marking-definition", marking["id"] + ".json",
|
2018-12-06 21:11:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert os.path.exists(marking_filepath)
|
|
|
|
|
|
|
|
os.remove(marking_filepath)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_store_get_stored_as_bundle(fs_store):
|
|
|
|
coa = fs_store.get("course-of-action--95ddb356-7ba0-4bd9-a889-247262b8946f")
|
|
|
|
assert coa.id == "course-of-action--95ddb356-7ba0-4bd9-a889-247262b8946f"
|
|
|
|
assert coa.type == "course-of-action"
|
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_store_get_stored_as_object(fs_store):
|
|
|
|
coa = fs_store.get("course-of-action--d9727aee-48b8-4fdb-89e2-4c49746ba4dd")
|
|
|
|
assert coa.id == "course-of-action--d9727aee-48b8-4fdb-89e2-4c49746ba4dd"
|
|
|
|
assert coa.type == "course-of-action"
|
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_store_all_versions(fs_store):
|
2018-12-06 21:11:30 +01:00
|
|
|
rels = fs_store.all_versions("relationship--70dc6b5c-c524-429e-a6ab-0dd40f0482c1")
|
|
|
|
assert len(rels) == 1
|
|
|
|
rel = rels[0]
|
2018-07-03 13:00:18 +02:00
|
|
|
assert rel.id == "relationship--70dc6b5c-c524-429e-a6ab-0dd40f0482c1"
|
|
|
|
assert rel.type == "relationship"
|
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_store_query(fs_store):
|
|
|
|
# query()
|
2018-07-12 20:33:00 +02:00
|
|
|
tools = fs_store.query([stix2.Filter("tool_types", "in", "tool")])
|
2018-07-03 13:00:18 +02:00
|
|
|
assert len(tools) == 2
|
|
|
|
assert "tool--242f3da3-4425-4d11-8f5c-b842886da966" in [tool.id for tool in tools]
|
|
|
|
assert "tool--03342581-f790-4f03-ba41-e82e67392e23" in [tool.id for tool in tools]
|
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_store_query_single_filter(fs_store):
|
2018-07-12 20:33:00 +02:00
|
|
|
query = stix2.Filter("tool_types", "in", "tool")
|
2018-07-03 13:00:18 +02:00
|
|
|
tools = fs_store.query(query)
|
|
|
|
assert len(tools) == 2
|
|
|
|
assert "tool--242f3da3-4425-4d11-8f5c-b842886da966" in [tool.id for tool in tools]
|
|
|
|
assert "tool--03342581-f790-4f03-ba41-e82e67392e23" in [tool.id for tool in tools]
|
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_store_empty_query(fs_store):
|
|
|
|
results = fs_store.query() # returns all
|
2018-12-06 21:11:30 +01:00
|
|
|
assert len(results) == 30
|
|
|
|
assert "tool--242f3da3-4425-4d11-8f5c-b842886da966" in [obj.id for obj in results]
|
|
|
|
assert "marking-definition--fa42a846-8d90-4e51-bc29-71d5b4802168" in [obj.id for obj in results]
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_store_query_multiple_filters(fs_store):
|
2018-07-12 20:33:00 +02:00
|
|
|
fs_store.source.filters.add(stix2.Filter("tool_types", "in", "tool"))
|
2018-07-03 15:40:51 +02:00
|
|
|
tools = fs_store.query(stix2.Filter("id", "=", "tool--242f3da3-4425-4d11-8f5c-b842886da966"))
|
2018-07-03 13:00:18 +02:00
|
|
|
assert len(tools) == 1
|
|
|
|
assert tools[0].id == "tool--242f3da3-4425-4d11-8f5c-b842886da966"
|
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_store_query_dont_include_type_folder(fs_store):
|
2018-07-03 15:40:51 +02:00
|
|
|
results = fs_store.query(stix2.Filter("type", "!=", "tool"))
|
2018-12-06 21:11:30 +01:00
|
|
|
assert len(results) == 28
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_store_add(fs_store):
|
|
|
|
# add()
|
2018-07-13 17:10:05 +02:00
|
|
|
camp1 = stix2.v21.Campaign(
|
|
|
|
name="Great Heathen Army",
|
|
|
|
objective="Targeting the government of United Kingdom and insitutions affiliated with the Church Of England",
|
|
|
|
aliases=["Ragnar"],
|
|
|
|
)
|
2018-07-03 13:00:18 +02:00
|
|
|
fs_store.add(camp1)
|
|
|
|
|
|
|
|
camp1_r = fs_store.get(camp1.id)
|
|
|
|
assert camp1_r.id == camp1.id
|
|
|
|
assert camp1_r.name == camp1.name
|
|
|
|
|
2018-12-06 21:19:50 +01:00
|
|
|
filepath = os.path.join(
|
|
|
|
FS_PATH, "campaign", camp1_r.id,
|
|
|
|
_timestamp2filename(camp1_r.modified) + ".json",
|
|
|
|
)
|
2018-12-06 21:11:30 +01:00
|
|
|
|
2018-07-03 13:00:18 +02:00
|
|
|
# remove
|
2018-12-06 21:11:30 +01:00
|
|
|
os.remove(filepath)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_store_add_as_bundle():
|
2018-07-03 15:40:51 +02:00
|
|
|
fs_store = stix2.FileSystemStore(FS_PATH, bundlify=True)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
2018-07-13 17:10:05 +02:00
|
|
|
camp1 = stix2.v21.Campaign(
|
|
|
|
name="Great Heathen Army",
|
|
|
|
objective="Targeting the government of United Kingdom and insitutions affiliated with the Church Of England",
|
|
|
|
aliases=["Ragnar"],
|
|
|
|
)
|
2018-07-03 13:00:18 +02:00
|
|
|
fs_store.add(camp1)
|
|
|
|
|
2018-12-06 21:19:50 +01:00
|
|
|
filepath = os.path.join(
|
|
|
|
FS_PATH, "campaign", camp1.id,
|
|
|
|
_timestamp2filename(camp1.modified) + ".json",
|
|
|
|
)
|
2018-12-06 21:11:30 +01:00
|
|
|
|
|
|
|
with open(filepath) as bundle_file:
|
2018-07-03 13:00:18 +02:00
|
|
|
assert '"type": "bundle"' in bundle_file.read()
|
|
|
|
|
|
|
|
camp1_r = fs_store.get(camp1.id)
|
|
|
|
assert camp1_r.id == camp1.id
|
|
|
|
assert camp1_r.name == camp1.name
|
|
|
|
|
|
|
|
shutil.rmtree(os.path.join(FS_PATH, "campaign"), True)
|
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_add_bundle_object(fs_store):
|
2018-07-03 15:40:51 +02:00
|
|
|
bundle = stix2.v21.Bundle()
|
2018-07-03 13:00:18 +02:00
|
|
|
fs_store.add(bundle)
|
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_store_add_invalid_object(fs_store):
|
2018-07-11 15:43:37 +02:00
|
|
|
ind = ('campaign', 'campaign--8e2e2d2b-17d4-4cbf-938f-98ee46b3cd3f') # tuple isn't valid
|
2018-07-03 13:00:18 +02:00
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
fs_store.add(ind)
|
|
|
|
assert 'stix_data must be' in str(excinfo.value)
|
|
|
|
assert 'a STIX object' in str(excinfo.value)
|
|
|
|
assert 'JSON formatted STIX' in str(excinfo.value)
|
|
|
|
assert 'JSON formatted STIX bundle' in str(excinfo.value)
|
|
|
|
|
|
|
|
|
2018-12-06 21:11:30 +01:00
|
|
|
def test_filesystem_store_add_marking(fs_store):
|
|
|
|
marking = stix2.v21.MarkingDefinition(
|
2019-05-03 15:58:45 +02:00
|
|
|
id="marking-definition--34098fce-860f-48ae-8e50-ebd3cc5e41da",
|
|
|
|
created="2017-01-20T00:00:00.000Z",
|
2018-12-06 21:11:30 +01:00
|
|
|
definition_type="tlp",
|
2018-12-06 21:19:50 +01:00
|
|
|
definition=stix2.v21.TLPMarking(tlp="green"),
|
2018-12-06 21:11:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
fs_store.add(marking)
|
|
|
|
marking_filepath = os.path.join(
|
2018-12-06 21:19:50 +01:00
|
|
|
FS_PATH, "marking-definition", marking["id"] + ".json",
|
2018-12-06 21:11:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert os.path.exists(marking_filepath)
|
|
|
|
|
|
|
|
marking_r = fs_store.get(marking["id"])
|
|
|
|
assert marking_r["id"] == marking["id"]
|
|
|
|
assert marking_r["definition"]["tlp"] == "green"
|
|
|
|
|
|
|
|
os.remove(marking_filepath)
|
|
|
|
|
|
|
|
|
2018-07-03 13:00:18 +02:00
|
|
|
def test_filesystem_object_with_custom_property(fs_store):
|
2018-07-13 17:10:05 +02:00
|
|
|
camp = stix2.v21.Campaign(
|
|
|
|
name="Scipio Africanus",
|
|
|
|
objective="Defeat the Carthaginians",
|
|
|
|
x_empire="Roman",
|
|
|
|
allow_custom=True,
|
|
|
|
)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
2018-07-09 21:20:04 +02:00
|
|
|
fs_store.add(camp)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
|
|
|
camp_r = fs_store.get(camp.id)
|
|
|
|
assert camp_r.id == camp.id
|
|
|
|
assert camp_r.x_empire == camp.x_empire
|
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_object_with_custom_property_in_bundle(fs_store):
|
2018-07-13 17:10:05 +02:00
|
|
|
camp = stix2.v21.Campaign(
|
|
|
|
name="Scipio Africanus",
|
|
|
|
objective="Defeat the Carthaginians",
|
|
|
|
x_empire="Roman",
|
|
|
|
allow_custom=True,
|
|
|
|
)
|
2018-07-03 13:00:18 +02:00
|
|
|
|
2018-07-03 15:40:51 +02:00
|
|
|
bundle = stix2.v21.Bundle(camp, allow_custom=True)
|
2018-07-03 13:00:18 +02:00
|
|
|
fs_store.add(bundle)
|
|
|
|
|
|
|
|
camp_r = fs_store.get(camp.id)
|
|
|
|
assert camp_r.id == camp.id
|
|
|
|
assert camp_r.x_empire == camp.x_empire
|
|
|
|
|
|
|
|
|
|
|
|
def test_filesystem_custom_object(fs_store):
|
2018-07-13 17:10:05 +02:00
|
|
|
@stix2.v21.CustomObject(
|
|
|
|
'x-new-obj', [
|
|
|
|
('property1', stix2.properties.StringProperty(required=True)),
|
|
|
|
],
|
|
|
|
)
|
2018-07-03 13:00:18 +02:00
|
|
|
class NewObj():
|
|
|
|
pass
|
|
|
|
|
|
|
|
newobj = NewObj(property1='something')
|
|
|
|
fs_store.add(newobj)
|
|
|
|
|
|
|
|
newobj_r = fs_store.get(newobj.id)
|
|
|
|
assert newobj_r["id"] == newobj["id"]
|
|
|
|
assert newobj_r["property1"] == 'something'
|
|
|
|
|
|
|
|
# remove dir
|
|
|
|
shutil.rmtree(os.path.join(FS_PATH, "x-new-obj"), True)
|
|
|
|
|
|
|
|
|
|
|
|
def test_relationships(rel_fs_store):
|
|
|
|
mal = rel_fs_store.get(MALWARE_ID)
|
|
|
|
resp = rel_fs_store.relationships(mal)
|
|
|
|
|
|
|
|
assert len(resp) == 3
|
|
|
|
assert any(x['id'] == RELATIONSHIP_IDS[0] for x in resp)
|
|
|
|
assert any(x['id'] == RELATIONSHIP_IDS[1] for x in resp)
|
|
|
|
assert any(x['id'] == RELATIONSHIP_IDS[2] for x in resp)
|
|
|
|
|
|
|
|
|
|
|
|
def test_relationships_by_type(rel_fs_store):
|
|
|
|
mal = rel_fs_store.get(MALWARE_ID)
|
|
|
|
resp = rel_fs_store.relationships(mal, relationship_type='indicates')
|
|
|
|
|
|
|
|
assert len(resp) == 1
|
|
|
|
assert resp[0]['id'] == RELATIONSHIP_IDS[0]
|
|
|
|
|
|
|
|
|
|
|
|
def test_relationships_by_source(rel_fs_store):
|
|
|
|
resp = rel_fs_store.relationships(MALWARE_ID, source_only=True)
|
|
|
|
|
|
|
|
assert len(resp) == 1
|
|
|
|
assert resp[0]['id'] == RELATIONSHIP_IDS[1]
|
|
|
|
|
|
|
|
|
|
|
|
def test_relationships_by_target(rel_fs_store):
|
|
|
|
resp = rel_fs_store.relationships(MALWARE_ID, target_only=True)
|
|
|
|
|
|
|
|
assert len(resp) == 2
|
|
|
|
assert any(x['id'] == RELATIONSHIP_IDS[0] for x in resp)
|
|
|
|
assert any(x['id'] == RELATIONSHIP_IDS[2] for x in resp)
|
|
|
|
|
|
|
|
|
|
|
|
def test_relationships_by_target_and_type(rel_fs_store):
|
|
|
|
resp = rel_fs_store.relationships(MALWARE_ID, relationship_type='uses', target_only=True)
|
|
|
|
|
|
|
|
assert len(resp) == 1
|
|
|
|
assert any(x['id'] == RELATIONSHIP_IDS[2] for x in resp)
|
|
|
|
|
|
|
|
|
|
|
|
def test_relationships_by_target_and_source(rel_fs_store):
|
|
|
|
with pytest.raises(ValueError) as excinfo:
|
|
|
|
rel_fs_store.relationships(MALWARE_ID, target_only=True, source_only=True)
|
|
|
|
|
|
|
|
assert 'not both' in str(excinfo.value)
|
|
|
|
|
|
|
|
|
|
|
|
def test_related_to(rel_fs_store):
|
|
|
|
mal = rel_fs_store.get(MALWARE_ID)
|
|
|
|
resp = rel_fs_store.related_to(mal)
|
|
|
|
|
|
|
|
assert len(resp) == 3
|
|
|
|
assert any(x['id'] == CAMPAIGN_ID for x in resp)
|
|
|
|
assert any(x['id'] == INDICATOR_ID for x in resp)
|
|
|
|
assert any(x['id'] == IDENTITY_ID for x in resp)
|
|
|
|
|
|
|
|
|
|
|
|
def test_related_to_by_source(rel_fs_store):
|
|
|
|
resp = rel_fs_store.related_to(MALWARE_ID, source_only=True)
|
|
|
|
|
|
|
|
assert len(resp) == 1
|
|
|
|
assert any(x['id'] == IDENTITY_ID for x in resp)
|
|
|
|
|
|
|
|
|
|
|
|
def test_related_to_by_target(rel_fs_store):
|
|
|
|
resp = rel_fs_store.related_to(MALWARE_ID, target_only=True)
|
|
|
|
|
|
|
|
assert len(resp) == 2
|
|
|
|
assert any(x['id'] == CAMPAIGN_ID for x in resp)
|
|
|
|
assert any(x['id'] == INDICATOR_ID for x in resp)
|
2018-12-06 21:11:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_auth_set_white1():
|
|
|
|
auth_set = AuthSet({"A"}, set())
|
|
|
|
|
|
|
|
assert auth_set.auth_type == AuthSet.WHITE
|
|
|
|
assert auth_set.values == {"A"}
|
|
|
|
|
|
|
|
|
|
|
|
def test_auth_set_white2():
|
|
|
|
auth_set = AuthSet(set(), set())
|
|
|
|
|
|
|
|
assert auth_set.auth_type == AuthSet.WHITE
|
|
|
|
assert len(auth_set.values) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_auth_set_white3():
|
|
|
|
auth_set = AuthSet({"A", "B"}, {"B", "C"})
|
|
|
|
|
|
|
|
assert auth_set.auth_type == AuthSet.WHITE
|
|
|
|
assert auth_set.values == {"A"}
|
|
|
|
|
|
|
|
|
|
|
|
def test_auth_set_black1():
|
|
|
|
auth_set = AuthSet(None, {"B", "C"})
|
|
|
|
|
|
|
|
assert auth_set.auth_type == AuthSet.BLACK
|
|
|
|
assert auth_set.values == {"B", "C"}
|
|
|
|
|
|
|
|
|
|
|
|
def test_optimize_types1():
|
|
|
|
filters = [
|
2018-12-06 21:19:50 +01:00
|
|
|
stix2.Filter("type", "=", "foo"),
|
2018-12-06 21:11:30 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
auth_types, auth_ids = _find_search_optimizations(filters)
|
|
|
|
|
|
|
|
assert auth_types.auth_type == AuthSet.WHITE
|
|
|
|
assert auth_types.values == {"foo"}
|
|
|
|
assert auth_ids.auth_type == AuthSet.BLACK
|
|
|
|
assert len(auth_ids.values) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_optimize_types2():
|
|
|
|
filters = [
|
|
|
|
stix2.Filter("type", "=", "foo"),
|
2018-12-06 21:19:50 +01:00
|
|
|
stix2.Filter("type", "=", "bar"),
|
2018-12-06 21:11:30 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
auth_types, auth_ids = _find_search_optimizations(filters)
|
|
|
|
|
|
|
|
assert auth_types.auth_type == AuthSet.WHITE
|
|
|
|
assert len(auth_types.values) == 0
|
|
|
|
assert auth_ids.auth_type == AuthSet.BLACK
|
|
|
|
assert len(auth_ids.values) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_optimize_types3():
|
|
|
|
filters = [
|
|
|
|
stix2.Filter("type", "in", ["A", "B", "C"]),
|
2018-12-06 21:19:50 +01:00
|
|
|
stix2.Filter("type", "in", ["B", "C", "D"]),
|
2018-12-06 21:11:30 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
auth_types, auth_ids = _find_search_optimizations(filters)
|
|
|
|
|
|
|
|
assert auth_types.auth_type == AuthSet.WHITE
|
|
|
|
assert auth_types.values == {"B", "C"}
|
|
|
|
assert auth_ids.auth_type == AuthSet.BLACK
|
|
|
|
assert len(auth_ids.values) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_optimize_types4():
|
|
|
|
filters = [
|
|
|
|
stix2.Filter("type", "in", ["A", "B", "C"]),
|
2018-12-06 21:19:50 +01:00
|
|
|
stix2.Filter("type", "in", ["D", "E", "F"]),
|
2018-12-06 21:11:30 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
auth_types, auth_ids = _find_search_optimizations(filters)
|
|
|
|
|
|
|
|
assert auth_types.auth_type == AuthSet.WHITE
|
|
|
|
assert len(auth_types.values) == 0
|
|
|
|
assert auth_ids.auth_type == AuthSet.BLACK
|
|
|
|
assert len(auth_ids.values) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_optimize_types5():
|
|
|
|
filters = [
|
|
|
|
stix2.Filter("type", "in", ["foo", "bar"]),
|
2018-12-06 21:19:50 +01:00
|
|
|
stix2.Filter("type", "!=", "bar"),
|
2018-12-06 21:11:30 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
auth_types, auth_ids = _find_search_optimizations(filters)
|
|
|
|
|
|
|
|
assert auth_types.auth_type == AuthSet.WHITE
|
|
|
|
assert auth_types.values == {"foo"}
|
|
|
|
assert auth_ids.auth_type == AuthSet.BLACK
|
|
|
|
assert len(auth_ids.values) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_optimize_types6():
|
|
|
|
filters = [
|
|
|
|
stix2.Filter("type", "!=", "foo"),
|
2018-12-06 21:19:50 +01:00
|
|
|
stix2.Filter("type", "!=", "bar"),
|
2018-12-06 21:11:30 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
auth_types, auth_ids = _find_search_optimizations(filters)
|
|
|
|
|
|
|
|
assert auth_types.auth_type == AuthSet.BLACK
|
|
|
|
assert auth_types.values == {"foo", "bar"}
|
|
|
|
assert auth_ids.auth_type == AuthSet.BLACK
|
|
|
|
assert len(auth_ids.values) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_optimize_types7():
|
|
|
|
filters = [
|
|
|
|
stix2.Filter("type", "=", "foo"),
|
2018-12-06 21:19:50 +01:00
|
|
|
stix2.Filter("type", "!=", "foo"),
|
2018-12-06 21:11:30 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
auth_types, auth_ids = _find_search_optimizations(filters)
|
|
|
|
|
|
|
|
assert auth_types.auth_type == AuthSet.WHITE
|
|
|
|
assert len(auth_types.values) == 0
|
|
|
|
assert auth_ids.auth_type == AuthSet.BLACK
|
|
|
|
assert len(auth_ids.values) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_optimize_types8():
|
|
|
|
filters = []
|
|
|
|
|
|
|
|
auth_types, auth_ids = _find_search_optimizations(filters)
|
|
|
|
|
|
|
|
assert auth_types.auth_type == AuthSet.BLACK
|
|
|
|
assert len(auth_types.values) == 0
|
|
|
|
assert auth_ids.auth_type == AuthSet.BLACK
|
|
|
|
assert len(auth_ids.values) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_optimize_types_ids1():
|
|
|
|
filters = [
|
|
|
|
stix2.Filter("type", "in", ["foo", "bar"]),
|
2018-12-06 21:19:50 +01:00
|
|
|
stix2.Filter("id", "=", "foo--00000000-0000-0000-0000-000000000000"),
|
2018-12-06 21:11:30 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
auth_types, auth_ids = _find_search_optimizations(filters)
|
|
|
|
|
|
|
|
assert auth_types.auth_type == AuthSet.WHITE
|
|
|
|
assert auth_types.values == {"foo"}
|
|
|
|
assert auth_ids.auth_type == AuthSet.WHITE
|
|
|
|
assert auth_ids.values == {"foo--00000000-0000-0000-0000-000000000000"}
|
|
|
|
|
|
|
|
|
|
|
|
def test_optimize_types_ids2():
|
|
|
|
filters = [
|
|
|
|
stix2.Filter("type", "=", "foo"),
|
2018-12-06 21:19:50 +01:00
|
|
|
stix2.Filter("id", "=", "bar--00000000-0000-0000-0000-000000000000"),
|
2018-12-06 21:11:30 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
auth_types, auth_ids = _find_search_optimizations(filters)
|
|
|
|
|
|
|
|
assert auth_types.auth_type == AuthSet.WHITE
|
|
|
|
assert len(auth_types.values) == 0
|
|
|
|
assert auth_ids.auth_type == AuthSet.WHITE
|
|
|
|
assert len(auth_ids.values) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_optimize_types_ids3():
|
|
|
|
filters = [
|
|
|
|
stix2.Filter("type", "in", ["foo", "bar"]),
|
2018-12-06 21:19:50 +01:00
|
|
|
stix2.Filter("id", "!=", "bar--00000000-0000-0000-0000-000000000000"),
|
2018-12-06 21:11:30 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
auth_types, auth_ids = _find_search_optimizations(filters)
|
|
|
|
|
|
|
|
assert auth_types.auth_type == AuthSet.WHITE
|
|
|
|
assert auth_types.values == {"foo", "bar"}
|
|
|
|
assert auth_ids.auth_type == AuthSet.BLACK
|
|
|
|
assert auth_ids.values == {"bar--00000000-0000-0000-0000-000000000000"}
|
|
|
|
|
|
|
|
|
|
|
|
def test_optimize_types_ids4():
|
|
|
|
filters = [
|
|
|
|
stix2.Filter("type", "in", ["A", "B", "C"]),
|
2018-12-06 21:19:50 +01:00
|
|
|
stix2.Filter(
|
|
|
|
"id", "in", [
|
|
|
|
"B--00000000-0000-0000-0000-000000000000",
|
|
|
|
"C--00000000-0000-0000-0000-000000000000",
|
|
|
|
"D--00000000-0000-0000-0000-000000000000",
|
|
|
|
],
|
|
|
|
),
|
2018-12-06 21:11:30 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
auth_types, auth_ids = _find_search_optimizations(filters)
|
|
|
|
|
|
|
|
assert auth_types.auth_type == AuthSet.WHITE
|
|
|
|
assert auth_types.values == {"B", "C"}
|
|
|
|
assert auth_ids.auth_type == AuthSet.WHITE
|
|
|
|
assert auth_ids.values == {
|
|
|
|
"B--00000000-0000-0000-0000-000000000000",
|
2018-12-06 21:19:50 +01:00
|
|
|
"C--00000000-0000-0000-0000-000000000000",
|
2018-12-06 21:11:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def test_optimize_types_ids5():
|
|
|
|
filters = [
|
|
|
|
stix2.Filter("type", "in", ["A", "B", "C"]),
|
|
|
|
stix2.Filter("type", "!=", "C"),
|
2018-12-06 21:19:50 +01:00
|
|
|
stix2.Filter(
|
|
|
|
"id", "in", [
|
|
|
|
"B--00000000-0000-0000-0000-000000000000",
|
|
|
|
"C--00000000-0000-0000-0000-000000000000",
|
|
|
|
"D--00000000-0000-0000-0000-000000000000",
|
|
|
|
],
|
|
|
|
),
|
|
|
|
stix2.Filter("id", "!=", "D--00000000-0000-0000-0000-000000000000"),
|
2018-12-06 21:11:30 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
auth_types, auth_ids = _find_search_optimizations(filters)
|
|
|
|
|
|
|
|
assert auth_types.auth_type == AuthSet.WHITE
|
|
|
|
assert auth_types.values == {"B"}
|
|
|
|
assert auth_ids.auth_type == AuthSet.WHITE
|
|
|
|
assert auth_ids.values == {"B--00000000-0000-0000-0000-000000000000"}
|
|
|
|
|
|
|
|
|
|
|
|
def test_optimize_types_ids6():
|
|
|
|
filters = [
|
2018-12-06 21:19:50 +01:00
|
|
|
stix2.Filter("id", "=", "A--00000000-0000-0000-0000-000000000000"),
|
2018-12-06 21:11:30 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
auth_types, auth_ids = _find_search_optimizations(filters)
|
|
|
|
|
|
|
|
assert auth_types.auth_type == AuthSet.WHITE
|
|
|
|
assert auth_types.values == {"A"}
|
|
|
|
assert auth_ids.auth_type == AuthSet.WHITE
|
|
|
|
assert auth_ids.values == {"A--00000000-0000-0000-0000-000000000000"}
|
|
|
|
|
|
|
|
|
|
|
|
def test_search_auth_set_white1():
|
|
|
|
auth_set = AuthSet(
|
|
|
|
{"attack-pattern", "doesntexist"},
|
2018-12-06 21:19:50 +01:00
|
|
|
set(),
|
2018-12-06 21:11:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
results = _get_matching_dir_entries(FS_PATH, auth_set, stat.S_ISDIR)
|
|
|
|
assert results == ["attack-pattern"]
|
|
|
|
|
|
|
|
results = _get_matching_dir_entries(FS_PATH, auth_set, stat.S_ISREG)
|
|
|
|
assert len(results) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_search_auth_set_white2():
|
|
|
|
auth_set = AuthSet(
|
|
|
|
{
|
|
|
|
"malware--6b616fc1-1505-48e3-8b2c-0d19337bff38",
|
2018-12-06 21:19:50 +01:00
|
|
|
"malware--92ec0cbd-2c30-44a2-b270-73f4ec949841",
|
2018-12-06 21:11:30 +01:00
|
|
|
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"malware--92ec0cbd-2c30-44a2-b270-73f4ec949841",
|
|
|
|
"malware--96b08451-b27a-4ff6-893f-790e26393a8e",
|
2018-12-06 21:19:50 +01:00
|
|
|
"doesntexist",
|
|
|
|
},
|
2018-12-06 21:11:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
results = _get_matching_dir_entries(
|
|
|
|
os.path.join(FS_PATH, "malware"),
|
2018-12-06 21:19:50 +01:00
|
|
|
auth_set, stat.S_ISDIR,
|
2018-12-06 21:11:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert results == ["malware--6b616fc1-1505-48e3-8b2c-0d19337bff38"]
|
|
|
|
|
|
|
|
|
|
|
|
def test_search_auth_set_white3():
|
|
|
|
auth_set = AuthSet({"20170531213258226477", "doesntexist"}, set())
|
|
|
|
|
|
|
|
results = _get_matching_dir_entries(
|
2018-12-06 21:19:50 +01:00
|
|
|
os.path.join(
|
|
|
|
FS_PATH, "malware",
|
|
|
|
"malware--6b616fc1-1505-48e3-8b2c-0d19337bff38",
|
|
|
|
),
|
|
|
|
auth_set, stat.S_ISREG, ".json",
|
2018-12-06 21:11:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert results == ["20170531213258226477.json"]
|
|
|
|
|
|
|
|
|
|
|
|
def test_search_auth_set_black1():
|
|
|
|
auth_set = AuthSet(
|
|
|
|
None,
|
2018-12-06 21:19:50 +01:00
|
|
|
{"tool--242f3da3-4425-4d11-8f5c-b842886da966", "doesntexist"},
|
2018-12-06 21:11:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
results = _get_matching_dir_entries(
|
|
|
|
os.path.join(FS_PATH, "tool"),
|
2018-12-06 21:19:50 +01:00
|
|
|
auth_set, stat.S_ISDIR,
|
2018-12-06 21:11:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert set(results) == {
|
2018-12-06 21:19:50 +01:00
|
|
|
"tool--03342581-f790-4f03-ba41-e82e67392e23",
|
2018-12-06 21:11:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def test_search_auth_set_white_empty():
|
|
|
|
auth_set = AuthSet(
|
|
|
|
set(),
|
2018-12-06 21:19:50 +01:00
|
|
|
set(),
|
2018-12-06 21:11:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
results = _get_matching_dir_entries(FS_PATH, auth_set, stat.S_ISDIR)
|
|
|
|
|
|
|
|
assert len(results) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_search_auth_set_black_empty(rel_fs_store):
|
|
|
|
# Ensure rel_fs_store fixture has run so that the type directories are
|
|
|
|
# predictable (it adds "campaign").
|
|
|
|
auth_set = AuthSet(
|
|
|
|
None,
|
2018-12-06 21:19:50 +01:00
|
|
|
set(),
|
2018-12-06 21:11:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
results = _get_matching_dir_entries(FS_PATH, auth_set, stat.S_ISDIR)
|
|
|
|
|
|
|
|
# Should get all dirs
|
|
|
|
assert set(results) == {
|
|
|
|
"attack-pattern",
|
|
|
|
"campaign",
|
|
|
|
"course-of-action",
|
|
|
|
"identity",
|
|
|
|
"indicator",
|
|
|
|
"intrusion-set",
|
|
|
|
"malware",
|
|
|
|
"marking-definition",
|
|
|
|
"relationship",
|
2018-12-06 21:19:50 +01:00
|
|
|
"tool",
|
2018-12-06 21:11:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def test_timestamp2filename_naive():
|
|
|
|
dt = datetime.datetime(
|
|
|
|
2010, 6, 15,
|
2018-12-06 21:19:50 +01:00
|
|
|
8, 30, 10, 1234,
|
2018-12-06 21:11:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
filename = _timestamp2filename(dt)
|
|
|
|
assert filename == "20100615083010001234"
|
|
|
|
|
|
|
|
|
|
|
|
def test_timestamp2filename_tz():
|
|
|
|
# one hour west of UTC (i.e. an hour earlier)
|
|
|
|
tz = pytz.FixedOffset(-60)
|
|
|
|
dt = datetime.datetime(
|
|
|
|
2010, 6, 15,
|
|
|
|
7, 30, 10, 1234,
|
2018-12-06 21:19:50 +01:00
|
|
|
tz,
|
2018-12-06 21:11:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
filename = _timestamp2filename(dt)
|
|
|
|
assert filename == "20100615083010001234"
|