forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.go
310 lines (258 loc) · 8.47 KB
/
events.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
package api
import (
"crypto/x509"
"os"
"sort"
"strings"
context "golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"www.velocidex.com/golang/velociraptor/acls"
actions_proto "www.velocidex.com/golang/velociraptor/actions/proto"
api_proto "www.velocidex.com/golang/velociraptor/api/proto"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
crypto_utils "www.velocidex.com/golang/velociraptor/crypto/utils"
file_store "www.velocidex.com/golang/velociraptor/file_store"
"www.velocidex.com/golang/velociraptor/file_store/api"
"www.velocidex.com/golang/velociraptor/paths/artifacts"
"www.velocidex.com/golang/velociraptor/result_sets"
"www.velocidex.com/golang/velociraptor/services"
users "www.velocidex.com/golang/velociraptor/users"
"www.velocidex.com/golang/velociraptor/utils"
)
func (self *ApiServer) PushEvents(
ctx context.Context,
in *api_proto.PushEventRequest) (*emptypb.Empty, error) {
// Get the TLS context from the peer and verify its
// certificate.
peer, ok := peer.FromContext(ctx)
if !ok {
return nil, status.Error(codes.InvalidArgument, "cant get peer info")
}
tlsInfo, ok := peer.AuthInfo.(credentials.TLSInfo)
if !ok {
return nil, status.Error(codes.InvalidArgument, "unable to get credentials")
}
// Authenticate API clients using certificates.
for _, peer_cert := range tlsInfo.State.PeerCertificates {
chains, err := peer_cert.Verify(
x509.VerifyOptions{Roots: self.ca_pool})
if err != nil {
return nil, err
}
if len(chains) == 0 {
return nil, status.Error(codes.InvalidArgument, "no chains verified")
}
peer_name := crypto_utils.GetSubjectName(peer_cert)
if peer_name != self.config.Client.PinnedServerName {
token, err := acls.GetEffectivePolicy(self.config, peer_name)
if err != nil {
return nil, err
}
// Check that the principal is allowed to push to the queue.
ok, err := acls.CheckAccessWithToken(token, acls.PUBLISH, in.Artifact)
if err != nil {
return nil, err
}
if !ok {
return nil, status.Error(codes.PermissionDenied,
"Permission denied: PUBLISH "+peer_name+" to "+in.Artifact)
}
}
rows, err := utils.ParseJsonToDicts([]byte(in.Jsonl))
if err != nil {
return nil, err
}
// Only return the first row
journal, err := services.GetJournal()
if err != nil {
return nil, err
}
// only broadcast the events for local listeners. Minions
// write the events themselves, so we just need to broadcast
// for any server event artifacts that occur.
journal.Broadcast(self.config,
rows, in.Artifact, in.ClientId, in.FlowId)
return &emptypb.Empty{}, err
}
return nil, status.Error(codes.InvalidArgument, "no peer certs?")
}
func (self *ApiServer) WriteEvent(
ctx context.Context,
in *actions_proto.VQLResponse) (*emptypb.Empty, error) {
// Get the TLS context from the peer and verify its
// certificate.
peer, ok := peer.FromContext(ctx)
if !ok {
return nil, status.Error(codes.InvalidArgument, "cant get peer info")
}
tlsInfo, ok := peer.AuthInfo.(credentials.TLSInfo)
if !ok {
return nil, status.Error(codes.InvalidArgument, "unable to get credentials")
}
// Authenticate API clients using certificates.
for _, peer_cert := range tlsInfo.State.PeerCertificates {
chains, err := peer_cert.Verify(
x509.VerifyOptions{Roots: self.ca_pool})
if err != nil {
return nil, err
}
if len(chains) == 0 {
return nil, status.Error(codes.InvalidArgument, "no chains verified")
}
peer_name := crypto_utils.GetSubjectName(peer_cert)
token, err := acls.GetEffectivePolicy(self.config, peer_name)
if err != nil {
return nil, err
}
// Check that the principal is allowed to push to the queue.
ok, err := acls.CheckAccessWithToken(token,
acls.MACHINE_STATE, in.Query.Name)
if err != nil {
return nil, err
}
if !ok {
return nil, status.Error(codes.PermissionDenied,
"Permission denied: MACHINE_STATE "+
peer_name+" to "+in.Query.Name)
}
rows, err := utils.ParseJsonToDicts([]byte(in.Response))
if err != nil {
return nil, err
}
// Only return the first row
if true {
journal, err := services.GetJournal()
if err != nil {
return nil, err
}
err = journal.PushRowsToArtifact(self.config,
rows, in.Query.Name, peer_name, "")
return &emptypb.Empty{}, err
}
}
return nil, status.Error(codes.InvalidArgument, "no peer certs?")
}
func (self *ApiServer) ListAvailableEventResults(
ctx context.Context,
in *api_proto.ListAvailableEventResultsRequest) (
*api_proto.ListAvailableEventResultsResponse, error) {
user_name := GetGRPCUserInfo(self.config, ctx, self.ca_pool).Name
user_record, err := users.GetUser(self.config, user_name)
if err != nil {
return nil, err
}
permissions := acls.READ_RESULTS
perm, err := acls.CheckAccess(self.config, user_record.Name, permissions)
if !perm || err != nil {
return nil, status.Error(codes.PermissionDenied,
"User is not allowed to view results.")
}
if in.Artifact == "" {
return listAvailableEventArtifacts(self, in)
}
return listAvailableEventTimestamps(ctx, self, in)
}
func listAvailableEventTimestamps(
ctx context.Context,
self *ApiServer, in *api_proto.ListAvailableEventResultsRequest) (
*api_proto.ListAvailableEventResultsResponse, error) {
path_manager, err := artifacts.NewArtifactPathManager(
self.config, in.ClientId, "", in.Artifact)
if err != nil {
return nil, err
}
result := &api_proto.ListAvailableEventResultsResponse{
Logs: []*api_proto.AvailableEvent{
{
Artifact: in.Artifact,
},
},
}
timestamps, err := listAvailableEventTimestampFiles(ctx, self, path_manager)
result.Logs[0].RowTimestamps = timestamps
timestamps, err = listAvailableEventTimestampFiles(
ctx, self, path_manager.Logs())
result.Logs[0].LogTimestamps = timestamps
return result, nil
}
func listAvailableEventTimestampFiles(
ctx context.Context, self *ApiServer, path_manager api.PathManager) ([]int32, error) {
result := []int32{}
file_store_factory := file_store.GetFileStore(self.config)
reader, err := result_sets.NewTimedResultSetReader(
ctx, file_store_factory, path_manager)
if err != nil {
return nil, err
}
for _, prop := range reader.GetAvailableFiles(ctx) {
result = append(result, int32(prop.StartTime.Unix()))
}
return result, nil
}
func listAvailableEventArtifacts(
self *ApiServer, in *api_proto.ListAvailableEventResultsRequest) (
*api_proto.ListAvailableEventResultsResponse, error) {
// Figure out where all the monitoring artifacts logs are
// stored by looking at some examples.
exemplar := "Generic.Client.Stats"
if in.ClientId == "" || in.ClientId == "server" {
exemplar = "Server.Monitor.Health"
}
path_manager, err := artifacts.NewArtifactPathManager(
self.config, in.ClientId, "", exemplar)
if err != nil {
return nil, err
}
// getAllArtifacts analyses the path name from disk and adds
// to the events list.
seen := make(map[string]*api_proto.AvailableEvent)
err = getAllArtifacts(self.config, path_manager.GetRootPath(), seen)
if err != nil {
return nil, err
}
err = getAllArtifacts(self.config, path_manager.Logs().GetRootPath(), seen)
if err != nil {
return nil, err
}
result := &api_proto.ListAvailableEventResultsResponse{}
for _, item := range seen {
result.Logs = append(result.Logs, item)
}
sort.Slice(result.Logs, func(i, j int) bool {
return result.Logs[i].Artifact < result.Logs[j].Artifact
})
return result, nil
}
func getAllArtifacts(
config_obj *config_proto.Config,
log_path api.FSPathSpec,
seen map[string]*api_proto.AvailableEvent) error {
file_store_factory := file_store.GetFileStore(config_obj)
return api.Walk(file_store_factory, log_path,
func(full_path api.FSPathSpec, info os.FileInfo) error {
// Walking the events directory will give us
// all the day json files. Each day json file
// is contained in a directory structure which
// reflects the name of the artifact, for
// example:
// <log_path>/Server.Monitor.Health/Prometheus/2021-08-01.json
// Corresponds to the artifact Server.Monitor.Health/Prometheus
if !info.IsDir() && info.Size() > 0 {
relative_path := full_path.Dir().
Components()[len(log_path.Components()):]
artifact_name := strings.Join(relative_path, "/")
event, pres := seen[artifact_name]
if !pres {
event = &api_proto.AvailableEvent{
Artifact: artifact_name,
}
seen[artifact_name] = event
}
}
return nil
})
}