From ecb6c4fe9085aaa1c5c7c7999bf9a84378a1677e Mon Sep 17 00:00:00 2001 From: Abhinay Jangde Date: Sat, 29 Jun 2024 07:34:17 +0530 Subject: [PATCH 1/2] If face some error when I run build.ps1 file so if found solution thats why I make some change and this is work correctly --- README.md | 2 + packer.pkr.hcl | 8 ++ updatedBuild.ps1 | 193 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 203 insertions(+) create mode 100644 packer.pkr.hcl create mode 100644 updatedBuild.ps1 diff --git a/README.md b/README.md index ff3f8ba1..eedbeb9f 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,8 @@ recipes, so that you can always return to an initial state (`vagrant restore fre If you want a _totally_ fresh snapshot, you can do the initialization with `vagrant up --no-provision`, then take a snapshot, followed by `vagrant provision`. +## Error Solutions +> If you are facing problem when you run build.ps1 file you can use updatedBuild.ps1 file and make sure packer.pkr.hcl is configure correctly. ## Vulnerabilities * [See the wiki page](https://github.com/rapid7/metasploitable3/wiki/Vulnerabilities) diff --git a/packer.pkr.hcl b/packer.pkr.hcl new file mode 100644 index 00000000..b48e570a --- /dev/null +++ b/packer.pkr.hcl @@ -0,0 +1,8 @@ +packer { + required_plugins { + vagrant = { + version = ">= 1.0.0" + source = "github.com/hashicorp/vagrant" + } + } +} \ No newline at end of file diff --git a/updatedBuild.ps1 b/updatedBuild.ps1 new file mode 100644 index 00000000..249f5104 --- /dev/null +++ b/updatedBuild.ps1 @@ -0,0 +1,193 @@ +$ErrorActionPreference = "Stop" + +$virtualBoxMinVersion = "6.1.0" +$packerMinVersion = "1.6.0" +$vagrantMinVersion = "1.9.0" +$vagrantreloadMinVersion = "0.0.1" +$packer = "packer.exe" +$expectedVBoxLocation = "C:\Program Files\Oracle\VirtualBox" +$expectedVagrantLocation = "C:\Program Files\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 -lt $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") { + + $vboxVersion = cmd.exe /c "$expectedVBoxLocation\VBoxManage.exe" -v + $vboxVersion = $vboxVersion.split("r")[0].Trim() + +} 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 +$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." + exit + +} + +If (Test-Path "$expectedVagrantLocation\vagrant.exe") { + + $vagrantVersion = cmd.exe /c "vagrant" -v + $vagrantVersion = $vagrantVersion.split(" ")[1].Trim() + +} 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") { + + 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 "" From 6bac08cdff06be447fb8994383a2501c370ba222 Mon Sep 17 00:00:00 2001 From: Abhinay Jangde Date: Sat, 29 Jun 2024 07:39:23 +0530 Subject: [PATCH 2/2] add vagrant file for bothe vmware and virtualbox --- ForVMware.txt | 54 ++++++++++++++++++++++++++++++++++++++++++ ForVirtualbox.txt | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 ForVMware.txt create mode 100644 ForVirtualbox.txt diff --git a/ForVMware.txt b/ForVMware.txt new file mode 100644 index 00000000..c3510725 --- /dev/null +++ b/ForVMware.txt @@ -0,0 +1,54 @@ +# -- mode: ruby -- +# vi: set ft=ruby : + +Vagrant.configure("2") do |config| + config.vm.synced_folder '.', '/vagrant', disabled: true + + # Define Ubuntu 14.04 VM + config.vm.define "ub1404" do |ub1404| + ub1404.vm.box = "rapid7/metasploitable3-ub1404" + ub1404.vm.hostname = "metasploitable3-ub1404" + config.ssh.username = 'vagrant' + config.ssh.password = 'vagrant' + + ub1404.vm.network "private_network", ip: '192.168.50.102' + + ub1404.vm.provider "vmware_desktop" do |v| + v.vmx["ethernet0.pcislotnumber"] = "33" + v.memory = 2048 + end + end + + # Define Windows Server 2008 VM + config.vm.define "win2k8" do |win2k8| + # Base configuration for the VM and provisioner + win2k8.vm.box = "rapid7/metasploitable3-win2k8" + win2k8.vm.hostname = "metasploitable3-win2k8" + win2k8.vm.communicator = "winrm" + win2k8.winrm.retry_limit = 60 + win2k8.winrm.retry_delay = 10 + + win2k8.vm.network "private_network", ip: '192.168.50.103' + + config.vm.provider "vmware_desktop" do |v| + v.vmx["ethernet0.pcislotnumber"] = "34" + v.memory = 4096 + v.cpus = 2 + v.gui= true + end + + # Configure Firewall to open up vulnerable services + case ENV['MS3_DIFFICULTY'] + when 'easy' + win2k8.vm.provision :shell, inline: "C:\\startup\\disable_firewall.bat" + else + win2k8.vm.provision :shell, inline: "C:\\startup\\enable_firewall.bat" + win2k8.vm.provision :shell, inline: "C:\\startup\\configure_firewall.bat" + end + + # Insecure share from the Linux machine + win2k8.vm.provision :shell, inline: "C:\\startup\\install_share_autorun.bat" + win2k8.vm.provision :shell, inline: "C:\\startup\\setup_linux_share.bat" + win2k8.vm.provision :shell, inline: "rm C:\\startup\\*" # Cleanup startup scripts + end +end diff --git a/ForVirtualbox.txt b/ForVirtualbox.txt new file mode 100644 index 00000000..78385757 --- /dev/null +++ b/ForVirtualbox.txt @@ -0,0 +1,60 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +Vagrant.configure("2") do |config| + config.vm.synced_folder '.', '/vagrant', disabled: true + config.vm.define "ub1404" do |ub1404| + ub1404.vm.box = "rapid7/metasploitable3-ub1404" + ub1404.vm.hostname = "metasploitable3-ub1404" + config.ssh.username = 'vagrant' + config.ssh.password = 'vagrant' + + ub1404.vm.network "private_network", ip: '172.28.128.3' + + ub1404.vm.provider "virtualbox" do |v| + v.name = "Metasploitable3-ub1404" + v.memory = 2048 + end + end + + config.vm.define "win2k8" do |win2k8| + # Base configuration for the VM and provisioner + win2k8.vm.box = "rapid7/metasploitable3-win2k8" + win2k8.vm.hostname = "metasploitable3-win2k8" + win2k8.vm.communicator = "winrm" + win2k8.winrm.retry_limit = 60 + win2k8.winrm.retry_delay = 10 + + win2k8.vm.network "private_network", type: "dhcp" + + win2k8.vm.provider "libvirt" do |v| + v.memory = 4096 + v.cpus = 2 + v.video_type = 'qxl' + v.input :type => "tablet", :bus => "usb" + v.channel :type => 'unix', :target_name => 'org.qemu.guest_agent.0', :target_type => 'virtio' + v.channel :type => 'spicevmc', :target_name => 'com.redhat.spice.0', :target_type => 'virtio' + v.graphics_type = "spice" + + # Enable Hyper-V enlightenments: https://blog.wikichoon.com/2014/07/enabling-hyper-v-enlightenments-with-kvm.html + v.hyperv_feature :name => 'stimer', :state => 'on' + v.hyperv_feature :name => 'relaxed', :state => 'on' + v.hyperv_feature :name => 'vapic', :state => 'on' + v.hyperv_feature :name => 'synic', :state => 'on' + end + + # Configure Firewall to open up vulnerable services + case ENV['MS3_DIFFICULTY'] + when 'easy' + win2k8.vm.provision :shell, inline: "C:\\startup\\disable_firewall.bat" + else + win2k8.vm.provision :shell, inline: "C:\\startup\\enable_firewall.bat" + win2k8.vm.provision :shell, inline: "C:\\startup\\configure_firewall.bat" + end + + # Insecure share from the Linux machine + win2k8.vm.provision :shell, inline: "C:\\startup\\install_share_autorun.bat" + win2k8.vm.provision :shell, inline: "C:\\startup\\setup_linux_share.bat" + win2k8.vm.provision :shell, inline: "rm C:\\startup\\*" # Cleanup startup scripts + end +end \ No newline at end of file