forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforeman.go
125 lines (105 loc) · 4.04 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
/*
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.CheckClientEventsVersion(client_id,
foreman_checkin.LastEventTableVersion) {
err := QueueMessageForClient(
config_obj, client_id,
client_event_manager.GetClientUpdateEventTableMessage(
client_id))
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
}
// Notify the hunt manager that we need to hunt this client.
err := services.GetJournal().PushRowsToArtifact(
[]*ordereddict.Dict{ordereddict.NewDict().
Set("HuntId", hunt.HuntId).
Set("ClientId", client_id).
Set("Participate", true),
}, "System.Hunt.Participation", client_id, "")
if err != nil {
return err
}
// Let the client know it needs to update its foreman state.
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.GetNotifier().NotifyListener(config_obj, client_id)
})
}