|
6 | 6 | from pycti import OpenCTIApiClient |
7 | 7 |
|
8 | 8 | # Variables |
9 | | -api_url = 'https://demo.opencti.io' |
10 | | -api_token = 'ab3c02bb-2849-4223-be5d-8f61207b4b43' |
| 9 | +api_url = 'http://localhost:4000' |
| 10 | +api_token = 'c2d944bb-aea6-4bd6-b3d7-6c10451e2256' |
11 | 11 |
|
12 | 12 | # OpenCTI initialization |
13 | 13 | opencti_api_client = OpenCTIApiClient(api_url, api_token) |
14 | 14 |
|
15 | 15 | # Define the date |
16 | 16 | date = parse('2019-12-01').strftime('%Y-%m-%dT%H:%M:%SZ') |
17 | 17 |
|
| 18 | +# Prepare all the elements of the report |
| 19 | +object_refs = [] |
| 20 | + |
18 | 21 | # Create the incident |
19 | 22 | incident = opencti_api_client.incident.create( |
20 | 23 | name="My new incident", |
21 | 24 | description="We have been compromised", |
22 | 25 | objective="Espionage" |
23 | 26 | ) |
24 | 27 | print(incident) |
| 28 | +object_refs.append(incident['id']) |
25 | 29 |
|
26 | 30 | # Create the associated report |
27 | 31 | report = opencti_api_client.report.create( |
|
32 | 36 | ) |
33 | 37 | print(report) |
34 | 38 |
|
35 | | -# Prepare all the elements of the report |
36 | | -object_refs = [] |
37 | | - |
38 | 39 | # Associate the TTPs to the incident |
39 | 40 |
|
40 | 41 | # Spearphishing Attachment |
|
43 | 44 | ttp1_relation = opencti_api_client.stix_relation.create( |
44 | 45 | fromType='Incident', |
45 | 46 | fromId=incident['id'], |
46 | | - toType='Incident', |
| 47 | + toType='Attack-Pattern', |
47 | 48 | toId=ttp1['id'], |
48 | 49 | relationship_type='uses', |
49 | 50 | description='We saw the attacker use Spearphishing Attachment.', |
|
56 | 57 | id=ttp1_relation['id'], |
57 | 58 | kill_chain_phase_id=kill_chain_phase_id |
58 | 59 | ) |
59 | | -# Add observables to the relation |
| 60 | + |
| 61 | +# Create the observable |
60 | 62 | observable_ttp1 = opencti_api_client.stix_observable.create( |
61 | | - type='Email-Addr', |
| 63 | + type='Email-Address', |
62 | 64 | observable_value='[email protected]' |
63 | 65 | ) |
| 66 | +# Indicates the incident itself |
| 67 | +observable_ttp1_incident_relation = opencti_api_client.stix_relation.create( |
| 68 | + fromType='Stix-Observable', |
| 69 | + fromId=observable_ttp1['id'], |
| 70 | + toType='Incident', |
| 71 | + toId=incident['id'], |
| 72 | + relationship_type='indicates', |
| 73 | + description='This email address is the sender of the spearphishing in this incident.', |
| 74 | + first_seen=date, |
| 75 | + last_seen=date |
| 76 | +) |
| 77 | +# Indicates the relation Incident => uses => TTP |
64 | 78 | observable_ttp1_relation = opencti_api_client.stix_relation.create( |
65 | 79 | fromType='Stix-Observable', |
66 | 80 | fromId=observable_ttp1['id'], |
|
72 | 86 | last_seen=date |
73 | 87 | ) |
74 | 88 | # Elements for the report |
75 | | -object_refs.extend([ttp1['id'], ttp1_relation['id'], observable_ttp1['id'], observable_ttp1_relation['id']]) |
| 89 | +object_refs.extend([ |
| 90 | + ttp1['id'], |
| 91 | + ttp1_relation['id'], |
| 92 | + observable_ttp1['id'], |
| 93 | + observable_ttp1_incident_relation['id'] |
| 94 | +]) |
76 | 95 |
|
77 | 96 | # Registry Run Keys / Startup Folder |
78 | 97 | ttp2 = opencti_api_client.attack_pattern.read(filters=[{'key': 'external_id', 'values': ['T1060']}]) |
|
81 | 100 | ttp2_relation = opencti_api_client.stix_relation.create( |
82 | 101 | fromType='Incident', |
83 | 102 | fromId=incident['id'], |
84 | | - toType='Incident', |
| 103 | + toType='Attack-Pattern', |
85 | 104 | toId=ttp2['id'], |
86 | 105 | relationship_type='uses', |
87 | 106 | description='We saw the attacker use Registry Run Keys / Startup Folder.', |
|
99 | 118 | type='Registry-Key', |
100 | 119 | observable_value='Disk security' |
101 | 120 | ) |
| 121 | +# Indicates the incident itself |
| 122 | +observable_ttp2_incident_relation = opencti_api_client.stix_relation.create( |
| 123 | + fromType='Stix-Observable', |
| 124 | + fromId=observable_ttp2['id'], |
| 125 | + toType='Incident', |
| 126 | + toId=incident['id'], |
| 127 | + relationship_type='indicates', |
| 128 | + description='This registry key is used for persistence of tools in this incident.', |
| 129 | + first_seen=date, |
| 130 | + last_seen=date |
| 131 | +) |
| 132 | +# Indicates the relation Incident => uses => TTP |
102 | 133 | observable_ttp2_relation = opencti_api_client.stix_relation.create( |
103 | 134 | fromType='Stix-Observable', |
104 | 135 | fromId=observable_ttp2['id'], |
|
110 | 141 | last_seen=date |
111 | 142 | ) |
112 | 143 | # Elements for the report |
113 | | -object_refs.extend([ttp2['id'], ttp2_relation['id'], observable_ttp2['id'], observable_ttp2_relation['id']]) |
| 144 | +object_refs.extend([ |
| 145 | + ttp2['id'], |
| 146 | + ttp2_relation['id'], |
| 147 | + observable_ttp2['id'], |
| 148 | + observable_ttp2_incident_relation['id'] |
| 149 | +]) |
114 | 150 |
|
115 | 151 | # Data Encrypted |
116 | 152 | ttp3 = opencti_api_client.attack_pattern.read(filters=[{'key': 'external_id', 'values': ['T1022']}]) |
117 | 153 | print(ttp3) |
118 | 154 | ttp3_relation = opencti_api_client.stix_relation.create( |
119 | 155 | fromType='Incident', |
120 | 156 | fromId=incident['id'], |
121 | | - toType='Incident', |
| 157 | + toType='Attack-Pattern', |
122 | 158 | toId=ttp3['id'], |
123 | 159 | relationship_type='uses', |
124 | 160 | description='We saw the attacker use Data Encrypted.', |
|
0 commit comments