-
Notifications
You must be signed in to change notification settings - Fork 758
/
goad.sh
executable file
·641 lines (594 loc) · 19.2 KB
/
goad.sh
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
#!/usr/bin/env bash
ERROR=$(tput setaf 1; echo -n "[!]"; tput sgr0)
OK=$(tput setaf 2; echo -n "[✓]"; tput sgr0)
INFO=$(tput setaf 3; echo -n "[-]"; tput sgr0)
# Global variables
LAB=
PROVIDER=
METHOD=
JOB=
PROVIDERS="virtualbox vmware azure proxmox"
LABS=$(ls -A ad/ |grep -v 'TEMPLATE')
TASKS="check install start stop status restart destroy disablevagrant enablevagrant"
ANSIBLE_PLAYBOOKS="edr.yml build.yml ad-servers.yml ad-parent_domain.yml ad-child_domain.yml ad-members.yml ad-trusts.yml ad-data.yml ad-gmsa.yml laps.yml ad-relations.yml adcs.yml ad-acl.yml servers.yml security.yml vulnerabilities.yml reboot.yml elk.yml sccm-install.yml sccm-config.yml"
METHODS="local docker"
ANSIBLE_ONLY=0
ANSIBLE_PLAYBOOK=
GOAD_VAGRANT_OPTIONS=
GOAD_EXTENSIONS="elk"
print_usage() {
echo "${ERROR} Usage: ./goad.sh -t task -l lab -p provider -m method"
echo "${INFO} -t : task must be one of the following:"
echo " - check : verify dependencies";
echo " - install : create the lab";
echo " - start : start the lab";
echo " - stop : stop the lab";
echo " - restart : reload the lab";
echo " - status : show lab info and status";
echo " - destroy : trash the lab";
echo "${INFO} -l : lab must be one of the following:"
for lab in $LABS; do
echo " - $lab";
done
echo "${INFO} -p : provider must be one of the following:"
for p in $PROVIDERS; do
echo " - $p";
done
echo "${INFO} -m : method must be one of the following (optional, default : local):"
echo " - local : to use local ansible install (default)";
echo " - docker : to use docker ansible install";
echo "${INFO} -a : to run only ansible on install (optional)";
echo "${INFO} -r : to run only one ansible playbook (optional)";
echo " - example : vulnerabilities.yml";
echo "${INFO} -e : to activate extension (separated by coma) (optional)";
for extension in $GOAD_EXTENSIONS; do
echo " - $extension";
done
echo "${INFO} -h : show this help";
echo
echo "${OK} example: ./goad.sh -t check -l GOAD -p virtualbox -m local";
exit 0
}
function exists_in_list() {
LIST=$1
VALUE=$2
echo $LIST | tr " " '\n' | grep -F -q -x "$VALUE"
}
while getopts t:l:p:m:ar:e:h flag
do
case "${flag}" in
t) TASK=${OPTARG};;
l) LAB=${OPTARG};;
p) PROVIDER=${OPTARG};;
m) METHOD=${OPTARG};;
a) ANSIBLE_ONLY=1;;
r) ANSIBLE_PLAYBOOK=${OPTARG};;
e) GOAD_VAGRANT_OPTIONS="$GOAD_VAGRANT_OPTIONS,${OPTARG}";;
h) print_usage; exit;
esac
done
if exists_in_list "$TASKS" "$TASK"; then
echo "${OK} Task: $TASK"
else
echo "${ERROR} Task: \"$TASK\" unknow"
print_usage
fi
if exists_in_list "$LABS" "$LAB"; then
echo "${OK} Lab: $LAB"
else
echo "${ERROR} Lab: $LAB not allowed"
print_usage
fi
if exists_in_list "$PROVIDERS" "$PROVIDER"; then
echo "${OK} Provider: $PROVIDER"
else
echo "${ERROR} Provider: $PROVIDER not allowed"
print_usage
fi
# loop on every extension
for GOAD_EXT in $(echo $GOAD_VAGRANT_OPTIONS | sed "s/,/ /g")
do
if exists_in_list "$GOAD_EXTENSIONS" "$GOAD_EXT"; then
echo "${OK} Extension: $GOAD_EXT"
else
echo "${ERROR} Extension: $GOAD_EXT not allowed"
print_usage
fi
done
if [ -z $METHOD ]; then
METHOD="local"
else
if exists_in_list "$METHODS" "$METHOD"; then
echo "${OK} Method: $METHOD"
else
echo "${ERROR} Method: $METHOD not allowed"
print_usage
fi
fi
if [[ ! -z $ANSIBLE_PLAYBOOK ]]; then
if exists_in_list "$ANSIBLE_PLAYBOOKS" "$ANSIBLE_PLAYBOOK"; then
echo "${OK} Ansible playbook: $ANSIBLE_PLAYBOOK"
else
echo "${ERROR} Ansible playbook: $ANSIBLE_PLAYBOOK not allowed"
print_usage
fi
fi
if [[ "$ANSIBLE_ONLY" -eq 1 ]]; then
echo "${OK} Run ansible only"
fi
# check if the lab provider folder exist
if [[ -d "ad/$LAB/providers/$PROVIDER" ]]; then
echo "${OK} folder ad/$LAB/providers/$PROVIDER found"
else
echo "${ERROR} folder ad/$LAB/providers/$PROVIDER not found"
exit 1
fi
print_azure_info() {
echo -e "\n\n"
echo "Ubuntu jumpbox IP: $public_ip"
echo "You can now connect to the jumpbox using the following command:"
echo "ssh -i ad/$lab/providers/$provider/ssh_keys/ubuntu-jumpbox.pem goad@$public_ip"
echo -e "\n\n"
echo "${OK} ssh/config :"
echo "Host goad_azure"
echo " Hostname $public_ip"
echo " User goad"
echo " Port 22"
echo " IdentityFile $CURRENT_DIR/ad/$lab/providers/$provider/ssh_keys/ubuntu-jumpbox.pem"
}
install_providing(){
lab=$1
provider=$2
case $provider in
"virtualbox"|"vmware")
cd "ad/$lab/providers/$provider"
echo "${OK} launch vagrant"
GOAD_VAGRANT_OPTIONS=$GOAD_VAGRANT_OPTIONS vagrant up
result=$?
if [ ! $result -eq 0 ]; then
cd -
echo "${ERROR} vagrant finish with error abort"
exit 1
fi
cd -
;;
"proxmox")
if [ -d "ad/$lab/providers/$provider/terraform" ]; then
cd "ad/$lab/providers/$provider/terraform"
echo "${OK} Initializing Terraform..."
terraform init
result=$?
if [ ! $result -eq 0 ]; then
echo "${ERROR} terraform init finish with error abort"
exit 1
fi
echo "${OK} Apply Terraform..."
terraform apply
result=$?
if [ ! $result -eq 0 ]; then
echo "${ERROR} terraform apply finish with error abort"
exit 1
fi
echo "${OK} Ready to launch provisioning"
cd -
else
echo "${ERROR} folder ad/$lab/providers/$provider/terraform not found"
exit 1
fi
;;
"azure")
if [ -d "ad/$lab/providers/$provider/terraform" ]; then
cd "ad/$lab/providers/$provider/terraform"
echo "${OK} Initializing Terraform..."
terraform init
result=$?
if [ ! $result -eq 0 ]; then
echo "${ERROR} terraform init finish with error abort"
exit 1
fi
echo "${OK} Apply Terraform..."
terraform apply
result=$?
if [ ! $result -eq 0 ]; then
echo "${ERROR} terraform apply finish with error abort"
exit 1
fi
# Get the public IP address of the VM
echo "${OK} Getting jumpbox IP address..."
public_ip=$(terraform output -raw ubuntu-jumpbox-ip)
print_azure_info
cd -
echo "${OK} Rsync goad to jumpbox"
rsync -a --exclude-from='.gitignore' -e "ssh -o 'StrictHostKeyChecking no' -i $CURRENT_DIR/ad/$lab/providers/$provider/ssh_keys/ubuntu-jumpbox.pem" "$CURRENT_DIR/" goad@$public_ip:~/GOAD/
echo "${OK} Running setup script on jumpbox..."
ssh -o "StrictHostKeyChecking no" -i "ad/$lab/providers/$provider/ssh_keys/ubuntu-jumpbox.pem" goad@$public_ip 'bash -s' <scripts/setup_azure.sh
echo "${OK} Ready to launch provisioning"
else
echo "${ERROR} folder ad/$lab/providers/$provider/terraform not found"
exit 1
fi
;;
esac
}
install_provisioning(){
lab=$1
provider=$2
method=$3
case $provider in
"virtualbox"|"vmware"|"proxmox")
case $method in
"local")
if [ -z $ANSIBLE_PLAYBOOK ]; then
cd ansible
export LAB=$lab PROVIDER=$provider
../scripts/provisionning.sh
cd -
else
cd ansible
ansible-playbook -i ../ad/$lab/data/inventory -i ../ad/$lab/providers/$provider/inventory $ANSIBLE_PLAYBOOK
cd -
fi
;;
"docker")
use_sudo=""
if id -nG "$USER" | grep -qw "docker"; then
echo $USER belongs to docker group
else
echo $USER does not belong to docker group
echo "${INFO} Root password could be asked for docker interaction"
use_sudo="sudo"
fi
ALREADY_BUILD=$($use_sudo docker images |grep -c "goadansible")
if [[ $ALREADY_BUILD -eq 0 ]]; then
echo "[+] Build container"
$use_sudo docker build -t goadansible .
echo "${OK} Container goadansible creation complete"
fi
if [ -z $ANSIBLE_PLAYBOOK ]; then
echo "${OK} Start provisioning from docker"
$use_sudo docker run -ti --rm --network host -h goadansible -v $(pwd):/goad -w /goad/ansible goadansible /bin/bash -c "LAB=$lab PROVIDER=$provider ../scripts/provisionning.sh"
else
echo "${OK} Start provisioning from docker"
$use_sudo docker run -ti --rm --network host -h goadansible -v $(pwd):/goad -w /goad/ansible goadansible /bin/bash -c "ansible-playbook -i ../ad/$lab/data/inventory -i ../ad/$lab/providers/$provider/inventory $ANSIBLE_PLAYBOOK"
fi
;;
esac
;;
"azure")
cd "ad/$lab/providers/$provider/terraform"
public_ip=$(terraform output -raw ubuntu-jumpbox-ip)
cd -
rsync -a --exclude-from='.gitignore' -e "ssh -o 'StrictHostKeyChecking no' -i $CURRENT_DIR/ad/$lab/providers/$provider/ssh_keys/ubuntu-jumpbox.pem" "$CURRENT_DIR/" goad@$public_ip:~/GOAD/
case $method in
"local")
if [ -z $ANSIBLE_PLAYBOOK ]; then
ssh -tt -o "StrictHostKeyChecking no" -i "$CURRENT_DIR/ad/$lab/providers/$provider/ssh_keys/ubuntu-jumpbox.pem" goad@$public_ip << EOF
cd /home/goad/GOAD/ansible
export LAB=$lab PROVIDER=$provider
../scripts/provisionning.sh
exit
EOF
else
ssh -tt -o "StrictHostKeyChecking no" -i "$CURRENT_DIR/ad/$lab/providers/$provider/ssh_keys/ubuntu-jumpbox.pem" goad@$public_ip << EOF
cd /home/goad/GOAD/ansible
ansible-playbook -i ../ad/$lab/data/inventory -i ../ad/$lab/providers/$provider/inventory $ANSIBLE_PLAYBOOK
exit
EOF
fi
print_azure_info
;;
*)
echo "${ERROR} $method install on azure not implemented, use local"
;;
esac
;;
esac
}
disablevagrant(){
echo "${OK} Will disable the vagrant user (vagrant up vagrant halt will no more work, you will have to start and stop vm by hand)"
read -r -p "Are you sure? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
echo "${OK} start disable vagrant user"
;;
*)
echo "abort"
exit
;;
esac
case $PROVIDER in
"virtualbox"|"vmware"|"proxmox")
case $METHOD in
"local")
cd ansible
ansible-playbook -i ../ad/$LAB/providers/$PROVIDER/inventory_disablevagrant disable_vagrant.yml
cd -
;;
"docker")
use_sudo=""
if id -nG "$USER" | grep -qw "docker"; then
echo $USER belongs to docker group
else
echo $USER does not belong to docker group
echo "${INFO} Root password could be asked for docker interaction"
use_sudo="sudo"
fi
ALREADY_BUILD=$($use_sudo docker images |grep -c "goadansible")
if [[ $ALREADY_BUILD -eq 0 ]]; then
echo "[+] Build container"
$use_sudo docker build -t goadansible .
echo "${OK} Container goadansible creation complete"
fi
echo "${OK} Start provisioning from docker"
$use_sudo docker run -ti --rm --network host -h goadansible -v $(pwd):/goad -w /goad/ansible goadansible /bin/bash -c "ansible-playbook -i ../ad/$LAB/providers/$PROVIDER/inventory_disablevagrant disable_vagrant.yml"
;;
esac
;;
"azure")
echo "Vagrant user not used in azure, skip."
;;
esac
}
enablevagrant(){
case $PROVIDER in
"virtualbox"|"vmware"|"proxmox")
case $METHOD in
"local")
cd ansible
ansible-playbook -i ../ad/$LAB/providers/$PROVIDER/inventory_disablevagrant enable_vagrant.yml
cd -
;;
"docker")
use_sudo=""
if id -nG "$USER" | grep -qw "docker"; then
echo $USER belongs to docker group
else
echo $USER does not belong to docker group
echo "${INFO} Root password could be asked for docker interaction"
use_sudo="sudo"
fi
ALREADY_BUILD=$($use_sudo docker images |grep -c "goadansible")
if [[ $ALREADY_BUILD -eq 0 ]]; then
echo "[+] Build container"
$use_sudo docker build -t goadansible .
echo "${OK} Container goadansible creation complete"
fi
echo "${OK} Start provisioning from docker"
$use_sudo docker run -ti --rm --network host -h goadansible -v $(pwd):/goad -w /goad/ansible goadansible /bin/bash -c "ansible-playbook -i ../ad/$LAB/providers/$PROVIDER/inventory_disablevagrant enable_vagrant.yml"
;;
esac
;;
"azure")
echo "Vagrant user not used in azure, skip."
;;
esac
}
install(){
echo "${OK} Launch installation for: $LAB / $PROVIDER / $METHOD"
cd $CURRENT_DIR
if [[ "$ANSIBLE_ONLY" -eq 0 ]]; then
install_providing $LAB $PROVIDER
fi
cd $CURRENT_DIR
install_provisioning $LAB $PROVIDER $METHOD
}
check(){
echo "${OK} Launch check : ./scripts/check.sh $PROVIDER $METHOD"
./scripts/check.sh $PROVIDER $METHOD
check_result=$?
if [ $check_result -eq 0 ]; then
echo "${OK} Check is ok, you can start the installation"
else
echo "${ERROR} Check is not ok, please fix the errors and retry"
echo "${INFO} You could also run the setup script"
fi
}
start(){
case $PROVIDER in
"virtualbox"|"vmware")
cd "ad/$LAB/providers/$PROVIDER"
echo "${OK} start vms"
GOAD_VAGRANT_OPTIONS=$GOAD_VAGRANT_OPTIONS vagrant up
cd -
;;
"proxmox")
if ! which qm >/dev/null; then
(echo >&2 "${ERROR} qm not found in your PATH")
exit 1
else
if [ -d "ad/$LAB/providers/$PROVIDER/terraform" ]; then
vms=$(cat ad/$LAB/providers/$PROVIDER/terraform/*.tf| grep -E 'name = ".*"'|cut -d '"' -f 2)
for vm in "${vms[@]}"
do
id=$(qm list | grep $vm | awk '{print $1}')
echo "[+] VM id : $id"
echo "[+] Starting $vm"
qm start "$id"
done
else
echo "${ERROR} folder ad/$LAB/providers/$PROVIDER/terraform not found"
exit 1
fi
fi
;;
"azure")
az vm start --ids $(az vm list --resource-group $LAB --query "[].id" -o tsv)
status
;;
esac
}
stop(){
case $PROVIDER in
"virtualbox"|"vmware")
cd "ad/$LAB/providers/$PROVIDER"
echo "${OK} stop vms"
GOAD_VAGRANT_OPTIONS=$GOAD_VAGRANT_OPTIONS vagrant halt
cd -
;;
"proxmox")
if ! which qm >/dev/null; then
(echo >&2 "${ERROR} qm not found in your PATH")
exit 1
else
if [ -d "ad/$LAB/providers/$PROVIDER/terraform" ]; then
vms=$(cat ad/$LAB/providers/$PROVIDER/terraform/*.tf| grep -E 'name = ".*"'|cut -d '"' -f 2)
for vm in "${vms[@]}"
do
id=$(qm list | grep $vm | awk '{print $1}')
echo "[+] VM id : $id"
echo "[+] Stopping $vm"
qm stop "$id" && qm wait "$id"
done
else
echo "${ERROR} folder ad/$LAB/providers/$PROVIDER/terraform not found"
exit 1
fi
fi
;;
"azure")
az vm stop --ids $(az vm list --resource-group $LAB --query "[].id" -o tsv)
status
;;
esac
}
restart(){
case $PROVIDER in
"virtualbox"|"vmware")
cd "ad/$LAB/providers/$PROVIDER"
echo "${OK} restart start vms"
vagrant reload
cd -
;;
"proxmox")
if ! which qm >/dev/null; then
(echo >&2 "${ERROR} qm not found in your PATH")
exit 1
else
if [ -d "ad/$LAB/providers/$PROVIDER/terraform" ]; then
vms=$(cat ad/$LAB/providers/$PROVIDER/terraform/*.tf| grep -E 'name = ".*"'|cut -d '"' -f 2)
for vm in "${vms[@]}"
do
id=$(qm list | grep $vm | awk '{print $1}')
echo "[+] VM id is : $id"
echo "[+] Stopping $vm"
qm stop "$id" && qm wait "$id"
echo "[+] Starting $vm"
qm start "$id"
done
else
echo "${ERROR} folder ad/$LAB/providers/$PROVIDER/terraform not found"
exit 1
fi
fi
;;
"azure")
az vm restart --ids $(az vm list --resource-group $LAB --query "[].id" -o tsv)
status
;;
esac
}
destroy(){
case $PROVIDER in
"virtualbox"|"vmware")
cd "ad/$LAB/providers/$PROVIDER"
echo "${OK} destroy the lab"
read -r -p "Are you sure? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
GOAD_VAGRANT_OPTIONS=$GOAD_VAGRANT_OPTIONS vagrant destroy --force
;;
*)
echo "abort"
;;
esac
cd -
;;
"proxmox"|"azure")
if [ -d "ad/$LAB/providers/$PROVIDER/terraform" ]; then
cd "ad/$LAB/providers/$PROVIDER/terraform"
echo "${OK} Destroy infrastructure..."
terraform destroy
else
echo "${ERROR} folder ad/$LAB/providers/$PROVIDER/terraform not found"
exit 1
fi
;;
esac
}
status(){
case $PROVIDER in
"virtualbox"|"vmware")
cd "ad/$LAB/providers/$PROVIDER"
GOAD_VAGRANT_OPTIONS=$GOAD_VAGRANT_OPTIONS vagrant status
cd -
;;
"proxmox")
if ! which qm >/dev/null; then
(echo >&2 "${ERROR} qm not found in your PATH")
exit 1
else
if [ -d "ad/$LAB/providers/$PROVIDER/terraform" ]; then
vms=$(cat ad/$LAB/providers/$PROVIDER/terraform/*.tf| grep -E 'name = ".*"'|cut -d '"' -f 2)
for vm in "${vms[@]}"
do
qm list | grep $vm
done
else
echo "${ERROR} folder ad/$LAB/providers/$PROVIDER/terraform not found"
exit 1
fi
fi
;;
"azure")
az vm list -g $LAB -d --output table
;;
esac
}
snapshot() {
# TODO : snapshot
echo "not implemeneted"
}
reset() {
# TODO : reset to last snapshot
echo "not implemeneted"
}
main() {
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd $CURRENT_DIR
case $TASK in
check)
check
;;
install)
install
;;
status)
status
;;
start)
start
;;
stop)
stop
;;
restart)
restart
;;
destroy)
destroy
;;
snapshot)
snapshot
;;
disablevagrant)
disablevagrant
;;
enablevagrant)
enablevagrant
;;
*)
echo "unknow option"
;;
esac
}
main