Skip to content

Commit 3090e2a

Browse files
author
Samuel Hassine
authored
Refactor the client (OpenCTI-Platform#14)
* [client] Start refactoring and adapt to new version * [client] Migrate the Python Client * [client] Add more examples * [client] End of refactor for new version * [client] End of refactor for new version
1 parent cdc5e19 commit 3090e2a

36 files changed

+4780
-3418
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ pycti.egg-info
77
logs
88
test.py
99
.idea
10-
*.iml
10+
*.iml
11+
examples/*.json

examples/config.yml.sample

-3
This file was deleted.

examples/create_intrusion_set.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# coding: utf-8
2+
3+
import datetime
4+
from pycti import OpenCTIApiClient
5+
6+
# Variables
7+
api_url = 'https://demo.opencti.io'
8+
api_token = '22566f94-9091-49ba-b583-efd76cf8b29c'
9+
10+
# OpenCTI initialization
11+
opencti_api_client = OpenCTIApiClient(api_url, api_token)
12+
13+
# Create the Intrusion Set
14+
intrusion_set = opencti_api_client.intrusion_set.create_or_update(
15+
name='My new Intrusion Set',
16+
description='Evil Cluster',
17+
first_seen=datetime.date.today().strftime('%Y-%m-%dT%H:%M:%S+00:00'),
18+
last_seen=datetime.date.today().strftime('%Y-%m-%dT%H:%M:%S+00:00'),
19+
update=True
20+
)
21+
22+
# Print
23+
print(intrusion_set)
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# coding: utf-8
2+
3+
import json
4+
from pycti import OpenCTIApiClient
5+
6+
# Variables
7+
api_url = 'https://demo.opencti.io'
8+
api_token = '22566f94-9091-49ba-b583-efd76cf8b29c'
9+
10+
# OpenCTI initialization
11+
opencti_api_client = OpenCTIApiClient(api_url, api_token)
12+
13+
# Get the intrusion set APT28
14+
intrusion_set = opencti_api_client.intrusion_set.read(filters=[{'key': 'name', 'values': ['APT28']}])
15+
16+
# Create the bundle
17+
bundle = opencti_api_client.stix2.export_entity('intrusion-set', intrusion_set['id'], 'full')
18+
json_bundle = json.dumps(bundle, indent=4)
19+
20+
# Write the bundle
21+
f = open('APT28_STIX2.json', 'w')
22+
f.write(json_bundle)
23+
f.close()

examples/export_report_stix2.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# coding: utf-8
2+
3+
import json
4+
from pycti import OpenCTIApiClient
5+
6+
# Variables
7+
api_url = 'https://demo.opencti.io'
8+
api_token = '22566f94-9091-49ba-b583-efd76cf8b29c'
9+
10+
# OpenCTI initialization
11+
opencti_api_client = OpenCTIApiClient(api_url, api_token)
12+
13+
# Get the report
14+
report = opencti_api_client.report.read(id='b52201d6-8da3-4e98-a3f5-e53318d8fb52')
15+
16+
# Create the bundle
17+
bundle = opencti_api_client.stix2.export_entity('report', report['id'], 'full')
18+
json_bundle = json.dumps(bundle, indent=4)
19+
20+
# Write the bundle
21+
f = open('Unit42_Sofacy.json', 'w')
22+
f.write(json_bundle)
23+
f.close()
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# coding: utf-8
2+
3+
from pycti import OpenCTIApiClient
4+
5+
# Variables
6+
api_url = 'https://demo.opencti.io'
7+
api_token = '22566f94-9091-49ba-b583-efd76cf8b29c'
8+
9+
# OpenCTI initialization
10+
opencti_api_client = OpenCTIApiClient(api_url, api_token)
11+
12+
# Get the intrusion set APT28
13+
intrusion_set = opencti_api_client.intrusion_set.read(filters=[{'key': 'name', 'values': ['APT28']}])
14+
15+
# Get the relations from APT28 to malwares
16+
stix_relations = opencti_api_client.stix_relation.list(fromId=intrusion_set['id'], toTypes=['Malware'])
17+
18+
# Print
19+
for stix_relation in stix_relations:
20+
print('[' + stix_relation['to']['stix_id_key'] + '] ' + stix_relation['to']['name'])

examples/get_marking_definitions.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# coding: utf-8
2+
3+
from pycti import OpenCTIApiClient, MarkingDefinition
4+
5+
# Variables
6+
api_url = 'https://demo.opencti.io'
7+
api_token = '22566f94-9091-49ba-b583-efd76cf8b29c'
8+
9+
# OpenCTI initialization
10+
opencti_api_client = OpenCTIApiClient(api_url, api_token)
11+
12+
# Get all marking definitions
13+
marking_definitions = opencti_api_client.marking_definition.list()
14+
15+
# Print
16+
for marking_definition in marking_definitions:
17+
print('[' + marking_definition['definition_type'] + '] ' + marking_definition['definition'])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# coding: utf-8
2+
3+
from pycti import OpenCTIApiClient
4+
5+
# Variables
6+
api_url = 'https://demo.opencti.io'
7+
api_token = '22566f94-9091-49ba-b583-efd76cf8b29c'
8+
9+
# OpenCTI initialization
10+
opencti_api_client = OpenCTIApiClient(api_url, api_token)
11+
12+
# Get the intrusion set APT28
13+
intrusion_set = opencti_api_client.intrusion_set.read(filters=[{'key': 'name', 'values': ['APT28']}])
14+
15+
# Get all reports
16+
reports = opencti_api_client.report.list(
17+
filters=[{'key': 'knowledgeContains', 'values': [intrusion_set['id']]}],
18+
orderBy='published',
19+
orderMode='asc'
20+
)
21+
22+
# Print
23+
for report in reports:
24+
print('[' + report['stix_id_key'] + '] ' + report['name'] + ' (' + report['published'] + ')')

examples/import_stix2_file.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# coding: utf-8
2+
3+
from pycti import OpenCTIApiClient
4+
5+
# Variables
6+
api_url = 'https://demo.opencti.io'
7+
api_token = '22566f94-9091-49ba-b583-efd76cf8b29c'
8+
9+
# OpenCTI initialization
10+
opencti_api_client = OpenCTIApiClient(api_url, api_token)
11+
12+
# File to import
13+
file_to_import = './enterprise-attack.json'
14+
15+
# Import the bundle
16+
opencti_api_client.stix2.import_bundle_from_file(file_to_import, True)

examples/observables/get_observables.py

-22
This file was deleted.

examples/stix2/export.py

-22
This file was deleted.

examples/stix2/import.py

-18
This file was deleted.

pycti/__init__.py

+19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@
66
from pycti.connector.opencti_connector import OpenCTIConnector
77
from pycti.connector.opencti_connector_helper import OpenCTIConnectorHelper
88

9+
from pycti.entities.opencti_marking_definition import MarkingDefinition
10+
from pycti.entities.opencti_external_reference import ExternalReference
11+
from pycti.entities.opencti_kill_chain_phase import KillChainPhase
12+
from pycti.entities.opencti_stix_entity import StixEntity
13+
from pycti.entities.opencti_stix_domain_entity import StixDomainEntity
14+
from pycti.entities.opencti_stix_observable import StixObservable
15+
from pycti.entities.opencti_stix_relation import StixRelation
16+
from pycti.entities.opencti_identity import Identity
17+
from pycti.entities.opencti_threat_actor import ThreatActor
18+
from pycti.entities.opencti_intrusion_set import IntrusionSet
19+
from pycti.entities.opencti_campaign import Campaign
20+
from pycti.entities.opencti_incident import Incident
21+
from pycti.entities.opencti_malware import Malware
22+
from pycti.entities.opencti_tool import Tool
23+
from pycti.entities.opencti_vulnerability import Vulnerability
24+
from pycti.entities.opencti_attack_pattern import AttackPattern
25+
from pycti.entities.opencti_course_of_action import CourseOfAction
26+
from pycti.entities.opencti_report import Report
27+
928
from pycti.utils.opencti_stix2 import OpenCTIStix2
1029
from pycti.utils.constants import ObservableTypes
1130
from pycti.utils.constants import CustomProperties

0 commit comments

Comments
 (0)