forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvql.go
337 lines (282 loc) · 8.14 KB
/
vql.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
/*
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 (
"fmt"
"io/ioutil"
"regexp"
"sort"
"strings"
"github.com/Velocidex/yaml/v2"
kingpin "gopkg.in/alecthomas/kingpin.v2"
api_proto "www.velocidex.com/golang/velociraptor/api/proto"
vql_subsystem "www.velocidex.com/golang/velociraptor/vql"
"www.velocidex.com/golang/vfilter/types"
)
var (
vql_info = app.Command("vql", "Show information about the VQL subsystem")
vql_info_list = vql_info.Command("list", "Print all VQL plugins and functions")
vql_info_export = vql_info.Command("export", "Export a YAML file with all the VQL plugins/function descriptions")
vql_info_export_old_file = vql_info_export.Arg(
"old_file", "Previous description file will contain additional descriptions").
File()
doc_regex = regexp.MustCompile("doc=(.+)")
type_regex = regexp.MustCompile("type: types.(Any|StoredQuery|LazyExpr)")
)
func formatPlugins(
scope types.Scope,
info *types.ScopeInformation,
type_map *types.TypeMap) string {
records := make(map[string]string)
names := []string{}
for _, item := range info.Plugins {
record := fmt.Sprintf("## %s\n\n%s\n\n", item.Name, item.Doc)
arg_desc, pres := type_map.Get(scope, item.ArgType)
if pres {
record += "Arg | Description | Type\n"
record += "----|-------------|-----\n"
for _, k := range arg_desc.Fields.Keys() {
v_any, _ := arg_desc.Fields.Get(k)
v, ok := v_any.(*types.TypeReference)
if !ok {
continue
}
target := v.Target
if v.Repeated {
target = " list of " + target
}
required := ""
if strings.Contains(v.Tag, "required") {
required = "(required)"
}
doc := ""
matches := doc_regex.FindStringSubmatch(v.Tag)
if matches != nil {
doc = matches[1]
}
record += fmt.Sprintf(
"%s | %s | %s %s\n", k, doc, target, required)
}
}
records[item.Name] = record
names = append(names, item.Name)
}
sort.Strings(names)
result := []string{}
for _, name := range names {
result = append(result, records[name])
}
return strings.Replace(strings.Join(result, "\n"), "types.Any", "Any", -1)
}
func formatFunctions(
scope types.Scope,
info *types.ScopeInformation,
type_map *types.TypeMap) string {
records := make(map[string]string)
names := []string{}
for _, item := range info.Functions {
record := fmt.Sprintf("## %s\n\n%s\n\n", item.Name, item.Doc)
arg_desc, pres := type_map.Get(scope, item.ArgType)
if pres {
record += "Arg | Description | Type\n"
record += "----|-------------|-----\n"
for _, k := range arg_desc.Fields.Keys() {
v_any, _ := arg_desc.Fields.Get(k)
v, ok := v_any.(*types.TypeReference)
if !ok {
continue
}
target := v.Target
if v.Repeated {
target = " list of " + target
}
required := ""
if strings.Contains(v.Tag, "required") {
required = "(required)"
}
doc := ""
matches := doc_regex.FindStringSubmatch(v.Tag)
if matches != nil {
doc = matches[1]
}
record += fmt.Sprintf(
"%s | %s | %s %s\n", k, doc, target, required)
}
}
records[item.Name] = record
names = append(names, item.Name)
}
sort.Strings(names)
result := []string{}
for _, name := range names {
result = append(result, records[name])
}
return strings.Replace(strings.Join(result, "\n"), "types.Any", "Any", -1)
}
func doVQLList() {
scope := vql_subsystem.MakeScope()
defer scope.Close()
type_map := types.NewTypeMap()
info := scope.Describe(type_map)
fmt.Println("VQL Functions")
fmt.Println("=============")
fmt.Println("")
fmt.Println(formatFunctions(scope, info, type_map))
fmt.Println("VQL Plugins")
fmt.Println("===========")
fmt.Println("")
fmt.Println(formatPlugins(scope, info, type_map))
}
func getOldItem(name, item_type string, old_data []*api_proto.Completion) *api_proto.Completion {
for _, item := range old_data {
if item.Name == name && item.Type == item_type {
return item
}
}
return nil
}
func doVQLExport() {
scope := vql_subsystem.MakeScope()
defer scope.Close()
type_map := types.NewTypeMap()
info := scope.Describe(type_map)
old_data := []*api_proto.Completion{}
if vql_info_export_old_file != nil {
data, err := ioutil.ReadAll(*vql_info_export_old_file)
if err == nil {
err = yaml.Unmarshal(data, &old_data)
kingpin.FatalIfError(err, "Unmarshal file")
}
}
new_data := []*api_proto.Completion{}
seen_plugins := make(map[string]bool)
seen_functions := make(map[string]bool)
for _, item := range info.Plugins {
seen_plugins[item.Name] = true
new_item := getOldItem(item.Name, "Plugin", old_data)
if new_item == nil {
new_item = &api_proto.Completion{
Name: item.Name,
Description: item.Doc,
Type: "Plugin",
}
} else {
// Override the args
new_item.Args = nil
}
arg_desc, pres := type_map.Get(scope, item.ArgType)
if pres {
for _, k := range arg_desc.Fields.Keys() {
v_any, _ := arg_desc.Fields.Get(k)
v, ok := v_any.(*types.TypeReference)
if !ok {
continue
}
arg := &api_proto.ArgDescriptor{
Repeated: v.Repeated,
Name: k,
Type: v.Target,
}
if strings.Contains(v.Tag, "required") {
arg.Required = true
}
matches := doc_regex.FindStringSubmatch(v.Tag)
if matches != nil {
arg.Description = matches[1]
}
new_item.Args = append(new_item.Args, arg)
}
}
new_data = append(new_data, new_item)
}
for _, item := range info.Functions {
seen_functions[item.Name] = true
new_item := getOldItem(item.Name, "Function", old_data)
if new_item == nil {
new_item = &api_proto.Completion{
Name: item.Name,
Description: item.Doc,
Type: "Function",
}
} else {
// Override the args
new_item.Args = nil
}
arg_desc, pres := type_map.Get(scope, item.ArgType)
if pres {
for _, k := range arg_desc.Fields.Keys() {
v_any, _ := arg_desc.Fields.Get(k)
v, ok := v_any.(*types.TypeReference)
if !ok {
continue
}
arg := &api_proto.ArgDescriptor{
Repeated: v.Repeated,
Type: v.Target,
Name: k,
}
if strings.Contains(v.Tag, "required") {
arg.Required = true
}
matches := doc_regex.FindStringSubmatch(v.Tag)
if matches != nil {
arg.Description = matches[1]
}
new_item.Args = append(new_item.Args, arg)
}
}
new_data = append(new_data, new_item)
}
// Add old data which have not been seen (This can happen if
// the last export was generated by a different arch than this
// one so some plugins were not registered).
for _, item := range old_data {
if item.Type == "Plugin" {
_, pres := seen_plugins[item.Name]
if !pres {
new_data = append(new_data, item)
}
} else if item.Type == "Function" {
_, pres := seen_functions[item.Name]
if !pres {
new_data = append(new_data, item)
}
}
}
// Sort to maintain stable output.
sort.Slice(new_data, func(i, j int) bool {
if new_data[i].Name == new_data[j].Name {
return new_data[i].Type < new_data[j].Type
}
return new_data[i].Name < new_data[j].Name
})
serialized, err := yaml.Marshal(new_data)
kingpin.FatalIfError(err, "Marshal")
fmt.Println("# Autogenerated! Do not edit.")
fmt.Println(type_regex.ReplaceAllString(string(serialized), "type: $1"))
}
func init() {
command_handlers = append(command_handlers, func(command string) bool {
switch command {
case vql_info_list.FullCommand():
doVQLList()
case vql_info_export.FullCommand():
doVQLExport()
default:
return false
}
return true
})
}