-
Notifications
You must be signed in to change notification settings - Fork 2
/
hivInfra.go
1500 lines (1204 loc) · 45.4 KB
/
hivInfra.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
//{{{{{{{ Terraforming Functions }}}}}}}
//By Rebujacker - Alvaro Folgado Rueda as an open source educative project
package main
import (
"os"
"os/exec"
"fmt"
"bytes"
"encoding/json"
)
/*
JSON Structures for De-Serialzing Operators Commands and get Infra data electronGUI jobs
These JSON structure will be used to fill up Terraform plans and execute them on Implant Generation
*/
/// Vps Parameters
type Amazon struct{
Accesskey string `json:"accesskey"`
Secretkey string `json:"secretkey"`
Region string `json:"region"`
Ami string `json:"ami"`
Sshkeyname string `json:"sshkeyname"`
Sshkey string `json:"sshkey"`
}
/// Domain Parameters
type Godaddy struct{
Domainkey string `json:"domainkey"`
Domainsecret string `json:"domainsecret"`
}
//Two different JSon to transform paramters to a data form with more info for red/bichito
type GmailP struct {
Creds string `json:"creds"`
Token string `json:"token"`
}
type Gmail struct {
Name string `json:"name"`
Creds string `json:"creds"`
Token string `json:"token"`
}
/// Staging Parameters
type Droplet struct{
HttpsPort string `json:"httpsport"`
Path string `json:"path"`
}
type Msf struct{
HttpsPort string `json:"httpsport"`
}
type Empire struct{
HttpsPort string `json:"httpsport"`
}
/*
Implants
This next section focus on the creation of Implants Infraestructure
*/
/*
Description: Generate Network Infraestructure for an Implant --> Redirectors,using terraform.
Terraform Scripts are dynamically generated merging strings from templates,related to modules.
Flow:
A.Prepare the Implant folder within Hive (Infra folder to save scripts and terraform binary)
B.To create the redirector plans from templates Script, switch between the different network modules (each network module has his own template script)
C.Select one network module, and retreive the template with the network module variables merged within (ports,domains,etc...)
C1. There is a differene between "SaaS" modules and normal ones in creation.SaaS will generate just one server.
On the other hand, normal network modules could have more than one redirector, so a inner loop will be needed.
This loop will generate a new section of terraform plan that will be appended dynamically to the Implant Plan.
D.Write the plan within the infra folder of an Implant, and execute the plan
*/
func generateImplantInfra(implantpath string,coms string,comsparams []string,redirectors []Red) string{
var (
//Object/Byte buffers
errbuf bytes.Buffer
vps *Vps
domainO *Domain
//Terraform Plan vars
vps_plan_string string
domain_plan_string string
//Error vars
errVps string
errDomain string
)
//Create Implant Infra Folder
infraFolder := exec.Command("/bin/sh","-c", "mkdir "+implantpath+"/infra;cp -r /usr/local/STHive/sources/src/infra/terraform "+implantpath+"/infra/")
infraFolder.Stderr = &errbuf
infraFolder.Start()
infraFolder.Wait()
infraFolderErr := errbuf.String()
if (infraFolderErr != "") {
errorT := fmt.Sprintf("%s%s",infraFolderErr)
elog := fmt.Sprintf("%s%s","InfraFolderCreation(ImplantGeneration):",errorT)
return elog
}
plan, err := os.Create(implantpath+"/infra/implant.tf")
if err != nil {
elog := fmt.Sprintf("%s%s","InfraFolderCreation(ImplantGeneration):",err)
return elog
}
defer plan.Close()
//Switch between network modules to select one terraform template,and merge vars into it (port,domain,...)
switch coms{
/*
SaaS network modules:
Is always one server. Redirectors are mapped against "connected apps" instead.
They don't have a domain Infrastructure
*/
case "gmailgo":
vps = getVpsFullDB(redirectors[0].Vps)
domainO = getDomainFullDB(redirectors[0].Domain)
//Create plans for redirector VPS
switch vps.Vtype{
case "aws_instance":
vps_plan_string,errVps = aws_instance_saas(vps,implantpath,domainO)
if errVps != "Success"{
return errVps
}
}
if _, err = plan.WriteString(vps_plan_string); err != nil {
elog := fmt.Sprintf("%s%s","InfraFolderCreation(ImplantGeneration):",err)
return elog
}
case "gmailmimic":
vps = getVpsFullDB(redirectors[0].Vps)
domainO = getDomainFullDB(redirectors[0].Domain)
//Create plans for redirector VPS
switch vps.Vtype{
case "aws_instance":
vps_plan_string,errVps = aws_instance_saas(vps,implantpath,domainO)
if errVps != "Success"{
return errVps
}
}
if _, err = plan.WriteString(vps_plan_string); err != nil {
elog := fmt.Sprintf("%s%s","InfraFolderCreation(ImplantGeneration):",err)
return elog
}
/*
Normal network modules:
They can have multiple redirectors, so a loop within each is necessary to write within the terraform Plan
each redirector appearance.
*/
case "paranoidhttpsgo":
for _,red := range redirectors{
//To change by DB,need pulling out DB row elements of each by name...
vps = getVpsFullDB(red.Vps)
domainO = getDomainFullDB(red.Domain)
//Create plans for redirector VPS
switch vps.Vtype{
case "aws_instance":
vps_plan_string,errVps = aws_instance_paranoidhttpsgo(comsparams[0],vps,implantpath,domainO)
if errVps != "Success"{
return errVps
}
}
if _, err = plan.WriteString(vps_plan_string); err != nil {
elog := fmt.Sprintf("%s%s","InfraFolderCreation(ImplantGeneration):",err)
return elog
}
// Create plans for the domain/saas
switch domainO.Dtype{
case "godaddy":
domain_plan_string,errDomain = godaddy(vps,domainO)
if errDomain != "Success"{
return errDomain
}
}
if _, err = plan.WriteString(domain_plan_string); err != nil {
elog := fmt.Sprintf("%s%s","InfraFolderCreation(ImplantGeneration):",err)
return elog
}
}
case "selfsignedhttpsgo":
for _,red := range redirectors{
//To change by DB,need pulling out DB row elements of each by name...
vps = getVpsFullDB(red.Vps)
domainO = getDomainFullDB(red.Domain)
//Create plans for redirector VPS
switch vps.Vtype{
case "aws_instance":
vps_plan_string,errVps = aws_instance_paranoidhttpsgo(comsparams[0],vps,implantpath,domainO)
if errVps != "Success"{
return errVps
}
}
if _, err = plan.WriteString(vps_plan_string); err != nil {
elog := fmt.Sprintf("%s%s","InfraFolderCreation(ImplantGeneration):",err)
return elog
}
// Create plans for the domain/saas
switch domainO.Dtype{
case "godaddy":
domain_plan_string,errDomain = godaddy(vps,domainO)
if errDomain != "Success"{
return errDomain
}
}
if _, err = plan.WriteString(domain_plan_string); err != nil {
elog := fmt.Sprintf("%s%s","InfraFolderCreation(ImplantGeneration):",err)
return elog
}
}
}
//Execute the plan with terraform
infralog, err := os.Create(implantpath+"/infra/infra.log")
if err != nil {
elog := fmt.Sprintf("%s%s","InfraLogCreation(ImplantGeneration):",err)
return elog
}
defer infralog.Close()
var initOutbuf,applyOutbuf,errInitbuf,errApplybuf bytes.Buffer
terraInit := exec.Command("/bin/sh","-c", "cd "+implantpath+"/infra;sudo ./terraform init -input=false ")
terraInit.Stdout = &initOutbuf
terraInit.Stdout = &errInitbuf
terraInit.Start()
errInfra1 := terraInit.Wait()
terraApply := exec.Command("/bin/sh","-c", "cd "+implantpath+"/infra;sudo ./terraform apply -input=false -auto-approve")
terraApply.Stdout = &applyOutbuf
terraApply.Stdout = &errApplybuf
terraApply.Start()
errInfra2 := terraApply.Wait()
//Let's save terraform output in log files
terraInitOut := initOutbuf.String()
terraInitError := errInitbuf.String()
terraApplyOut := applyOutbuf.String()
terraApplyError := errApplybuf.String()
//Some Logging
if _, err = infralog.WriteString("OutInit: "+terraInitOut+"ErrInit:"+terraInitError+"OutApply: "+terraApplyOut+"ErrApply: "+terraApplyError); err != nil {
elog := fmt.Sprintf("%s%s","InfraFolderCreation(ImplantGeneration):",err)
return elog
}
if (errInfra1 != nil){
elog := "TerraformerError(ImplantGeneration)-- Init: "+errInfra1.Error()
return elog
}
if (errInfra2 != nil){
elog := "TerraformerError(ImplantGeneration)(Normally happens because incorrect VPS/Domain credentials)-- Apply: "+errInfra2.Error() +"--"+errApplybuf.String()
return elog
}
return "Done"
}
/*VPC Creation Plans -->
These functions will be very similar and change small elements between modules. The general Flow:
A.Retrieve every needed variable for target module to merge them within a template
B.Terraform Template Explained:
B1.Seelect the provider (AWS,AZURE,...)
B2.Define the security group to be created(ports that will be opened, etc...)
B3.Define Instance itself (assign the security group,the type,...)
B4.Terraform-Script: Set hostname, upload redirector binary,provide permissions,folder...
B5.Terraform-Script: Upload Pem keys
B6.Upload Pre-defined Service for redirectors (./src/redirector/redirector.service).
B7.Terraform-Script: Enable Redirector Service in target machine,reboot.
*/
func aws_instance_paranoidhttpsgo(comsparams string,vps *Vps,implantpath string,domainO *Domain) (string,string){
var vps_plan_string string
redport := comsparams
var amazon *Amazon
errDaws := json.Unmarshal([]byte(vps.Parameters), &amazon)
if errDaws != nil {
elog := fmt.Sprintf("%s%s","InfraFolderCreation(Amazon Parameters Decoding Error):",errDaws.Error())
return vps_plan_string,elog
}
accesskey := amazon.Accesskey
secretkey := amazon.Secretkey
region := amazon.Region
keyname := amazon.Sshkeyname
ami := amazon.Ami
keypath := implantpath+"/infra/"+domainO.Name+".pem"
key_String := amazon.Sshkey
//create key for target aws in target implant
vpskey, err := os.Create(implantpath+"/infra/"+domainO.Name+".pem")
if err != nil {
elog := fmt.Sprintf("%s%s","InfraFolderCreation(ImplantGeneration):",err)
return vps_plan_string,elog
}
defer vpskey.Close()
if _, err = vpskey.WriteString(key_String); err != nil {
elog := fmt.Sprintf("%s%s","InfraFolderCreation(ImplantGeneration):",err)
return vps_plan_string,elog
}
vps_plan_string =
fmt.Sprintf(
`
provider "aws" {
alias = "%s"
access_key = "%s"
secret_key = "%s"
region = "%s"
}
resource "aws_security_group" "%s" {
provider = aws.%s
name = "%s"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = %s
to_port = %s
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_instance" "%s" {
provider = aws.%s
depends_on = [aws_security_group.%s]
ami = "%s"
instance_type = "t2.micro"
key_name = "%s"
security_groups = ["%s"]
provisioner "remote-exec" {
inline = [
"sudo hostnamectl set-hostname %s",
"sudo mkdir /usr/local/redirector",
"sudo chown ubuntu:ubuntu -R /usr/local/redirector",
]
connection {
type = "ssh"
user = "ubuntu"
private_key = "${file("%s")}"
host = "${aws_instance.%s.public_ip}"
}
}
provisioner "file" {
source = "%s/red.key"
destination = "/usr/local/redirector/red.key"
connection {
type = "ssh"
user = "ubuntu"
private_key = "${file("%s")}"
host = "${aws_instance.%s.public_ip}"
}
}
provisioner "file" {
source = "%s/red.pem"
destination = "/usr/local/redirector/red.pem"
connection {
type = "ssh"
user = "ubuntu"
private_key = "${file("%s")}"
host = "${aws_instance.%s.public_ip}"
}
}
provisioner "file" {
source = "%s/redirector"
destination = "/usr/local/redirector/redirector"
connection {
type = "ssh"
user = "ubuntu"
private_key = "${file("%s")}"
host = "${aws_instance.%s.public_ip}"
}
}
provisioner "file" {
source = "/usr/local/STHive/sources/src/redirector/redirector.service"
destination = "/usr/local/redirector/redirector.service"
connection {
type = "ssh"
user = "ubuntu"
private_key = "${file("%s")}"
host = "${aws_instance.%s.public_ip}"
}
}
provisioner "remote-exec" {
inline = [
"sudo chmod +x /usr/local/redirector/redirector",
"sudo cp /usr/local/redirector/redirector.service /etc/systemd/system/",
"sudo chmod 664 /etc/systemd/system/redirector.service",
"sudo systemctl daemon-reload",
"sudo systemctl enable redirector.service",
"sudo reboot",
]
on_failure = continue
connection {
type = "ssh"
user = "ubuntu"
private_key = "${file("%s")}"
host = "${aws_instance.%s.public_ip}"
}
}
}`,domainO.Name,accesskey,secretkey,region,domainO.Name,domainO.Name,domainO.Name,redport,redport,domainO.Name,domainO.Name,domainO.Name,ami,keyname,domainO.Name,domainO.Domain,keypath,domainO.Name,implantpath,keypath,domainO.Name,implantpath,keypath,domainO.Name,implantpath,keypath,domainO.Name,keypath,domainO.Name,keypath,domainO.Name)
return vps_plan_string,"Success"
}
func aws_instance_saas(vps *Vps,implantpath string,domainO *Domain) (string,string){
var vps_plan_string string
var amazon *Amazon
errDaws := json.Unmarshal([]byte(vps.Parameters), &amazon)
if errDaws != nil {
elog := fmt.Sprintf("%s%s","InfraFolderCreation(Amazon Parameters Decoding Error):",errDaws.Error())
return vps_plan_string,elog
}
accesskey := amazon.Accesskey
secretkey := amazon.Secretkey
region := amazon.Region
keyname := amazon.Sshkeyname
ami := amazon.Ami
keypath := implantpath+"/infra/"+domainO.Name+".pem"
key_String := amazon.Sshkey
//fmt.Println(accesskey,secretkey,region,ami,keypath,key_String)
//create key for target aws in target implant
vpskey, err := os.Create(implantpath+"/infra/"+domainO.Name+".pem")
if err != nil {
elog := fmt.Sprintf("%s%s","InfraFolderCreation(ImplantGeneration):",err)
return vps_plan_string,elog
}
defer vpskey.Close()
if _, err = vpskey.WriteString(key_String); err != nil {
elog := fmt.Sprintf("%s%s","InfraFolderCreation(ImplantGeneration):",err)
return vps_plan_string,elog
}
vps_plan_string =
fmt.Sprintf(
`
provider "aws" {
alias = "%s"
access_key = "%s"
secret_key = "%s"
region = "%s"
}
resource "aws_security_group" "%s" {
provider = aws.%s
name = "%s"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_instance" "%s" {
provider = aws.%s
depends_on = [aws_security_group.%s]
ami = "%s"
instance_type = "t2.micro"
key_name = "%s"
security_groups = ["%s"]
provisioner "remote-exec" {
inline = [
"sudo hostnamectl set-hostname %s",
"sudo mkdir /usr/local/redirector",
"sudo chown ubuntu:ubuntu -R /usr/local/redirector",
]
connection {
type = "ssh"
user = "ubuntu"
private_key = "${file("%s")}"
host = "${aws_instance.%s.public_ip}"
}
}
provisioner "file" {
source = "%s/redirector"
destination = "/usr/local/redirector/redirector"
connection {
type = "ssh"
user = "ubuntu"
private_key = "${file("%s")}"
host = "${aws_instance.%s.public_ip}"
}
}
provisioner "file" {
source = "/usr/local/STHive/sources/src/redirector/redirector.service"
destination = "/usr/local/redirector/redirector.service"
connection {
type = "ssh"
user = "ubuntu"
private_key = "${file("%s")}"
host = "${aws_instance.%s.public_ip}"
}
}
provisioner "remote-exec" {
inline = [
"sudo chmod +x /usr/local/redirector/redirector",
"sudo cp /usr/local/redirector/redirector.service /etc/systemd/system/",
"sudo chmod 664 /etc/systemd/system/redirector.service",
"sudo systemctl daemon-reload",
"sudo systemctl enable redirector.service",
"sudo reboot",
]
on_failure = continue
connection {
type = "ssh"
user = "ubuntu"
private_key = "${file("%s")}"
host = "${aws_instance.%s.public_ip}"
}
}
}`,domainO.Name,accesskey,secretkey,region,domainO.Name,domainO.Name,domainO.Name,domainO.Name,domainO.Name,domainO.Name,ami,keyname,domainO.Name,domainO.Domain,keypath,domainO.Name,implantpath,keypath,domainO.Name,keypath,domainO.Name,keypath,domainO.Name)
return vps_plan_string,"Success"
}
/*Domains Creation Plans -->
These functions will be very similar and change small elements between Domain modules. The general Flow:
A.Retrieve every needed variable for target module to merge them within a template
*/
func godaddy(vps *Vps,domainO *Domain) (string,string){
var domain_plan_string string
var godaddy *Godaddy
errDaws := json.Unmarshal([]byte(domainO.Parameters), &godaddy)
if errDaws != nil {
elog := fmt.Sprintf("%s%s","InfraFolderCreation(Godaddy Parameters Decoding Error):",errDaws.Error())
return domain_plan_string,elog
}
//Rmoved: nameservers = ["ns7.domains.com", "ns6.domains.com"]
domainkey := godaddy.Domainkey
domainsecret := godaddy.Domainsecret
domain_plan_string =
fmt.Sprintf(
`
provider "godaddy" {
alias = "%s"
key = "%s"
secret = "%s"
}
resource "godaddy_domain_record" "%s" {
provider = godaddy.%s
domain = "%s"
depends_on = ["%s.%s"]
addresses = ["${%s.%s.public_ip}"]
}`,domainO.Name,domainkey,domainsecret,domainO.Name,domainO.Name,domainO.Domain,vps.Vtype,domainO.Name,vps.Vtype,domainO.Name)
return domain_plan_string,"Success"
}
//Utility Infra Implant Functions: Interact with resources, to destroy them, or hide stuff...
/*
Description: Destroy target Implant Infraestructure
Flow:
A.Execute terraform destroy over the cretion plans
B.Check errors to make sure is removed
*/
func destroyImplantInfra(implantpath string) string{
terraApply := exec.Command("/bin/sh","-c", "cd "+implantpath+"/infra;sudo ./terraform destroy -auto-approve")
terraApply.Start()
errInfra1 := terraApply.Wait()
if (errInfra1 != nil){
elog := "TerraformerError(ImplantDestroy)-- Destroy: "+errInfra1.Error()
return elog
}
return "Done"
}
/*
Stagings
This next section focus on the creation of Staging Servers Infraestructure
*/
/*
Description: Generate Staging Servers using terraform. Terraform Scripts are dynamically generated merging strings from templates,related to modules.
Flow:
A.Prepare the Staging folder within Hive (Infra folder to save scripts and terraform binary)
B.Prepare files that will be needed for Staging deployment:
B1.Terraform Plan
B2.Post-Creation Script (this will be used to configure metasploit,empire,ssh severs...)
B3.Linux Service for Stagings
B4.Removal Script for staging asset destroy later on
C.Select one type of Staging to create the target "Post-Creation" Script. Merge each type of parameters with templates.
Note: The network port to be used within the VPC plan creation, will be returned by the Staging Script creation, since it will be part of the params
D.Select one Infrastructure type for both VPC/Domain, create the terraform plan by merging variables
E.Deploy Staging with terraform
F.Run scripts to install target Post-Exploitation Software
*/
func generateStagingInfra(stagingName string,stype string,tunnelPort string,parameters string,vpsName string,domainName string) string{
var (
//Object/Bytes buffers
errbuf bytes.Buffer
vps *Vps
domainO *Domain
port string
//Terraform Plan vars
vps_plan_string string
domain_plan_string string
installScript_string string
removeScript_string string
serviceScript_string string
//Error vars
errVps string
errDomain string
errStaging string
)
//To change by DB,need pulling out DB row elements of each by name...
vps = getVpsFullDB(vpsName)
domainO = getDomainFullDB(domainName)
keypath := "/usr/local/STHive/stagings/"+stagingName+"/"+domainO.Name+".pem"
//Create Stager Infra Folder
infraFolder := exec.Command("/bin/sh","-c", "cp -r /usr/local/STHive/sources/src/infra/terraform /usr/local/STHive/stagings/"+stagingName+"/")
infraFolder.Stderr = &errbuf
infraFolder.Start()
infraFolder.Wait()
infraFolderErr := errbuf.String()
if (infraFolderErr != "") {
errorT := fmt.Sprintf("%s%s",infraFolderErr)
elog := fmt.Sprintf("%s%s","StagingFolderCreation(StagingGeneration):",errorT)
return elog
}
//Create files where the different merged strings template will be written
plan, err := os.Create("/usr/local/STHive/stagings/"+stagingName+"/staging.tf")
if err != nil {
elog := fmt.Sprintf("%s%s","StagingTFCreation(StagingGeneration):",err)
return elog
}
defer plan.Close()
installScript, err := os.Create("/usr/local/STHive/stagings/"+stagingName+"/installScript.sh")
if err != nil {
elog := fmt.Sprintf("%s%s","StagingScriptCreation(StagingGeneration):",err)
return elog
}
defer installScript.Close()
serviceScript, err := os.Create("/usr/local/STHive/stagings/"+stagingName+"/"+stagingName+".service")
if err != nil {
elog := fmt.Sprintf("%s%s","StagingServiceCreation(StagingGeneration):",err)
return elog
}
defer serviceScript.Close()
removeScript, err := os.Create("/usr/local/STHive/stagings/"+stagingName+"/removeScript.sh")
if err != nil {
elog := fmt.Sprintf("%s%s","StagingScriptCreation(StagingGeneration):",err)
return elog
}
defer removeScript.Close()
//Select Post/Staging type for create a particular installation script
switch stype{
case "https_droplet_letsencrypt":
installScript_string,port,errStaging = staging_https_droplet_letsencrypt(parameters,domainO,keypath,stagingName)
if errStaging != "Success"{
return errStaging
}
case "ssh_rev_shell":
installScript_string,port,errStaging = staging_ssh_rev_shell(domainO,keypath,stagingName)
if errStaging != "Success"{
return errStaging
}
case "https_msft_letsencrypt":
installScript_string,port,errStaging = staging_https_msft_letsencrypt(parameters,domainO,keypath,stagingName)
if errStaging != "Success"{
return errStaging
}
case "https_empire_letsencrypt":
installScript_string,port,errStaging = staging_https_empire_letsencrypt(parameters,domainO,keypath,stagingName)
if errStaging != "Success"{
return errStaging
}
}
//Prepare Service/Removal scripts
serviceScript_string = staging_tunnelService(domainO,tunnelPort,stagingName)
removeScript_string = staging_removeScript(stagingName)
//Generate a terraform plan for one VPC type
switch vps.Vtype{
//String with awsPlan
case "aws_instance":
vps_plan_string,errVps = staging_aws_instance(vps,keypath,domainO,port)
if errVps != "Success"{
return errVps
}
}
//Generate a terraform plan for one Domain type
switch domainO.Dtype{
case "godaddy":
domain_plan_string,errDomain = staging_godaddy(vps,domainO,domainName)
if errDomain != "Success"{
return errDomain
}
}
//Write all strings in previously created files
if _, err = serviceScript.WriteString(serviceScript_string); err != nil {
elog := fmt.Sprintf("%s%s","StagingServiceWrite(StagingGeneration):",err)
return elog
}
if _, err = installScript.WriteString(installScript_string); err != nil {
elog := fmt.Sprintf("%s%s","StagingScriptWrite(StagingGeneration):",err)
return elog
}
if _, err = removeScript.WriteString(removeScript_string); err != nil {
elog := fmt.Sprintf("%s%s","StagingScriptWrite(StagingGeneration):",err)
return elog
}
if _, err = plan.WriteString(vps_plan_string); err != nil {
elog := fmt.Sprintf("%s%s","StagingTFWrite(StagingGeneration):",err)
return elog
}
if _, err = plan.WriteString(domain_plan_string); err != nil {
elog := fmt.Sprintf("%s%s","StagingCreation(TF Domain Write Error):",err)
return elog
}
//Prepare Logs to register terraform errors
infralog, err := os.Create("/usr/local/STHive/stagings/"+stagingName+"/"+"infra.log")
if err != nil {
elog := fmt.Sprintf("%s%s","InfraLogCreation(ImplantGeneration):",err)
return elog
}
defer infralog.Close()
//Execute terraform plans
var initOutbuf,applyOutbuf,errInitbuf,errApplybuf bytes.Buffer
terraInit := exec.Command("/bin/sh","-c", "cd /usr/local/STHive/stagings/"+stagingName+";sudo ./terraform init -input=false ")
terraInit.Stdout = &initOutbuf
terraInit.Stdout = &errInitbuf
terraInit.Start()
errInfra1 := terraInit.Wait()
terraApply := exec.Command("/bin/sh","-c", "cd /usr/local/STHive/stagings/"+stagingName+";sudo ./terraform apply -input=false -auto-approve")
terraApply.Stdout = &applyOutbuf
terraApply.Stdout = &errApplybuf
terraApply.Start()
errInfra2 := terraApply.Wait()
//Let's save terraform output in log files
terraInitOut := initOutbuf.String()
terraInitError := errInitbuf.String()
terraApplyOut := applyOutbuf.String()
terraApplyError := errApplybuf.String()
if (errInfra1 != nil){
elog := "TerraformerError(StagingGeneration)-- Init: "+errInfra1.Error()
return elog
}
if (errInfra2 != nil){
elog := "TerraformerError(StagingGeneration)--(Normally happens because incorrect VPS/Domain credentials)-Apply: "+errInfra2.Error()+"--"+errApplybuf.String()
return elog
}
//Apply Staging Script
//The Staging Script will:
// A. Wait 3 min for Domain Re-Freshment and Install required certificates/software in target server
// B. Install a tunneling Service on hive to open the staging SSH to ST Clients
var scriptOutbuf,scriptErrbuf bytes.Buffer
instScript := exec.Command("/bin/bash","/usr/local/STHive/stagings/"+stagingName+"/installScript.sh")
instScript.Stdout = &scriptOutbuf
instScript.Stderr = &scriptErrbuf
instScript.Start()
instScript.Wait()
if _, err = infralog.WriteString("OutInit: "+terraInitOut+"ErrInit:"+terraInitError+"OutApply: "+terraApplyOut+"ErrApply: "+terraApplyError+"ScriptOut: "+scriptOutbuf.String()+"ScriptError: "+scriptErrbuf.String()); err != nil {
elog := fmt.Sprintf("%s%s","InfraFolderCreation(ImplantGeneration):",err)
return elog
}
return "Done"
}
/*Staging VPC/Domains Creation Plans -->
These functions will be very similar and change small elements between modules. The general Flow:
Flows are similar to the Implant ones
*/
func staging_aws_instance(vps *Vps,keypath string,domainO *Domain,port string) (string,string){
var vps_plan_string string
var amazon *Amazon
errDaws := json.Unmarshal([]byte(vps.Parameters), &amazon)
if errDaws != nil {
elog := fmt.Sprintf("%s%s","StagingCreation(Staging Amazon Parameters Decoding Error):",errDaws)
return vps_plan_string,elog
}
accesskey := amazon.Accesskey
secretkey := amazon.Secretkey
region := amazon.Region
keyname := amazon.Sshkeyname
ami := amazon.Ami
key_String := amazon.Sshkey
vpskey, err := os.Create(keypath)
if err != nil {
elog := fmt.Sprintf("%s%s","StagingVpsPemFileCreation(StagingGeneration):",err)
return vps_plan_string,elog
}
defer vpskey.Close()
if _, err = vpskey.WriteString(key_String); err != nil {
elog := fmt.Sprintf("%s%s","StagingVpsPemFileWriteCreation(StagingGeneration):",err)
return vps_plan_string,elog
}
vps_plan_string =
fmt.Sprintf(
`
provider "aws" {
alias = "%s"
access_key = "%s"
secret_key = "%s"
region = "%s"
}
resource "aws_security_group" "%s" {
provider = aws.%s
name = "%s"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = %s
to_port = %s
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}