forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilebased.go
479 lines (384 loc) · 12.4 KB
/
filebased.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
Velociraptor - Dig Deeper
Copyright (C) 2019-2024 Rapid7 Inc.
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/>.
*/
// This is a file based data store. It is the default data store we
// use for both small and large deployments. The datastore is only
// used for storing local metadata and uses no locking:
// Object IO is considered atomic - there are no locks. This can
// result in races for contentended objects but the Velociraptor
// design avoids file contention at all times.
// Files can be written as protobuf encoding (this is the old
// standard) but this makes it harder to debug so now most files will
// be written as json encoded blobs. There is fallback code to be able
// to read only protobuf encoded files if they are there but newer
// files will be written as JSON.
package datastore
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"sync"
"github.com/go-errors/errors"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
"www.velocidex.com/golang/velociraptor/constants"
"www.velocidex.com/golang/velociraptor/file_store/api"
"www.velocidex.com/golang/velociraptor/file_store/path_specs"
"www.velocidex.com/golang/velociraptor/logging"
"www.velocidex.com/golang/velociraptor/utils"
)
var (
file_based_imp = &FileBaseDataStore{}
datastoreNotConfiguredError = errors.New("Datastore not configured")
invalidFileError = errors.New("Invalid file error")
insufficientDiskSpace = errors.New("Insufficient disk space!")
)
const (
// On windows all file paths must be prefixed by this to
// support long paths.
WINDOWS_LFN_PREFIX = "\\\\?\\"
)
type FileBaseDataStore struct {
mu sync.Mutex
err error
}
/*
Gets a protobuf encoded struct from the data store. Objects are
addressed by the urn (URNs are typically managed by a path manager)
*/
func (self *FileBaseDataStore) GetSubject(
config_obj *config_proto.Config,
urn api.DSPathSpec,
message proto.Message) error {
defer InstrumentWithDelay("read", "FileBaseDataStore", urn)()
Trace(config_obj, "GetSubject", urn)
serialized_content, err := readContentFromFile(config_obj, urn)
if err != nil {
return fmt.Errorf("While opening %v: %w", urn.AsClientPath(),
os.ErrNotExist)
}
if len(serialized_content) == 0 {
// JSON encoded files must contain at least '{}' two
// characters. If the file is empty something went wrong -
// usually this is because the disk was full.
if urn.Type() == api.PATH_TYPE_DATASTORE_JSON {
return invalidFileError
}
return nil
}
// It is really a JSON blob
if serialized_content[0] == '{' {
err = protojson.Unmarshal(serialized_content, message)
} else {
err = proto.Unmarshal(serialized_content, message)
}
if err != nil {
return fmt.Errorf("While opening %v: %w",
urn.AsClientPath(), os.ErrNotExist)
}
return nil
}
func (self *FileBaseDataStore) Debug(config_obj *config_proto.Config) {
filepath.Walk(config_obj.Datastore.Location,
func(path string, info os.FileInfo, err error) error {
fmt.Printf("%v -> %v %v\n", path, info.Size(), info.Mode())
return nil
})
}
func (self *FileBaseDataStore) SetSubject(
config_obj *config_proto.Config,
urn api.DSPathSpec,
message proto.Message) error {
return self.SetSubjectWithCompletion(config_obj, urn, message, nil)
}
func (self *FileBaseDataStore) SetSubjectWithCompletion(
config_obj *config_proto.Config,
urn api.DSPathSpec,
message proto.Message, completion func()) error {
defer InstrumentWithDelay("write", "FileBaseDataStore", urn)()
err := self.Error()
if err != nil {
return err
}
// Make sure to call the completer on all exit points
// (FileBaseDataStore is actually synchronous).
defer func() {
if completion != nil &&
!utils.CompareFuncs(completion, utils.SyncCompleter) {
completion()
}
}()
Trace(config_obj, "SetSubject", urn)
// Encode as JSON
if urn.Type() == api.PATH_TYPE_DATASTORE_JSON {
serialized_content, err := protojson.Marshal(message)
if err != nil {
return err
}
return writeContentToFile(config_obj, urn, serialized_content)
}
serialized_content, err := proto.Marshal(message)
if err != nil {
return errors.Wrap(err, 0)
}
return writeContentToFile(config_obj, urn, serialized_content)
}
func (self *FileBaseDataStore) DeleteSubjectWithCompletion(
config_obj *config_proto.Config,
urn api.DSPathSpec, completion func()) error {
err := self.DeleteSubject(config_obj, urn)
if completion != nil &&
!utils.CompareFuncs(completion, utils.SyncCompleter) {
completion()
}
return err
}
func (self *FileBaseDataStore) DeleteSubject(
config_obj *config_proto.Config,
urn api.DSPathSpec) error {
defer InstrumentWithDelay("delete", "FileBaseDataStore", urn)()
Trace(config_obj, "DeleteSubject", urn)
err := os.Remove(urn.AsDatastoreFilename(config_obj))
// It is ok to remove a file that does not exist.
if err != nil && os.IsExist(err) {
return errors.Wrap(err, 0)
}
// Note: We do not currently remove empty intermediate
// directories.
return nil
}
func listChildNames(config_obj *config_proto.Config,
urn api.DSPathSpec) (
[]string, error) {
defer InstrumentWithDelay("list", "FileBaseDataStore", urn)()
return utils.ReadDirNames(
urn.AsDatastoreDirectory(config_obj))
}
func listChildren(config_obj *config_proto.Config,
urn api.DSPathSpec) ([]os.FileInfo, error) {
defer InstrumentWithDelay("list", "FileBaseDataStore", urn)()
children, err := utils.ReadDirUnsorted(
urn.AsDatastoreDirectory(config_obj))
if err != nil {
if os.IsNotExist(err) {
return []os.FileInfo{}, nil
}
return nil, errors.Wrap(err, 0)
}
max_dir_size := int(config_obj.Datastore.MaxDirSize)
if max_dir_size == 0 {
max_dir_size = 50000
}
if len(children) > max_dir_size {
logger := logging.GetLogger(config_obj, &logging.FrontendComponent)
logger.Error(
"listChildren: Encountered a large directory %v (%v files), "+
"truncating to %v", urn.AsClientPath(),
len(children), max_dir_size)
return children[:max_dir_size], nil
}
return children, nil
}
// Lists all the children of a URN.
func (self *FileBaseDataStore) ListChildren(
config_obj *config_proto.Config,
urn api.DSPathSpec) (
[]api.DSPathSpec, error) {
TraceDirectory(config_obj, "ListChildren", urn)
all_children, err := listChildren(config_obj, urn)
if err != nil {
return nil, err
}
// In the same directory we may have files and directories
children := make([]os.FileInfo, 0, len(all_children))
for _, child := range all_children {
if strings.HasSuffix(child.Name(), ".db") || child.IsDir() {
children = append(children, child)
}
}
// Sort entries by modified time.
sort.Slice(children, func(i, j int) bool {
return children[i].ModTime().UnixNano() < children[j].ModTime().UnixNano()
})
// Slice the result according to the required offset and count.
result := make([]api.DSPathSpec, 0, len(children))
for _, child := range children {
var child_pathspec api.DSPathSpec
if child.IsDir() {
name := utils.UnsanitizeComponent(child.Name())
result = append(result, urn.AddUnsafeChild(name).SetDir())
continue
}
// Strip data store extensions
spec_type, extension := api.GetDataStorePathTypeFromExtension(
child.Name())
if spec_type == api.PATH_TYPE_DATASTORE_UNKNOWN {
continue
}
name := utils.UnsanitizeComponent(child.Name()[:len(extension)])
// Skip over files that do not belong in the data store.
if spec_type == api.PATH_TYPE_DATASTORE_UNKNOWN {
continue
} else {
child_pathspec = urn.AddUnsafeChild(name).SetType(spec_type)
}
result = append(result, child_pathspec)
}
return result, nil
}
// Called to close all db handles etc. Not thread safe.
func (self *FileBaseDataStore) Close() {}
func writeContentToFile(config_obj *config_proto.Config,
urn api.DSPathSpec, data []byte) error {
if config_obj.Datastore == nil {
return datastoreNotConfiguredError
}
filename := urn.AsDatastoreFilename(config_obj)
// Truncate the file immediately so we dont need to make a seocnd
// syscall. Empirically on Linux, a truncate call always works,
// even if there is no available disk space to accommodate the
// required file size. This means we can not avoid file corruption
// when the disk is full! We may as well truncate to 0 on open and
// hope the file write succeeds later.
file, err := os.OpenFile(
filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0660)
// Try to create intermediate directories and try again.
if err != nil && os.IsNotExist(err) {
err = os.MkdirAll(filepath.Dir(filename), 0700)
if err != nil {
return err
}
file, err = os.OpenFile(
filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0660)
if err != nil {
return err
}
}
if err != nil {
logging.GetLogger(config_obj, &logging.FrontendComponent).Error(
"Unable to open file %v: %v", filename, err)
return errors.Wrap(err, 0)
}
defer file.Close()
_, err = file.Write(data)
if err != nil {
return errors.Wrap(err, 0)
}
return nil
}
func readContentFromFile(
config_obj *config_proto.Config, urn api.DSPathSpec) ([]byte, error) {
if config_obj.Datastore == nil {
return nil, datastoreNotConfiguredError
}
file, err := os.Open(urn.AsDatastoreFilename(config_obj))
if err == nil {
defer file.Close()
result, err := ioutil.ReadAll(
io.LimitReader(file, constants.MAX_MEMORY))
if err != nil {
return nil, errors.Wrap(err, 0)
}
return result, nil
}
// Try to read older protobuf based files for backwards
// compatibility.
if os.IsNotExist(err) &&
urn.Type() == api.PATH_TYPE_DATASTORE_JSON {
file, err := os.Open(urn.
SetType(api.PATH_TYPE_DATASTORE_PROTO).
AsDatastoreFilename(config_obj))
if err == nil {
defer file.Close()
result, err := ioutil.ReadAll(
io.LimitReader(file, constants.MAX_MEMORY))
if err != nil {
return nil, errors.Wrap(err, 0)
}
return result, nil
}
}
return nil, errors.Wrap(err, 0)
}
// Convert a file name from the data store to a DSPathSpec
func FilenameToURN(config_obj *config_proto.Config,
filename string) api.DSPathSpec {
if runtime.GOOS == "windows" {
filename = strings.TrimPrefix(filename, WINDOWS_LFN_PREFIX)
}
filename = strings.TrimPrefix(filename, config_obj.Datastore.Location)
components := []string{}
// DS filenames are always clean so a strings split is fine.
for _, component := range strings.Split(
filename, string(os.PathSeparator)) {
if component != "" {
components = append(components, component)
}
}
// Strip any extension from the last component.
if len(components) > 0 {
last := len(components) - 1
components[last] = strings.TrimPrefix(
strings.TrimSuffix(components[last], ".db"), ".json")
}
return path_specs.NewSafeDatastorePath(components...)
}
func Trace(config_obj *config_proto.Config,
name string, filename api.DSPathSpec) {
return
fmt.Printf("Trace FileBaseDataStore: %v: %v\n", name,
filename.AsDatastoreFilename(config_obj))
}
func TraceDirectory(config_obj *config_proto.Config,
name string, filename api.DSPathSpec) {
return
fmt.Printf("Trace FileBaseDataStore: %v: %v\n", name,
filename.AsDatastoreDirectory(config_obj))
}
// Support RawDataStore interface
func (self *FileBaseDataStore) GetBuffer(
config_obj *config_proto.Config,
urn api.DSPathSpec) ([]byte, error) {
return readContentFromFile(config_obj, urn)
}
func (self *FileBaseDataStore) Error() error {
self.mu.Lock()
defer self.mu.Unlock()
return self.err
}
func (self *FileBaseDataStore) SetError(err error) {
self.mu.Lock()
defer self.mu.Unlock()
self.err = err
}
func (self *FileBaseDataStore) SetBuffer(
config_obj *config_proto.Config,
urn api.DSPathSpec, data []byte, completion func()) error {
err := self.Error()
if err != nil {
return err
}
err = writeContentToFile(config_obj, urn, data)
if completion != nil &&
!utils.CompareFuncs(completion, utils.SyncCompleter) {
completion()
}
return err
}