-
Notifications
You must be signed in to change notification settings - Fork 794
/
agentService.go
365 lines (327 loc) · 11.6 KB
/
agentService.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
Merlin is a post-exploitation command and control framework.
This file is part of Merlin.
Copyright (C) 2023 Russel Van Tuyl
Merlin is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
Merlin 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with Merlin. If not, see <http://www.gnu.org/licenses/>.
*/
package rpc
import (
// Standard
"context"
"fmt"
"log/slog"
"strings"
"time"
// 3rd Party
"github.com/google/uuid"
"google.golang.org/protobuf/types/known/emptypb"
// Internal
"github.com/Ne0nd0g/merlin/v2/pkg/agents"
"github.com/Ne0nd0g/merlin/v2/pkg/logging"
pb "github.com/Ne0nd0g/merlin/v2/pkg/rpc"
)
/* RPC METHODS TO INTERACT WITH THE AGENT SERVICE */
// agentToAgentInfo converts a server-side Agent structure into a protobuf AgentInfo structure
func (s *Server) agentToAgentInfo(a agents.Agent) *pb.AgentInfo {
build := &pb.Build{
Build: a.Build().Build,
Version: a.Build().Version,
}
host := &pb.Host{
Architecture: a.Host().Architecture,
Platform: a.Host().Platform,
Name: a.Host().Name,
IPs: a.Host().IPs,
}
comms := &pb.Comms{
Failed: int32(a.Comms().Failed),
JA3: a.Comms().JA3,
KillDate: a.Comms().Kill,
Padding: int32(a.Comms().Padding),
Protocol: a.Comms().Proto,
Retry: int32(a.Comms().Retry),
Skew: a.Comms().Skew,
Wait: a.Comms().Wait,
}
process := &pb.Process{
ID: int32(a.Process().ID),
IntegrityLevel: int32(a.Process().Integrity),
Name: a.Process().Name,
Username: a.Process().UserName,
UserGUID: a.Process().UserGUID,
Domain: a.Process().Domain,
}
var links []string
for _, link := range a.Links() {
links = append(links, link.String())
}
status, err := s.agentService.Status(a.ID())
if err != nil {
slog.Error(err.Error())
}
// Create the AgentInfo structure
agentInfo := &pb.AgentInfo{
ID: a.ID().String(),
Alive: a.Alive(),
Authenticated: a.Authenticated(),
Build: build,
Host: host,
Comms: comms,
Process: process,
InitialCheckin: a.Initial().Format(time.RFC3339),
LastCheckin: a.StatusCheckin().Format(time.RFC3339),
Listener: a.Listener().String(),
Links: links,
Note: a.Note(),
Status: status,
Groups: s.agentService.Groups(),
}
return agentInfo
}
// GetAgent returns Agent configuration information for the provided id
func (s *Server) GetAgent(ctx context.Context, id *pb.ID) (agentInfo *pb.AgentInfo, err error) {
slog.Log(context.Background(), logging.LevelTrace, "entering into function", "context", ctx, "id", id)
agentInfo = &pb.AgentInfo{}
// Parse the UUID from the request
agentID, err := uuid.Parse(id.Id)
if err != nil {
err = fmt.Errorf("there was an error parsing '%s' as a UUID: %s", id.Id, err)
return
}
// Get the Agent
a, err := s.agentService.Agent(agentID)
if err != nil {
err = fmt.Errorf("there was an error getting agent %s: %s", agentID, err)
return
}
agentInfo = s.agentToAgentInfo(a)
return
}
// GetAgentLinks returns a list of linked child Agent IDs
func (s *Server) GetAgentLinks(ctx context.Context, id *pb.ID) (*pb.Slice, error) {
slog.Log(context.Background(), logging.LevelTrace, "entering into function", "context", ctx, "id", id)
// Parse the UUID from the request
agentID, err := uuid.Parse(id.Id)
if err != nil {
return nil, err
}
links, err := s.agentService.Links(agentID)
if err != nil {
err = fmt.Errorf("there was an error getting the links for Agent %s: %s", agentID, err)
slog.Error(err.Error())
return nil, err
}
var linkIDs []string
for _, link := range links {
linkIDs = append(linkIDs, link.String())
}
return &pb.Slice{Data: linkIDs}, nil
}
// GetAgentRows returns certain pieces of information for all Agents that can later be displayed in a table on the client
func (s *Server) GetAgentRows(ctx context.Context, e *emptypb.Empty) (*pb.TableData, error) {
slog.Log(context.Background(), logging.LevelTrace, "entering into function", "context", ctx, "empty", e)
data := &pb.TableData{}
data.Header = []string{"Agent GUID", "Transport", "Platform", "Host", "User", "Process", "Status", "Last Checkin", "Note"}
var rows []*pb.TableRows
for _, a := range s.agentService.Agents() {
if a.Alive() {
status, err := s.GetAgentStatus(context.TODO(), &pb.ID{Id: a.ID().String()})
if err != nil {
return nil, err
}
lastTime := lastCheckin(a.StatusCheckin())
// Get the process name, sans full path
var proc string
if a.Host().Platform == "windows" {
proc = a.Process().Name[strings.LastIndex(a.Process().Name, "\\")+1:]
} else {
proc = a.Process().Name[strings.LastIndex(a.Process().Name, "/")+1:]
}
p := fmt.Sprintf("%s(%d)", proc, a.Process().ID)
row := []string{
a.ID().String(),
a.Comms().Proto,
a.Host().Platform + "/" + a.Host().Architecture,
a.Host().Name,
a.Process().UserName,
p,
status.Message,
lastTime,
a.Note(),
}
rows = append(rows, &pb.TableRows{Row: row})
}
}
data.Rows = rows
return data, nil
}
// GetAgents returns a list of existing Agent UUID values
func (s *Server) GetAgents(ctx context.Context, e *emptypb.Empty) (*pb.Slice, error) {
slog.Log(context.Background(), logging.LevelTrace, "entering into function", "context", ctx, "empty", e)
var agentIDs []string
for _, a := range s.agentService.Agents() {
if a.Alive() {
agentIDs = append(agentIDs, a.ID().String())
}
}
return &pb.Slice{Data: agentIDs}, nil
}
// GetAgentStatus returns the status of an Agent (e.g., alive, dead, or delayed)
func (s *Server) GetAgentStatus(ctx context.Context, id *pb.ID) (msg *pb.Message, err error) {
slog.Log(context.Background(), logging.LevelTrace, "entering into function", "context", ctx, "id", id)
msg = &pb.Message{}
// Parse the UUID from the request
agentID, err := uuid.Parse(id.Id)
if err != nil {
err = fmt.Errorf("there was an error parsing '%s' as a UUID: %s", id.Id, err)
return
}
a, err := s.agentService.Agent(agentID)
if err != nil {
err = fmt.Errorf("there was an error getting agent %s: %s", agentID, err)
return
}
comms := a.Comms()
dur, errDur := time.ParseDuration(comms.Wait)
if errDur != nil && comms.Wait != "" {
err = fmt.Errorf(fmt.Sprintf("Error converting %s to a time duration: %s", comms.Wait, errDur))
return
}
if comms.Wait == "" {
msg.Message = "Init"
} else if a.StatusCheckin().Add(dur).After(time.Now()) {
msg.Message = "Active"
} else if a.StatusCheckin().Add(dur * time.Duration(comms.Retry+1)).After(time.Now()) { // +1 to account for skew
msg.Message = "Delayed"
} else {
msg.Message = "Dead"
}
return
}
// GroupAdd adds an Agent to the provided group
func (s *Server) GroupAdd(ctx context.Context, in *pb.Group) (msg *pb.Message, err error) {
slog.Log(context.Background(), logging.LevelTrace, "entering into function", "context", ctx, "in", in)
msg = &pb.Message{}
// Parse the UUID from the request
agentUUID, err := uuid.Parse(in.AgentID)
if err != nil {
err = fmt.Errorf("there was an error parsing '%s' as a UUID: %s", in.AgentID, err)
slog.Error(err.Error())
return
}
err = s.agentService.AddAgentToGroup(in.Group, agentUUID)
if err != nil {
err = fmt.Errorf("there was an error adding agent %s to group %s: %s", agentUUID, in.Group, err)
slog.Error(err.Error())
return
}
msg = NewPBSuccessMessage(fmt.Sprintf("Agent %s added to group %s", in.AgentID, in.Group))
return
}
// GroupList lists Agents that are part of a specific group
// id.Id contains the group name as a string
func (s *Server) GroupList(ctx context.Context, id *pb.ID) (*pb.Slice, error) {
slog.Log(context.Background(), logging.LevelTrace, "entering into function", "context", ctx, "id", id)
out := &pb.Slice{}
if _, ok := s.agentService.GroupMembers()[id.Id]; ok {
for _, member := range s.agentService.GroupMembers()[id.Id] {
out.Data = append(out.Data, member.String())
}
}
return out, nil
}
// GroupListAll returns all existing Agent groups and their members
func (s *Server) GroupListAll(ctx context.Context, e *emptypb.Empty) (*pb.GroupMembers, error) {
slog.Log(context.Background(), logging.LevelTrace, "entering into function", "context", ctx, "empty", e)
groups := &pb.GroupMembers{}
for group, members := range s.agentService.GroupMembers() {
var memberIDs []string
for _, member := range members {
memberIDs = append(memberIDs, member.String())
}
groups.Members = append(groups.Members, &pb.GroupMember{Group: group, Members: memberIDs})
}
return groups, nil
}
// GroupRemove removes an Agent from an Agent group
func (s *Server) GroupRemove(ctx context.Context, in *pb.Group) (msg *pb.Message, err error) {
slog.Log(context.Background(), logging.LevelTrace, "entering into function", "context", ctx, "in", in)
msg = &pb.Message{}
// Parse the UUID from the request
agentUUID, err := uuid.Parse(in.AgentID)
if err != nil {
err = fmt.Errorf("there was an error parsing '%s' as a UUID: %s", in.AgentID, err)
slog.Error(err.Error())
return
}
err = s.agentService.RemoveAgentFromGroup(in.Group, agentUUID)
if err != nil {
err = fmt.Errorf("there was an error removing agent %s from group %s: %s", agentUUID, in.Group, err)
slog.Error(err.Error())
return
}
msg = NewPBSuccessMessage(fmt.Sprintf("Agent %s removed from group %s", in.AgentID, in.Group))
return
}
// Groups return a list of all Agent groups on the RPC server
func (s *Server) Groups(ctx context.Context, e *emptypb.Empty) (*pb.Slice, error) {
slog.Log(context.Background(), logging.LevelTrace, "entering into function", "context", ctx, "empty", e)
out := &pb.Slice{}
out.Data = s.agentService.Groups()
return out, nil
}
// lastCheckin returns a nicely formatted string for time since the last checkin (HH:MM:SS)
func lastCheckin(t time.Time) string {
lastTime := time.Since(t)
lastTimeStr := fmt.Sprintf("%d:%02d:%02d ago",
int(lastTime.Hours()),
int(lastTime.Minutes())%60,
int(lastTime.Seconds())%60)
return lastTimeStr
}
// Note sets a note on the Agent's Note field
// args[0:] = the note to set
func (s *Server) Note(ctx context.Context, in *pb.AgentCMD) (msg *pb.Message, err error) {
slog.Log(context.Background(), logging.LevelTrace, "entering into function", "context", ctx, "in", in)
// Parse the UUID from the request
agentID, err := uuid.Parse(in.ID)
if err != nil {
err = fmt.Errorf("there was an error parsing '%s' as a UUID: %s", in.ID, err)
slog.Error(err.Error())
return
}
err = s.agentService.UpdateNote(agentID, strings.Join(in.Arguments, " "))
if err != nil {
err = fmt.Errorf("there was an error updating the agent's note: %s", err)
slog.Error(err.Error())
return
}
msg = NewPBSuccessMessage(fmt.Sprintf("Successfully set note for agent %s", agentID))
return
}
// Remove deletes the agent from the server
func (s *Server) Remove(ctx context.Context, id *pb.ID) (msg *pb.Message, err error) {
slog.Log(context.Background(), logging.LevelTrace, "entering into function", "context", ctx, "id", id)
msg = &pb.Message{}
// Parse the UUID from the request
agentUUID, err := uuid.Parse(id.Id)
if err != nil {
err = fmt.Errorf("there was an error parsing '%s' as a UUID: %s", id, err)
slog.Error(err.Error())
return
}
err = s.agentService.Remove(agentUUID)
if err == nil {
msg = NewPBSuccessMessage(fmt.Sprintf("Agent %s was removed from the server", agentUUID))
}
return
}