forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
344 lines (280 loc) · 9.93 KB
/
fs.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
/*
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 main
import (
"io"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/Velocidex/ordereddict"
kingpin "gopkg.in/alecthomas/kingpin.v2"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
"www.velocidex.com/golang/velociraptor/file_store"
"www.velocidex.com/golang/velociraptor/file_store/api"
"www.velocidex.com/golang/velociraptor/glob"
logging "www.velocidex.com/golang/velociraptor/logging"
"www.velocidex.com/golang/velociraptor/reporting"
"www.velocidex.com/golang/velociraptor/services"
"www.velocidex.com/golang/velociraptor/uploads"
vql_subsystem "www.velocidex.com/golang/velociraptor/vql"
vfilter "www.velocidex.com/golang/vfilter"
)
var (
accessor_reg = regexp.MustCompile(
"^(file|ntfs|reg|registry|zip|raw_reg|lazy_ntfs|file_links|fs)://(.+)$")
fs_command = app.Command("fs", "Run filesystem commands.")
fs_command_accessor = fs_command.Flag(
"accessor", "The FS accessor to use").Default("file").String()
fs_command_verbose = fs_command.Flag(
"details", "Show more verbose info").Short('l').
Default("false").Bool()
fs_command_format = fs_command.Flag("format", "Output format to use (text,json,jsonl,csv).").
Default("jsonl").Enum("text", "json", "jsonl", "csv")
fs_command_ls = fs_command.Command("ls", "List files")
fs_command_ls_path = fs_command_ls.Arg(
"path", "The path or glob to list").Default("/").String()
fs_command_cp = fs_command.Command("cp", "Copy files to a directory.")
fs_command_cp_path = fs_command_cp.Arg(
"path", "The path or glob to list").Required().String()
fs_command_cp_outdir = fs_command_cp.Arg(
"dumpdir", "The directory to store files at.").Required().
String()
fs_command_cat = fs_command.Command("cat", "Dump a file to the terminal")
fs_command_cat_path = fs_command_cat.Arg(
"path", "The path to cat").Required().String()
fs_command_rm = fs_command.Command("rm", "Remove file (only filestore supported)")
fs_command_rm_path = fs_command_rm.Arg(
"path", "The path or glob to remove").Required().String()
)
func eval_query(
config_obj *config_proto.Config, format, query string, scope vfilter.Scope,
env *ordereddict.Dict) {
if config_obj.ApiConfig != nil && config_obj.ApiConfig.Name != "" {
logging.GetLogger(config_obj, &logging.ToolComponent).
Info("API Client configuration loaded - will make gRPC connection.")
doRemoteQuery(config_obj, format, []string{query}, env)
return
}
eval_local_query(config_obj, *fs_command_format, query, scope)
}
func eval_local_query(
config_obj *config_proto.Config, format string,
query string, scope vfilter.Scope) {
vqls, err := vfilter.MultiParse(query)
kingpin.FatalIfError(err, "Unable to parse VQL Query")
ctx := InstallSignalHandler(scope)
for _, vql := range vqls {
switch format {
case "text":
table := reporting.EvalQueryToTable(ctx, scope, vql, os.Stdout)
table.Render()
case "csv":
outputCSV(ctx, scope, vql, os.Stdout)
case "jsonl":
outputJSONL(ctx, scope, vql, os.Stdout)
case "json":
outputJSON(ctx, scope, vql, os.Stdout)
}
}
}
func doLS(path, accessor string) {
config_obj, err := APIConfigLoader.WithNullLoader().LoadAndValidate()
kingpin.FatalIfError(err, "Load Config ")
sm, err := startEssentialServices(config_obj)
kingpin.FatalIfError(err, "Starting services.")
defer sm.Close()
matches := accessor_reg.FindStringSubmatch(path)
if matches != nil {
accessor = matches[1]
path = matches[2]
}
if len(path) > 0 && (path[len(path)-1] == '/' ||
path[len(path)-1] == '\\') {
path += "*"
}
builder := services.ScopeBuilder{
Config: config_obj,
ACLManager: vql_subsystem.NullACLManager{},
Logger: log.New(os.Stderr, "velociraptor: ", 0),
Env: ordereddict.NewDict().
Set(vql_subsystem.ACL_MANAGER_VAR,
vql_subsystem.NewRoleACLManager("administrator")).
Set("accessor", accessor).
Set("path", path),
}
manager, err := services.GetRepositoryManager()
kingpin.FatalIfError(err, "GetRepositoryManager")
scope := manager.BuildScope(builder)
defer scope.Close()
query := "SELECT Name, Size, Mode.String AS Mode, Mtime, Data " +
"FROM glob(globs=path, accessor=accessor) "
if *fs_command_verbose {
query = strings.Replace(query, "Name", "FullPath", 1)
}
// Special handling for ntfs.
if !*fs_command_verbose && accessor == "ntfs" {
query += " WHERE Sys.name_type != 'DOS' "
}
eval_query(config_obj, *fs_command_format, query, scope, builder.Env)
}
func doRM(path, accessor string) {
config_obj, err := APIConfigLoader.WithNullLoader().LoadAndValidate()
kingpin.FatalIfError(err, "Load Config ")
sm, err := startEssentialServices(config_obj)
kingpin.FatalIfError(err, "Starting services.")
defer sm.Close()
matches := accessor_reg.FindStringSubmatch(path)
if matches != nil {
accessor = matches[1]
path = matches[2]
}
if len(path) > 0 && (path[len(path)-1] == '/' ||
path[len(path)-1] == '\\') {
path += "*"
}
if accessor != "fs" {
kingpin.Fatalf("Only fs:// URLs support removal")
}
builder := services.ScopeBuilder{
Config: config_obj,
ACLManager: vql_subsystem.NewRoleACLManager("administrator"),
Logger: log.New(os.Stderr, "velociraptor: ", 0),
Env: ordereddict.NewDict().
Set("accessor", accessor).
Set("path", path),
}
manager, err := services.GetRepositoryManager()
kingpin.FatalIfError(err, "GetRepositoryManager")
scope := manager.BuildScope(builder)
defer scope.Close()
query := "SELECT FullPath, Size, Mode.String AS Mode, Mtime, " +
"file_store_delete(path=FullPath) AS Deletion " +
"FROM glob(globs=path, accessor=accessor) "
eval_query(config_obj, *fs_command_format, query, scope, builder.Env)
}
func doCp(path, accessor string, dump_dir string) {
config_obj, err := APIConfigLoader.WithNullLoader().LoadAndValidate()
kingpin.FatalIfError(err, "Load Config ")
sm, err := startEssentialServices(config_obj)
kingpin.FatalIfError(err, "Starting services.")
defer sm.Close()
matches := accessor_reg.FindStringSubmatch(path)
if matches != nil {
accessor = matches[1]
path = matches[2]
}
if len(path) > 0 && (path[len(path)-1] == '/' ||
path[len(path)-1] == '\\') {
path += "*"
}
if accessor == "file" {
path, _ = filepath.Abs(path)
}
output_accessor := ""
output_path := dump_dir
matches = accessor_reg.FindStringSubmatch(dump_dir)
if matches != nil {
output_accessor = matches[1]
output_path = matches[2]
}
builder := services.ScopeBuilder{
Config: config_obj,
Logger: log.New(&LogWriter{config_obj}, "Velociraptor: ", 0),
Env: ordereddict.NewDict().
Set("accessor", accessor).
Set("path", path),
ACLManager: vql_subsystem.NewRoleACLManager("administrator"),
}
switch output_accessor {
case "", "file":
builder.Uploader = &uploads.FileBasedUploader{
UploadDir: output_path,
}
case "fs":
builder.Uploader = api.NewFileStoreUploader(
config_obj,
file_store.GetFileStore(config_obj),
output_path)
default:
kingpin.Fatalf("Can not write to accessor %v\n", output_accessor)
}
manager, err := services.GetRepositoryManager()
kingpin.FatalIfError(err, "GetRepositoryManager")
scope := manager.BuildScope(builder)
defer scope.Close()
scope.Log("Copy from %v (%v) to %v (%v)",
path, accessor, output_path, output_accessor)
eval_query(config_obj, *fs_command_format, `
SELECT * from foreach(
row={
SELECT Name, Size, Mode.String AS Mode,
Mtime, Data, FullPath
FROM glob(globs=path, accessor=accessor)
}, query={
SELECT Name, Size, Mode, Mtime, Data,
upload(file=FullPath, accessor=accessor, name=Name) AS Upload
FROM scope()
})`, scope, builder.Env)
}
func doCat(path, accessor_name string) {
_, err := APIConfigLoader.WithNullLoader().LoadAndValidate()
kingpin.FatalIfError(err, "Load Config ")
matches := accessor_reg.FindStringSubmatch(path)
if matches != nil {
accessor_name = matches[1]
path = matches[2]
}
scope := vql_subsystem.MakeScope()
accessor, err := glob.GetAccessor(accessor_name, scope)
kingpin.FatalIfError(err, "GetAccessor")
fd, err := accessor.Open(path)
kingpin.FatalIfError(err, "ReadFile")
_, err = io.Copy(os.Stdout, fd)
kingpin.FatalIfError(err, "Copy file")
}
// Install a fs accessor to enable access to the file store. But make
// it lazy - no need to connect to the file store un-neccesarily.
type FileStoreAccessorFactory struct {
config_obj *config_proto.Config
}
func (self FileStoreAccessorFactory) New(scope vfilter.Scope) (glob.FileSystemAccessor, error) {
return file_store.GetFileStoreFileSystemAccessor(self.config_obj)
}
// Only register the filesystem accessor if we have a proper valid server config.
func initFilestoreAccessor(config_obj *config_proto.Config) error {
if config_obj.Datastore != nil {
glob.Register("fs", &FileStoreAccessorFactory{config_obj})
}
return nil
}
func init() {
command_handlers = append(command_handlers, func(command string) bool {
switch command {
case fs_command_ls.FullCommand():
doLS(*fs_command_ls_path, *fs_command_accessor)
case fs_command_rm.FullCommand():
doRM(*fs_command_rm_path, *fs_command_accessor)
case fs_command_cp.FullCommand():
doCp(*fs_command_cp_path, *fs_command_accessor, *fs_command_cp_outdir)
case fs_command_cat.FullCommand():
doCat(*fs_command_cat_path, *fs_command_accessor)
default:
return false
}
return true
})
}