forked from AmgdGocha/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv.go
342 lines (283 loc) · 8.53 KB
/
csv.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
Velociraptor - Hunting Evil
Copyright (C) 2019 Velocidex Innovations.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package api
import (
"regexp"
"time"
errors "github.com/pkg/errors"
context "golang.org/x/net/context"
"www.velocidex.com/golang/velociraptor/datastore"
file_store "www.velocidex.com/golang/velociraptor/file_store"
"www.velocidex.com/golang/velociraptor/file_store/api"
"www.velocidex.com/golang/velociraptor/file_store/csv"
"www.velocidex.com/golang/velociraptor/paths"
"www.velocidex.com/golang/velociraptor/paths/artifacts"
"www.velocidex.com/golang/velociraptor/result_sets"
"www.velocidex.com/golang/velociraptor/services"
"www.velocidex.com/golang/velociraptor/timelines"
api_proto "www.velocidex.com/golang/velociraptor/api/proto"
artifacts_proto "www.velocidex.com/golang/velociraptor/artifacts/proto"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
)
func getTable(
ctx context.Context,
config_obj *config_proto.Config,
in *api_proto.GetTableRequest) (
*api_proto.GetTableResponse, error) {
rows := uint64(0)
if in.Rows == 0 {
in.Rows = 500
}
result := &api_proto.GetTableResponse{
ColumnTypes: getColumnTypes(config_obj, in),
}
path_spec, err := getPathSpec(config_obj, in)
if err != nil {
return result, err
}
file_store_factory := file_store.GetFileStore(config_obj)
options := result_sets.ResultSetOptions{}
if in.SortColumn != "" {
options.SortColumn = in.SortColumn
options.SortAsc = in.SortDirection
}
if in.FilterColumn != "" &&
in.FilterRegex != "" {
options.FilterColumn = in.FilterColumn
options.FilterRegex, err = regexp.Compile("(?i)" + in.FilterRegex)
if err != nil {
return nil, err
}
}
rs_reader, err := result_sets.NewResultSetReaderWithOptions(
ctx, config_obj,
file_store_factory, path_spec, options)
if err != nil {
return result, nil
}
defer rs_reader.Close()
// Let the browser know how many rows we have in total.
result.TotalRows = rs_reader.TotalRows()
// FIXME: Backwards compatibility: Just give a few
// rows if the result set does not have an index. This
// is the same as the previous behavior but for new
// collections, an index is created and we respect the
// number of rows the callers asked for. Eventually
// this will not be needed.
if result.TotalRows < 0 {
in.Rows = 100
}
// Seek to the row we need.
err = rs_reader.SeekToRow(int64(in.StartRow))
if err != nil {
return nil, err
}
// Unpack the rows into the output protobuf
for row := range rs_reader.Rows(ctx) {
if result.Columns == nil {
result.Columns = row.Keys()
}
row_data := make([]string, 0, len(result.Columns))
for _, key := range result.Columns {
value, _ := row.Get(key)
row_data = append(row_data, csv.AnyToString(value))
}
result.Rows = append(result.Rows, &api_proto.Row{
Cell: row_data,
})
rows += 1
if rows > in.Rows {
break
}
}
return result, nil
}
// The GUI is requesting table data. This function tries to figure out
// the column types.
func getColumnTypes(
config_obj *config_proto.Config,
in *api_proto.GetTableRequest) []*artifacts_proto.ColumnType {
// For artifacts column types are specified in the `column_types`
// artifact definition.
if in.Artifact != "" {
manager, err := services.GetRepositoryManager(config_obj)
if err != nil {
return nil
}
repository, err := manager.GetGlobalRepository(config_obj)
if err != nil {
return nil
}
artifact, pres := repository.Get(config_obj, in.Artifact)
if pres {
return artifact.ColumnTypes
}
}
// For notebooks, the column_types are set in the notebook metadata.
if in.NotebookId != "" {
notebook_path_manager := paths.NewNotebookPathManager(
in.NotebookId)
db, err := datastore.GetDB(config_obj)
if err != nil {
return nil
}
notebook := &api_proto.NotebookMetadata{}
err = db.GetSubject(config_obj, notebook_path_manager.Path(), notebook)
if err == nil {
return notebook.ColumnTypes
}
}
return nil
}
func getPathSpec(
config_obj *config_proto.Config,
in *api_proto.GetTableRequest) (api.FSPathSpec, error) {
if in.FlowId != "" && in.Artifact != "" {
path_manager, err := artifacts.NewArtifactPathManager(
config_obj, in.ClientId, in.FlowId, in.Artifact)
if err != nil {
return nil, err
}
return path_manager.Path(), nil
} else if in.FlowId != "" && in.Type != "" {
flow_path_manager := paths.NewFlowPathManager(
in.ClientId, in.FlowId)
switch in.Type {
case "log":
return flow_path_manager.Log(), nil
case "uploads":
return flow_path_manager.UploadMetadata(), nil
}
} else if in.HuntId != "" && in.Type == "clients" {
return paths.NewHuntPathManager(in.HuntId).Clients(), nil
} else if in.HuntId != "" && in.Type == "hunt_status" {
return paths.NewHuntPathManager(in.HuntId).ClientErrors(), nil
} else if in.NotebookId != "" && in.CellId != "" {
return paths.NewNotebookPathManager(in.NotebookId).Cell(
in.CellId).QueryStorage(in.TableId).Path(), nil
}
return nil, errors.New("Invalid request")
}
func getEventTable(
ctx context.Context,
config_obj *config_proto.Config,
in *api_proto.GetTableRequest) (
*api_proto.GetTableResponse, error) {
path_manager, err := artifacts.NewArtifactPathManager(
config_obj, in.ClientId, in.FlowId, in.Artifact)
if err != nil {
return nil, err
}
return getEventTableWithPathManager(ctx, config_obj, in, path_manager)
}
func getEventTableLogs(
ctx context.Context,
config_obj *config_proto.Config,
in *api_proto.GetTableRequest) (
*api_proto.GetTableResponse, error) {
path_manager, err := artifacts.NewArtifactLogPathManager(
config_obj, in.ClientId, "", in.Artifact)
if err != nil {
return nil, err
}
return getEventTableWithPathManager(ctx, config_obj, in, path_manager)
}
func getEventTableWithPathManager(
ctx context.Context,
config_obj *config_proto.Config,
in *api_proto.GetTableRequest,
path_manager api.PathManager) (
*api_proto.GetTableResponse, error) {
rows := uint64(0)
if in.Rows == 0 {
in.Rows = 10
}
result := &api_proto.GetTableResponse{}
file_store_factory := file_store.GetFileStore(config_obj)
rs_reader, err := result_sets.NewTimedResultSetReader(ctx,
file_store_factory, path_manager)
if err != nil {
return nil, err
}
defer rs_reader.Close()
err = rs_reader.SeekToTime(time.Unix(int64(in.StartTime), 0))
if err != nil {
return nil, err
}
if in.EndTime != 0 {
rs_reader.SetMaxTime(time.Unix(int64(in.EndTime), 0))
}
// Unpack the rows into the output protobuf
for row := range rs_reader.Rows(ctx) {
if result.Columns == nil {
result.Columns = row.Keys()
}
row_data := make([]string, 0, len(result.Columns))
for _, key := range result.Columns {
value, _ := row.Get(key)
row_data = append(row_data, csv.AnyToString(value))
}
result.Rows = append(result.Rows, &api_proto.Row{
Cell: row_data,
})
rows += 1
if rows > in.Rows {
break
}
}
return result, nil
}
func getTimeline(
ctx context.Context,
config_obj *config_proto.Config,
in *api_proto.GetTableRequest) (*api_proto.GetTableResponse, error) {
if in.NotebookId == "" {
return nil, errors.New("NotebookId must be specified")
}
path_manager := paths.NewNotebookPathManager(in.NotebookId).
SuperTimeline(in.Timeline)
reader, err := timelines.NewSuperTimelineReader(
config_obj, path_manager, in.SkipComponents)
if err != nil {
return nil, err
}
defer reader.Close()
result := &api_proto.GetTableResponse{
Columns: []string{"_Source", "Time", "Data"},
StartTime: int64(in.StartTime),
}
if in.StartTime != 0 {
ts := time.Unix(0, int64(in.StartTime))
reader.SeekToTime(ts)
}
rows := uint64(0)
for item := range reader.Read(ctx) {
if result.StartTime == 0 {
result.StartTime = item.Time.UnixNano()
}
result.EndTime = item.Time.UnixNano()
result.Rows = append(result.Rows, &api_proto.Row{
Cell: []string{
item.Source,
csv.AnyToString(item.Time),
csv.AnyToString(item.Row)},
})
rows += 1
if rows > in.Rows {
break
}
}
return result, nil
}