forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlauncher.go
236 lines (195 loc) · 7.64 KB
/
launcher.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
package services
// Many parts of Velociraptor require launching new collections. The
// logic for preparing, verifying, Compiling and launching new
// collections is extracted into a service so it can be accessible
// from many components.
// Velociraptor treats all input as artifacts - users can launch new
// artifact collection on endpoints by naming the artifact and
// providing parameters. However the endpoint itself is not directly
// running the artifacts - it simply runs VQL statements. We do this
// so that artifacts can be edited and customized on the server
// without needing to deploy new clients.
// On the server, collections are created using ArtifactCollectorArgs
// On the client, VQL is executing from VQLCollectorArgs
// Ultimately the launcher is responsible for compiling the requested
// ArtifactCollectorArgs collection into the VQLCollectorArgs protobuf
// that will be sent to the client. Compiling the artifact means:
// 1. Converting the artifact definition into a sequence of VQL
// 2. Populating the query environment from the artifact definition
// defaults and merging the user's parameters into the initial
// query environment.
// 3. Include any dependent artifacts in the VQLCollectorArgs. On the
// client, these additional artifacts will be compiled into a
// temporary artifact repository for execution (i.e. the client
// never uses its built in artifacts).
// 4. Adding any required tools by the artifact and filling in their
// tool details (required hash, and download location).
// Most callers will only need to call ScheduleArtifactCollection()
// which does all the required steps and launches the collection.
// It is possible for callers to pre-compile the artifact and cache
// the VQLCollectorArgs for later use to avoid the cost of compiling
// the artifact. This is useful e.g. in hunts to be able to scale the
// launching of similar collections on many hosts at the same time.
import (
"context"
"time"
"github.com/Velocidex/ordereddict"
actions_proto "www.velocidex.com/golang/velociraptor/actions/proto"
api_proto "www.velocidex.com/golang/velociraptor/api/proto"
artifacts_proto "www.velocidex.com/golang/velociraptor/artifacts/proto"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
crypto_proto "www.velocidex.com/golang/velociraptor/crypto/proto"
flows_proto "www.velocidex.com/golang/velociraptor/flows/proto"
"www.velocidex.com/golang/velociraptor/result_sets"
vql_subsystem "www.velocidex.com/golang/velociraptor/vql"
)
const (
// When the principal is set to this below we avoid audit logging
// the call.
NoAuditLogging = ""
DryRunOnly = false
)
type DeleteFlowResponse struct {
Type string `json:"type"`
Data *ordereddict.Dict `json:"data"`
Error string `json:"error"`
}
func GetLauncher(config_obj *config_proto.Config) (Launcher, error) {
org_manager, err := GetOrgManager()
if err != nil {
return nil, err
}
return org_manager.Services(config_obj.OrgId).Launcher()
}
type CompilerOptions struct {
// Should names be obfuscated in the resulting VQL?
ObfuscateNames bool
// Generate precondition queries.
DisablePrecondition bool
// Ignore Missing Artifacts without raising an error.
IgnoreMissingArtifacts bool
LogBatchTime uint64
}
type FlowSummary struct {
FlowId string `json:"FlowId"`
Artifacts []string `json:"Artifacts"`
Created uint64 `json:"Created"`
Creator string `json:"Creator"`
}
type FlowStorer interface {
WriteFlow(
ctx context.Context,
config_obj *config_proto.Config,
flow *flows_proto.ArtifactCollectorContext,
completion func()) error
WriteFlowIndex(
ctx context.Context,
config_obj *config_proto.Config,
flow *flows_proto.ArtifactCollectorContext) error
WriteTask(
ctx context.Context,
config_obj *config_proto.Config,
client_id string,
msg *crypto_proto.VeloMessage) error
DeleteFlow(
ctx context.Context,
config_obj *config_proto.Config,
client_id string, flow_id string, principal string,
really_do_it bool) ([]*DeleteFlowResponse, error)
LoadCollectionContext(
ctx context.Context,
config_obj *config_proto.Config,
client_id, flow_id string) (*flows_proto.ArtifactCollectorContext, error)
ListFlows(
ctx context.Context,
config_obj *config_proto.Config,
client_id string,
options result_sets.ResultSetOptions,
offset, length int64) ([]*FlowSummary, int64, error)
// Get the exact requests that were sent for this collection (for
// provenance).
GetFlowRequests(
ctx context.Context,
config_obj *config_proto.Config,
client_id string, flow_id string,
offset uint64, count uint64) (*api_proto.ApiFlowRequestDetails, error)
}
type Launcher interface {
// Only used for tests to force a predictable flow id.
SetFlowIdForTests(flow_id string)
Storage() FlowStorer
// Check any declared tools exist and are available - possibly
// by downloading them.
EnsureToolsDeclared(
ctx context.Context,
config_obj *config_proto.Config,
artifact *artifacts_proto.Artifact) error
// Calculates the dependent artifacts
GetDependentArtifacts(
ctx context.Context, config_obj *config_proto.Config,
repository Repository,
names []string) ([]string, error)
// Compiles an ArtifactCollectorArgs (for example as passed
// into CreateHunt() or CollectArtifact() API into a list of
// VQLCollectorArgs - the messages sent to the client to
// actually collect the artifact. On the client a
// VQLCollectorArgs is collected serially in a single
// goroutine. This means all the artifacts in the
// ArtifactCollectorArgs will be collected one after the other
// in turn. If callers want to collect artifacts in parallel
// then they need to perpare several VQLCollectorArgs and
// launch them as separate messages.
// This method is only useful when the caller wants to cache
// the compilation process once and run it many times (e.g. in
// a hunt).
CompileCollectorArgs(
ctx context.Context,
config_obj *config_proto.Config,
acl_manager vql_subsystem.ACLManager,
repository Repository,
options CompilerOptions,
collector_request *flows_proto.ArtifactCollectorArgs) (
[]*actions_proto.VQLCollectorArgs, error)
// Take the compiled requests from above and schedule them on the
// client.
WriteArtifactCollectionRecord(
ctx context.Context,
config_obj *config_proto.Config,
collector_request *flows_proto.ArtifactCollectorArgs,
vql_collector_args []*actions_proto.VQLCollectorArgs,
completion func(task *crypto_proto.VeloMessage)) (string, error)
// Main entry point to launch an artifact collection.
ScheduleArtifactCollection(
ctx context.Context,
config_obj *config_proto.Config,
acl_manager vql_subsystem.ACLManager,
repository Repository,
collector_request *flows_proto.ArtifactCollectorArgs,
completion func()) (string, error)
// The following methods are used to manage collections
// Get a list of collections summary from a client.
GetFlows(
ctx context.Context,
config_obj *config_proto.Config,
client_id string,
options result_sets.ResultSetOptions,
offset, length int64) (*api_proto.ApiFlowResponse, error)
// Get the details of a flow - this has a lot more information
// than the previous method.
GetFlowDetails(
ctx context.Context,
config_obj *config_proto.Config,
client_id string, flow_id string) (*api_proto.FlowDetails, error)
// Actively cancel the collection
CancelFlow(
ctx context.Context,
config_obj *config_proto.Config,
client_id, flow_id, principal string) (
res *api_proto.StartFlowResponse, err error)
DeleteEvents(
ctx context.Context,
config_obj *config_proto.Config,
principal, artifact, client_id string,
start_time, end_time time.Time,
really_do_it bool) ([]*DeleteFlowResponse, error)
}