forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforeman.go
156 lines (131 loc) · 4.64 KB
/
foreman.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
/*
Velociraptor - Hunting Evil
Copyright (C) 2019 Velocidex Innovations.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// The foreman is a Well Known Flow for clients to check for hunt
// memberships. Each client periodically informs the foreman of the
// most recent hunt it executed, and the foreman launches the relevant
// flow on the client.
// The process goes like this:
// 1. The client sends a message to the foreman periodically with the
// timestamp of the most recent hunt it ran (as well latest event
// table version).
// 2. If a newer hunt exists, the foreman sends the hunt_condition
// query to the client with the response directed to the
// System.Hunt.Participation artifact monitoring queue.
// 3. The hunt manager service scans the System.Hunt.Participation
// monitoring queue and launches the relevant flows on each client.
package flows
import (
"context"
errors "github.com/pkg/errors"
actions_proto "www.velocidex.com/golang/velociraptor/actions/proto"
api_proto "www.velocidex.com/golang/velociraptor/api/proto"
artifacts "www.velocidex.com/golang/velociraptor/artifacts"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
constants "www.velocidex.com/golang/velociraptor/constants"
crypto_proto "www.velocidex.com/golang/velociraptor/crypto/proto"
"www.velocidex.com/golang/velociraptor/services"
)
// ForemanProcessMessage processes a ForemanCheckin message from the
// client.
func ForemanProcessMessage(
ctx context.Context,
config_obj *config_proto.Config,
client_id string,
foreman_checkin *actions_proto.ForemanCheckin) error {
if foreman_checkin == nil {
return errors.New("Expected args of type ForemanCheckin")
}
// Update the client's event tables.
if foreman_checkin.LastEventTableVersion < services.GetClientEventsVersion() {
err := QueueMessageForClient(
config_obj, client_id,
services.GetClientUpdateEventTableMessage())
if err != nil {
return err
}
}
// Process any needed hunts.
dispatcher := services.GetHuntDispatcher()
client_last_timestamp := foreman_checkin.LastHuntTimestamp
// Can we get away without a lock?
hunts_last_timestamp := dispatcher.GetLastTimestamp()
if client_last_timestamp >= hunts_last_timestamp {
return nil
}
// Nop - we need to lock and examine the hunts more carefully.
return dispatcher.ApplyFuncOnHunts(func(hunt *api_proto.Hunt) error {
// Hunt is stopped we dont care about it.
if hunt.State != api_proto.Hunt_RUNNING {
return nil
}
// This hunt is not relevant to this client.
if hunt.StartTime <= client_last_timestamp {
return nil
}
flow_condition_query, err := calculateFlowConditionQuery(
config_obj, hunt)
if err != nil {
return err
}
err = QueueMessageForClient(
config_obj, client_id,
&crypto_proto.GrrMessage{
SessionId: constants.MONITORING_WELL_KNOWN_FLOW,
RequestId: processVQLResponses,
VQLClientAction: flow_condition_query})
if err != nil {
return err
}
err = QueueMessageForClient(
config_obj, client_id,
&crypto_proto.GrrMessage{
SessionId: constants.MONITORING_WELL_KNOWN_FLOW,
RequestId: constants.IgnoreResponseState,
UpdateForeman: &actions_proto.ForemanCheckin{
LastHuntTimestamp: hunt.StartTime,
},
})
if err != nil {
return err
}
return services.NotifyClient(client_id)
})
}
func calculateFlowConditionQuery(
config_obj *config_proto.Config,
hunt *api_proto.Hunt) (
*actions_proto.VQLCollectorArgs, error) {
// TODO.
default_query := getDefaultCollectorArgs(hunt)
err := artifacts.Obfuscate(config_obj, default_query)
return default_query, err
}
func getDefaultCollectorArgs(hunt *api_proto.Hunt) *actions_proto.VQLCollectorArgs {
return &actions_proto.VQLCollectorArgs{
Env: []*actions_proto.VQLEnv{
&actions_proto.VQLEnv{
Key: "HuntId",
Value: hunt.HuntId,
},
},
Query: []*actions_proto.VQLRequest{
&actions_proto.VQLRequest{
Name: "System.Hunt.Participation",
VQL: "SELECT now() as Timestamp, Fqdn, HuntId, " +
"true as Participate from info()",
},
},
}
}