2016-09-27 17:55:41 +02:00
$ErrorActionPreference = " Stop "
2016-10-12 23:21:20 +02:00
2016-10-31 18:29:50 +01:00
$virtualBoxMinVersion = " 5.1.0 "
2016-09-27 17:55:41 +02:00
$packerMinVersion = " 0.10.0 "
2016-10-31 18:29:50 +01:00
$vagrantMinVersion = " 1.8.6 "
2016-09-27 21:57:51 +02:00
$vagrantreloadMinVersion = " 0.0.1 "
2016-09-27 17:55:41 +02:00
function CompareVersions ( $actualVersion , $expectedVersion , $exactMatch = $False ) {
$actualVersion = $actualVersion . split ( " . " )
$expectedVersion = $expectedVersion . split ( " . " )
If ( $exactMatch ) {
If ( $actualVersion -eq $expectedVersion ) {
return $True
} else {
return $False
}
}
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
}
If ( $ ( Test-Path " C:\Program Files\Oracle\VirtualBox\VBoxManage.exe " ) -eq $True ) {
$vboxVersion = cmd . exe / c " C:\Program Files\Oracle\VirtualBox\VBoxManage.exe " -v
$vboxVersion = $vboxVersion . split ( " r " ) [ 0 ]
}
If ( CompareVersions -actualVersion $vboxVersion -expectedVersion $virtualBoxMinVersion ) {
2016-09-22 22:20:07 +02:00
Write-Host " Compatible version of VirtualBox found. "
} else {
Write-Host " Could not find a compatible version of VirtualBox at C:\Program Files\Oracle\VirtualBox\. Please download and install it from https://www.virtualbox.org/wiki/Downloads. "
exit
}
2016-09-27 17:55:41 +02:00
$packerVersion = cmd . exe / c " packer " -v
If ( CompareVersions -actualVersion $packerVersion -expectedVersion $packerMinVersion ) {
2016-09-22 22:20:07 +02:00
Write-Host " Compatible version of packer found. "
} else {
2016-09-27 17:55:41 +02:00
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. "
2016-09-22 22:20:07 +02:00
exit
}
2016-09-27 17:55:41 +02:00
If ( $ ( Test-Path " C:\HashiCorp\Vagrant\bin\vagrant.exe " ) -eq $True ) {
$vagrantVersion = cmd . exe / c " vagrant " -v
$vagrantVersion = $vagrantVersion . split ( " " ) [ 1 ]
}
2016-10-31 18:29:50 +01:00
If ( CompareVersions -actualVersion $vagrantVersion -expectedVersion $vagrantMinVersion ) {
2016-09-22 22:20:07 +02:00
Write-Host " Compatible version of Vagrant found. "
} else {
2016-10-31 18:29:50 +01:00
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. "
2016-09-22 22:20:07 +02:00
exit
}
2016-09-27 17:55:41 +02:00
$vagrantPlugins = cmd . exe / c " vagrant plugin list " | select-string -pattern " vagrant-reload "
2016-09-22 22:20:07 +02:00
2016-09-27 17:55:41 +02:00
If ( ! [ string ] :: IsNullOrEmpty ( $vagrantPlugins ) ) {
$vagrantPlugins = $vagrantPlugins . ToString ( ) . Trim ( )
$vagrantreloadVersion = $vagrantPlugins . Replace ( " ( " , " " )
$vagrantreloadVersion = $vagrantreloadVersion . Replace ( " ) " , " " )
$vagrantreloadVersion = $vagrantreloadVersion . split ( " " ) [ 1 ]
2016-09-22 22:20:07 +02:00
2016-09-27 17:55:41 +02:00
If ( CompareVersions -actualVersion $vagrantreloadVersion -expectedVersion $vagrantreloadMinVersion ) {
Write-Host " Compatible version of vagrant-reload plugin found. "
}
2016-09-22 22:20:07 +02:00
} else {
Write-Host " Could not find a compatible version of vagrant-reload plugin. Attempting to install... "
cmd . exe / c " vagrant plugin install vagrant-reload "
2016-09-27 17:55:41 +02:00
# 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. "
}
2016-09-22 22:20:07 +02:00
}
Write-Host " All requirements found. Proceeding... "
If ( $ ( Test-Path " windows_2008_r2_virtualbox.box " ) -eq $True ) {
2016-09-27 17:55:41 +02:00
Write-Host " It looks like the Vagrant box already exists. Skipping the Packer build. "
2016-09-22 22:20:07 +02:00
} else {
2016-09-27 17:55:41 +02:00
Write-Host " Building the Vagrant box... "
2016-09-22 22:20:07 +02:00
cmd . exe / c packer build windows_2008_r2 . json
2016-09-27 17:55:41 +02:00
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. "
}
2016-09-22 22:20:07 +02:00
}
echo " Attempting to add the box to Vagrant... "
$vagrant_box_list = cmd . exe / c " vagrant box list " | select-string -pattern " metasploitable3 "
If ( $vagrant_box_list ) { $vagrant_box_list = $vagrant_box_list . ToString ( ) . Trim ( ) }
If ( $vagrant_box_list -eq " metasploitable3 " ) {
Write-Host " metasploitable3 already found in Vagrant box repository. Skipping the addition to Vagrant. "
} else {
2016-09-27 17:55:41 +02:00
2016-11-07 17:50:48 +01:00
cmd . exe / c vagrant box add metasploitable3 windows_2008_r2_virtualbox . box
2016-09-27 17:55:41 +02:00
if ( $ ? ) {
Write-Host " Box successfully added to Vagrant. "
} else {
throw " Error adding box to Vagrant. See the above output for any error messages. "
}
2016-09-22 22:20:07 +02:00
}
2016-09-27 17:55:41 +02:00
Write-Host " SUCCESS: Run 'vagrant up' to provision and start metasploitable3. "
Write-Host " NOTE: The VM will need Internet access to provision properly. "