Skip to content

Commit 5d0484a

Browse files
authored
[examples] Fix examples and add examples to drone pipeline (OpenCTI-Platform#286)
1 parent 069319f commit 5d0484a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+266
-189
lines changed

.drone.yml

+9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ steps:
2626
- pip3 install -r test-requirements.txt --user
2727
- python3 -m pytest --no-header -vv --disable-warnings --cov=pycti --drone
2828

29+
# always run the examples last since they don't clean up
30+
- name: example-tests
31+
image: python:3.10
32+
commands:
33+
- pip3 install -r requirements.txt --user
34+
- pip3 install .
35+
- cd examples/
36+
- /bin/bash run_all.sh
37+
2938
- name: slack
3039
image: plugins/slack
3140
settings:

examples/add_external_reference_to_report.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from pycti import OpenCTIApiClient
66

77
# Variables
8-
api_url = "https://demo.opencti.io"
9-
api_token = "YOUR_TOKEN"
8+
api_url = "http://opencti:4000"
9+
api_token = "bfa014e0-e02e-4aa6-a42b-603b19dcf159"
1010

1111
# OpenCTI initialization
1212
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/add_label_to_malware.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from pycti import OpenCTIApiClient
44

55
# Variables
6-
api_url = "https://demo.opencti.io"
7-
api_token = "YOUR_TOKEN"
6+
api_url = "http://opencti:4000"
7+
api_token = "bfa014e0-e02e-4aa6-a42b-603b19dcf159"
88

99
# OpenCTI initialization
1010
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/add_label_to_observable.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from pycti import OpenCTIApiClient
44

55
# Variables
6-
api_url = "https://demo.opencti.io"
7-
api_token = "YOUR_TOKEN"
6+
api_url = "http://opencti:4000"
7+
api_token = "bfa014e0-e02e-4aa6-a42b-603b19dcf159"
88

99
# OpenCTI initialization
1010
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/add_organization_to_sector.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
from pycti import OpenCTIApiClient
44

55
# Variables
6-
api_url = "https://demo.opencti.io"
7-
api_token = "YOUR_TOKEN"
6+
api_url = "http://opencti:4000"
7+
api_token = "bfa014e0-e02e-4aa6-a42b-603b19dcf159"
88

99
# OpenCTI initialization
1010
opencti_api_client = OpenCTIApiClient(api_url, api_token)
1111

1212
# Get the sector
13-
sector = opencti_api_client.identity.read(
14-
filters=[{"key": "name", "values": ["Banking institutions"]}]
13+
sector = opencti_api_client.identity.create(
14+
type="Sector", name="Banking institutions", description="Banks"
1515
)
1616

1717
# Create the organization

examples/add_tool_usage_to_intrusion-set.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from pycti import OpenCTIApiClient
44

55
# Variables
6-
api_url = "https://demo.opencti.io"
7-
api_token = "YOUR_TOKEN"
6+
api_url = "http://opencti:4000"
7+
api_token = "bfa014e0-e02e-4aa6-a42b-603b19dcf159"
88

99
# OpenCTI initialization
1010
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/ask_enrichment_of_observable.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
from pycti import OpenCTIApiClient
33

44
# Variables
5-
api_url = "https://demo.opencti.io"
6-
api_token = "YOUR_TOKEN"
5+
api_url = "http://opencti:4000"
6+
api_token = "bfa014e0-e02e-4aa6-a42b-603b19dcf159"
77
# Define name of INTERNAL_ENRICHMENT Connector which can enrich IPv4 addresses
88
connector_name = "AbuseIPDB"
99

examples/cmd_line_tag_latest_indicators_of_threat.py

+95-90
Original file line numberDiff line numberDiff line change
@@ -6,104 +6,109 @@
66
from pycti import OpenCTIApiClient
77

88
# Variables
9-
api_url = "https://demo.opencti.io"
10-
api_token = "YOUR_TOKEN"
9+
api_url = "http://opencti:4000"
10+
api_token = "bfa014e0-e02e-4aa6-a42b-603b19dcf159"
1111

1212
# OpenCTI initialization
1313
opencti_api_client = OpenCTIApiClient(api_url, api_token)
1414

15-
# Parameters
16-
parser = argparse.ArgumentParser(description="Mandatory arguments.")
17-
parser.add_argument(
18-
"--entity-type",
19-
dest="entity_type",
20-
default="Intrusion-Set",
21-
required=True,
22-
help="Type of the threat (Threat-Actor, Intrusion-Set, Campaign, X-OpenCTI,-Incident, Malware, Tool, Attack-Pattern)",
23-
)
24-
parser.add_argument(
25-
"--name",
26-
dest="name",
27-
required=True,
28-
help="Name of the threat",
29-
)
30-
parser.add_argument(
31-
"--created-after",
32-
dest="createdAfter",
33-
help="Indicator created before (ISO date)",
34-
)
35-
parser.add_argument(
36-
"--created-before",
37-
dest="createdBefore",
38-
help="Indicator created after (ISO date)",
39-
)
40-
parser.add_argument(
41-
"--tags",
42-
dest="tags",
43-
required=True,
44-
help="Tags to add or remove (separated by ,)",
45-
)
46-
parser.add_argument(
47-
"--operation",
48-
dest="operation",
49-
required=True,
50-
default="add",
51-
help="Operation (add/remove)",
52-
)
53-
args = parser.parse_args()
5415

55-
entity_type = args.entity_type
56-
name = args.name
57-
created_after = parse(args.createdAfter).strftime("%Y-%m-%dT%H:%M:%SZ")
58-
created_before = parse(args.createdBefore).strftime("%Y-%m-%dT%H:%M:%SZ")
59-
tags = args.tags.split(",")
60-
operation = args.operation
16+
def main():
17+
# Parameters
18+
parser = argparse.ArgumentParser(description="Mandatory arguments.")
19+
parser.add_argument(
20+
"--entity-type",
21+
dest="entity_type",
22+
default="Intrusion-Set",
23+
required=True,
24+
help="Type of the threat (Threat-Actor, Intrusion-Set, Campaign, X-OpenCTI,-Incident, Malware, Tool, Attack-Pattern)",
25+
)
26+
parser.add_argument(
27+
"--name",
28+
dest="name",
29+
required=True,
30+
help="Name of the threat",
31+
)
32+
parser.add_argument(
33+
"--created-after",
34+
dest="createdAfter",
35+
help="Indicator created before (ISO date)",
36+
)
37+
parser.add_argument(
38+
"--created-before",
39+
dest="createdBefore",
40+
help="Indicator created after (ISO date)",
41+
)
42+
parser.add_argument(
43+
"--tags",
44+
dest="tags",
45+
required=True,
46+
help="Tags to add or remove (separated by ,)",
47+
)
48+
parser.add_argument(
49+
"--operation",
50+
dest="operation",
51+
required=True,
52+
default="add",
53+
help="Operation (add/remove)",
54+
)
55+
args = parser.parse_args()
6156

62-
# Resolve the entity
63-
threat = opencti_api_client.stix_domain_object.read(
64-
types=[entity_type], filters=[{"key": "name", "values": [name]}]
65-
)
57+
entity_type = args.entity_type
58+
name = args.name
59+
created_after = parse(args.createdAfter).strftime("%Y-%m-%dT%H:%M:%SZ")
60+
created_before = parse(args.createdBefore).strftime("%Y-%m-%dT%H:%M:%SZ")
61+
tags = args.tags.split(",")
62+
operation = args.operation
6663

67-
if not threat:
68-
raise ValueError("Cannot find the entity with the name " + name)
64+
# Resolve the entity
65+
threat = opencti_api_client.stix_domain_object.read(
66+
types=[entity_type], filters=[{"key": "name", "values": [name]}]
67+
)
6968

70-
# Resolve all tags
71-
labels = []
72-
for tag in tags:
73-
labels.append(opencti_api_client.label.create(value=tag))
69+
if not threat:
70+
raise ValueError("Cannot find the entity with the name " + name)
7471

75-
# Get indicators
76-
custom_attributes = """
77-
id
78-
created_at
79-
"""
72+
# Resolve all tags
73+
labels = []
74+
for tag in tags:
75+
labels.append(opencti_api_client.label.create(value=tag))
8076

77+
# Get indicators
78+
custom_attributes = """
79+
id
80+
created_at
81+
"""
8182

82-
data = {"pagination": {"hasNextPage": True, "endCursor": None}}
83-
while data["pagination"]["hasNextPage"]:
84-
after = data["pagination"]["endCursor"]
85-
data = opencti_api_client.indicator.list(
86-
first=50,
87-
after=after,
88-
customAttributes=custom_attributes,
89-
filters=[
90-
{"key": "indicates", "values": [threat["id"]]},
91-
{"key": "created_at", "values": [created_after], "operator": "gt"},
92-
{"key": "created_at", "values": [created_before], "operator": "lt"},
93-
],
94-
orderBy="created_at",
95-
orderMode="asc",
96-
withPagination=True,
97-
)
98-
for indicator in data["entities"]:
99-
print("[" + indicator["created_at"] + "] " + indicator["id"])
100-
if operation == "add":
101-
for label in labels:
102-
opencti_api_client.stix_domain_object.add_label(
103-
id=indicator["id"], label_id=label["id"]
104-
)
105-
elif operation == "remove":
106-
for label in labels:
107-
opencti_api_client.stix_domain_object.remove_label(
108-
id=indicator["id"], label_id=label["id"]
109-
)
83+
data = {"pagination": {"hasNextPage": True, "endCursor": None}}
84+
while data["pagination"]["hasNextPage"]:
85+
after = data["pagination"]["endCursor"]
86+
data = opencti_api_client.indicator.list(
87+
first=50,
88+
after=after,
89+
customAttributes=custom_attributes,
90+
filters=[
91+
{"key": "indicates", "values": [threat["id"]]},
92+
{"key": "created_at", "values": [created_after], "operator": "gt"},
93+
{"key": "created_at", "values": [created_before], "operator": "lt"},
94+
],
95+
orderBy="created_at",
96+
orderMode="asc",
97+
withPagination=True,
98+
)
99+
for indicator in data["entities"]:
100+
print("[" + indicator["created_at"] + "] " + indicator["id"])
101+
if operation == "add":
102+
for label in labels:
103+
opencti_api_client.stix_domain_object.add_label(
104+
id=indicator["id"], label_id=label["id"]
105+
)
106+
elif operation == "remove":
107+
for label in labels:
108+
opencti_api_client.stix_domain_object.remove_label(
109+
id=indicator["id"], label_id=label["id"]
110+
)
111+
112+
113+
if __name__ == "__main__":
114+
main()

examples/create_campaign_attributed-to_intrusion_set.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from pycti import OpenCTIApiClient
66

77
# Variables
8-
api_url = "https://demo.opencti.io"
9-
api_token = "YOUR_TOKEN"
8+
api_url = "http://opencti:4000"
9+
api_token = "bfa014e0-e02e-4aa6-a42b-603b19dcf159"
1010

1111
# OpenCTI initialization
1212
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/create_file_with_hashes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from pycti import OpenCTIApiClient
44

55
# Variables
6-
api_url = "https://demo.opencti.io"
7-
api_token = "98481988-5aac-42e3-9be1-e1328ef86419"
6+
api_url = "http://opencti:4000"
7+
api_token = "bfa014e0-e02e-4aa6-a42b-603b19dcf159"
88

99
# OpenCTI initialization
1010
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/create_incident_with_ttps_and_indicators.py

+34-10
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from pycti import OpenCTIApiClient
66

77
# Variables
8-
api_url = "https://demo.opencti.io"
9-
api_token = "YOUR_TOKEN"
8+
api_url = "http://opencti:4000"
9+
api_token = "bfa014e0-e02e-4aa6-a42b-603b19dcf159"
1010

1111
# OpenCTI initialization
1212
opencti_api_client = OpenCTIApiClient(api_url, api_token)
@@ -35,10 +35,17 @@
3535

3636
# Associate the TTPs to the incident
3737

38-
# Spearphishing Attachment
39-
ttp1 = opencti_api_client.attack_pattern.read(
40-
filters=[{"key": "x_mitre_id", "values": ["T1193"]}]
38+
kcp_ia = opencti_api_client.kill_chain_phase.create(
39+
phase_name="initial-access", kill_chain_name="mitre-attack"
4140
)
41+
42+
ttp1 = opencti_api_client.attack_pattern.create(
43+
name="Phishing: Spearphishing Attachment",
44+
description="Adversaries may send spearphishing emails with a malicious attachment in an attempt to gain access to victim systems. Spearphishing attachment is a specific variant of spearphishing. Spearphishing attachment is different from other forms of spearphishing in that it employs the use of malware attached to an email. All forms of spearphishing are electronically delivered social engineering targeted at a specific individual, company, or industry. In this scenario, adversaries attach a file to the spearphishing email and usually rely upon User Execution to gain execution. Spearphishing may also involve social engineering techniques, such as posing as a trusted source.",
45+
x_mitre_id="T1566.001",
46+
killChainPhases=[kcp_ia["id"]],
47+
)
48+
ttp1 = opencti_api_client.attack_pattern.read(id=ttp1["id"])
4249
ttp1_relation = opencti_api_client.stix_core_relationship.create(
4350
fromId=incident["id"],
4451
toId=ttp1["id"],
@@ -53,7 +60,6 @@
5360
id=ttp1_relation["id"], kill_chain_phase_id=kill_chain_phase_id
5461
)
5562

56-
5763
# Create the observable and indicator and indicates to the relation
5864
# Create the observable
5965
observable_ttp1 = opencti_api_client.stix_cyber_observable.create(
@@ -84,10 +90,21 @@
8490
)
8591
observable_refs.append(observable_ttp1["id"])
8692

93+
kcp_p = opencti_api_client.kill_chain_phase.create(
94+
phase_name="persistence", kill_chain_name="mitre-attack"
95+
)
96+
kcp_pe = opencti_api_client.kill_chain_phase.create(
97+
phase_name="privilege-escalation", kill_chain_name="mitre-attack"
98+
)
99+
87100
# Registry Run Keys / Startup Folder
88-
ttp2 = opencti_api_client.attack_pattern.read(
89-
filters=[{"key": "x_mitre_id", "values": ["T1060"]}]
101+
ttp2 = opencti_api_client.attack_pattern.create(
102+
name="Boot or Logon Autostart Execution: Registry Run Keys / Startup Folder ",
103+
description="Adversaries may achieve persistence by adding a program to a startup folder or referencing it with a Registry run key. Adding an entry to the 'run keys' in the Registry or startup folder will cause the program referenced to be executed when a user logs in. These programs will be executed under the context of the user and will have the account's associated permissions level.",
104+
x_mitre_id="T1547.001",
105+
killChainPhases=[kcp_pe["id"], kcp_p["id"]],
90106
)
107+
ttp2 = opencti_api_client.attack_pattern.read(id=ttp2["id"])
91108
# Create the relation
92109
ttp2_relation = opencti_api_client.stix_core_relationship.create(
93110
fromId=incident["id"],
@@ -132,10 +149,17 @@
132149
)
133150
observable_refs.append(observable_ttp2["id"])
134151

152+
kcp_c = opencti_api_client.kill_chain_phase.create(
153+
phase_name="collection", kill_chain_name="mitre-attack"
154+
)
135155
# Data Encrypted
136-
ttp3 = opencti_api_client.attack_pattern.read(
137-
filters=[{"key": "x_mitre_id", "values": ["T1022"]}]
156+
ttp3 = opencti_api_client.attack_pattern.create(
157+
name=" Archive Collected Data",
158+
description="An adversary may compress and/or encrypt data that is collected prior to exfiltration. Compressing the data can help to obfuscate the collected data and minimize the amount of data sent over the network. Encryption can be used to hide information that is being exfiltrated from detection or make exfiltration less conspicuous upon inspection by a defender.",
159+
x_mitre_id="T1560",
160+
killChainPhases=[kcp_c["id"]],
138161
)
162+
ttp3 = opencti_api_client.attack_pattern.read(id=ttp3["id"])
139163
ttp3_relation = opencti_api_client.stix_core_relationship.create(
140164
fromId=incident["id"],
141165
toId=ttp3["id"],

0 commit comments

Comments
 (0)