Skip to content

Commit 96b8189

Browse files
author
Samuel Hassine
committed
[client] Add more examples
1 parent 6b22b71 commit 96b8189

13 files changed

+100
-32
lines changed

examples/create_incident_with_ttps_and_indicators.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
description="We have been compromised",
2626
objective="Espionage"
2727
)
28-
print(incident)
2928
object_refs.append(incident['id'])
3029
# Create the associated report
3130
report = opencti_api_client.report.create(
@@ -34,13 +33,11 @@
3433
published=date,
3534
report_class="Internal Report"
3635
)
37-
print(report)
3836

3937
# Associate the TTPs to the incident
4038

4139
# Spearphishing Attachment
4240
ttp1 = opencti_api_client.attack_pattern.read(filters=[{'key': 'external_id', 'values': ['T1193']}])
43-
print(ttp1)
4441
ttp1_relation = opencti_api_client.stix_relation.create(
4542
fromType='Incident',
4643
fromId=incident['id'],
@@ -64,10 +61,8 @@
6461
type='Email-Address',
6562
observable_value='phishing@mail.com'
6663
)
67-
print(observable_ttp1)
6864
# Get the indicator
6965
indicator_ttp1 = observable_ttp1['indicators'][0]
70-
print(indicator_ttp1)
7166
# Indicates the relation Incident => uses => TTP
7267
indicator_ttp1_relation = opencti_api_client.stix_relation.create(
7368
fromType='Indicator',
@@ -91,7 +86,6 @@
9186

9287
# Registry Run Keys / Startup Folder
9388
ttp2 = opencti_api_client.attack_pattern.read(filters=[{'key': 'external_id', 'values': ['T1060']}])
94-
print(ttp2)
9589
# Create the relation
9690
ttp2_relation = opencti_api_client.stix_relation.create(
9791
fromType='Incident',
@@ -116,10 +110,8 @@
116110
type='Registry-Key',
117111
observable_value='Disk security'
118112
)
119-
print(observable_ttp2)
120113
# Get the indicator
121114
indicator_ttp2 = observable_ttp2['indicators'][0]
122-
print(indicator_ttp2)
123115
# Indicates the relation Incident => uses => TTP
124116
indicator_ttp2_relation = opencti_api_client.stix_relation.create(
125117
fromType='Indicator',
@@ -142,7 +134,6 @@
142134

143135
# Data Encrypted
144136
ttp3 = opencti_api_client.attack_pattern.read(filters=[{'key': 'external_id', 'values': ['T1022']}])
145-
print(ttp3)
146137
ttp3_relation = opencti_api_client.stix_relation.create(
147138
fromType='Incident',
148139
fromId=incident['id'],
@@ -166,4 +157,14 @@
166157
for object_ref in object_refs:
167158
opencti_api_client.report.add_stix_entity(id=report['id'], report=report, entity_id=object_ref)
168159
for observable_ref in observable_refs:
169-
opencti_api_client.report.add_stix_observable(id=report['id'], report=report, stix_observable_id=observable_ref)
160+
opencti_api_client.report.add_stix_observable(id=report['id'], report=report, stix_observable_id=observable_ref)
161+
opencti_api_client.stix_relation.create(
162+
fromType='Stix-Observable',
163+
fromId=observable_ref,
164+
toType='Incident',
165+
toId=incident['id'],
166+
relationship_type='related-to',
167+
description='This observable is related to the incident.',
168+
first_seen=date,
169+
last_seen=date
170+
)

examples/export_async_of_indicators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pycti import OpenCTIApiClient
55

66
# Variables
7-
api_url = 'http://localhost:4000'
7+
api_url = 'https://demo.opencti.io'
88
api_token = 'bb4aca90-b98c-49ee-9582-7eac92b61b82'
99

1010
# OpenCTI initialization

examples/export_async_of_malware.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pycti import OpenCTIApiClient
55

66
# Variables
7-
api_url = 'http://localhost:4000'
7+
api_url = 'https://demo.opencti.io'
88
api_token = 'bb4aca90-b98c-49ee-9582-7eac92b61b82'
99

1010
# OpenCTI initialization

examples/export_incident_stix2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pycti import OpenCTIApiClient
55

66
# Variables
7-
api_url = 'http://localhost:4000'
7+
api_url = 'https://demo.opencti.io'
88
api_token = 'bb4aca90-b98c-49ee-9582-7eac92b61b82'
99

1010
# OpenCTI initialization

examples/export_incidents_stix2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pycti import OpenCTIApiClient
55

66
# Variables
7-
api_url = 'http://localhost:4000'
7+
api_url = 'https://demo.opencti.io'
88
api_token = 'bb4aca90-b98c-49ee-9582-7eac92b61b82'
99

1010
# OpenCTI initialization

examples/get_all_indicators.py

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

55
# Variables
6-
api_url = 'http://localhost:4000'
6+
api_url = 'https://demo.opencti.io'
77
api_token = 'bb4aca90-b98c-49ee-9582-7eac92b61b82'
88

99
# OpenCTI initialization
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# coding: utf-8
2+
3+
from pycti import OpenCTIApiClient
4+
5+
# Variables
6+
api_url = 'https://demo.opencti.io'
7+
api_token = 'bb4aca90-b98c-49ee-9582-7eac92b61b82'
8+
9+
# OpenCTI initialization
10+
opencti_api_client = OpenCTIApiClient(api_url, api_token)

examples/get_malwares_of_intrusion_set.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
intrusion_set = opencti_api_client.intrusion_set.read(filters=[{'key': 'name', 'values': ['APT28']}])
1414

1515
# Get the relations from APT28 to malwares
16-
stix_relations = opencti_api_client.stix_relation.list(fromId=intrusion_set['id'], toTypes=['Malware'])
16+
stix_relations = opencti_api_client.stix_relation.list(fromId=intrusion_set['id'], toTypes=['Malware'], inferred=True)
1717

1818
# Print
1919
for stix_relation in stix_relations:
20-
print('[' + stix_relation['to']['stix_id_key'] + '] ' + stix_relation['to']['name'])
20+
print('[' + stix_relation['to']['stix_id_key'] + '] ' + stix_relation['to']['name'])

pycti/entities/opencti_incident.py

+51-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def __init__(self, opencti):
8989
id
9090
}
9191
}
92-
}
92+
}
9393
observableRefs {
9494
edges {
9595
node {
@@ -102,7 +102,7 @@ def __init__(self, opencti):
102102
id
103103
}
104104
}
105-
}
105+
}
106106
"""
107107

108108
"""
@@ -327,6 +327,55 @@ def create(self, **kwargs):
327327
markingDefinitions=marking_definitions,
328328
)
329329

330+
"""
331+
Add a Stix-Observable object to Incident object (observable_refs)
332+
333+
:param id: the id of the Incident
334+
:param entity_id: the id of the Stix-Observable
335+
:return Boolean
336+
"""
337+
338+
def add_stix_observable(self, **kwargs):
339+
id = kwargs.get('id', None)
340+
incident = kwargs.get('incident', None)
341+
stix_observable_id = kwargs.get('stix_observable_id', None)
342+
if id is not None and stix_observable_id is not None:
343+
if incident is None:
344+
incident = self.read(id=id)
345+
if incident is None:
346+
self.opencti.log('error', '[opencti_incident] Cannot add Object Ref, incident not found')
347+
return False
348+
print(incident)
349+
if stix_observable_id in incident['observableRefsIds']:
350+
return True
351+
else:
352+
self.opencti.log(
353+
'info',
354+
'Adding Stix-Observable {' + stix_observable_id + '} to Incident {' + id + '}'
355+
)
356+
query = """
357+
mutation IncidentEdit($id: ID!, $input: RelationAddInput) {
358+
incidentEdit(id: $id) {
359+
relationAdd(input: $input) {
360+
id
361+
}
362+
}
363+
}
364+
"""
365+
self.opencti.query(query, {
366+
'id': id,
367+
'input': {
368+
'fromRole': 'observables_aggregation',
369+
'toId': stix_observable_id,
370+
'toRole': 'soo',
371+
'through': 'observable_refs'
372+
}
373+
})
374+
return True
375+
else:
376+
self.opencti.log('error', '[opencti_incident] Missing parameters: id and stix_observable_id')
377+
return False
378+
330379
"""
331380
Export an Incident object in STIX2
332381

pycti/entities/opencti_stix_domain_entity.py

+13
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ def __init__(self, opencti, file):
118118
objective
119119
first_seen
120120
last_seen
121+
observableRefs {
122+
edges {
123+
node {
124+
id
125+
entity_type
126+
stix_id_key
127+
observable_value
128+
}
129+
relation {
130+
id
131+
}
132+
}
133+
}
121134
}
122135
... on Malware {
123136
killChainPhases {

pycti/entities/opencti_stix_relation.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -460,22 +460,20 @@ def add_kill_chain_phase(self, **kwargs):
460460
id = kwargs.get('id', None)
461461
kill_chain_phase_id = kwargs.get('kill_chain_phase_id', None)
462462
if id is not None and kill_chain_phase_id is not None:
463-
self.opencti.log('info',
464-
'Adding Kill-Chain-Phase {' + kill_chain_phase_id + '} to Stix-Entity {' + id + '}')
465463
stix_entity = self.read(id=id)
466464
kill_chain_phases_ids = []
467465
for marking in stix_entity['killChainPhases']:
468466
kill_chain_phases_ids.append(marking['id'])
469467
if kill_chain_phase_id in kill_chain_phases_ids:
470468
return True
471469
else:
470+
self.opencti.log('info',
471+
'Adding Kill-Chain-Phase {' + kill_chain_phase_id + '} to Stix-Entity {' + id + '}')
472472
query = """
473473
mutation StixRelationAddRelation($id: ID!, $input: RelationAddInput) {
474474
stixRelationEdit(id: $id) {
475475
relationAdd(input: $input) {
476-
node {
477-
id
478-
}
476+
id
479477
}
480478
}
481479
}

pycti/utils/opencti_stix2.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@
1414
import stix2
1515
from stix2.pattern_visitor import create_pattern_object
1616
from stix2 import ObjectPath, ObservationExpression, EqualityComparisonExpression, HashConstant
17-
from pycti.utils.constants import ObservableTypes, CustomProperties
17+
from pycti.utils.constants import ObservableTypes, IdentityTypes, CustomProperties
1818

1919
datefinder.ValueError = ValueError, OverflowError
2020
utc = pytz.UTC
2121

22-
# Identity
23-
IDENTITY_TYPES = ['user', 'city', 'country', 'region', 'organization', 'sector']
24-
2522
# ObservableRelations
2623
OBSERVABLE_RELATIONS = ['corresponds', 'belongs']
2724

@@ -804,7 +801,7 @@ def export_entity(self, entity_type, entity_id, mode='simple', max_marking_defin
804801
'objects': []
805802
}
806803
# Map types
807-
if entity_type in IDENTITY_TYPES:
804+
if IdentityTypes.has_value(entity_type):
808805
entity_type = 'identity'
809806

810807
# Export
@@ -852,7 +849,7 @@ def export_list(self,
852849
'objects': []
853850
}
854851

855-
if entity_type in IDENTITY_TYPES:
852+
if IdentityTypes.has_value(entity_type):
856853
if filters is not None:
857854
filters.append({'key': 'entity_type', 'values': [entity_type]})
858855
else:
@@ -1077,7 +1074,7 @@ def prepare_export(self, entity, stix_object, mode='simple', max_marking_definit
10771074
# Get extra objects
10781075
for entity_object in objects_to_get:
10791076
# Map types
1080-
if entity_object['entity_type'] in IDENTITY_TYPES:
1077+
if IdentityTypes.has_value(entity_object['entity_type']):
10811078
entity_object['entity_type'] = 'identity'
10821079
do_export = exporter.get(entity_object['entity_type'],
10831080
lambda **kwargs: self.unknown_type({'type': entity_object['entity_type']}))

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
print("warning: pypandoc module not found, could not convert Markdown to RST")
1414
read_md = lambda f: open(f, 'r').read()
1515

16-
VERSION = "2.1.10"
16+
VERSION = "2.1.11"
1717

1818

1919
class VerifyVersionCommand(install):

0 commit comments

Comments
 (0)