forked from freetaxii/libstix2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s_common.go
265 lines (234 loc) · 6.74 KB
/
s_common.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
// Copyright 2015-2019 Bret Jordan, All rights reserved.
//
// Use of this source code is governed by an Apache 2.0 license that can be
// found in the LICENSE file in the root of the source tree.
package sqlite3
import (
"bytes"
"fmt"
"github.com/highkay/libstix2/objects/properties"
)
// ----------------------------------------------------------------------
//
// Common Tables Private Functions and Methods
//
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
//
// Aliases Table
//
// ----------------------------------------------------------------------
/*
commonAliasesProperties - This method will return the properties for aliases
Used by:
campaign
intrusion set
threat actor
*/
func commonAliasesProperties() string {
return baseDBProperties() + `
"aliases" TEXT NOT NULL
`
}
// ----------------------------------------------------------------------
//
// Authors Table
//
// ----------------------------------------------------------------------
/*
commonAuthorsProperties - This method will return the properties for common authors
Used by:
note
opinion
*/
func commonAuthorsProperties() string {
return baseDBProperties() + `
"authors" TEXT NOT NULL
`
}
// ----------------------------------------------------------------------
//
// Goals Table
//
// ----------------------------------------------------------------------
/*
commonGoalsProperties - This method will return the properties for goals
Used by:
intrusion set
threat actor
*/
func commonGoalsProperties() string {
return baseDBProperties() + `
"goals" TEXT NOT NULL
`
}
// ----------------------------------------------------------------------
//
// Hashes Table
//
// ----------------------------------------------------------------------
/*
commonHashesProperties - This method will return the properties for hashes
Used by:
external references
TODO need find a way to link this back to an actual external reference instance
maybe this should be called external references hashes. Otherwise how will you
know which object in the database it is tied to.
*/
func commonHashesProperties() string {
return baseDBProperties() + `
"hash" TEXT NOT NULL,
"value" TEXT NOT NULL
`
}
// ----------------------------------------------------------------------
//
// Kill Chain Phases Table
//
// ----------------------------------------------------------------------
/*
commonKillChainPhasesProperties - This method will return the properties for kill chain phases
Used by:
attack pattern
indicator
malware
tool
*/
func commonKillChainPhasesProperties() string {
return baseDBProperties() + `
"kill_chain_name" TEXT NOT NULL,
"phase_name" TEXT NOT NULL
`
}
/*
addKillChainPhase - This method will add a kill chain phase for a given object
to the database.
*/
func (ds *Store) addKillChainPhase(objectID int, obj *properties.KillChainPhase) error {
ds.Logger.Levelln("Function", "FUNC: addKillChainPhase start")
// Create SQL Statement
/*
INSERT INTO
s_kill_chain_phases (
"datastore_id",
"kill_chain_name",
"phase_name"
)
values (?, ?)
*/
tblKillChain := DB_TABLE_STIX_KILL_CHAIN_PHASES
var sqlstmt bytes.Buffer
sqlstmt.WriteString("INSERT INTO ")
sqlstmt.WriteString(tblKillChain)
sqlstmt.WriteString(" (datastore_id, kill_chain_name, phase_name) values (?, ?, ?)")
stmt := sqlstmt.String()
// Make SQL Call
_, err := ds.DB.Exec(stmt, objectID, obj.KillChainName, obj.PhaseName)
if err != nil {
ds.Logger.Levelln("Function", "FUNC: addKillChainPhase exited with an error")
return fmt.Errorf("database execution error inserting kill chain phase: ", err)
}
ds.Logger.Levelln("Function", "FUNC: addKillChainPhase end")
return nil
}
/*
getKillChainPhases - This method will get the kill chain phases for a given
object ID.
*/
func (ds *Store) getKillChainPhases(objectID int) (*properties.KillChainPhasesProperty, error) {
ds.Logger.Levelln("Function", "FUNC: getKillChainPhases start")
var kcPhases properties.KillChainPhasesProperty
// Create SQL Statement
/*
SELECT
kill_chain_name,
phase_name
FROM
s_kill_chain_phases
WHERE
datastore_id = ?
*/
tblKCP := DB_TABLE_STIX_KILL_CHAIN_PHASES
var sqlstmt bytes.Buffer
sqlstmt.WriteString("SELECT ")
sqlstmt.WriteString("kill_chain_name, phase_name ")
sqlstmt.WriteString("FROM ")
sqlstmt.WriteString(tblKCP)
sqlstmt.WriteString(" WHERE datastore_id = ?")
stmt := sqlstmt.String()
// Make SQL Call
rows, err := ds.DB.Query(stmt, objectID)
if err != nil {
ds.Logger.Levelln("Function", "FUNC: getKillChainPhases exited with an error")
return nil, fmt.Errorf("database execution error getting kill chain phases: ", err)
}
defer rows.Close()
for rows.Next() {
var name, phase string
p, _ := kcPhases.NewKillChainPhase()
if err := rows.Scan(&name, &phase); err != nil {
rows.Close()
ds.Logger.Levelln("Function", "FUNC: getKillChainPhases exited with an error")
return nil, fmt.Errorf("database scan error getting kill chain phases: ", err)
}
p.SetName(name)
p.SetPhase(phase)
}
// Errors can cause the rows.Next() to exit prematurely, if this happens lets
// check for the error and handle it.
if err := rows.Err(); err != nil {
rows.Close()
ds.Logger.Levelln("Function", "FUNC: getKillChainPhases exited with an error")
return nil, fmt.Errorf("database rows error getting kill chain phases: ", err)
}
ds.Logger.Levelln("Function", "FUNC: getKillChainPhases end")
return &kcPhases, nil
}
// ----------------------------------------------------------------------
//
// Object Refs Table
//
// ----------------------------------------------------------------------
/*
commonObjectRefsProperties - This method will return the properties for object refs
Used by:
note
opinion
report
*/
func commonObjectRefsProperties() string {
return baseDBProperties() + `
"object_refs" TEXT NOT NULL
`
}
// ----------------------------------------------------------------------
//
// Personal Motivations Table
//
// ----------------------------------------------------------------------
/*
commonPersonalMotivationsProperties - This method will return the properties for personal motivations
Used by:
threat actor
*/
func commonPersonalMotivationsProperties() string {
return baseDBProperties() + `
"personal_motivations" TEXT NOT NULL
`
}
// ----------------------------------------------------------------------
//
// Secondary Motivations Table
//
// ----------------------------------------------------------------------
/*
commonSecondaryMotivationsProperties - This method will return the properties for secondary motivations
Used by:
intrusion set
threat actor
*/
func commonSecondaryMotivationsProperties() string {
return baseDBProperties() + `
"secondary_motivations" TEXT NOT NULL
`
}