-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathparser.go
More file actions
257 lines (222 loc) · 6.9 KB
/
parser.go
File metadata and controls
257 lines (222 loc) · 6.9 KB
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
package mitre
import (
"encoding/json"
"fmt"
"sort"
"github.com/pkg/errors"
"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/stringutils"
)
const (
subTechniqueIDSep = "."
)
// UnmarshalAndExtractMitreAttackBundle parses raw MITRE data per the MITRE specification, and extracts MITRE ATT&CK
// vectors from MITRE objects into array of `storage.MitreAttackVector`, for given MITRE domain and platform.
func UnmarshalAndExtractMitreAttackBundle(domain Domain, platform []Platform, data []byte) (*storage.MitreAttackBundle, error) {
var rawBundle mitreBundle
if err := json.Unmarshal(data, &rawBundle); err != nil {
return nil, errors.Wrap(err, "parsing MITRE ATT&CK raw data")
}
return ExtractMitreAttackBundle(domain, platform, rawBundle.Objects)
}
// ExtractMitreAttackBundle extracts MITRE ATT&CK vectors from MITRE objects into array of `storage.MitreAttackVector`,
// for given MITRE domain and platform.
func ExtractMitreAttackBundle(domain Domain, platforms []Platform, objs []mitreObject) (*storage.MitreAttackBundle, error) {
platformMap := make(map[Platform]struct{})
for _, p := range platforms {
platformMap[p] = struct{}{}
}
tactics := make(map[string]*storage.MitreTactic)
// This map stores tactic short names to IDs. We need this map since the references from techniques to tactics
// are by short names and not IDs :(
tacticShortNameMap := make(map[string]string)
// Collect all the tactics.
for i := range objs {
obj := objs[i]
if obj.Type != tactic {
continue
}
if !appliesToDomain(obj, domain) {
continue
}
id := getExternalID(obj)
if id == "" {
continue
}
tactics[id] = &storage.MitreTactic{
Id: id,
Name: obj.Name,
Description: obj.Description,
}
tacticShortNameMap[obj.XMitreShortname] = id
}
techniques := make(map[string]*storage.MitreTechnique)
subTechiquesMap := make(map[string]struct{})
techniquesMatrixMap := make(map[Platform]map[string]struct{})
tacticTechniquesMap := make(map[string]map[string]struct{})
// Collect all the techniques applicable to the platform.
for i := range objs {
obj := objs[i]
// "attackPattern" represents techniques.
if obj.Type != attackPattern {
continue
}
if !appliesToDomain(obj, domain) {
continue
}
matchedPlatforms, ok := appliesToAnyPlatform(obj, platformMap)
if !ok {
continue
}
techniqueID := getExternalID(obj)
if techniqueID == "" {
continue
}
techniques[techniqueID] = &storage.MitreTechnique{
Id: techniqueID,
Name: obj.Name,
Description: obj.Description,
}
if obj.XMitreIsSubtechnique {
subTechiquesMap[techniqueID] = struct{}{}
}
for _, platform := range matchedPlatforms {
if techniquesMatrixMap[platform] == nil {
techniquesMatrixMap[platform] = make(map[string]struct{})
}
techniquesMatrixMap[platform][techniqueID] = struct{}{}
}
// Obtain the reference to tactics.
for _, phase := range obj.KillChainPhases {
if phase.KillChainName != mitreAttackDataSrc {
continue
}
tacticID := tacticShortNameMap[phase.PhaseName]
if tacticID == "" {
continue
}
if tacticTechniquesMap[tacticID] == nil {
tacticTechniquesMap[tacticID] = make(map[string]struct{})
}
tacticTechniquesMap[tacticID][techniqueID] = struct{}{}
}
}
// Build composite names for sub-techniques.
for id, technique := range techniques {
if _, ok := subTechiquesMap[id]; !ok {
continue
}
pID, _ := stringutils.Split2(id, subTechniqueIDSep)
if pID == "" {
return nil, errors.Errorf("MITRE ATT&CK sub-technique ID %s does not contain technique ID", id)
}
pTechnique := techniques[pID]
if pTechnique == nil {
return nil, errors.Errorf("MITRE ATT&CK technique %s not found", pID)
}
technique.Name = fmt.Sprintf("%s: %s", pTechnique.GetName(), technique.GetName())
}
var version string
for i := range objs {
obj := objs[i]
if obj.Type != metadata {
continue
}
version = obj.Version
}
// Build full vectors.
vectors := buildVectors(tactics, techniques, tacticTechniquesMap)
// Build bundles.
return generateBundle(version, domain, techniquesMatrixMap, vectors...), nil
}
func buildVectors(
tactics map[string]*storage.MitreTactic,
techniques map[string]*storage.MitreTechnique,
tacticTechniquesMap map[string]map[string]struct{},
) []*storage.MitreAttackVector {
var vectors []*storage.MitreAttackVector
for tacticID, techniquesMap := range tacticTechniquesMap {
vector := &storage.MitreAttackVector{
Tactic: tactics[tacticID],
}
for techniqueID := range techniquesMap {
vector.Techniques = append(vector.Techniques, techniques[techniqueID])
}
vectors = append(vectors, vector)
sort.SliceStable(vector.GetTechniques(), func(i, j int) bool {
return vector.GetTechniques()[i].GetId() < vector.GetTechniques()[j].GetId()
})
}
sort.SliceStable(vectors, func(i, j int) bool {
return vectors[i].GetTactic().GetId() < vectors[j].GetTactic().GetId()
})
return vectors
}
// Builds out vectors into MITRE ATT&CK matrix per Domain+Platform applicability.
func generateBundle(
version string,
domain Domain,
techniqueMatrix map[Platform]map[string]struct{},
vectors ...*storage.MitreAttackVector,
) *storage.MitreAttackBundle {
bundle := &storage.MitreAttackBundle{
Version: version,
}
for platform, techniqueIDs := range techniqueMatrix {
var filteredVectors []*storage.MitreAttackVector
for _, vector := range vectors {
var filteredTechniques []*storage.MitreTechnique
for _, technique := range vector.GetTechniques() {
if _, ok := techniqueIDs[technique.GetId()]; ok {
filteredTechniques = append(filteredTechniques, technique)
}
}
if len(filteredTechniques) == 0 {
continue
}
filteredVectors = append(filteredVectors, &storage.MitreAttackVector{
Tactic: vector.GetTactic(),
Techniques: filteredTechniques,
})
}
if len(filteredVectors) == 0 {
continue
}
bundle.Matrices = append(bundle.Matrices, &storage.MitreAttackMatrix{
MatrixInfo: &storage.MitreAttackMatrix_MatrixInfo{
Domain: domain.String(),
Platform: platform.String(),
},
Vectors: filteredVectors,
})
}
sort.SliceStable(bundle.GetMatrices(), func(i, j int) bool {
return bundle.GetMatrices()[i].GetMatrixInfo().GetPlatform() < bundle.GetMatrices()[j].GetMatrixInfo().GetPlatform()
})
return bundle
}
func appliesToDomain(mitreObj mitreObject, domain Domain) bool {
for _, d := range mitreObj.XMitreDomains {
if d == domain {
return true
}
}
return false
}
func appliesToAnyPlatform(mitreObj mitreObject, platforms map[Platform]struct{}) ([]Platform, bool) {
var ret []Platform
for _, p := range mitreObj.XMitrePlatforms {
if _, ok := platforms[p]; ok {
ret = append(ret, p)
}
}
return ret, len(ret) > 0
}
func getExternalID(mitreObj mitreObject) string {
for _, extRef := range mitreObj.ExternalReferences {
if extRef.SourceName == mitreAttackDataSrc {
return extRef.ExternalID
}
}
return ""
}