forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocols.go
215 lines (174 loc) · 4.34 KB
/
protocols.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
package accessors
import (
"context"
"www.velocidex.com/golang/velociraptor/utils"
vql_subsystem "www.velocidex.com/golang/velociraptor/vql"
"www.velocidex.com/golang/vfilter"
"www.velocidex.com/golang/vfilter/protocols"
)
type _BoolOSPath struct{}
func (self _BoolOSPath) Applicable(a vfilter.Any) bool {
_, ok := a.(*OSPath)
return ok
}
func (self _BoolOSPath) Bool(ctx context.Context, scope vfilter.Scope, a vfilter.Any) bool {
os_path, ok := a.(*OSPath)
return ok && len(os_path.Components) > 0
}
type _EqualOSPath struct{}
func (self _EqualOSPath) Applicable(a vfilter.Any, b vfilter.Any) bool {
_, ok := a.(*OSPath)
if !ok {
return false
}
_, ok = b.(*OSPath)
return ok
}
func (self _EqualOSPath) Eq(scope vfilter.Scope, a vfilter.Any, b vfilter.Any) bool {
a_os_path, ok := a.(*OSPath)
if !ok {
return false
}
b_os_path, ok := b.(*OSPath)
if !ok {
return false
}
if !utils.StringSliceEq(a_os_path.Components, b_os_path.Components) {
return false
}
return a_os_path.String() == b_os_path.String()
}
type _RegexOSPath struct{}
func (self _RegexOSPath) Applicable(a vfilter.Any, b vfilter.Any) bool {
_, ok := b.(*OSPath)
if !ok {
return false
}
_, ok = a.(string)
return ok
}
func (self _RegexOSPath) Match(scope vfilter.Scope, a vfilter.Any, b vfilter.Any) bool {
b_os_path, ok := b.(*OSPath)
if !ok {
return false
}
// Shortcut for matches against "." or an empty string will match
// anything - we do not need to expand the OSPath
a_str, ok := a.(string)
if !ok || a_str == "" || a_str == "." {
return true
}
return scope.Match(a, b_os_path.String())
}
type _AddOSPath struct{}
func (self _AddOSPath) Applicable(a vfilter.Any, b vfilter.Any) bool {
_, ok := a.(*OSPath)
if !ok {
return false
}
switch b.(type) {
case *OSPath, string, []vfilter.Any:
return true
}
return false
}
func (self _AddOSPath) Add(scope vfilter.Scope, a vfilter.Any, b vfilter.Any) vfilter.Any {
a_os_path, ok := a.(*OSPath)
if !ok {
return false
}
var b_os_path *OSPath
switch t := b.(type) {
case *OSPath:
b_os_path = t
case string:
parsed, err := ParsePath(t, "")
if err != nil {
return vfilter.Null{}
}
b_os_path = parsed
// Support OSPath("/root") + ["foo", "bar"] -> OSPath("/root/foo/bar")
case []vfilter.Any:
components := make([]string, 0, len(t))
for _, item := range t {
str_item, ok := item.(string)
if ok {
components = append(components, str_item)
}
}
return a_os_path.Append(components...)
}
return a_os_path.Append(b_os_path.Components...)
}
type _AssociativeOSPath struct{}
// Filter some method calls to be more useful.
func (self _AssociativeOSPath) Applicable(a vfilter.Any, b vfilter.Any) bool {
_, ok := a.(*OSPath)
if !ok {
return false
}
switch b.(type) {
case []*int64, string, int64:
return true
}
return false
}
func (self _AssociativeOSPath) Associative(
scope vfilter.Scope, a vfilter.Any, b vfilter.Any) (vfilter.Any, bool) {
a_os_path, ok := a.(*OSPath)
if !ok {
return &vfilter.Null{}, false
}
length := int64(len(a_os_path.Components))
switch t := b.(type) {
case []*int64:
first_item := int64(0)
if t[0] != nil {
first_item = *t[0]
}
second_item := length
if t[1] != nil {
second_item = *t[1]
}
if first_item < 0 {
first_item += length
}
if second_item < 0 {
second_item += length
}
if second_item <= first_item {
return a_os_path.Clear(), true
}
return a_os_path.Clear().Append(
a_os_path.Components[first_item:second_item]...), true
case int64:
if t < 0 {
t += length
}
if t < 0 || t >= length {
return &vfilter.Null{}, true
}
return a_os_path.Components[t], true
case string:
switch t {
case "HumanString":
return a_os_path.HumanString(scope), true
default:
return protocols.DefaultAssociative{}.Associative(scope, a, b)
}
default:
return protocols.DefaultAssociative{}.Associative(scope, a, b)
}
}
func (self _AssociativeOSPath) GetMembers(scope vfilter.Scope, a vfilter.Any) []string {
result := protocols.DefaultAssociative{}.GetMembers(scope, a)
result = append(result, "HumanString")
return result
}
func init() {
vql_subsystem.RegisterProtocol(&_BoolOSPath{})
vql_subsystem.RegisterProtocol(&_EqualOSPath{})
vql_subsystem.RegisterProtocol(&_AddOSPath{})
vql_subsystem.RegisterProtocol(&_RegexOSPath{})
vql_subsystem.RegisterProtocol(&_AssociativeOSPath{})
}