From 4036153886ab73e4dde5480ecaba25685b6048b9 Mon Sep 17 00:00:00 2001 From: Chan9390 Date: Thu, 19 Apr 2018 22:49:59 +0530 Subject: [PATCH 1/2] Added build support for both boxes in powershell script --- build_win2008.ps1 => build.ps1 | 335 ++++++++++++++++++++------------- 1 file changed, 203 insertions(+), 132 deletions(-) rename build_win2008.ps1 => build.ps1 (58%) mode change 100644 => 100755 diff --git a/build_win2008.ps1 b/build.ps1 old mode 100644 new mode 100755 similarity index 58% rename from build_win2008.ps1 rename to build.ps1 index 8285a08..3286601 --- a/build_win2008.ps1 +++ b/build.ps1 @@ -1,132 +1,203 @@ -$ErrorActionPreference = "Stop" - -$virtualBoxMinVersion = "5.1.10" -$packerMinVersion = "0.10.0" -$vagrantMinVersion = "1.9.0" -$vagrantreloadMinVersion = "0.0.1" -$packer = "packer" - -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 -} - -$expectedVBoxLocation = "C:\Program Files\Oracle\VirtualBox" -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 "C:\HashiCorp\Vagrant\bin\vagrant.exe") -eq $True) { - $vagrantVersion = cmd.exe /c "vagrant" -v - $vagrantVersion = $vagrantVersion.split(" ")[1] -} - -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." - } -} - -Write-Host "All requirements found. Proceeding..." - -If ($(Test-Path "packer\templates\windows_2008_r2_virtualbox.box") -eq $True) { - Write-Host "It looks like the Vagrant box already exists. Skipping the Packer build." -} else { - Write-Host "Building the Vagrant box..." - cmd.exe /c $packer build --only=virtualbox-iso packer\templates\windows_2008_r2.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." - } -} - -echo "Attempting to add the box to Vagrant..." - -$vagrant_box_list = cmd.exe /c "vagrant box list" | select-string -pattern "metasploitable3-win2k8" - -If ($vagrant_box_list) { $vagrant_box_list = $vagrant_box_list.ToString().Trim() } - -If ($vagrant_box_list -eq "metasploitable3-win2k8") { - Write-Host "metasploitable3-win2k8 already found in Vagrant box repository. Skipping the addition to Vagrant." -} else { - - cmd.exe /c vagrant box add metasploitable3-win2k8 packer\builds\windows_2008_r2_virtualbox.box - - if($?) { - Write-Host "Box successfully added to Vagrant." - } else { - throw "Error adding box to Vagrant. See the above output for any error messages." - } -} - -Write-Host "SUCCESS: Run 'vagrant up' to provision and start metasploitable3." -Write-Host "NOTE: The VM will need Internet access to provision properly." +$ErrorActionPreference = "Stop" + +$virtualBoxMinVersion = "5.1.10" +$packerMinVersion = "0.10.0" +$vagrantMinVersion = "1.9.0" +$vagrantreloadMinVersion = "0.0.1" +$packer = "packer" + + +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 ""; +$expectedVBoxLocation = "C:\Program Files\Oracle\VirtualBox" + +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 "C:\HashiCorp\Vagrant\bin\vagrant.exe") -eq $True) { + + $vagrantVersion = cmd.exe /c "vagrant" -v + $vagrantVersion = $vagrantVersion.split(" ")[1] + +} + + +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." + } + } + + echo "Attempting to add metasploitable3-$os_short box to Vagrant..." + $vagrant_box_list = cmd.exe /c "vagrant box list" + + If ($vagrant_box_list -match "metasploitable3-$os_short") { + Write-Host "metasploitable3-$os_short already found in Vagrant box repository. Skipping the addition to Vagrant." + } else { + + cmd.exe /c vagrant box add metasploitable3-$os_short packer\builds\$($os_full)_virtualbox_$boxversion.box + + if($?) { + Write-Host "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 ""; \ No newline at end of file From ec5de71ef61aeae9b9c7ec278e761484cb0c8117 Mon Sep 17 00:00:00 2001 From: Chandrapal Date: Thu, 19 Apr 2018 23:12:00 +0530 Subject: [PATCH 2/2] Updated Readme - Added powershell build commands - Added vagrant commands --- README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 102fde2..db71487 100644 --- a/README.md +++ b/README.md @@ -19,13 +19,16 @@ Requirements: * [VirtualBox](https://www.virtualbox.org/wiki/Downloads) * Internet connection -To build automatically: +### To build automatically: -1. On Linux/OSX run `./build.sh windows2008` to build the Windows box or `./build.sh ubuntu1404` to build the Linux box. On Windows, run `build_win2008.ps1` in a powershell terminal to build the Windows box. -2. If the command completes successfully, run `vagrant up`. +1. - On **Linux/OSX** run `./build.sh windows2008` to build the Windows box or `./build.sh ubuntu1404` to build the Linux box. + - On **Windows**, open powershell terminal and run `.\build.ps1 windows2008` to build the Windows box or `.\build.ps1 ubuntu1404` to build the Linux box. If no option is passed to the script i.e. `.\build.ps1`, then both the boxes are built. +2. If both the boxes were successfully built, run `vagrant up` to start both. To start any one VM, you can use: + - `vagrant up ub1404` : to start the Linux box + - `vagrant up win2k8` : to start the Windows box 3. When this process completes, you should be able to open the VM within VirtualBox and login. The default credentials are U: `vagrant` and P: `vagrant`. -To build manually: +### To build manually: 1. Clone this repo and navigate to the main directory. 2. Build the base VM image by running `packer build --only=-iso windows_2008_r2.json` where `` is your preferred virtualization platform. Currently `virtualbox` and `vmware` providers are supported. This will take a while the first time you run it since it has to download the OS installation ISO.