-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection_windows.go
206 lines (171 loc) · 4.31 KB
/
collection_windows.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
package main
import (
"archive/zip"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"www.velocidex.com/golang/go-ntfs/parser"
_ "embed"
)
//go:embed targets/default_windows.quack
var defaultQuack []byte
func DefaulRootPaths() []string {
return []string{
"C:\\",
}
}
func GetPathsRaw(cfg Configuration) ([]string, error) {
scanned := 0
matchers, paths, err := LoadMatchers(cfg)
if err != nil {
return nil, fmt.Errorf("load matchers: %w", err)
}
roots := cfg.CollectionRoots
if len(roots) == 0 {
roots = DefaulRootPaths()
}
for _, root := range roots {
driveLetter := filepath.VolumeName(root)
fd, err := os.Open("\\\\.\\" + driveLetter)
if err != nil {
return nil, fmt.Errorf("open drive: %s: %w", root, err)
}
defer fd.Close()
drive := NewSectorReaderAt(fd, 512)
ntfs, err := parser.GetNTFSContext(drive, 0)
if err != nil {
return nil, fmt.Errorf("get ntfs context: %w", err)
}
mft, err := ntfs.GetMFT(5)
if err != nil {
return nil, fmt.Errorf("get mft 5: %w", err)
}
mft, err = mft.Open(ntfs, "")
if err != nil {
return nil, fmt.Errorf("open mft: %w", err)
}
err = walkDirRaw(ntfs, root, mft, func(path string, info *parser.FileInfo, err error) error {
if err != nil {
WarnLogger.Printf("traverse | %v", err)
return fs.SkipDir
}
scanned++
pathTrimmed := strings.TrimPrefix(path, root)
if !info.IsDir {
for _, match := range matchers {
if match(path) || match(pathTrimmed) {
paths = append(paths, path)
break
}
}
}
return nil
})
if err != nil {
return paths, err
}
}
InfoLogger.Printf("traverse | scanned %d paths, resulted in %d files to collect", scanned, len(paths))
return paths, err
}
func CollectFileRaw(cfg Configuration, archive *zip.Writer, path string) error {
rel, err := filepath.Rel(filepath.VolumeName(path)+"/", filepath.ToSlash(path))
if err != nil {
return err
}
driveLetter := filepath.VolumeName(path)
fd, err := os.Open("\\\\.\\" + driveLetter)
if err != nil {
return fmt.Errorf("open drive: %s: %w", path, err)
}
defer fd.Close()
drive := NewSectorReaderAt(fd, 512)
ntfsCtx, err := parser.GetNTFSContext(drive, 0)
if err != nil {
return fmt.Errorf("get ntfs context: %w", err)
}
r, err := parser.GetDataForPath(ntfsCtx, rel)
if err != nil {
return fmt.Errorf("get data stream: %w", err)
}
fh, err := archive.Create(rel)
if err != nil {
return fmt.Errorf("add file to archive: %w", err)
}
buf := make([]byte, 1024*1024*10)
offset := int64(0)
for {
n, err := r.ReadAt(buf, offset)
if n == 0 || err != nil {
if err == nil || errors.Is(err, io.EOF) {
return nil
}
return fmt.Errorf("read from disk: %w", err)
}
_, err = fh.Write(buf[:n])
if err != nil {
return fmt.Errorf("write to archive: %w", err)
}
offset += int64(n)
}
}
type SectorReaderAt struct {
r io.ReaderAt
sectorSize int
}
func NewSectorReaderAt(r io.ReaderAt, sectorSize int) *SectorReaderAt {
return &SectorReaderAt{r: r, sectorSize: sectorSize}
}
func (r *SectorReaderAt) ReadAt(p []byte, off int64) (int, error) {
sector := int(off) / r.sectorSize
sectorOff := int64(sector) * int64(r.sectorSize)
misalignment := int(off) % r.sectorSize
size := roundUp(len(p)+int(misalignment), r.sectorSize)
buf := make([]byte, size)
n, err := r.r.ReadAt(buf, sectorOff)
copy(p, buf[misalignment:])
return n, err
}
func roundUp(num int, multiple int) int {
if multiple == 0 {
return num
}
remainder := num % multiple
if remainder == 0 {
return num
}
return num + multiple - remainder
}
func walkDirRaw(ctx *parser.NTFSContext, path string, mft *parser.MFT_ENTRY, walkDirFn func(path string, d *parser.FileInfo, err error) error) error {
fi := parser.Stat(ctx, mft)[0]
if err := walkDirFn(path, fi, nil); err != nil || !fi.IsDir {
if err == fs.SkipDir && fi.IsDir {
// Successfully skipped directory.
err = nil
}
return err
}
records := mft.Dir(ctx)
for _, r1 := range records {
path1 := filepath.Join(path, r1.File().Name())
if r1.File().Name() == "" || path == path1 {
// avoid infite loop
continue
}
mft1, err := ctx.GetMFT(int64(r1.MftReference()))
if err != nil {
return err
}
if err := walkDirRaw(ctx, path1, mft1, walkDirFn); err != nil {
if err == fs.SkipDir {
break
}
return err
}
}
return nil
}