This repository has been archived by the owner on Jun 26, 2021. It is now read-only.
/
dyndb.go
243 lines (200 loc) · 5.62 KB
/
dyndb.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
package dyndb
import (
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/oktasecuritylabs/sgt/logger"
osq_types "github.com/oktasecuritylabs/sgt/osquery_types"
)
type DynDB struct {
DB *dynamodb.DynamoDB
}
// DbInstance creates a new pointer to dynamodb from assumed role by ec2 instance
func DbInstance() *dynamodb.DynamoDB {
sess := session.Must(session.NewSession(
&aws.Config{
Region: aws.String("us-east-1"),
}))
creds := credentials.NewChainCredentials(
[]credentials.Provider{
&credentials.EnvProvider{},
&ec2rolecreds.EC2RoleProvider{
Client: ec2metadata.New(sess),
},
})
dynamoDB := dynamodb.New(session.Must(session.NewSession(&aws.Config{
Region: aws.String("us-east-1"),
Credentials: creds,
})))
return dynamoDB
}
func NewDynamoDB() DynDB {
dynDB := DynDB{}
dynDB.DB = DbInstance()
return dynDB
}
// BuildNamedConfig returns the fully built Named config, minus the credentials which are supplied during node config
func (dyn DynDB) SearchDistributedNodeKey(nk string) (osq_types.DistributedQuery, error) {
type nodequery struct {
NodeKey string `json:"node_key"`
}
querystring := nodequery{nk}
marshalmap, err := dynamodbattribute.MarshalMap(querystring)
dq := osq_types.DistributedQuery{}
if err != nil {
logger.Error(err)
return dq, err
}
item := dynamodb.GetItemInput{
TableName: aws.String("osquery_distributed_queries"),
Key: marshalmap,
}
resp, err := dyn.DB.GetItem(&item)
if err != nil {
logger.Error(err)
return dq, err
}
if len(resp.Item) > 0 {
err = dynamodbattribute.UnmarshalMap(resp.Item, &dq)
if err != nil {
logger.Error(err)
return dq, err
}
}
return dq, nil
}
// UpsertClient upsers an osqueryClient
func (db DynDB) UpsertClient(oc osq_types.OsqueryClient) (error) {
logger.Debugf("Upserting Client: %v", oc)
av, err := dynamodbattribute.MarshalMap(oc)
if err != nil {
logger.Warn("Marshal failed")
logger.Warn(err)
return err
}
_, err = db.DB.PutItem(&dynamodb.PutItemInput{
TableName: aws.String("osquery_clients"),
Item: av,
})
if err != nil {
logger.Error(err)
return err
}
return nil
}
func (db DynDB) SearchByHostIdentifier(hid string) ([]osq_types.OsqueryClient, error) {
Results := []osq_types.OsqueryClient{}
type BS struct {
HostIdentifier string `json:"host_identifier"`
}
var bs BS
bs.HostIdentifier = hid
scanParams := &dynamodb.ScanInput{
TableName: aws.String("osquery_clients"),
}
pageNum := 0
err := db.DB.ScanPages(scanParams,
func(page *dynamodb.ScanOutput, lastPage bool) bool {
pageNum++
if hid != "" {
for _, i := range page.Items {
o := osq_types.OsqueryClient{}
err2 := dynamodbattribute.UnmarshalMap(i, &o)
if err2 != nil {
logger.Error(err2)
}
if hid == string(o.HostIdentifier) {
Results = append(Results, o)
fmt.Println(o)
}
}
} else {
for _, i := range page.Items {
client := osq_types.OsqueryClient{}
err3 := dynamodbattribute.UnmarshalMap(i, &client)
if err3 != nil {
logger.Error(err3)
}
Results = append(Results, client)
}
}
return pageNum <= 10
})
if err != nil {
logger.Error(err)
return Results, err
}
return Results, nil
}
func (db DynDB) ValidNode(nodeKey string) (error) {
osqNode, err := db.SearchByNodeKey(nodeKey)
if err != nil {
return err
}
if len(osqNode.NodeKey) == 0 {
return errors.New("size of osquery node key is 0")
}
if osqNode.PendingRegistrationApproval == true {
return errors.New("node is pending registration approval")
}
return nil
}
func (db DynDB) APIGetPackQueries() ([]osq_types.PackQuery, error) {
results := []osq_types.PackQuery{}
scanItems, err := db.DB.Scan(&dynamodb.ScanInput{
TableName: aws.String("osquery_packqueries"),
})
if err != nil {
logger.Error(err)
return results, err
}
for _, i := range scanItems.Items {
packquery := osq_types.PackQuery{}
err = dynamodbattribute.UnmarshalMap(i, &packquery)
if err != nil {
logger.Error(err)
return results, err
}
results = append(results, packquery)
}
return results, nil
}
// GetPackByName returns pack specified by name
func GetPackByName(s string, db *dynamodb.DynamoDB) (string, error) {
type qs struct {
PackName string `json:"packName"`
PackOS string `json:"pack_os"`
}
query := qs{}
type pack struct {
PackName string `json:"packName"`
PackOS string `json:"pack_os"`
Queries string `json:"queries"`
}
p := pack{}
query.PackName = s
query.PackOS = "Linux"
js, err := dynamodbattribute.MarshalMap(query)
item := dynamodb.GetItemInput{
TableName: aws.String("osquery_packs"),
Key: js,
}
resp, err := db.GetItem(&item)
if err != nil {
logger.Info(err)
}
if len(resp.Item) > 0 {
err = dynamodbattribute.UnmarshalMap(resp.Item, &p)
if err != nil {
logger.Info(err)
}
return p.Queries, nil
}
return p.Queries, nil
}