forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.go
379 lines (315 loc) · 8.24 KB
/
container.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package reporting
import (
"compress/flate"
"context"
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"io"
"os"
"path"
"strings"
"time"
"github.com/Velocidex/ordereddict"
"github.com/alexmullins/zip"
"github.com/pkg/errors"
"www.velocidex.com/golang/velociraptor/actions"
actions_proto "www.velocidex.com/golang/velociraptor/actions/proto"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
"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/uploads"
"www.velocidex.com/golang/velociraptor/utils"
vql_subsystem "www.velocidex.com/golang/velociraptor/vql"
"www.velocidex.com/golang/vfilter"
concurrent_zip "github.com/Velocidex/zip"
)
type Container struct {
// The underlying file writer
fd io.WriteCloser
level int
// We write data to this zip file using the concurrent zip
// implementation.
zip *concurrent_zip.Writer
// If a password is set, we create a new zip file here, and a
// member within it then redirect the zip above to write on
// it.
delegate_zip *zip.Writer
delegate_fd io.Writer
}
func (self *Container) Create(name string, mtime time.Time) (io.WriteCloser, error) {
header := &concurrent_zip.FileHeader{
Name: name,
Method: concurrent_zip.Deflate,
Modified: mtime,
}
if self.level == 0 {
header.Method = concurrent_zip.Store
}
return self.zip.CreateHeader(header)
}
func (self *Container) StoreArtifact(
config_obj *config_proto.Config,
ctx context.Context,
scope vfilter.Scope,
query *actions_proto.VQLRequest,
format string) (err error) {
query_log := actions.QueryLog.AddQuery(query.VQL)
defer query_log.Close()
vql, err := vfilter.Parse(query.VQL)
if err != nil {
return err
}
artifact_name := query.Name
// Dont store un-named queries but run them anyway.
if artifact_name == "" {
for range vql.Eval(ctx, scope) {
}
return nil
}
// The name to use in the zip file to store results from this artifact
path_manager := paths.NewContainerPathManager(artifact_name)
fd, err := self.Create(path_manager.Path(), time.Time{})
if err != nil {
return err
}
// Preserve the error for our caller.
defer func() {
err_ := fd.Close()
if err == nil {
err = err_
}
}()
// Optionally include CSV in the output
var csv_writer *csv.CSVWriter
if format == "csv" {
csv_fd, err := self.Create(path_manager.CSVPath(), time.Time{})
if err != nil {
return err
}
csv_writer = csv.GetCSVAppender(
scope, csv_fd, true /* write_headers */)
// Preserve the error for our caller.
defer func() {
csv_writer.Close()
err_ := csv_fd.Close()
if err == nil {
err = err_
}
}()
}
// Store as line delimited JSON
marshaler := vql_subsystem.MarshalJsonl(scope)
for row := range vql.Eval(ctx, scope) {
// Re-serialize it as compact json.
serialized, err := marshaler([]vfilter.Row{row})
if err != nil {
continue
}
_, err = fd.Write(serialized)
if err != nil {
return errors.WithStack(err)
}
if csv_writer != nil {
csv_writer.Write(row)
}
}
return nil
}
func sanitize_upload_name(store_as_name string) string {
components := []string{}
// Normalize and clean up the path so the zip file is more
// usable by fragile zip programs like Windows explorer.
for _, component := range utils.SplitComponents(store_as_name) {
if component == "." || component == ".." {
continue
}
components = append(components, sanitize(component))
}
// Zip members must not have absolute paths.
return path.Join(components...)
}
func sanitize(component string) string {
component = strings.Replace(component, ":", "", -1)
component = strings.Replace(component, "?", "", -1)
return component
}
func (self *Container) Upload(
ctx context.Context,
scope vfilter.Scope,
filename string,
accessor string,
store_as_name string,
expected_size int64,
mtime time.Time,
atime time.Time,
ctime time.Time,
btime time.Time,
reader io.Reader) (*api.UploadResponse, error) {
if store_as_name == "" {
store_as_name = accessor + "/" + filename
}
sanitized_name := sanitize_upload_name(store_as_name)
scope.Log("Collecting file %s into %s (%v bytes)",
filename, store_as_name, expected_size)
// Try to collect sparse files if possible
result, err := self.maybeCollectSparseFile(
ctx, reader, store_as_name, sanitized_name, mtime)
if err == nil {
return result, nil
}
writer, err := self.Create(sanitized_name, mtime)
if err != nil {
return nil, err
}
defer writer.Close()
sha_sum := sha256.New()
md5_sum := md5.New()
n, err := utils.Copy(ctx, utils.NewTee(writer, sha_sum, md5_sum), reader)
if err != nil {
return &api.UploadResponse{
Error: err.Error(),
}, err
}
return &api.UploadResponse{
Path: sanitized_name,
Size: uint64(n),
Sha256: hex.EncodeToString(sha_sum.Sum(nil)),
Md5: hex.EncodeToString(md5_sum.Sum(nil)),
}, nil
}
func (self *Container) maybeCollectSparseFile(
ctx context.Context,
reader io.Reader, store_as_name, sanitized_name string, mtime time.Time) (
*api.UploadResponse, error) {
// Can the reader produce ranges?
range_reader, ok := reader.(uploads.RangeReader)
if !ok {
return nil, errors.New("Not supported")
}
writer, err := self.Create(sanitized_name, mtime)
if err != nil {
return nil, err
}
defer writer.Close()
sha_sum := sha256.New()
md5_sum := md5.New()
// The byte count we write to the output file.
count := 0
// An index array for sparse files.
index := []*ordereddict.Dict{}
is_sparse := false
for _, rng := range range_reader.Ranges() {
file_length := rng.Length
if rng.IsSparse {
file_length = 0
}
index = append(index, ordereddict.NewDict().
Set("file_offset", count).
Set("original_offset", rng.Offset).
Set("file_length", file_length).
Set("length", rng.Length))
if rng.IsSparse {
is_sparse = true
continue
}
_, err = range_reader.Seek(rng.Offset, io.SeekStart)
if err != nil {
return &api.UploadResponse{
Error: err.Error(),
}, err
}
n, err := utils.CopyN(ctx, utils.NewTee(writer, sha_sum, md5_sum),
range_reader, rng.Length)
if err != nil {
return &api.UploadResponse{
Error: err.Error(),
}, err
}
count += n
}
// If there were any sparse runs, create an index.
if is_sparse {
writer, err := self.Create(sanitized_name+".idx", time.Time{})
if err != nil {
return nil, err
}
defer writer.Close()
serialized, err := utils.DictsToJson(index, nil)
if err != nil {
return &api.UploadResponse{
Error: err.Error(),
}, err
}
_, err = writer.Write(serialized)
if err != nil {
return &api.UploadResponse{
Error: err.Error(),
}, err
}
}
return &api.UploadResponse{
Path: sanitized_name,
Size: uint64(count),
Sha256: hex.EncodeToString(sha_sum.Sum(nil)),
Md5: hex.EncodeToString(md5_sum.Sum(nil)),
}, nil
}
func (self *Container) Close() error {
self.zip.Close()
if self.delegate_zip != nil {
self.delegate_zip.Close()
}
return self.fd.Close()
}
func NewContainer(path string, password string, level int64) (*Container, error) {
fd, err := os.OpenFile(
path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return nil, err
}
if level < 0 || level > 9 {
level = 5
}
result := &Container{
fd: fd,
level: int(level),
}
// We need to build a protected container.
if password != "" {
result.delegate_zip = zip.NewWriter(fd)
// We are writing a zip file into here - no need to
// compress.
fh := &zip.FileHeader{
Name: "data.zip",
Method: zip.Store,
}
fh.SetPassword(password)
result.delegate_fd, err = result.delegate_zip.CreateHeader(fh)
if err != nil {
return nil, err
}
result.zip = concurrent_zip.NewWriter(result.delegate_fd)
} else {
result.zip = concurrent_zip.NewWriter(result.fd)
result.zip.RegisterCompressor(
zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
return flate.NewWriter(out, int(level))
})
}
return result, nil
}
// Turns os.Stdout into into file_store.WriteSeekCloser
type StdoutWrapper struct {
io.Writer
}
func (self *StdoutWrapper) Seek(offset int64, whence int) (int64, error) {
return 0, nil
}
func (self *StdoutWrapper) Close() error {
return nil
}
func (self *StdoutWrapper) Truncate(offset int64) error {
return nil
}