forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanipulators.go
709 lines (597 loc) · 17.2 KB
/
manipulators.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
package accessors
import (
"fmt"
"regexp"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"www.velocidex.com/golang/velociraptor/json"
"www.velocidex.com/golang/velociraptor/utils"
)
var (
osPathSerializations = promauto.NewCounter(prometheus.CounterOpts{
Name: "ospath_serialization_count",
Help: "Number of times an os path is serialized.",
})
osPathUnserializations = promauto.NewCounter(prometheus.CounterOpts{
Name: "ospath_unserialization_count",
Help: "Number of times an os path is unserialized.",
})
)
// This is a generic Path manipulator that implements the escaping
// standard as used by Velociraptor:
// 1. Path separators are / but will be able to use \\ to parse.
// 2. Each component is optionally quoted if it contains special
// characters (like path separators).
type GenericPathManipulator struct {
Sep string
}
func (self GenericPathManipulator) PathParse(path string, result *OSPath) error {
osPathUnserializations.Inc()
err := maybeParsePathSpec(path, result)
if err != nil {
return err
}
result.Components = utils.SplitComponents(result.pathspec.Path)
return nil
}
func (self GenericPathManipulator) AsPathSpec(path *OSPath) *PathSpec {
// Make a copy of the pathspec.
var result PathSpec
if path.pathspec != nil {
result = *path.pathspec
}
components := path.Components
sep := self.Sep
if sep == "" {
sep = "/"
}
result.Path = utils.JoinComponents(components, sep)
return &result
}
func (self GenericPathManipulator) PathJoin(path *OSPath) string {
osPathSerializations.Inc()
result := self.AsPathSpec(path)
if result.GetDelegateAccessor() == "" && result.GetDelegatePath() == "" {
return result.Path
}
return result.String()
}
// Like NewGenericOSPath but panics if an error
func MustNewGenericOSPath(path string) *OSPath {
res, err := NewGenericOSPath(path)
if err != nil {
panic(err)
}
return res
}
func MustNewGenericOSPathWithBackslashSeparator(path string) *OSPath {
manipulator := GenericPathManipulator{Sep: "\\"}
result := &OSPath{
Manipulator: manipulator,
}
err := manipulator.PathParse(path, result)
if err != nil {
panic(err)
}
return result
}
func NewGenericOSPath(path string) (*OSPath, error) {
manipulator := GenericPathManipulator{}
result := &OSPath{
Manipulator: manipulator,
}
err := manipulator.PathParse(path, result)
return result, err
}
// Responsible for serialization of linux paths
type LinuxPathManipulator struct{ GenericPathManipulator }
func (self LinuxPathManipulator) PathParse(path string, result *OSPath) error {
osPathUnserializations.Inc()
err := maybeParsePathSpec(path, result)
if err != nil {
return err
}
path = result.pathspec.Path
components := strings.Split(path, "/")
result.Components = make([]string, 0, len(components))
for _, c := range components {
if c == "" || c == "." || c == ".." {
continue
}
result.Components = append(result.Components, c)
}
return nil
}
func (self LinuxPathManipulator) AsPathSpec(path *OSPath) *PathSpec {
result := path.pathspec
if result == nil {
result = &PathSpec{}
path.pathspec = result
} else {
result = result.Copy()
}
result.Path = "/" + strings.Join(path.Components, "/")
return result
}
func MustNewLinuxOSPath(path string) *OSPath {
res, err := NewLinuxOSPath(path)
if err != nil {
panic(err)
}
return res
}
func NewLinuxOSPath(path string) (*OSPath, error) {
manipulator := LinuxPathManipulator{}
result := &OSPath{
pathspec: &PathSpec{},
Manipulator: manipulator,
}
err := manipulator.PathParse(path, result)
return result, err
}
var (
// For convenience we transform paths like c:\Windows -> \\.\c:\Windows
driveRegex = regexp.MustCompile(
`(?i)^[/\\]?([a-z]:)(.*)`)
// https://docs.microsoft.com/en-us/dotnet/standard/io/file-path-formats#unc-paths
uncRegex = regexp.MustCompile(
`(?i)^(\\\\[^\\]+)\\(.*)`)
deviceDriveRegex = regexp.MustCompile(
`(?i)^(\\\\[\?\.]\\[a-zA-Z]:)(.*)`)
deviceDirectoryRegex = regexp.MustCompile(
`(?i)^(\\\\[\?\.]\\GLOBALROOT\\Device\\[^/\\]+)([/\\]?.*)`)
)
// Breaks a client path into components. The client's path may consist
// of a drive letter or a device which will be treated as a single
// component. For example:
// C:\Windows -> "C:\", "Windows"
// \\.\c:\Windows -> "\\.\C:", "Windows"
// We also support UNC paths like:
// \\hostname\path\to\file -> "\\hostname", "path", "to", "file"
// Other components that contain path separators need to be properly
// quoted as usual:
// HKEY_LOCAL_MACHINE\Software\Microsoft\"http://www.google.com"\Foo ->
// "HKEY_LOCAL_MACHINE", "Software", "Microsoft", "http://www.google.com", "Foo"
type WindowsPathManipulator struct{ GenericPathManipulator }
func (self WindowsPathManipulator) PathParse(path string, result *OSPath) error {
osPathUnserializations.Inc()
err := maybeParsePathSpec(path, result)
if err != nil {
return err
}
path = result.pathspec.Path
m := deviceDriveRegex.FindStringSubmatch(path)
if len(m) != 0 {
result.Components = append([]string{m[1]}, utils.SplitComponents(m[2])...)
return nil
}
m = deviceDirectoryRegex.FindStringSubmatch(path)
if len(m) != 0 {
result.Components = append([]string{m[1]}, utils.SplitComponents(m[2])...)
return nil
}
m = uncRegex.FindStringSubmatch(path)
if len(m) != 0 {
result.Components = append([]string{m[1]}, utils.SplitComponents(m[2])...)
return nil
}
result.Components = utils.SplitComponents(path)
return nil
}
func (self WindowsPathManipulator) PathJoin(path *OSPath) string {
osPathSerializations.Inc()
result := self.AsPathSpec(path)
if result.GetDelegateAccessor() == "" && result.GetDelegatePath() == "" {
return result.Path
}
return result.String()
}
func (self WindowsPathManipulator) AsPathSpec(path *OSPath) *PathSpec {
result := path.pathspec
if result == nil {
result = &PathSpec{}
path.pathspec = result
}
// The first component is usually the drive letter or device and
// although it can contain path separators it must not be quoted
components := path.Components
if len(components) > 0 {
// No leading \\ as first component is drive letter
result.Path = components[0] + utils.JoinComponents(components[1:], "\\")
} else {
result.Path = ""
}
return result
}
func MustNewWindowsOSPath(path string) *OSPath {
res, err := NewWindowsOSPath(path)
if err != nil {
panic(err)
}
return res
}
func NewWindowsOSPath(path string) (*OSPath, error) {
manipulator := WindowsPathManipulator{}
result := &OSPath{
Manipulator: manipulator,
}
err := manipulator.PathParse(path, result)
return result, err
}
// Handle device paths especially.
type WindowsNTFSManipulator struct{ WindowsPathManipulator }
func (self WindowsNTFSManipulator) PathParse(path string, result *OSPath) error {
err := self.WindowsPathManipulator.PathParse(path, result)
if err != nil {
return err
}
// Drive names are stored as devices in the ntfs accessors. So if
// a user specifies open C:\Windows, we automatically open the
// \\.\C: device
if len(result.Components) > 0 &&
driveRegex.MatchString(result.Components[0]) {
// Drive names should be uppercased
result.Components[0] = "\\\\.\\" + strings.ToUpper(result.Components[0])
}
return nil
}
func ConvertToDevice(component string) string {
if driveRegex.MatchString(component) {
return "\\\\.\\" + strings.ToUpper(component)
}
return component
}
func (self WindowsNTFSManipulator) AsPathSpec(path *OSPath) *PathSpec {
result := path.pathspec
if result == nil {
result = &PathSpec{}
path.pathspec = result
} else {
result = result.Copy()
}
// The first component is usually the drive letter or device and
// although it can contain path separators it must not be quoted
components := path.Components
switch len(components) {
case 0:
return result
case 1:
result.Path = components[0]
default:
// No leading \\ as first component is drive letter
result.Path = components[0] + utils.JoinComponents(components[1:], "\\")
}
return result
}
func (self WindowsNTFSManipulator) PathJoin(path *OSPath) string {
osPathSerializations.Inc()
result := self.AsPathSpec(path)
if result.GetDelegateAccessor() == "" && result.GetDelegatePath() == "" {
return result.Path
}
return result.String()
}
func MustNewWindowsNTFSPath(path string) *OSPath {
res, err := NewWindowsNTFSPath(path)
if err != nil {
panic(err)
}
return res
}
func NewWindowsNTFSPath(path string) (*OSPath, error) {
manipulator := WindowsNTFSManipulator{}
result := &OSPath{
Manipulator: manipulator,
}
err := manipulator.PathParse(path, result)
return result, err
}
func WindowsNTFSPathFromOSPath(path *OSPath) *OSPath {
result := &OSPath{
Manipulator: WindowsNTFSManipulator{},
Components: make([]string, 0, len(path.Components)),
}
for i, component := range path.Components {
if i == 0 {
result.Components = append(result.Components,
ConvertToDevice(component))
} else {
result.Components = append(result.Components, component)
}
}
return result
}
// Windows registry paths begin with a hive name. There are a number
// of abbreviations for the hive names and we want to standardize.
type WindowsRegistryPathManipulator struct{ GenericPathManipulator }
func (self WindowsRegistryPathManipulator) AsPathSpec(path *OSPath) *PathSpec {
result := path.pathspec
if result == nil {
result = &PathSpec{}
path.pathspec = result
}
// The first component is usually the drive letter or device and
// although it can contain path separators it must not be quoted
components := path.Components
result.Path = strings.TrimPrefix(utils.JoinComponents(components, "\\"), "\\")
return result
}
func (self WindowsRegistryPathManipulator) PathJoin(path *OSPath) string {
osPathSerializations.Inc()
result := self.AsPathSpec(path)
if result.GetDelegateAccessor() == "" && result.GetDelegatePath() == "" {
return result.Path
}
return result.String()
}
func (self WindowsRegistryPathManipulator) PathParse(
path string, result *OSPath) error {
osPathUnserializations.Inc()
err := maybeParsePathSpec(path, result)
if err != nil {
return err
}
result.Components = utils.SplitComponents(result.pathspec.Path)
if len(result.Components) > 0 {
// First component is usually a hive name in upper case.
hive_name := result.Components[0]
hive_name_caps := strings.ToUpper(result.Components[0])
switch hive_name_caps {
case "HKCU":
hive_name = "HKEY_CURRENT_USER"
case "HKLM":
hive_name = "HKEY_LOCAL_MACHINE"
case "HKU":
hive_name = "HKEY_USERS"
default:
if strings.HasPrefix(hive_name, "HKEY_") {
hive_name = hive_name_caps
}
}
result.Components[0] = hive_name
}
return nil
}
func MustNewWindowsRegistryPath(path string) *OSPath {
res, err := NewWindowsRegistryPath(path)
if err != nil {
panic(err)
}
return res
}
func NewWindowsRegistryPath(path string) (*OSPath, error) {
manipulator := WindowsRegistryPathManipulator{}
result := &OSPath{
Manipulator: manipulator,
}
err := manipulator.PathParse(path, result)
return result, err
}
// Raw pathspec paths expect the path to be a json encoded PathSpec
// object. They do not have any special interpretation of the Path
// parameter and so they do not break it up at all. These are used in
// very limited situations when we do not want to represent
// hierarchical data at all.
type PathSpecPathManipulator struct{ GenericPathManipulator }
func (self PathSpecPathManipulator) PathParse(path string, result *OSPath) error {
osPathUnserializations.Inc()
pathspec, err := PathSpecFromString(path)
if err != nil {
return err
}
result.pathspec = pathspec
result.Components = []string{pathspec.Path}
return nil
}
func (self PathSpecPathManipulator) AsPathSpec(path *OSPath) *PathSpec {
result := path.pathspec
if result == nil {
result = &PathSpec{}
path.pathspec = result
} else {
result = result.Copy()
}
return result
}
func (self PathSpecPathManipulator) PathJoin(path *OSPath) string {
osPathSerializations.Inc()
if path.pathspec != nil {
return path.pathspec.String()
}
if len(path.Components) == 1 {
return path.Components[0]
}
return ""
}
func MustNewPathspecOSPath(path string) *OSPath {
res, err := NewPathspecOSPath(path)
if err != nil {
panic(err)
}
return res
}
func NewPathspecOSPath(path string) (*OSPath, error) {
manipulator := PathSpecPathManipulator{}
result := &OSPath{
Manipulator: manipulator,
}
err := manipulator.PathParse(path, result)
return result, err
}
func maybeParsePathSpec(path string, result *OSPath) error {
if strings.HasPrefix(path, "{") {
pathspec := &PathSpec{}
err := json.Unmarshal([]byte(path), pathspec)
if err != nil {
return fmt.Errorf("While decoding pathspec: %w", err)
}
result.pathspec = pathspec
return nil
}
result.pathspec = &PathSpec{
Path: path,
}
return nil
}
// Windows registry paths begin with a hive name. There are a number
// of abbreviations for the hive names and we want to standardize.
type FileStorePathManipulator struct{}
func (self FileStorePathManipulator) AsPathSpec(path *OSPath) *PathSpec {
result := path.pathspec
if result == nil {
result = &PathSpec{}
path.pathspec = result
} else {
result = result.Copy()
}
// The first component is usually the drive letter or device and
// although it can contain path separators it must not be quoted
components := path.Components
result.Path = strings.TrimPrefix(utils.JoinComponents(components, "/"), "/")
return result
}
func (self FileStorePathManipulator) PathJoin(path *OSPath) string {
osPathSerializations.Inc()
return path.pathspec.DelegatePath + utils.JoinComponents(path.Components, "/")
}
func (self FileStorePathManipulator) PathParse(
path string, result *OSPath) error {
osPathUnserializations.Inc()
err := maybeParsePathSpec(path, result)
if err != nil {
return err
}
result.Components = utils.SplitComponents(result.pathspec.Path)
if len(result.Components) > 0 {
if result.Components[0] == "fs:" {
result.Components = result.Components[1:]
result.pathspec = &PathSpec{
DelegateAccessor: "fs",
DelegatePath: "fs:",
}
return nil
}
if result.Components[0] == "ds:" {
result.Components = result.Components[1:]
result.pathspec = &PathSpec{
DelegateAccessor: "fs",
DelegatePath: "ds:",
}
return nil
}
}
result.pathspec = &PathSpec{
DelegateAccessor: "fs",
DelegatePath: "fs:",
}
return nil
}
// Like NewGenericOSPath but panics if an error
func MustNewFileStorePath(path string) *OSPath {
res, err := NewFileStorePath(path)
if err != nil {
panic(err)
}
return res
}
func NewFileStorePath(path string) (*OSPath, error) {
manipulator := &FileStorePathManipulator{}
result := &OSPath{
Manipulator: manipulator,
}
err := manipulator.PathParse(path, result)
return result, err
}
// The OSPath object for raw files is unchanged - We must pass exactly
// the same form as given to the underlying filesystem APIs. On
// Windows this is some kind of device description like
// \\?\GLOBALROOT\Device\Harddisk0\DR0 for example, but we never
// attempt to parse it - just forward to the API as is.
type RawFileManipulator struct{}
func (self RawFileManipulator) AsPathSpec(path *OSPath) *PathSpec {
result := &PathSpec{}
if len(path.Components) == 0 {
return result
}
result.Path = path.Components[0]
return result
}
func (self RawFileManipulator) PathJoin(path *OSPath) string {
if len(path.Components) == 0 {
return ""
}
return path.Components[0]
}
func (self RawFileManipulator) PathParse(
path string, result *OSPath) error {
result.Components = []string{path}
return nil
}
func NewRawFilePath(path string) (*OSPath, error) {
manipulator := &RawFileManipulator{}
return &OSPath{
Components: []string{path},
Manipulator: manipulator,
}, nil
}
// Represent files inside the zip file for the offline collector -
// Similar to LinuxPathManipulator except that extra escaping is used
// to avoid more characters.
type ZipFileManipulator struct{}
func (self ZipFileManipulator) AsPathSpec(path *OSPath) *PathSpec {
result := path.pathspec
if result == nil {
result = &PathSpec{}
path.pathspec = result
}
components := make([]string, 0, len(path.Components))
for _, c := range path.Components {
if c != "" {
components = append(components, utils.SanitizeString(c))
}
}
result.Path = "/" + strings.Join(components, "/")
return result
}
func (self ZipFileManipulator) PathJoin(path *OSPath) string {
components := []string{}
for _, c := range path.Components {
components = append(components, utils.SanitizeStringForZip(c))
}
return "/" + strings.Join(components, "/")
}
func (self ZipFileManipulator) PathParse(
path string, result *OSPath) error {
osPathUnserializations.Inc()
err := maybeParsePathSpec(path, result)
if err != nil {
return err
}
path = result.pathspec.Path
components := strings.Split(path, "/")
result.Components = make([]string, 0, len(components))
for _, c := range components {
if c == "" || c == "." || c == ".." {
continue
}
result.Components = append(result.Components,
utils.UnsanitizeComponentForZip(c))
}
return nil
}
func NewZipFilePath(path string) (*OSPath, error) {
manipulator := &ZipFileManipulator{}
result := &OSPath{
Manipulator: manipulator,
}
err := manipulator.PathParse(path, result)
return result, err
}
func MustNewZipFilePath(path string) *OSPath {
res, err := NewZipFilePath(path)
if err != nil {
panic(err)
}
return res
}