-
Notifications
You must be signed in to change notification settings - Fork 6
/
example_test.go
320 lines (292 loc) · 9.18 KB
/
example_test.go
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright 2020 Joakim Kennedy. All rights reserved. Use of
// this source code is governed by the included BSD license.
package stix2_test
import (
"bytes"
"encoding/json"
"fmt"
"github.com/TcM1911/stix2"
)
func ExampleFromJSON() {
data := []byte(
`
[
{
"type":"infrastructure",
"spec_version":"2.1",
"id":"infrastructure--d09c50cf-5bab-465e-9e2d-543912148b73",
"created":"2016-11-22T09:22:30.000Z",
"modified":"2016-11-22T09:22:30.000Z",
"name":"Example Target List Host",
"infrastructure_types":[
"hosting-target-lists"
]
},
{
"type":"relationship",
"spec_version":"2.1",
"id":"relationship--37ac0c8d-f86d-4e56-aee9-914343959a4c",
"created":"2016-11-23T08:17:27.000Z",
"modified":"2016-11-23T08:17:27.000Z",
"relationship_type":"uses",
"source_ref":"malware--3a41e552-999b-4ad3-bedc-332b6d9ff80c",
"target_ref":"infrastructure--d09c50cf-5bab-465e-9e2d-543912148b73"
},
{
"type":"malware",
"spec_version":"2.1",
"id":"malware--3a41e552-999b-4ad3-bedc-332b6d9ff80c",
"created":"2016-11-12T14:31:09.000Z",
"modified":"2016-11-12T14:31:09.000Z",
"is_family":true,
"malware_types":[
"bot"
],
"name":"IMDDOS"
},
{
"type":"relationship",
"spec_version":"2.1",
"id":"relationship--81f12913-1372-4c96-85ec-E9034ac98aba",
"created":"2016-11-23T10:42:39.000Z",
"modified":"2016-11-23T10:42:39.000Z",
"relationship_type":"consists-of",
"source_ref":"infrastructure--d09c50cf-5bab-465e-9e2d-543912148b73",
"target_ref":"domain-name--3c10e93f-798e-5a26-a0c1-08156efab7f5"
},
{
"id":"domain-name--3c10e93f-798e-5a26-a0c1-08156efab7f5",
"type":"domain-name",
"value":"example.com"
}
]
`)
collection, _ := stix2.FromJSON(data)
fmt.Println(collection.DomainName("domain-name--3c10e93f-798e-5a26-a0c1-08156efab7f5").Value)
fmt.Println(collection.Malware("malware--3a41e552-999b-4ad3-bedc-332b6d9ff80c").Name)
// Output:
// example.com
// IMDDOS
}
func ExampleCollection_ToBundle() {
c := stix2.New()
ip, err := stix2.NewIPv4Address("10.0.0.1")
if err != nil {
fmt.Println(err)
}
c.Add(ip)
ip, err = stix2.NewIPv4Address("10.0.0.2")
if err != nil {
fmt.Println(err)
}
c.Add(ip)
b, err := c.ToBundle()
if err != nil {
fmt.Println(err)
}
data, err := json.Marshal(b)
if err != nil {
fmt.Println(err)
}
if !bytes.Contains(data, []byte("10.0.0.2")) {
fmt.Println("IP not in bundle")
}
// Output:
}
func Example() {
// Taken from: https://docs.oasis-open.org/cti/stix/v2.1/csprd02/stix-v2.1-csprd02.html#_Toc26789941
collection := stix2.New()
domain, err := stix2.NewDomainName("example.com")
if err != nil {
fmt.Println(err)
}
collection.Add(domain)
mal, err := stix2.NewMalware(
false,
stix2.OptionName("IMDDOS"),
stix2.OptionTypes([]string{stix2.MalwareTypeBot}),
)
if err != nil {
fmt.Println(err)
}
collection.Add(mal)
infra, err := stix2.NewInfrastructure(
"Example Target List Host",
)
if err != nil {
fmt.Println(err)
}
collection.Add(infra)
ref, err := mal.AddUses(infra.ID)
if err != nil {
fmt.Println(err)
}
collection.Add(ref)
ref, err = infra.AddConsistsOf(domain.ID)
if err != nil {
fmt.Println(err)
}
collection.Add(ref)
b, err := collection.ToBundle()
if err != nil {
fmt.Println(err)
return
}
data, err := json.MarshalIndent(b, "", "\t")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(data))
}
func ExampleCustomObject() {
// Define the custom fields
ext := &stix2.CustomObject{}
ext.Set("some_new_property", "a string value")
// The extension definition.
ed, _ := stix2.NewExtensionDefinition(
"A custom extension",
"https://example.com/v1/schema",
"1.0",
[]stix2.ExtensionType{stix2.ExtensionTypePropertyExtension},
)
// Create a DomainName object with the additional field.
d, _ := stix2.NewDomainName("example.com", stix2.OptionExtension(string(ed.ID), ext))
fmt.Println(d.Extensions[string(ed.ID)].(*stix2.CustomObject).GetAsString("some_new_property"))
//Output:
// a string value
}
func ExampleCustomObject_attack() {
data := []byte(`[{
"id": "attack-pattern--3fc9b85a-2862-4363-a64d-d692e3ffbee0",
"description": "Adversaries may search for common password storage locations to obtain user credentials. Passwords are stored in several places on a system, depending on the operating system or application holding the credentials. There are also specific applications that store passwords to make it easier for users manage and maintain. Once credentials are obtained, they can be used to perform lateral movement and access restricted information.",
"name": "Credentials from Password Stores",
"created_by_ref": "identity--c78cb6e5-0c4b-4611-8297-d1b8b55e40b5",
"object_marking_refs": [
"marking-definition--fa42a846-8d90-4e51-bc29-71d5b4802168"
],
"external_references": [
{
"source_name": "mitre-attack",
"external_id": "T1555",
"url": "https://attack.mitre.org/techniques/T1555"
}
],
"type": "attack-pattern",
"kill_chain_phases": [
{
"kill_chain_name": "mitre-attack",
"phase_name": "credential-access"
}
],
"modified": "2021-04-29T21:00:19.428Z",
"created": "2020-02-11T18:48:28.456Z",
"x_mitre_platforms": [
"Linux",
"macOS",
"Windows"
],
"x_mitre_is_subtechnique": false,
"x_mitre_version": "1.0",
"x_mitre_detection": "Monitor system calls, file read events, and processes for suspicious activity that could indicate searching for a password or other activity related to performing keyword searches (e.g. password, pwd, login, store, secure, credentials, etc.) in process memory for credentials. File read events should be monitored surrounding known password storage applications.",
"x_mitre_permissions_required": [
"Administrator"
],
"x_mitre_data_sources": [
"Process: Process Creation",
"File: File Access",
"Command: Command Execution",
"Process: OS API Execution",
"Process: Process Access"
],
"spec_version": "2.1",
"x_mitre_domains": [
"enterprise-attack"
],
"x_mitre_modified_by_ref": "identity--c78cb6e5-0c4b-4611-8297-d1b8b55e40b5"
}]`)
col, err := stix2.FromJSON(data)
if err != nil {
fmt.Println(err)
}
obj := col.AttackPattern(stix2.Identifier("attack-pattern--3fc9b85a-2862-4363-a64d-d692e3ffbee0"))
fmt.Println(obj.Name)
// Get the custom properties.
ext := obj.GetExtendedTopLevelProperties()
fmt.Println(ext.GetAsString("x_mitre_version"))
//Output:
// Credentials from Password Stores
// 1.0
}
func ExampleCustomObject_customAttackType() {
data := []byte(`[{
"id": "attack-pattern--3fc9b85a-2862-4363-a64d-d692e3ffbee0",
"description": "Adversaries may search for common password storage locations to obtain user credentials. Passwords are stored in several places on a system, depending on the operating system or application holding the credentials. There are also specific applications that store passwords to make it easier for users manage and maintain. Once credentials are obtained, they can be used to perform lateral movement and access restricted information.",
"name": "Credentials from Password Stores",
"created_by_ref": "identity--c78cb6e5-0c4b-4611-8297-d1b8b55e40b5",
"object_marking_refs": [
"marking-definition--fa42a846-8d90-4e51-bc29-71d5b4802168"
],
"external_references": [
{
"source_name": "mitre-attack",
"external_id": "T1555",
"url": "https://attack.mitre.org/techniques/T1555"
}
],
"type": "attack-pattern",
"kill_chain_phases": [
{
"kill_chain_name": "mitre-attack",
"phase_name": "credential-access"
}
],
"modified": "2021-04-29T21:00:19.428Z",
"created": "2020-02-11T18:48:28.456Z",
"x_mitre_platforms": [
"Linux",
"macOS",
"Windows"
],
"x_mitre_is_subtechnique": false,
"x_mitre_version": "1.0",
"x_mitre_detection": "Monitor system calls, file read events, and processes for suspicious activity that could indicate searching for a password or other activity related to performing keyword searches (e.g. password, pwd, login, store, secure, credentials, etc.) in process memory for credentials. File read events should be monitored surrounding known password storage applications.",
"x_mitre_permissions_required": [
"Administrator"
],
"x_mitre_data_sources": [
"Process: Process Creation",
"File: File Access",
"Command: Command Execution",
"Process: OS API Execution",
"Process: Process Access"
],
"spec_version": "2.1",
"x_mitre_domains": [
"enterprise-attack"
],
"x_mitre_modified_by_ref": "identity--c78cb6e5-0c4b-4611-8297-d1b8b55e40b5"
}]`)
type mitreAttackPattern struct {
stix2.AttackPattern
Platforms []string `json:"x_mitre_platforms"`
IsSubTechnique bool `json:"x_mitre_is_subtechnique"`
Version string `json:"x_mitre_version"`
}
col, err := stix2.FromJSON(data, stix2.UseCustomParser(stix2.TypeAttackPattern, func(data []byte) (stix2.STIXObject, error) {
var obj mitreAttackPattern
err := json.Unmarshal(data, &obj)
return &obj, err
}))
if err != nil {
fmt.Println(err)
return
}
obj := col.Get(stix2.Identifier("attack-pattern--3fc9b85a-2862-4363-a64d-d692e3ffbee0")).(*mitreAttackPattern)
fmt.Println(obj.Name)
// Get the custom properties.
fmt.Println(obj.Version)
//Output:
// Credentials from Password Stores
// 1.0
}