forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv.go
82 lines (65 loc) · 2.05 KB
/
csv.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
package main
import (
"log"
"os"
"github.com/Velocidex/ordereddict"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"www.velocidex.com/golang/velociraptor/reporting"
"www.velocidex.com/golang/velociraptor/services"
vql_subsystem "www.velocidex.com/golang/velociraptor/vql"
"www.velocidex.com/golang/vfilter"
)
var (
csv_cmd = app.Command("csv", "Convert a CSV file to another format")
csv_cmd_filter = csv_cmd.Flag("where", "A WHERE condition for the query").String()
csv_format = csv_cmd.Flag("format", "Output format").
Default("jsonl").Enum("text", "json", "jsonl")
csv_cmd_files = csv_cmd.Arg("files", "CSV files to parse").Required().Strings()
)
func doCSV() {
config_obj, err := makeDefaultConfigLoader().WithNullLoader().LoadAndValidate()
kingpin.FatalIfError(err, "Load Config ")
sm, err := startEssentialServices(config_obj)
kingpin.FatalIfError(err, "Starting services.")
defer sm.Close()
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("Files", *csv_cmd_files),
}
manager, err := services.GetRepositoryManager()
kingpin.FatalIfError(err, "GetRepositoryManager")
scope := manager.BuildScope(builder)
defer scope.Close()
query := "SELECT * FROM parse_csv(filename=Files)"
if *csv_cmd_filter != "" {
query += " WHERE " + *csv_cmd_filter
}
vql, err := vfilter.Parse(query)
kingpin.FatalIfError(err, "Unable to parse VQL Query")
ctx := InstallSignalHandler(scope)
switch *csv_format {
case "text":
table := reporting.EvalQueryToTable(ctx, scope, vql, os.Stdout)
table.Render()
case "jsonl":
outputJSONL(ctx, scope, vql, os.Stdout)
case "json":
outputJSON(ctx, scope, vql, os.Stdout)
}
}
func init() {
command_handlers = append(command_handlers, func(command string) bool {
switch command {
case csv_cmd.FullCommand():
doCSV()
default:
return false
}
return true
})
}