/
functions.go
284 lines (240 loc) · 7.02 KB
/
functions.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
package functions
import (
"context"
"encoding/hex"
"fmt"
"io/ioutil"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"github.com/Velocidex/ordereddict"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
"www.velocidex.com/golang/vfilter/arg_parser"
"www.velocidex.com/golang/vfilter/types"
"www.velocidex.com/golang/vfilter/utils/dict"
)
// A helper function to build a dict within the query.
// e.g. dict(foo=5, bar=6)
type _DictFunc struct{}
func (self _DictFunc) Info(scope types.Scope, type_map *types.TypeMap) *types.FunctionInfo {
return &types.FunctionInfo{
Name: "dict",
Doc: "Construct a dict from arbitrary keyword args.",
}
}
func (self _DictFunc) Call(ctx context.Context, scope types.Scope, args *ordereddict.Dict) types.Any {
return dict.RowToDict(ctx, scope, args)
}
type _TimestampArg struct {
Epoch int64 `vfilter:"optional,field=epoch"`
WinFileTime int64 `vfilter:"optional,field=winfiletime"`
}
type _Timestamp struct{}
func (self _Timestamp) Info(scope types.Scope, type_map *types.TypeMap) *types.FunctionInfo {
return &types.FunctionInfo{
Name: "timestamp",
Doc: "Convert seconds from epoch into a string.",
ArgType: type_map.AddType(scope, _TimestampArg{}),
}
}
func (self _Timestamp) Call(ctx context.Context, scope types.Scope, args *ordereddict.Dict) types.Any {
arg := &_TimestampArg{}
err := arg_parser.ExtractArgs(scope, args, arg)
if err != nil {
scope.Log("timestamp: %s", err.Error())
return types.Null{}
}
if arg.Epoch > 0 {
return time.Unix(arg.Epoch, 0)
}
if arg.WinFileTime > 0 {
return time.Unix((arg.WinFileTime/10000000)-11644473600, 0)
}
return types.Null{}
}
type _SubSelectFunctionArgs struct {
VQL types.StoredQuery `vfilter:"required,field=vql"`
}
type _SplitFunctionArgs struct {
String string `vfilter:"required,field=string,doc=The value to split"`
Sep string `vfilter:"optional,field=sep,doc=The separator that will be used to split"`
Sep_string string `vfilter:"optional,field=sep_string,doc=The separator as string that will be used to split"`
}
type _SplitFunction struct{}
func (self _SplitFunction) Info(scope types.Scope, type_map *types.TypeMap) *types.FunctionInfo {
return &types.FunctionInfo{
Name: "split",
Doc: "Splits a string into an array based on a regexp or string separator.",
ArgType: type_map.AddType(scope, _SplitFunctionArgs{}),
}
}
func (self _SplitFunction) Call(ctx context.Context, scope types.Scope, args *ordereddict.Dict) types.Any {
arg := &_SplitFunctionArgs{}
err := arg_parser.ExtractArgs(scope, args, arg)
if err != nil {
scope.Log("split: %s", err.Error())
return types.Null{}
}
if arg.Sep != "" {
re, err := regexp.Compile(arg.Sep)
if err != nil {
scope.Log("split: %s", err.Error())
return types.Null{}
}
return re.Split(arg.String, -1)
} else if arg.Sep_string != "" {
return strings.Split(arg.String, arg.Sep_string)
} else {
scope.Log("split: requires either sep or sep_string")
return types.Null{}
}
}
type _GetFunctionArgs struct {
Item types.Any `vfilter:"optional,field=item"`
Member string `vfilter:"required,field=member"`
}
type _GetFunction struct{}
func (self _GetFunction) Info(scope types.Scope, type_map *types.TypeMap) *types.FunctionInfo {
return &types.FunctionInfo{
Name: "get",
Doc: "Gets the member field from item.",
ArgType: type_map.AddType(scope, _GetFunctionArgs{}),
}
}
func (self _GetFunction) Call(
ctx context.Context,
scope types.Scope,
args *ordereddict.Dict) types.Any {
arg := &_GetFunctionArgs{}
err := arg_parser.ExtractArgs(scope, args, arg)
if err != nil {
scope.Log("get: %s", err.Error())
return types.Null{}
}
result := arg.Item
if result == nil {
result = scope
}
var next_result types.Any
var pres bool
for _, member := range strings.Split(arg.Member, ".") {
int_member, err := strconv.Atoi(member)
if err == nil {
// If it looks like an int it might be an
// index reference.
next_result, pres = scope.Associative(
result, int_member)
} else {
next_result, pres = scope.Associative(
result, member)
}
if !pres {
return types.Null{}
}
result = next_result
}
return result
}
type _EncodeFunctionArgs struct {
String types.Any `vfilter:"required,field=string"`
Type string `vfilter:"required,field=type"`
}
type _EncodeFunction struct{}
func (self _EncodeFunction) Info(scope types.Scope, type_map *types.TypeMap) *types.FunctionInfo {
return &types.FunctionInfo{
Name: "encode",
Doc: "Encodes a string as as different type. Currently supported types include 'hex', 'base64'.",
ArgType: type_map.AddType(scope, _EncodeFunctionArgs{}),
}
}
func (self _EncodeFunction) Call(
ctx context.Context,
scope types.Scope,
args *ordereddict.Dict) types.Any {
arg := &_EncodeFunctionArgs{}
err := arg_parser.ExtractArgs(scope, args, arg)
if err != nil {
scope.Log("hex: %s", err.Error())
return types.Null{}
}
var arg_string string
switch t := arg.String.(type) {
case string:
arg_string = t
case []byte:
arg_string = string(t)
case fmt.Stringer:
arg_string = fmt.Sprintf("%s", t)
default:
arg_string = fmt.Sprintf("%v", t)
}
switch arg.Type {
case "hex":
return hex.EncodeToString([]byte(arg_string))
case "string":
return arg_string
// Read a UTF16 encoded string and convert it to utf8
case "utf16":
codec := unicode.UTF16(unicode.LittleEndian, unicode.UseBOM)
rd := strings.NewReader(arg_string)
decoded, err := ioutil.ReadAll(
transform.NewReader(
rd, codec.NewDecoder()))
if err != nil {
scope.Log("encoder: %s", err.Error())
}
return string(decoded)
default:
scope.Log("hex: encoding %s not supported.", arg.Type)
}
return types.Null{}
}
type LenFunctionArgs struct {
List types.Any `vfilter:"required,field=list,doc=A list of items"`
}
type LenFunction struct{}
func (self LenFunction) Call(ctx context.Context,
scope types.Scope,
args *ordereddict.Dict) types.Any {
arg := &LenFunctionArgs{}
err := arg_parser.ExtractArgs(scope, args, arg)
if err != nil {
scope.Log("len: %s", err.Error())
return &types.Null{}
}
slice := reflect.ValueOf(arg.List)
// A slice of strings. Only the following are supported
// https://golang.org/pkg/reflect/#Value.Len
if slice.Type().Kind() == reflect.Slice ||
slice.Type().Kind() == reflect.Map ||
slice.Type().Kind() == reflect.Array ||
slice.Type().Kind() == reflect.String {
return slice.Len()
}
dict, ok := arg.List.(*ordereddict.Dict)
if ok {
return dict.Len()
}
return 0
}
func (self LenFunction) Info(scope types.Scope, type_map *types.TypeMap) *types.FunctionInfo {
return &types.FunctionInfo{
Name: "len",
Doc: "Returns the length of an object.",
ArgType: type_map.AddType(scope, &LenFunctionArgs{}),
}
}
func Materialize(ctx context.Context,
scope types.Scope, stored_query types.StoredQuery) []types.Row {
result := []types.Row{}
// Materialize both queries to an array.
new_scope := scope.Copy()
defer new_scope.Close()
for item := range stored_query.Eval(ctx, new_scope) {
result = append(result, item)
}
return result
}