forked from freetaxii/libstix2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s_indicator.go
282 lines (251 loc) · 7.62 KB
/
s_indicator.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
// 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"
"database/sql"
"errors"
"fmt"
"github.com/highkay/libstix2/objects/indicator"
)
// ----------------------------------------------------------------------
//
// Private Functions - Indicator Table
// Table property names and SQL statements
//
// ----------------------------------------------------------------------
/*
indicatorProperties - This function will return the properties for the
indicator SDO table
*/
func indicatorProperties() string {
return baseDBProperties() + `
"name" TEXT,
"description" TEXT,
"pattern" TEXT NOT NULL,
"valid_from" TEXT NOT NULL,
"valid_until" TEXT
`
// indicator_types
// kill_chain_phases
}
/*
indicatorTypeProperties - This function will return the properties for the
indicator types table
*/
func indicatorTypesProperties() string {
return baseDBProperties() + `
"indicator_type" TEXT NOT NULL
`
}
// ----------------------------------------------------------------------
//
// Private Methods - Indicator Table
// addIndicator()
// getIndicator()
// addIndicatorType()
//
// ----------------------------------------------------------------------
/*
addIndicator - This method will add an indicator to the database.
*/
func (ds *Store) addIndicator(obj *indicator.Indicator) error {
ds.Logger.Levelln("Function", "FUNC: addIndicator start")
datastoreID, err := ds.addBaseObject(&obj.CommonObjectProperties)
if err != nil {
ds.Logger.Levelln("Function", "FUNC: addIndicator exited with an error")
return err
}
ds.Logger.Debugln("DEBUG: Adding Indicator to datastore with database ID", datastoreID)
// Create SQL Statement
/*
INSERT INTO
s_indicator (
"datastore_id",
"name",
"description",
"pattern",
"valid_from",
"valid_until"
)
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
*/
tblInd := DB_TABLE_STIX_INDICATOR
var sqlstmt bytes.Buffer
sqlstmt.WriteString("INSERT INTO ")
sqlstmt.WriteString(tblInd)
sqlstmt.WriteString(" (datastore_id, name, description, pattern, valid_from, valid_until) ")
sqlstmt.WriteString("values (?, ?, ?, ?, ?, ?)")
stmt := sqlstmt.String()
// Make SQL Call
_, err1 := ds.DB.Exec(stmt,
datastoreID,
obj.Name,
obj.Description,
obj.Pattern,
obj.ValidFrom,
obj.ValidUntil)
// TODO if there is an error, we probably need to back out all of the INSERTS
if err1 != nil {
ds.Logger.Levelln("Function", "FUNC: addIndicator exited with an error")
return fmt.Errorf("database execution error inserting indicator: ", err)
}
// ----------------------------------------------------------------------
// Add Indicator Types
// ----------------------------------------------------------------------
if obj.IndicatorTypes != nil {
for _, itype := range obj.IndicatorTypes {
err := ds.addIndicatorType(datastoreID, itype)
if err != nil {
ds.Logger.Levelln("Function", "FUNC: addIndicator exited with an error")
return err
}
}
}
// ----------------------------------------------------------------------
// Add Kill Chains
// ----------------------------------------------------------------------
if obj.KillChainPhases != nil {
for _, v := range obj.KillChainPhases {
err := ds.addKillChainPhase(datastoreID, &v)
if err != nil {
ds.Logger.Levelln("Function", "FUNC: addIndicator exited with an error")
return err
}
}
}
ds.Logger.Levelln("Function", "FUNC: addIndicator end")
return nil
}
/*
getIndicator - This method will get a specific indicator from the database based
on the STIX ID and version.
*/
func (ds *Store) getIndicator(stixid, version string) (*indicator.Indicator, error) {
ds.Logger.Levelln("Function", "FUNC: getIndicator start")
var i indicator.Indicator
// Get Base Object - this will give us the datastoreID
// Then copy base object data in to Indicator object
baseObject, errBase := ds.getBaseObject(stixid, version)
if errBase != nil {
ds.Logger.Levelln("Function", "FUNC: getIndicator exited with an error")
return nil, errBase
}
i.CommonObjectProperties = *baseObject
// Create SQL Statement
/*
SELECT
s_indicator.name,
s_indicator.description,
s_indicator.pattern,
s_indicator.valid_from,
s_indicator.valid_until,
group_concat(s_indicator_types.indicator_type)
FROM
s_indicator
LEFT JOIN
s_indicator_types ON
s_indicator.datastore_id = s_indicator_types.datastore_id
WHERE
s_indicator.datastore_id = ?
*/
tblInd := DB_TABLE_STIX_INDICATOR
tblIndType := DB_TABLE_STIX_INDICATOR_TYPES
var sqlstmt bytes.Buffer
sqlstmt.WriteString("SELECT ")
sqlstmt.WriteString(tblInd)
sqlstmt.WriteString(".name, ")
sqlstmt.WriteString(tblInd)
sqlstmt.WriteString(".description, ")
sqlstmt.WriteString(tblInd)
sqlstmt.WriteString(".pattern, ")
sqlstmt.WriteString(tblInd)
sqlstmt.WriteString(".valid_from, ")
sqlstmt.WriteString(tblInd)
sqlstmt.WriteString(".valid_until, ")
sqlstmt.WriteString("group_concat(")
sqlstmt.WriteString(tblIndType)
sqlstmt.WriteString(".indicator_type) ")
sqlstmt.WriteString("FROM ")
sqlstmt.WriteString(tblInd)
sqlstmt.WriteString(" LEFT JOIN ")
sqlstmt.WriteString(tblIndType)
sqlstmt.WriteString(" ON ")
sqlstmt.WriteString(tblInd)
sqlstmt.WriteString(".datastore_id = ")
sqlstmt.WriteString(tblIndType)
sqlstmt.WriteString(".datastore_id ")
sqlstmt.WriteString("WHERE ")
sqlstmt.WriteString(tblInd)
sqlstmt.WriteString(".datastore_id = ?")
stmt := sqlstmt.String()
// Make SQL Call
var name, description, pattern, validFrom, validUntil, indTypes sql.NullString
err := ds.DB.QueryRow(stmt, i.DatastoreID).Scan(&name, &description, &pattern, &validFrom, &validUntil, &indTypes)
if err != nil {
if err == sql.ErrNoRows {
ds.Logger.Levelln("Function", "FUNC: getIndicator exited with an error")
return nil, errors.New("no indicator record found")
}
ds.Logger.Levelln("Function", "FUNC: getIndicator exited with an error")
return nil, fmt.Errorf("database execution error getting indicator: ", err)
}
if name.Valid {
i.SetName(name.String)
}
if description.Valid {
i.SetDescription(description.String)
}
if indTypes.Valid {
i.AddTypes(indTypes.String)
}
if pattern.Valid {
i.SetPattern(pattern.String)
}
if validFrom.Valid {
i.SetValidFrom(validFrom.String)
}
if validUntil.Valid {
i.SetValidUntil(validUntil.String)
}
killChainPhases, errkc := ds.getKillChainPhases(i.DatastoreID)
if errkc != nil {
ds.Logger.Levelln("Function", "FUNC: getIndicator exited with an error")
return nil, errkc
}
i.KillChainPhasesProperty = *killChainPhases
ds.Logger.Levelln("Function", "FUNC: getIndicator end")
return &i, nil
}
/*
addIndicatorType - This method will add all of the indicator types to the
database for a specific indicator based on its database ID.
*/
func (ds *Store) addIndicatorType(datastoreID int, itype string) error {
ds.Logger.Levelln("Function", "FUNC: addIndicatorType start")
// Create SQL Statement
/*
INSERT INTO
s_indicator_types (
"datastore_id",
"indicator_type"
)
values (?, ?)
*/
tblIndTypes := DB_TABLE_STIX_INDICATOR_TYPES
var sqlstmt bytes.Buffer
sqlstmt.WriteString("INSERT INTO ")
sqlstmt.WriteString(tblIndTypes)
sqlstmt.WriteString(" (datastore_id, indicator_type) values (?, ?)")
stmt := sqlstmt.String()
// Make SQL Call
_, err := ds.DB.Exec(stmt, datastoreID, itype)
if err != nil {
ds.Logger.Levelln("Function", "FUNC: addIndicatorType exited with an error")
return fmt.Errorf("database execution error inserting indicator type: ", err)
}
ds.Logger.Levelln("Function", "FUNC: addIndicatorType end")
return nil
}