forked from rapid7/metasploitable3
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.ps1
executable file
·210 lines (144 loc) · 6.49 KB
/
build.ps1
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
$ErrorActionPreference = "Stop"
$virtualBoxMinVersion = "5.1.10"
$packerMinVersion = "0.10.0"
$vagrantMinVersion = "1.9.0"
$vagrantreloadMinVersion = "0.0.1"
$packer = "packer.exe"
$expectedVBoxLocation = "C:\Program Files\Oracle\VirtualBox"
$expectedVagrantLocation="C:\HashiCorp\Vagrant\bin"
function CompareVersions ($actualVersion, $expectedVersion, $exactMatch = $False) {
If ($exactMatch) {
If ($actualVersion -eq $expectedVersion) {
return $True
} else {
return $False
}
}
$actualVersion = $actualVersion.split(".")
$expectedVersion = $expectedVersion.split(".")
for($i=0; $i -le $expectedVersion.length; $i++) {
If([INT]$actualVersion[$i] -gt [INT]$expectedVersion[$i]) {
return $True
}
If([INT]$actualVersion[$i] -lt [INT]$expectedVersion[$i]) {
return $False
}
}
return $True
}
Write-Host "";
If ($(Test-Path "$expectedVBoxLocation\VBoxManage.exe") -eq $True) {
$vboxVersion = cmd.exe /c "$expectedVBoxLocation\VBoxManage.exe" -v
$vboxVersion = $vboxVersion.split("r")[0]
} else {
Write-Host "VirtualBox is not installed (or not in the expected location of $expectedVBoxLocation\)"
Write-Host "Please download and install it from https://www.virtualbox.org/"
exit
}
If (CompareVersions -actualVersion $vboxVersion -expectedVersion $virtualBoxMinVersion -exactMatch $False) {
Write-Host "Compatible version of VirtualBox found."
} else {
Write-Host "A compatible version of VirtualBox was not found."
Write-Host "Current Version=[$vboxVersion], Minimum Version=[$virtualBoxMinVersion]"
Write-Host "Please download and install it from https://www.virtualbox.org/"
exit
}
$packerVersion = cmd.exe /c $packer -v
If (CompareVersions -actualVersion $packerVersion -expectedVersion $packerMinVersion) {
Write-Host "Compatible version of Packer found."
} else {
Write-Host "Could not find a compatible version of packer. Please download it from https://www.packer.io/downloads.html and add it to your PATH."
exit
}
If ($(Test-Path "$expectedVagrantLocation\vagrant.exe") -eq $True) {
$vagrantVersion = cmd.exe /c "vagrant" -v
$vagrantVersion = $vagrantVersion.split(" ")[1]
} else {
Write-Host "Vagrant is not installed (or not in the expected location of $expectedVagrantLocation\)"
Write-Host "Please download and install it from https://www.vagrantup.com/downloads.html/"
exit
}
If (CompareVersions -actualVersion $vagrantVersion -expectedVersion $vagrantMinVersion) {
Write-Host "Compatible version of Vagrant found."
} else {
Write-Host "Could not find a compatible version of Vagrant at C:\HashiCorp\Vagrant\bin\. Please download and install it from https://www.vagrantup.com/downloads.html."
exit
}
$vagrantPlugins = cmd.exe /c "vagrant plugin list" | select-string -pattern "vagrant-reload"
If (![string]::IsNullOrEmpty($vagrantPlugins)) {
$vagrantPlugins = $vagrantPlugins.ToString().Trim()
$vagrantreloadVersion = $vagrantPlugins.Replace("(", "")
$vagrantreloadVersion = $vagrantreloadVersion.Replace(")", "")
$vagrantreloadVersion = $vagrantreloadVersion.split(" ")[1]
If (CompareVersions -actualVersion $vagrantreloadVersion -expectedVersion $vagrantreloadMinVersion) {
Write-Host "Compatible version of vagrant-reload plugin found."
}
} else {
Write-Host "Could not find a compatible version of vagrant-reload plugin. Attempting to install..."
cmd.exe /c "vagrant plugin install vagrant-reload"
# Hacky version of Try-Catch for non-terminating errors.
# See http://stackoverflow.com/questions/1142211/try-catch-does-not-seem-to-have-an-effect
if($?) {
Write-Host "The vagrant-reload plugin was successfully installed."
} else {
throw "Error installing vagrant-reload plugin. Please check the output above for any error messages."
}
}
function InstallBox($os_full, $os_short)
{
$boxversion = Get-Content .\packer\templates\$os_full.json | Select-String -Pattern "box_version" | Select-String -Pattern "[0-9]\.[0-9]\.[0-9]+"
$boxversion = $boxversion.toString().trim().split('"')[3]
Write-Host "Building metasploitable3-$os_short Vagrant box..."
If ($(Test-Path "packer\builds\$($os_full)_virtualbox_$boxversion.box") -eq $True) {
Write-Host "It looks like the Vagrant box already exists. Skipping the Packer build."
} else {
cmd.exe /c $packer build --only=virtualbox-iso packer\templates\$os_full.json
if($?) {
Write-Host "Box successfully built by Packer."
} else {
throw "Error building the Vagrant box using Packer. Please check the output above for any error messages."
}
}
Write-Host "Attempting to add metasploitable3-$os_short box to Vagrant..."
$vagrant_box_list = cmd.exe /c "vagrant box list"
If ($vagrant_box_list -match "rapid7/metasploitable3-$os_short") {
Write-Host "rapid7/metasploitable3-$os_short already found in Vagrant box repository. Skipping the addition to Vagrant."
} else {
cmd.exe /c vagrant box add packer\builds\$($os_full)_virtualbox_$boxversion.box --name rapid7/metasploitable3-$os_short
if($?) {
Write-Host "rapid7/metasploitable3-$os_short box successfully added to Vagrant."
} else {
throw "Error adding metasploitable3-$os_short box to Vagrant. See the above output for any error messages."
}
}
}
Write-Host "All requirements found. Proceeding..."
if($args.Length -eq 0)
{
$option = Read-Host -Prompt 'No box name passed as input. Build both the boxes ? (y/n)';
if ($option -eq 'y')
{
InstallBox -os_full "windows_2008_r2" -os_short "win2k8";
InstallBox -os_full "ubuntu_1404" -os_short "ub1404";
} else {
Write-Host "To build metasploitable boxes separately, use the following commands:";
Write-Host "- .\build.ps1 windows2008";
Write-Host "- .\build.ps1 ubuntu1404";
}
}
ElseIf ($args.Length -eq 1)
{
if ($args -eq "windows2008")
{
InstallBox -os_full "windows_2008_r2" -os_short "win2k8";
}
ElseIf ($args -eq "ubuntu1404")
{
InstallBox -os_full "ubuntu_1404" -os_short "ub1404";
}
Else
{
Write-Host "Invalid OS. Valid options are 'ubuntu1404' and 'windows2008'";
}
}
Write-Host "";