Skip to content

Skip packing option #636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Requirements:
* Internet connection

### To build automatically:
0. Clone this repo and navigate to the main directory. While in the main directory, navigate to `packer` folder. Once you are in the folder you need to open the `packer.pck.hcl` with your preferred code editor, open terminal and run `$packer init .`

1. - On **Linux/OSX** run `./build.sh windows2008` to build the Windows box or `./build.sh ubuntu1404` to build the Linux box. If /tmp is small, use `TMPDIR=/var/tmp ./build.sh ...` to store temporary packer disk images under /var/tmp.
- 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.
Expand All @@ -50,7 +51,8 @@ Requirements:

### To build manually:

1. Clone this repo and navigate to the main directory.
1. Clone this repo and navigate to the main directory. While in the main directory, navigate to `packer` folder. Once you are in the folder you need to open the `packer.pck.hcl` with your preferred code editor, open terminal and run `$packer init .`

2. Build the base VM image by running `packer build --only=<provider> ./packer/templates/windows_2008_r2.json` where `<provider>` is your preferred virtualization platform. Currently `virtualbox-iso`, `qemu`, and `vmware-iso` providers are supported. This will take a while the first time you run it since it has to download the OS installation ISO.
3. After the base Vagrant box is created you need to add it to your Vagrant environment. This can be done with the command `vagrant box add packer/builds/windows_2008_r2_*_0.1.0.box --name=rapid7/metasploitable3-win2k8`.
4. Use `vagrant plugin install vagrant-reload` to install the reload vagrant provisioner if you haven't already.
Expand Down
81 changes: 32 additions & 49 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ $vagrantMinVersion = "1.9.0"
$vagrantreloadMinVersion = "0.0.1"
$packer = "packer.exe"
$expectedVBoxLocation = "C:\Program Files\Oracle\VirtualBox"
$expectedVagrantLocation="C:\HashiCorp\Vagrant\bin"


$expectedVagrantLocation = "C:\Program Files\Vagrant\bin"

function CompareVersions ($actualVersion, $expectedVersion, $exactMatch = $False) {
If ($exactMatch) {
Expand All @@ -22,7 +20,7 @@ function CompareVersions ($actualVersion, $expectedVersion, $exactMatch = $False
$actualVersion = $actualVersion.split(".")
$expectedVersion = $expectedVersion.split(".")

for($i=0; $i -le $expectedVersion.length; $i++) {
for($i=0; $i -lt $expectedVersion.length; $i++) {
If([INT]$actualVersion[$i] -gt [INT]$expectedVersion[$i]) {
return $True
}
Expand All @@ -34,12 +32,12 @@ function CompareVersions ($actualVersion, $expectedVersion, $exactMatch = $False
return $True
}

Write-Host "";
Write-Host ""

If ($(Test-Path "$expectedVBoxLocation\VBoxManage.exe") -eq $True) {
If (Test-Path "$expectedVBoxLocation\VBoxManage.exe") {

$vboxVersion = cmd.exe /c "$expectedVBoxLocation\VBoxManage.exe" -v
$vboxVersion = $vboxVersion.split("r")[0]
$vboxVersion = $vboxVersion.split("r")[0].Trim()

} else {

Expand All @@ -49,7 +47,6 @@ If ($(Test-Path "$expectedVBoxLocation\VBoxManage.exe") -eq $True) {

}


If (CompareVersions -actualVersion $vboxVersion -expectedVersion $virtualBoxMinVersion -exactMatch $False) {

Write-Host "Compatible version of VirtualBox found."
Expand All @@ -64,23 +61,23 @@ If (CompareVersions -actualVersion $vboxVersion -expectedVersion $virtualBoxMinV
}

$packerVersion = cmd.exe /c $packer -v
$packerVersion = $packerVersion -replace '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."
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) {
If (Test-Path "$expectedVagrantLocation\vagrant.exe") {

$vagrantVersion = cmd.exe /c "vagrant" -v
$vagrantVersion = $vagrantVersion.split(" ")[1]
$vagrantVersion = $vagrantVersion.split(" ")[1].Trim()

} else {

Expand All @@ -101,18 +98,15 @@ If (CompareVersions -actualVersion $vagrantVersion -expectedVersion $vagrantMinV

}


$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."
Expand All @@ -124,7 +118,6 @@ If (![string]::IsNullOrEmpty($vagrantPlugins)) {
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

Expand All @@ -143,10 +136,10 @@ function InstallBox($os_full, $os_short)

Write-Host "Building metasploitable3-$os_short Vagrant box..."

If ($(Test-Path "packer\builds\$($os_full)_virtualbox_$boxversion.box") -eq $True) {
If (Test-Path "packer\builds\$($os_full)_virtualbox_$boxversion.box") {

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
Expand All @@ -166,7 +159,7 @@ function InstallBox($os_full, $os_short)
} 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 {
Expand All @@ -175,36 +168,26 @@ function InstallBox($os_full, $os_short)
}
}



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'";
}
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 "";

Write-Host ""
Empty file added packer/packer.pck.hcl
Empty file.