forked from OpenCTI-Platform/client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_indicators.py
119 lines (93 loc) · 3.41 KB
/
test_indicators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# coding: utf-8
import json
import pytest
from dateutil.parser import parse
from stix2 import TLP_GREEN, TLP_WHITE
from pycti import OpenCTIApiClient
@pytest.fixture
def api_client():
return OpenCTIApiClient(
"https://demo.opencti.io",
"e9613371-2fcc-4d7a-9ba1-ac93df589e5f",
ssl_verify=True,
)
@pytest.fixture
def test_indicator(api_client):
# Define the date
date = parse("2019-12-01").strftime("%Y-%m-%dT%H:%M:%SZ")
date2 = parse("2021-12-01").strftime("%Y-%m-%dT%H:%M:%SZ")
marking_definition_green = api_client.marking_definition.read(id=TLP_GREEN["id"])
marking_definition_white = api_client.marking_definition.read(id=TLP_WHITE["id"])
# Create the organization
organization = api_client.identity.create(
type="Organization", name="Testing Inc.", description="OpenCTI Test Org"
)
return api_client.indicator.create(
name="C2 server of the new campaign",
description="This is the C2 server of the campaign",
pattern_type="stix",
pattern="[domain-name:value = 'www.5z8.info' AND domain-name:resolves_to_refs[*].value = '198.51.100.1/32']",
x_opencti_main_observable_type="IPv4-Addr",
confidence=60,
x_opencti_score=80,
x_opencti_detection=True,
valid_from=date,
valid_until=date2,
created=date,
modified=date,
createdBy=organization["id"],
objectMarking=[
marking_definition_green["id"],
marking_definition_white["id"],
],
update=True,
# TODO: killChainPhases
)
def test_create_indicator(test_indicator):
assert test_indicator["id"] is not None or test_indicator["id"] != ""
def test_read_indicator_by_id(api_client, test_indicator):
indicator = api_client.indicator.read(id=test_indicator["id"])
assert indicator["id"] is not None or indicator["id"] != ""
assert indicator["id"] == test_indicator["id"]
def test_read_indicator_by_filter(api_client, test_indicator):
indicator2 = api_client.indicator.read(
filters=[
{
"key": "name",
"values": ["C2 server of the new campaign"],
}
]
)
assert indicator2["id"] is not None or indicator2["id"] != ""
assert indicator2["id"] == test_indicator["id"]
def test_get_100_indicators_with_pagination(api_client):
# Get 100 Indicators using the pagination
custom_attributes = """
id
revoked
created
"""
final_indicators = []
data = api_client.indicator.list(
first=50, customAttributes=custom_attributes, withPagination=True
)
final_indicators = final_indicators + data["entities"]
assert len(final_indicators) == 50
after = data["pagination"]["endCursor"]
data = api_client.indicator.list(
first=50,
after=after,
customAttributes=custom_attributes,
withPagination=True,
)
final_indicators = final_indicators + data["entities"]
assert len(final_indicators) == 100
def test_indicator_stix_marshall(api_client):
with open("tests/data/indicator_stix.json", "r") as content_file:
content = content_file.read()
json_data = json.loads(content)
for indic in json_data["objects"]:
imported_indicator = api_client.indicator.import_from_stix2(
stixObject=indic, update=True
)
assert imported_indicator is not None