forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_chan.go
170 lines (136 loc) · 3.98 KB
/
search_chan.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
package search
// Implement client searching with channel based API
import (
"context"
"errors"
"regexp"
"strings"
api_proto "www.velocidex.com/golang/velociraptor/api/proto"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
"www.velocidex.com/golang/velociraptor/datastore"
"www.velocidex.com/golang/velociraptor/glob"
"www.velocidex.com/golang/velociraptor/paths"
"www.velocidex.com/golang/vfilter"
)
// Get the recent clients viewed by the principal sorted in most
// recently used order.
func searchRecentsChan(
ctx context.Context,
scope vfilter.Scope,
config_obj *config_proto.Config,
principal string) (
chan *api_proto.ApiClient, error) {
path_manager := &paths.UserPathManager{principal}
db, err := datastore.GetDB(config_obj)
if err != nil {
return nil, err
}
children, err := db.ListChildren(config_obj, path_manager.MRUIndex())
if err != nil {
return nil, err
}
output_chan := make(chan *api_proto.ApiClient)
go func() {
defer close(output_chan)
// Sort the children in reverse order - most recent first.
for i := len(children) - 1; i >= 0; i-- {
client_id := children[i].Base()
api_client, err := FastGetApiClient(
ctx, config_obj, client_id)
if err != nil {
continue
}
select {
case <-ctx.Done():
return
case output_chan <- api_client:
}
}
}()
return output_chan, nil
}
func SearchClientsChan(
ctx context.Context,
scope vfilter.Scope,
config_obj *config_proto.Config,
search_term string, principal string) (chan *api_proto.ApiClient, error) {
operator, term := splitIntoOperatorAndTerms(search_term)
switch operator {
case "label", "host", "all":
// Include the operator in these search terms
return searchClientIndexChan(ctx, scope, config_obj, search_term)
case "client":
return searchClientIndexChan(ctx, scope, config_obj, term)
case "":
return searchClientIndexChan(ctx, scope, config_obj, "host:"+term)
case "recent":
return searchRecentsChan(ctx, scope, config_obj, principal)
default:
return nil, errors.New("Invalid search operator " + operator)
}
}
func searchClientIndexChan(
ctx context.Context,
scope vfilter.Scope,
config_obj *config_proto.Config,
search_term string) (chan *api_proto.ApiClient, error) {
// The search term may contain wild cards but in the index we can
// only search for prefixes. So we need to first extract the
// search prefix then apply the regex to filter out the hits based
// on the full search term.
prefix, filter := splitSearchTermIntoPrefixAndFilter(scope, search_term)
output_chan := make(chan *api_proto.ApiClient)
go func() {
defer close(output_chan)
// Microseconds
seen := make(map[string]bool)
for hit := range SearchIndexWithPrefix(ctx, config_obj, prefix) {
if hit == nil {
continue
}
// If the search term is complicated we need to check the
// filter against the retrieved term.
if filter != nil && !filter.MatchString(hit.Term) {
continue
}
client_id := hit.Entity
// Uniquify the client ID
_, pres := seen[client_id]
if pres {
continue
}
seen[client_id] = true
api_client, err := FastGetApiClient(ctx, config_obj, client_id)
if err != nil {
continue
}
select {
case <-ctx.Done():
return
case output_chan <- api_client:
}
}
}()
return output_chan, nil
}
// When searching the index, the user may provide wild cards.
func splitSearchTermIntoPrefixAndFilter(
scope vfilter.Scope, search_term string) (string, *regexp.Regexp) {
parts := strings.Split(search_term, "*")
// No wild cards present
if len(parts) == 1 {
return search_term, nil
}
// Last component is a wildcard, just ignore it (e.g. win* )
if len(parts) == 2 && parts[1] == "" {
return parts[0], nil
}
// Try to interpret the filter as a glob
filter_regex := "(?i)" + glob.FNmatchTranslate(search_term)
filter, err := regexp.Compile(filter_regex)
if err != nil {
scope.Log("ClientSearch while Matching %v: %v", search_term, err)
return parts[0], nil
}
return parts[0], filter
}