forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
316 lines (258 loc) · 8.49 KB
/
server.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
311
312
313
314
315
316
/*
Velociraptor - Dig Deeper
Copyright (C) 2019-2022 Rapid7 Inc.
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/>.
*/
//
package server
import (
"context"
"errors"
"fmt"
"runtime"
"sync"
"time"
"github.com/juju/ratelimit"
"github.com/prometheus/client_golang/prometheus"
actions_proto "www.velocidex.com/golang/velociraptor/actions/proto"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
"www.velocidex.com/golang/velociraptor/constants"
"www.velocidex.com/golang/velociraptor/crypto"
crypto_proto "www.velocidex.com/golang/velociraptor/crypto/proto"
crypto_server "www.velocidex.com/golang/velociraptor/crypto/server"
"www.velocidex.com/golang/velociraptor/flows"
"www.velocidex.com/golang/velociraptor/logging"
"www.velocidex.com/golang/velociraptor/services"
"www.velocidex.com/golang/velociraptor/utils"
)
type Server struct {
manager *crypto_server.ServerCryptoManager
logger *logging.LogContext
// Limit concurrency for processing messages.
mu sync.Mutex
concurrency *utils.Concurrency
reader_concurrency *utils.Concurrency
concurrency_timeout time.Duration
throttler *utils.Throttler
// The server dynamically adjusts concurrency. This signals exit.
done chan bool
Bucket *ratelimit.Bucket
Healthy int32
}
func (self *Server) Concurrency() *utils.Concurrency {
self.mu.Lock()
defer self.mu.Unlock()
return self.concurrency
}
func (self *Server) ReaderConcurrency() *utils.Concurrency {
self.mu.Lock()
defer self.mu.Unlock()
return self.reader_concurrency
}
func (self *Server) Close() {
close(self.done)
if self.throttler != nil {
self.throttler.Close()
}
}
func NewServer(ctx context.Context,
config_obj *config_proto.Config,
wg *sync.WaitGroup) (*Server, error) {
if config_obj.Frontend == nil {
return nil, errors.New("Frontend not configured")
}
manager, err := crypto_server.NewServerCryptoManager(ctx, config_obj, wg)
if err != nil {
return nil, err
}
// This number mainly affects memory use during large tranfers
// as it controls the number of concurrent clients that may be
// transferring data (each will use some memory to
// buffer). This should not be too large relative to the
// available CPU cores.
concurrency := config_obj.Frontend.Resources.Concurrency
if concurrency == 0 {
concurrency = 2 * uint64(runtime.GOMAXPROCS(0))
}
concurrency_timeout := config_obj.Frontend.Resources.ConcurrencyTimeout
if concurrency_timeout == 0 {
concurrency_timeout = 600
}
result := Server{
manager: manager,
logger: logging.GetLogger(config_obj, &logging.FrontendComponent),
concurrency_timeout: time.Duration(concurrency_timeout) * time.Second,
done: make(chan bool),
}
result.concurrency = utils.NewConcurrencyControl(
int(concurrency), result.concurrency_timeout)
result.reader_concurrency = utils.NewConcurrencyControl(
int(100), result.concurrency_timeout)
if config_obj.Frontend.Resources.ConnectionsPerSecond > 0 {
result.logger.Info("Throttling connections to %v QPS",
config_obj.Frontend.Resources.ConnectionsPerSecond)
result.throttler = utils.NewThrottler(config_obj.Frontend.Resources.ConnectionsPerSecond)
}
if config_obj.Frontend.Resources.GlobalUploadRate > 0 {
result.logger.Info("Global upload rate set to %v bytes per second",
config_obj.Frontend.Resources.GlobalUploadRate)
result.Bucket = ratelimit.NewBucketWithRate(
float64(config_obj.Frontend.Resources.GlobalUploadRate),
1024*1024)
}
return &result, nil
}
// We only process enrollment messages when the client is not fully
// authenticated.
func (self *Server) ProcessSingleUnauthenticatedMessage(
ctx context.Context,
message *crypto_proto.VeloMessage) error {
if message.CSR != nil {
org_manager, err := services.GetOrgManager()
if err != nil {
return err
}
config_obj, err := org_manager.GetOrgConfig(message.OrgId)
if err != nil {
return err
}
err = enroll(ctx, config_obj, self, message.CSR)
if err != nil {
self.logger.Error(fmt.Sprintf("Enrol Error: %s", err))
}
return err
}
return nil
}
func (self *Server) ProcessUnauthenticatedMessages(
ctx context.Context, config_obj *config_proto.Config,
message_info *crypto.MessageInfo) error {
return message_info.IterateJobs(ctx, config_obj,
self.ProcessSingleUnauthenticatedMessage)
}
func (self *Server) DecryptForReader(ctx context.Context, request []byte) (
*crypto.MessageInfo, error) {
// Keep track of the average time the request spends
// waiting for a concurrency slot. If this time is too
// long it means concurrency may need to be increased.
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
concurrencyWaitHistorgram.Observe(v)
}))
cancel, err := self.ReaderConcurrency().StartConcurrencyControl(ctx)
if err != nil {
timeoutCounter.Inc()
return nil, errors.New("Timeout")
}
defer cancel()
timer.ObserveDuration()
return self.Decrypt(ctx, request)
}
func (self *Server) Decrypt(ctx context.Context, request []byte) (
*crypto.MessageInfo, error) {
message_info, err := self.manager.Decrypt(request)
if err != nil {
return nil, err
}
return message_info, nil
}
func (self *Server) Process(
ctx context.Context,
message_info *crypto.MessageInfo,
drain_requests_for_client bool) (
[]byte, int, error) {
// json.TraceMessage(message_info.Source, message_info)
org_manager, err := services.GetOrgManager()
if err != nil {
return nil, 0, err
}
config_obj, err := org_manager.GetOrgConfig(message_info.OrgId)
if err != nil {
return nil, 0, err
}
// Older clients
if message_info.Version < constants.CLIENT_API_VERSION_0_6_8 {
runner := flows.NewLegacyFlowRunner(config_obj)
defer runner.Close(ctx)
err = runner.ProcessMessages(ctx, message_info)
} else {
// Newer clients maintain flow state on the client so need a
// much cheaper flow runner.
runner := flows.NewFlowRunner(ctx, config_obj)
defer runner.Close(ctx)
err = runner.ProcessMessages(ctx, message_info)
}
if err != nil {
return nil, 0, err
}
client_info_manager, err := services.GetClientInfoManager(config_obj)
if err != nil {
return nil, 0, err
}
err = client_info_manager.UpdateStats(ctx, message_info.Source,
&services.Stats{
Ping: uint64(time.Now().UnixNano() / 1000),
IpAddress: message_info.RemoteAddr,
})
if err != nil {
// If we can not read the client_info from disk, we cache a
// fake client_info. This can happen during enrollment when a
// proper client_info is not written yet but hunts are still
// outstanding. Eventually the real client info will be
// properly updated.
err = client_info_manager.Set(ctx, &services.ClientInfo{
actions_proto.ClientInfo{
ClientId: message_info.Source,
Ping: uint64(time.Now().UnixNano() / 1000),
IpAddress: message_info.RemoteAddr,
}})
if err != nil {
return nil, 0, err
}
}
message_list := &crypto_proto.MessageList{}
if drain_requests_for_client {
tasks, err := client_info_manager.GetClientTasks(ctx, message_info.Source)
if err == nil {
message_list.Job = append(message_list.Job, tasks...)
}
}
/*
for i := 0; i < len(message_list.Job); i++ {
json.TraceMessage(message_info.Source+"_out", message_list.Job[i])
}
*/
nonce := ""
if config_obj.Client != nil {
nonce = config_obj.Client.Nonce
}
// Send the client any outstanding tasks.
response, err := self.manager.EncryptMessageList(
message_list,
// Messages sent to clients are typically small and do not
// benefit from compression.
crypto_proto.PackedMessageList_UNCOMPRESSED,
nonce,
message_info.Source)
if err != nil {
return nil, 0, err
}
return response, len(message_list.Job), nil
}
func (self *Server) Error(format string, v ...interface{}) {
self.logger.Error(format, v...)
}
func (self *Server) Info(format string, v ...interface{}) {
self.logger.Info(format, v...)
}
func (self *Server) Debug(format string, v ...interface{}) {
self.logger.Debug(format, v...)
}