forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
191 lines (158 loc) · 5.08 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
/*
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/>.
*/
//
package server
import (
"context"
"errors"
"fmt"
"time"
"github.com/juju/ratelimit"
actions_proto "www.velocidex.com/golang/velociraptor/actions/proto"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
"www.velocidex.com/golang/velociraptor/crypto"
crypto_proto "www.velocidex.com/golang/velociraptor/crypto/proto"
"www.velocidex.com/golang/velociraptor/datastore"
"www.velocidex.com/golang/velociraptor/flows"
"www.velocidex.com/golang/velociraptor/logging"
"www.velocidex.com/golang/velociraptor/paths"
"www.velocidex.com/golang/velociraptor/utils"
)
type Server struct {
config *config_proto.Config
manager *crypto.CryptoManager
logger *logging.LogContext
db datastore.DataStore
// Limit concurrency for processing messages.
concurrency *utils.Concurrency
Bucket *ratelimit.Bucket
Healthy int32
}
func (self *Server) Close() {
self.db.Close()
}
func NewServer(config_obj *config_proto.Config) (*Server, error) {
if config_obj.Frontend == nil {
return nil, errors.New("Frontend not configured")
}
manager, err := crypto.NewServerCryptoManager(config_obj)
if err != nil {
return nil, err
}
db, err := datastore.GetDB(config_obj)
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).
concurrency := int(config_obj.Frontend.Concurrency)
if concurrency == 0 {
concurrency = 20
}
result := Server{
config: config_obj,
manager: manager,
db: db,
logger: logging.GetLogger(config_obj,
&logging.FrontendComponent),
concurrency: utils.NewConcurrencyControl(concurrency, 60*time.Second),
}
if config_obj.Frontend.GlobalUploadRate > 0 {
result.logger.Info("Global upload rate set to %v bytes per second",
config_obj.Frontend.GlobalUploadRate)
result.Bucket = ratelimit.NewBucketWithRate(
float64(config_obj.Frontend.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.GrrMessage) {
if message.CSR != nil {
err := enroll(ctx, self.config, self, message.CSR)
if err != nil {
self.logger.Error(fmt.Sprintf("Enrol Error: %s", err))
}
}
}
func (self *Server) ProcessUnauthenticatedMessages(
ctx context.Context,
message_info *crypto.MessageInfo) error {
return message_info.IterateJobs(ctx, self.ProcessSingleUnauthenticatedMessage)
}
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) {
runner := flows.NewFlowRunner(self.config)
defer runner.Close()
err := runner.ProcessMessages(ctx, message_info)
if err != nil {
return nil, 0, err
}
// Record some stats about the client.
client_info := &actions_proto.ClientInfo{
Ping: uint64(time.Now().UTC().UnixNano() / 1000),
IpAddress: message_info.RemoteAddr,
}
client_path_manager := paths.NewClientPathManager(message_info.Source)
err = self.db.SetSubject(
self.config, client_path_manager.Ping().Path(), client_info)
if err != nil {
return nil, 0, err
}
message_list := &crypto_proto.MessageList{}
if drain_requests_for_client {
message_list.Job = append(
message_list.Job,
self.DrainRequestsForClient(message_info.Source)...)
}
// Messages sent to clients are typically small and we do not
// benefit from compression.
response, err := self.manager.EncryptMessageList(
message_list,
crypto_proto.PackedMessageList_UNCOMPRESSED,
message_info.Source)
if err != nil {
return nil, 0, err
}
return response, len(message_list.Job), nil
}
func (self *Server) DrainRequestsForClient(client_id string) []*crypto_proto.GrrMessage {
result, err := self.db.GetClientTasks(self.config, client_id, false)
if err == nil {
return result
}
return []*crypto_proto.GrrMessage{}
}
func (self *Server) Error(msg string, err error) {
self.logger.Error(fmt.Sprintf(msg, err))
}
func (self *Server) Info(format string, v ...interface{}) {
self.logger.Info(format, v...)
}