forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforeman.go
161 lines (137 loc) · 5.05 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
157
158
159
160
161
/*
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"
"github.com/Velocidex/ordereddict"
errors "github.com/pkg/errors"
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"
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.
client_event_manager := services.ClientEventManager()
if client_event_manager != nil &&
client_event_manager.CheckClientEventsVersion(
config_obj, client_id,
foreman_checkin.LastEventTableVersion) {
err := QueueMessageForClient(
config_obj, client_id,
client_event_manager.GetClientUpdateEventTableMessage(
config_obj, client_id))
if err != nil {
return err
}
}
// Process any needed hunts.
dispatcher := services.GetHuntDispatcher()
if dispatcher == nil {
return nil
}
client_last_timestamp := foreman_checkin.LastHuntTimestamp
// Can we get away without a lock? If the client is already up
// to date we dont need to look further.
hunts_last_timestamp := dispatcher.GetLastTimestamp()
if client_last_timestamp >= hunts_last_timestamp {
return nil
}
// Take a snapshot of the hunts that we need to run on this
// client to reduce the time under lock.
hunts := make([]*api_proto.Hunt, 0)
err := 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
}
// Take a snapshot of the hunt id and start time.
hunts = append(hunts, &api_proto.Hunt{
HuntId: hunt.HuntId,
StartTime: hunt.StartTime,
})
return nil
})
// Nothing to do, return
if len(hunts) == 0 {
return err
}
// Now schedule the client for all the hunts that it needs to run.
journal, err := services.GetJournal()
if err != nil {
return err
}
// Record the latest timestamp
latest_timestamp := uint64(0)
for _, hunt := range hunts {
// Notify the hunt manager that we need to hunt this client.
err = journal.PushRowsToArtifact(config_obj,
[]*ordereddict.Dict{ordereddict.NewDict().
Set("HuntId", hunt.HuntId).
Set("ClientId", client_id),
}, "System.Hunt.Participation", client_id, "")
if err != nil {
return err
}
if hunt.StartTime > latest_timestamp {
latest_timestamp = hunt.StartTime
}
}
// Let the client know it needs to update its foreman state to
// the latest time. We schedule an UpdateForeman message for
// the client. Note that it is possible that the client does
// not update its timestamp immediately and therefore might
// end up sending multiple participation events to the hunt
// manager - this is ok since the hunt manager keeps hunt
// participation index and will automatically skip multiple
// messages.
return QueueMessageForClient(
config_obj, client_id,
&crypto_proto.VeloMessage{
SessionId: constants.MONITORING_WELL_KNOWN_FLOW,
RequestId: constants.IgnoreResponseState,
UpdateForeman: &actions_proto.ForemanCheckin{
LastHuntTimestamp: latest_timestamp,
},
})
}