forked from freetaxii/libstix2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createTables.go
196 lines (168 loc) · 8.01 KB
/
createTables.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
// 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 (
"github.com/highkay/libstix2/vocabs"
)
// ----------------------------------------------------------------------
//
// Public Methods
//
// ----------------------------------------------------------------------
/*
CreateTAXIITables - This method will create all of the tables needed to store
STIX content in the database.
*/
func (ds *Store) CreateTAXIITables() {
ds.createTAXIITable(DB_TABLE_TAXII_COLLECTION_DATA, collectionDataProperties())
ds.createTAXIITable(DB_TABLE_TAXII_COLLECTIONS, collectionProperties())
ds.createTAXIITable(DB_TABLE_TAXII_COLLECTION_MEDIA_TYPE, collectionMediaTypeProperties())
ds.createTAXIITable(DB_TABLE_TAXII_MEDIA_TYPES, mediaTypeProperties())
ds.createTAXIIIndexes(DB_TABLE_TAXII_COLLECTION_DATA)
ds.insertMediaTypes(DB_TABLE_TAXII_MEDIA_TYPES)
}
/*
CreateSTIXTables - This method will create all of the tables needed to store
STIX content in the database.
*/
func (ds *Store) CreateSTIXTables() {
ds.createSTIXTable(DB_TABLE_STIX_BASE_OBJECT, baseObjectProperties())
ds.createSTIXTable(DB_TABLE_STIX_ATTACK_PATTERN, attackPatternProperties())
ds.createSTIXTable(DB_TABLE_STIX_CAMPAIGN, campaignProperties())
ds.createSTIXTable(DB_TABLE_STIX_COURSE_OF_ACTION, courseOfActionProperties())
ds.createSTIXTable(DB_TABLE_STIX_IDENTITY, identityProperties())
ds.createSTIXTable(DB_TABLE_STIX_IDENTITY_SECTORS, identitySectorsProperties())
ds.createSTIXTable(DB_TABLE_STIX_INDICATOR, indicatorProperties())
ds.createSTIXTable(DB_TABLE_STIX_INDICATOR_TYPES, indicatorTypesProperties())
ds.createSTIXTable(DB_TABLE_STIX_INTRUSION_SET, intrusionSetProperties())
ds.createSTIXTable(DB_TABLE_STIX_LOCATION, locationProperties())
ds.createSTIXTable(DB_TABLE_STIX_MALWARE, malwareProperties())
ds.createSTIXTable(DB_TABLE_STIX_NOTE, noteProperties())
ds.createSTIXTable(DB_TABLE_STIX_OBSERVED_DATA, observedDataProperties())
ds.createSTIXTable(DB_TABLE_STIX_OPINION, opinionProperties())
ds.createSTIXTable(DB_TABLE_STIX_REPORT, reportProperties())
ds.createSTIXTable(DB_TABLE_STIX_THREAT_ACTOR, threatActorProperties())
ds.createSTIXTable(DB_TABLE_STIX_THREAT_ACTOR_ROLES, threatActorRolesProperties())
ds.createSTIXTable(DB_TABLE_STIX_TOOL, toolProperties())
ds.createSTIXTable(DB_TABLE_STIX_VULNERABILITY, vulnerabilityProperties())
ds.createSTIXTable(DB_TABLE_STIX_ALIASES, commonAliasesProperties())
ds.createSTIXTable(DB_TABLE_STIX_AUTHORS, commonAuthorsProperties())
ds.createSTIXTable(DB_TABLE_STIX_EXTERNAL_REFERENCES, commonExternalReferencesProperties())
ds.createSTIXTable(DB_TABLE_STIX_GOALS, commonGoalsProperties())
ds.createSTIXTable(DB_TABLE_STIX_HASHES, commonHashesProperties())
ds.createSTIXTable(DB_TABLE_STIX_KILL_CHAIN_PHASES, commonKillChainPhasesProperties())
ds.createSTIXTable(DB_TABLE_STIX_LABELS, commonLabelsProperties())
ds.createSTIXTable(DB_TABLE_STIX_OBJECT_MARKING_REFS, commonObjectMarkingRefsProperties())
ds.createSTIXTable(DB_TABLE_STIX_OBJECT_REFS, commonObjectRefsProperties())
ds.createSTIXTable(DB_TABLE_STIX_SECONDARY_MOTIVATIONS, commonSecondaryMotivationsProperties())
ds.createSTIXTable(DB_TABLE_STIX_PERSONAL_MOTIVATIONS, commonPersonalMotivationsProperties())
}
/*
CreateVocabTables - This method will create all of the tables needed to store
STIX content in the database.
*/
func (ds *Store) CreateVocabTables() {
ds.createVocabTable(DB_TABLE_VOCAB_ATTACK_MOTIVATIONS, vocabProperties())
ds.createVocabTable(DB_TABLE_VOCAB_ATTACK_RESOURCE_LEVEL, vocabProperties())
ds.createVocabTable(DB_TABLE_VOCAB_IDENTITY_CLASS, vocabProperties())
ds.createVocabTable(DB_TABLE_VOCAB_INDICATOR_LABEL, vocabProperties())
ds.createVocabTable(DB_TABLE_VOCAB_INDUSTRY_SECTOR, vocabProperties())
ds.createVocabTable(DB_TABLE_VOCAB_MALWARE_LABEL, vocabProperties())
ds.createVocabTable(DB_TABLE_VOCAB_REPORT_LABEL, vocabProperties())
ds.createVocabTable(DB_TABLE_VOCAB_THREAT_ACTOR_LABEL, vocabProperties())
ds.createVocabTable(DB_TABLE_VOCAB_THREAT_ACTOR_ROLE, vocabProperties())
ds.createVocabTable(DB_TABLE_VOCAB_THREAT_ACTOR_SOPHISTICATION, vocabProperties())
ds.createVocabTable(DB_TABLE_VOCAB_TOOL_LABEL, vocabProperties())
}
/*
PopulateVocabTables - This method will insert all of the vocabulary data
into the right database tables.
*/
func (ds *Store) PopulateVocabTables() {
ds.insertVocabData(DB_TABLE_VOCAB_ATTACK_MOTIVATIONS, vocabs.AttackMotivation)
ds.insertVocabData(DB_TABLE_VOCAB_ATTACK_RESOURCE_LEVEL, vocabs.AttackResourceLevel)
ds.insertVocabData(DB_TABLE_VOCAB_IDENTITY_CLASS, vocabs.IdentityClass)
ds.insertVocabData(DB_TABLE_VOCAB_INDICATOR_LABEL, vocabs.IndicatorLabel)
ds.insertVocabData(DB_TABLE_VOCAB_INDUSTRY_SECTOR, vocabs.IndustrySector)
ds.insertVocabData(DB_TABLE_VOCAB_MALWARE_LABEL, vocabs.MalwareType)
ds.insertVocabData(DB_TABLE_VOCAB_REPORT_LABEL, vocabs.ReportType)
ds.insertVocabData(DB_TABLE_VOCAB_THREAT_ACTOR_LABEL, vocabs.ThreatActorType)
ds.insertVocabData(DB_TABLE_VOCAB_THREAT_ACTOR_ROLE, vocabs.ThreatActorRole)
ds.insertVocabData(DB_TABLE_VOCAB_THREAT_ACTOR_SOPHISTICATION, vocabs.ThreatActorSophistication)
ds.insertVocabData(DB_TABLE_VOCAB_TOOL_LABEL, vocabs.ToolType)
}
// ----------------------------------------------------------------------
//
// Private Methods
//
// ----------------------------------------------------------------------
func (ds *Store) createTAXIITable(name, properties string) {
var stmt = `CREATE TABLE IF NOT EXISTS "` + name + `" (` + properties + `)`
_, err := ds.DB.Exec(stmt)
if err != nil {
ds.Logger.Println("ERROR: The", name, "table could not be created due to error:", err)
}
}
func (ds *Store) createTAXIIIndexes(name string) {
var stmt string
if name == DB_TABLE_TAXII_COLLECTION_DATA {
stmt = `CREATE INDEX "idx_` + name + `" ON ` + name + ` ("collection_id" COLLATE BINARY ASC, "stix_id" COLLATE BINARY ASC)`
}
if stmt != "" {
_, err := ds.DB.Exec(stmt)
if err != nil {
ds.Logger.Println("ERROR: The indexes for table", name, "could not be created due to error:", err)
}
}
}
func (ds *Store) insertMediaTypes(name string) {
var stmt = `INSERT INTO "` + name + `" (media_type) values (?)`
mediaTypes := []string{"application/stix+json;version=2.0", "application/stix+json;version=2.1", "application/stix+json;version=2.2", "application/stix+json;version=2.3"}
for _, value := range mediaTypes {
var err error
_, err = ds.DB.Exec(stmt, value)
if err != nil {
ds.Logger.Println("ERROR: The media type item could not be inserted in to the", name, "table")
}
}
}
func (ds *Store) createSTIXTable(name, properties string) {
var stmt = `CREATE TABLE IF NOT EXISTS "` + name + `" (` + properties + `)`
_, err := ds.DB.Exec(stmt)
if err != nil {
ds.Logger.Println("ERROR: The", name, "table could not be created due to error:", err)
}
ds.createSTIXIndexes(name)
}
func (ds *Store) createSTIXIndexes(name string) {
var stmt string
if name == DB_TABLE_STIX_BASE_OBJECT {
stmt = `CREATE INDEX "idx_` + name + `" ON ` + name + ` ("object_id" COLLATE BINARY ASC, "id" COLLATE BINARY ASC)`
} else {
stmt = `CREATE INDEX "idx_` + name + `" ON ` + name + ` ("object_id" COLLATE BINARY ASC)`
}
_, err := ds.DB.Exec(stmt)
if err != nil {
ds.Logger.Println("ERROR: The indexes for table", name, "could not be created due to error:", err)
}
}
func (ds *Store) createVocabTable(name, properties string) {
var stmt = `CREATE TABLE IF NOT EXISTS "` + name + `" (` + properties + `)`
_, err := ds.DB.Exec(stmt)
if err != nil {
ds.Logger.Println("ERROR: The", name, "table could not be created due to error:", err)
}
}
// InsertVocabData - This method will add a vocabulary item to its table
func (ds *Store) insertVocabData(name string, data []string) {
var stmt = `INSERT INTO "` + name + `" (value) values (?)`
var err error
for _, value := range data {
_, err = ds.DB.Exec(stmt, value)
}
if err != nil {
ds.Logger.Println("ERROR: The vocabulary item could not be inserted in to the", name, "table")
}
}