/
CS-Build-a-Lab.sh
588 lines (554 loc) · 18.1 KB
/
CS-Build-a-Lab.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
#!/bin/bash
# Build-a-lab (KVM/VirtualBox) v0.1.2
# Coded by: Cyber Secrets - Information Warfare Center
# Github: https://github.com/infosecwriter
# This just sets up the environment for KVM or VirtualBox and has a few extra items
trap 'printf "\n"; stop 1; exit 1' 2
clear
VINSTALL=""
echo "Setting up..."
#sudo apt update
#sudo apt-get install p7zip -y
startmenu() {
banner
printf "\e[92m Build-a-Lab with KVM or VirtualBox...\n"
printf " The Libvirt/KVM Lab = 1\n"
printf " The VirtualBox Lab = 2\n"
printf " Download Vuln Virtual Machines to start building your lab! = 3\n"
printf " Convert VM file type = 90\n"
printf " Compress VM file = 91\n"
printf " Clean up Temp folder = 92\n"
printf " Remove KVM/VirtualBox = 98\n"
printf " Exit = 99\n"
printf "\n"
read -p $' Choose an option: \e[37;1m' option
case $option in
99) stop 1;;
1|01) kvmmenu ;;
2|02) vbmenu ;;
3|03) labmenu
mkdir temp > /dev/null 2>&1
;;
90) read -p $' File type to convert to "qcow2, vmdk, vdi, raw":' vmtype;
read -p $' File to convert from: ' vmif;
read -p $' Output file name:' vmof;
qemu-img convert -c -O $vmtype $vmif $vmof; read 'Press enter to continue.';
startmenu ;;
91) read -p $' File type to compress "qcow2, vmdk, vdi, raw": ' vmtype;
read -p $' File to convert from: ' vmif;
qemu-img convert -c -O $vmtype $vmif $vmif-1 && rm $vmif && mv $vmif-1 $vmif; read 'Press enter to continue.';
startmenu ;;
92) rm -rf temp;
read 'Press enter to continue.';
startmenu ;;
98) read -p $' Remove KVM (1) or VirtualBox (2): ' vmremove
case $vmremove in
1|01) sudo apt purge qemu-kvm libvirt-clients qemu-utils libvirt-daemon-system virt-manager && startmenu ;;
2|02) sudo apt purge virtualbox virtualbox-ext-pack && startmenu ;;
*)
startmenu ;;
esac ;;
*)
printf "\e[1;93m [!] Invalid option!\e[0m\n"
clear
startmenu
;;
esac
}
kvmmenu() {
banner
printf "\e[92m Build-a-Lab with KVM or VirtualBox...\n"
printf " Install Libvirt/KVM = 1\n"
printf " Run Virt-Manager = 2\n"
printf " List KVM Virtual Machines = 3\n"
printf " Start a headless KVM Virtual Machine = 4\n"
printf " Stop a headless KVM Virtual Machine = 5\n"
printf " Shut down ALL KVM Virtual Machines = 6\n"
printf " Compress ALL KVM Virtual Machines in VMs folder = 10\n"
printf " Exit to Previous Menu = 99\n"
printf "\n"
read -p $' Choose an option: \e[37;1m' option
case $option in
99) startmenu;;
1|01) sudo apt-get update && sudo apt-get dist-upgrade && sudo apt-get autoremove && sudo apt install qemu-kvm libvirt-clients qemu-utils libvirt-daemon-system virt-manager; kvmmenu ;;
2|02) virt-manager; kvmmenu ;;
3|03) kvmlist; kvmmenu ;;
4|04) kvmlist; read -p $'Name the VM you want to start: ' val; virsh start $val && kvmmenu ;;
5|05) kvmlist; read -p $'Name the VM you want to shutdown: ' val; virsh shutdown $val; sleep 30; virsh destroy $val; kvmmenu ;;
6|06) echo "Try to cleanly shut down all running KVM domains..."
touch /tmp/shutdown-kvm-guests
virsh list | grep running | awk '{ print $2}' | while read DOMAIN; do
virsh shutdown $DOMAIN
done
END_TIME=$(date -d "120 seconds" +%s)
while [ $(date +%s) -lt $END_TIME ]; do
printf "."
test -z "$(virsh list | grep running | awk '{ print $2}')" && break
sleep 1
done
printf "\n"
virsh list | grep running | awk '{ print $2}' | while read DOMAIN; do
virsh destroy $DOMAIN
sleep 3
done
kvmmenu
;;
10) ls -la --block-size=M ~/VMs/ | grep -e qcow2 | tee vm-pre-conv.txt
ls ~/VMs/ | grep -e qcow2 | grep -v -e "-smallme." | awk '{gsub(/.*[/]|[.]{1}[^.]+$/, "", $0)} 1' | while read convert; do
printf "Compressing ~/VMs/$convert.qcow2...\n"
qemu-img convert -O qcow2 -c ~/VMs/$convert.qcow2 ~/VMs/$convert-smallme.qcow2 && printf "...done\n" && rm ~/VMs/$convert.qcow2 && mv ~/VMs/$convert-smallme.qcow2 ~/VMs/$convert.qcow2 || "...failed\n"
done
ls -la --block-size=M ~/VMs/ | grep -e qcow2 | tee vm-conv.txt
read -p $'Notice the size difference: ' Readme
kvmmenu
;;
*)
printf "\e[1;93m [!] Invalid option!\e[0m\n"
clear
kvmmenu
;;
esac
}
kvmlist() {
echo "KVM Virtual Machine IP Adress List:"
virsh list | grep running | awk '{ print $2}' | while read DOMAIN; do
printf "* $DOMAIN : "
IP=$(virsh domifaddr $DOMAIN | grep ipv4 | awk '{print $4}')
[ -z "$IP" ] && printf "No IP Address...\n" || echo $IP
done
printf "\n"
virsh list --all | tee VMs.txt
read -p $'Press enter to continue.' val
}
vbmenu() {
banner
printf "\e[92m Build-a-Lab with KVM or VirtualBox...\n"
printf " Install VirtualBox = 1\n"
printf " Run VirtualBox = 2\n"
printf " Start a headless VirtualBox Virtual Machine = 4\n"
printf " Stop a headless VirtualBox Virtual Machine = 5\n"
printf " Exit to Previous Menu = 99\n"
printf "\n"
read -p $' Choose an option: \e[37;1m' option
case $option in
99) startmenu;;
1|01) sudo apt-get update
sudo apt-get dist-upgrade -y
sudo apt-get autoremove -y
sudo apt-get -y install gcc make linux-headers-$(uname -r) dkms
sudo apt-get -y install virtualbox virtualbox-ext-pack
startmenu;;
2|02) virtualbox &
vbmenu ;;
3|03) VBoxManage list vms | tee VMs.txt; read -p $'Press enter to continue.' val; vbmenu ;;
4|04) read -p $'Name the VM you want to start: ' val; VBoxManage startvm "$val" --type headless && vbmenu ;;
5|05) read -p $'Name the VM you want to shutdown: ' val; VBoxManage controlvm "$val" poweroff --type headless && vbmenu ;;
*)
printf "\e[1;93m [!] Invalid option!\e[0m\n"
clear
vbmenu
;;
esac
}
labmenu() {
banner
printf "\e[92m Fill up your lab... (You need 3x the space for conversion)\n"
printf " Download Vulnerable Machines = 1\n"
printf " Download Red Team Distros = 2\n"
# printf " Download Blue Team Distros = 3\n"
# printf " Download Privacy Distros = 4\n"
printf " Download Common Distros = 5\n"
printf " Exit to Previous Menu = 99\n"
printf "\n"
read -p $' Choose an option: \e[37;1m' option
mkdir temp > /dev/null 2>&1
case $option in
99) startmenu ;;
1|01) vulnlabmenu ;;
2|02) rtlabmenu ;;
3|03) btlabmenu ;;
4|04) privlabmenu ;;
5|05) comlabmenu ;;
*)
printf "\e[1;93m [!] Invalid option!\e[0m\n"
clear
labmenu
;;
esac
}
vulnlabmenu() {
banner
printf "\e[92m Fill up your lab... (You need 3x the space for conversion)\n"
printf " Download Metasploitable2 2 GB = 1\n"
printf " Download Impossible Mission Force (IMF) 1.6 GB = 2\n"
printf " Download Necromancer 330 MB = 3\n"
printf " Download Mr Robot 740 MB = 4\n"
printf " Exit to Previous Menu = 99\n"
printf "\n"
read -p $' Choose an option: \e[37;1m' option
case $option in
99) labmenu;;
1|01) FOLDER="Metasploitable2-Linux"
VMNAME="Metasploitable"
VTYPE="vmdk"
VOS="linux"
VDOWN="https://download.vulnhub.com/metasploitable/metasploitable-linux-2.0.0.zip"
FILE=temp/${VDOWN##*/}
if [ ! -f $FILE ]; then
wget -P temp $VDOWN
unpack
else
echo " File $FILE exist."
fi
buildvm
vulnlabmenu
;;
2|02) FOLDER="IMF"
VMNAME="IMF-disk1"
VTYPE="vmdk"
VOS="linux"
VDOWN="https://download.vulnhub.com/imf/IMF.ova"
FILE=temp/${VDOWN##*/}
if [ ! -f $FILE ]; then
mkdir temp/$FOLDER > /dev/null 2>&1
wget -P temp $VDOWN
unpack
else
echo " File $FILE exist."
fi
buildvm
vulnlabmenu
;;
3|03) FOLDER="Necromancer"
VMNAME="necromancer-disk1"
VTYPE="vmdk"
VOS="OpenBSD_64"
VDOWN="https://download.vulnhub.com/necromancer/necromancer.ova"
FILE=temp/${VDOWN##*/}
if [ ! -f $FILE ]; then
mkdir temp/$FOLDER > /dev/null 2>&1
wget -P temp $VDOWN
unpack
else
echo " File $FILE exist."
fi
buildvm
vulnlabmenu
;;
4|04) FOLDER="MrRobot"
VMNAME="mrRobot-disk1"
VTYPE="vmdk"
VOS="linux"
VDOWN="https://download.vulnhub.com/mrrobot/mrRobot.ova"
FILE=temp/${VDOWN##*/}
if [ ! -f $FILE ]; then
mkdir temp/$FOLDER > /dev/null 2>&1
wget -P temp $VDOWN
unpack
else
echo " File $FILE exist."
fi
buildvm
vulnlabmenu
;;
*)
printf "\e[1;93m [!] Invalid option!\e[0m\n"
clear
vulnlabmenu
;;
esac
}
rtlabmenu() {
banner
printf "\e[92m Fill up your lab... (You need 3x the space for conversion)\n"
printf " Download Kali Rolling 3.8GB = 2\n"
printf " Download Parrot Security 13.5 GB = 3\n"
printf " Exit to Previous Menu = 99\n"
printf "\n"
read -p $' Choose an option: \e[37;1m' option
case $option in
99) labmenu;;
2|02) FOLDER="Kali-Rolling"
VMNAME="Kali-Linux-2018.4-vbox-amd64-disk001"
VTYPE="vmdk"
VOS="debian"
VDOWN="https://images.offensive-security.com/virtual-images/kali-linux-2018.4-vbox-amd64.ova"
FILE=temp/${VDOWN##*/}
printf " Username – root\n"
printf " Password – toor\n"
if [ ! -f $FILE ]; then
wget -P temp wget -P temp $VDOWN
mkdir temp/$FOLDER > /dev/null 2>&1
unpack
else
echo " File $FILE exist."
fi
buildvm
rtlabmenu
;;
3|03) FOLDER="Parrot_Security"
VMNAME="Parrot_Security"
VTYPE="vdi"
VOS="debian"
VDOWN="https://sourceforge.net/projects/osboxes/files/v/vb/40-P-rt/4.2.2/42264.7z"
FILE=temp/${VDOWN##*/}
printf " Username – osboxes\n"
printf " Password – osboxes.org\n"
if [ ! -f $FILE ]; then
wget -P temp wget -P temp $VDOWN
mkdir temp/$FOLDER > /dev/null 2>&1
unpack
mv temp/$FOLDER/Parrot\ Security\ 4.2.2\ \(64bit\).vdi temp/$FOLDER/$VMNAME.vdi
else
echo " File $FILE exist."
fi
buildvm
rtlabmenu
;;
*)
printf "\e[1;93m [!] Invalid option!\e[0m\n"
clear
rtlabmenu
;;
esac
}
btlabmenu() {
banner
printf "\e[92m Fill up your lab... (You need 3x the space for conversion)\n"
# printf " Download Debian = 3\n"
printf " Exit to Previous Menu = 99\n"
printf "\n"
read -p $' Choose an option: \e[37;1m' option
case $option in
99) labmenu;;
3|03) FOLDER="Debian"
VMNAME="Debian_Stretch"
VTYPE="vdi"
VOS="debian"
VDOWN="https://sourceforge.net/projects/osboxes/files/v/vb/14-D-b/9.5/9564.7z"
FILE=temp/${VDOWN##*/}
printf " Username – osboxes\n"
printf " Password – osboxes.org\n"
if [ ! -f $FILE ]; then
wget -P temp wget -P temp $VDOWN
mkdir temp/$FOLDER > /dev/null 2>&1
unpack
mv temp/$FOLDER/Debian\ 9.5\ \(64bit\).vdi temp/$FOLDER/$VMNAME.vdi
read "test"
else
echo " File $FILE exist."
fi
buildvm
comlabmenu
;;
*)
printf "\e[1;93m [!] Invalid option!\e[0m\n"
clear
btlabmenu
;;
esac
}
privlabmenu() {
banner
printf "\e[92m Fill up your lab... (You need 3x the space for conversion)\n"
printf " Download Whonix Tor Gatway & Workstation 1.6GB = 3\n"
printf " Exit to Previous Menu = 99\n"
printf "\n"
read -p $' Choose an option: \e[37;1m' option
case $option in
99) labmenu;;
3|03) FOLDER="Whonix-Gateway"
VMNAME="Whonix-Gateway-XFCE-14.0.0.9.9-disk001"
VTYPE="vmdk"
VOS="linux"
VDOWN="https://download.whonix.org/linux//14.0.0.9.9/Whonix-Gateway-XFCE-14.0.0.9.9.ova"
FILE=temp/${VDOWN##*/}
printf " Username – user\n"
printf " Password – changeme\n"
if [ ! -f $FILE ]; then
mkdir temp/$FOLDER > /dev/null 2>&1
wget -P temp $VDOWN
unpack
else
echo " File $FILE exist."
fi
echo " Go to the Whonix-Gateway terminal."
echo " Log in with the default credentials."
echo " Issue the command sudo su to log in as the root user."
echo " Issue the command passwd."
echo " Enter a new password."
echo " Confirm the new password."
echo " Issue the command passwd user."
echo " Enter a new password."
echo " Confirm the new password."
buildvm
echo " Now working on the workstation..."
FOLDER="Whonix-Workstation"
VMNAME="Whonix-Workstation-XFCE-14.0.0.9.9-disk001"
VTYPE="vmdk"
VOS="linux"
VDOWN="https://download.whonix.org/linux//14.0.0.9.9/Whonix-Workstation-XFCE-14.0.0.9.9.ova"
FILE=temp/${VDOWN##*/}
printf " Username – user\n"
printf " Password – changeme\n"
if [ ! -f $FILE ]; then
mkdir temp/$FOLDER > /dev/null 2>&1
wget -P temp $VDOWN
unpack
else
echo " File $FILE exist."
fi
buildvm
privlabmenu
;;
*)
printf "\e[1;93m [!] Invalid option!\e[0m\n"
clear
privlabmenu
;;
esac
}
comlabmenu() {
banner
printf "\e[92m Fill up your lab... (You need 3x the space for conversion)\n"
# printf " Download Kali Rolling = 1\n"
printf " Download Black Lab 7.1GB = 2\n"
printf " Download CentOS 4.8GB = 3\n"
printf " Download Debian 6GB = 4\n"
printf " Exit to Previous Menu = 99\n"
printf "\n"
read -p $' Choose an option: \e[37;1m' option
case $option in
99) labmenu;;
2|02) FOLDER="Black_Lab"
VMNAME="Black_Lab"
VTYPE="vdi"
VOS="linux"
VDOWN="https://sourceforge.net/projects/osboxes/files/v/vb/6-Bl-Lb/11.60/116064.7z"
FILE=temp/${VDOWN##*/}
printf " Username – osboxes\n"
printf " Password – osboxes.org\n"
if [ ! -f $FILE ]; then
wget -P temp wget -P temp $VDOWN
mkdir temp/$FOLDER > /dev/null 2>&1
unpack
mv temp/$FOLDER/CentOS\ 7-1804\ \(64bit\).vdi temp/$FOLDER/$VMNAME.vdi
else
echo " File $FILE exist."
fi
buildvm
comlabmenu
;;
3|03) FOLDER="CentOS"
VMNAME="CentOS"
VTYPE="vdi"
VOS="linux"
VDOWN="https://sourceforge.net/projects/osboxes/files/v/vb/10-C-nt/7/7-1804/180464.7z"
FILE=temp/${VDOWN##*/}
printf " Username – osboxes\n"
printf " Password – osboxes.org\n"
if [ ! -f $FILE ]; then
wget -P temp wget -P temp $VDOWN
mkdir temp/$FOLDER > /dev/null 2>&1
unpack
mv temp/$FOLDER/Debian\ 9.5\ \(64bit\).vdi temp/$FOLDER/$VMNAME.vdi
read "test"
else
echo " File $FILE exist."
fi
buildvm
comlabmenu
;;
4|04) FOLDER="Debian"
VMNAME="Debian_Stretch"
VTYPE="vdi"
VOS="debian"
VDOWN="https://sourceforge.net/projects/osboxes/files/v/vb/14-D-b/9.5/9564.7z"
FILE=temp/${VDOWN##*/}
printf " Username – osboxes\n"
printf " Password – osboxes.org\n"
if [ ! -f $FILE ]; then
wget -P temp wget -P temp $VDOWN
mkdir temp/$FOLDER > /dev/null 2>&1
unpack
mv temp/$FOLDER/Debian\ 9.5\ \(64bit\).vdi temp/$FOLDER/$VMNAME.vdi
else
echo " File $FILE exist."
fi
buildvm
comlabmenu
;;
*)
printf "\e[1;93m [!] Invalid option!\e[0m\n"
clear
comlabmenu
;;
esac
}
unpack() {
case $FILE in
*".zip"*)
unzip -d temp $FILE ;;
*".7z"*)
7z e $FILE -otemp/$FOLDER ;;
*".ova"*)
tar -C temp/$FOLDER -xvf $FILE ;;
*".rar"*)
;;
*)
echo "No compression";;
esac
}
buildvm() {
read -p $' Set up for ONLY KVM (1) or VirtualBox (2): \e[37;1m' vmoption
case $vmoption in
1|01) makekvm $FOLDER $VMNAME $VTYPE $VOS && virt-manager & ;;
2|02) makevb $FOLDER $VMNAME $VTYPE $VOS && virtualbox & ;;
*)
;;
esac
}
makekvm() {
mkdir ~/VMs > /dev/null 2>&1
printf "\n Building VirtualBox disk and compressing $2. May take a few minutes\n"
qemu-img convert -c -O qcow2 temp/$1/$2.$3 ~/VMs/$2.qcow2 || read -p $'Conversion failed... The reason should be above. Press Enter to Continue: ' ReadMe
virt-install --name=$FOLDER --vcpus=1 --memory=1024 --disk ~/VMs/$VMNAME.qcow2 --os-type $VOS --os-variant generic --graphics spice,listen=127.0.0.1 --console pty,target_type=serial $VINSTALL --import
}
makevb() {
if [[ $VDOWN == *".ova"* ]]; then
CONVB=$(echo $FILE | cut -d"/" -f2 | cut -d"." -f1)
VBoxManage import $FILE
#VBoxManage startvm $CONVB
virtualbox
else
mkdir ~/VirtualBox\ VMs/$1
printf "Building VirtualBox disk\n"
cp temp/$1/$2.$3 ~/VirtualBox\ VMs/$1
cd ~/VirtualBox\ VMs
VBoxManage createvm --name $1 --ostype $4 --register
VBoxManage modifyvm $1 --memory 512
modifyvm $1 --vtxvpid off
VBoxManage storagectl $1 --name "IDE Controller" --add ide --controller PIIX4
VBoxManage storageattach $1 --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium ~/VirtualBox\ VMs/$1/$2.vmdk
VBoxManage startvm $1
fi
}
stop() {
# Cleaning up your mess
printf "\nCleaning up\n"
rm -rf temp
}
banner() {
clear
printf "\n"
printf "\e[1;93m .:.:.\e[0m\e[1;77m Build-a-Lab with KVM or VirtualBox \e[0m\e[1;93m.:.:.\e[0m\n"
printf "\e[1;93m .:.:.\e[0m\e[1;77m Tool coded by: @InfoSecWriter \e[0m\e[1;93m.:.:.\e[0m\n"
printf "\e[1;93m .:.:.\e[0m\e[1;77m https://github.com/infosecwriter/ \e[0m\e[1;93m.:.:.\e[0m\n"
printf "\e[1;93m .:.:.\e[0m\e[1;77m CyberSecrets.org : IntelligentHacking.com \e[0m\e[1;93m.:.:.\e[0m\n"
printf "\n"
printf " \e[101m\e[1;77m:: VirtualBox does not play well with KVM. ::\e[0m\n"
printf " \e[101m\e[1;77m:: Pick KVM or VirtualBox or run VirtualBox first... ::\e[0m\n"
printf "\n"
}
banner
startmenu