Adding an ESXi development branch

This commit is contained in:
Chris Long
2020-03-05 22:07:55 -08:00
parent fc23f5a2d9
commit 52ba931948
7 changed files with 393 additions and 0 deletions

31
ESXi/README.md Normal file
View File

@@ -0,0 +1,31 @@
# Building DetectionLab on ESXi
NOTE: This is a work in progress and provisioning is not yet functional. At this time here is what works:
* Building Packer Windows images on ESXi
* Bringing the Windows Images online using Terraform
## Prereqs
1. Install the [requirements from the ESXi Terraform Provider](https://github.com/josenk/terraform-provider-esxi#requirements)
2. Build and install the [terraform-provider-esxi](https://github.com/josenk/terraform-provider-esxi#building-the-provider) provider
3. Your ESXi must have at least two separate networks - one that is accessible from your host (VM Network) and a NAT network to allow the VMs to have internet access (NAT Network). Here's a decent guide to help you with the NAT network: https://medium.com/@glmdev/how-to-set-up-virtualized-pfsense-on-vmware-esxi-6-x-2c2861b25931
## Steps
1. Edit the following variables in `windows_10_esxi.json` and `windows_2016_esxi.json` to match your ESXi configuration:
* remote_datastore
* remote_host
* remote_username
* remote_password
2. From the ESXi directory, run `packer build windows_10_esxi.json` and `packer build windows_2016_esxi.json`. These can be run in parallel from two separate terminal sessions.
3. Once the Packer builds complete, ensure you now see Windows10 and WindowsServer2016 in your ESXi console
4. Edit the variables in `ESXi/variables.tf` to match your local ESXi configuration
5. Run `terraform init && terraform apply`
It takes quite some time for the linked clones to be created, but once they're finished, they should be accessible!
## Future work required
* The logger host needs to be implemented. This should be fairly straightforward.
* The provisioning for the Windows hosts needs to be figured out. I'm not sure if it makes more sense to build it all out in Packer and then just bring the VMs online using Terraform, or if it makes more sense to provision them using something like Ansible.
I'm completely open to any and all input here as this is not my area of expertise :)

119
ESXi/main.tf Normal file
View File

@@ -0,0 +1,119 @@
#########################################
# ESXI Provider host/login details
#########################################
#
# Use of variables here to hide/move the variables to a separate file
#
provider "esxi" {
esxi_hostname = var.esxi_hostname
esxi_hostport = var.esxi_hostport
esxi_username = var.esxi_username
esxi_password = var.esxi_password
}
#########################################
# ESXI Guest resource
#########################################
#
# This Guest VM is a clone of an existing Guest VM named "centos7" (must exist and
# be powered off), located in the "Templates" resource pool. vmtest02 will be powered
# on by default by terraform. The virtual network "VM Network", must already exist on
# your esxi host!
#
# https://github.com/josenk/vagrant-vmware-esxi/wiki/VMware-ESXi-6.5-guestOS-types
resource "esxi_guest" "dc" {
guest_name = "dc"
disk_store = "datastore2"
guestos = "windows9srv-64"
boot_disk_type = "thin"
boot_disk_size = "35"
memsize = "2048"
numvcpus = "2"
resource_pool_name = "/"
power = "on"
# clone_from_vm uses ovftool to clone an existing Guest on your esxi host. This example will clone a Guest VM named "centos7", located in the "Templates" resource pool.
# ovf_source uses ovftool to produce a clone from an ovf or vmx image. (typically produced using the ovf_tool).
# Basically clone_from_vm clones from sources on the esxi host and ovf_source clones from sources on your local hard disk or a URL.
# These two options are mutually exclusive.
clone_from_vm = "WindowsServer2016"
network_interfaces {
virtual_network = var.vm_network
mac_address = "00:50:56:a1:b1:c2"
nic_type = "e1000"
}
network_interfaces {
virtual_network = var.nat_network
}
guest_startup_timeout = 45
guest_shutdown_timeout = 30
}
resource "esxi_guest" "wef" {
guest_name = "wef"
disk_store = "datastore2"
guestos = "windows9srv-64"
boot_disk_type = "thin"
boot_disk_size = "35"
memsize = "2048"
numvcpus = "2"
resource_pool_name = "/"
power = "on"
# clone_from_vm uses ovftool to clone an existing Guest on your esxi host. This example will clone a Guest VM named "centos7", located in the "Templates" r$
# ovf_source uses ovftool to produce a clone from an ovf or vmx image. (typically produced using the ovf_tool).
# Basically clone_from_vm clones from sources on the esxi host and ovf_source clones from sources on your local hard disk or a URL.
# These two options are mutually exclusive.
clone_from_vm = "WindowsServer2016"
network_interfaces {
virtual_network = "var.vm_network"
mac_address = "00:50:56:a1:b1:c2"
nic_type = "e1000"
}
network_interfaces {
virtual_network = "var.nat_network"
}
guest_startup_timeout = 45
guest_shutdown_timeout = 30
}
resource "esxi_guest" "win10" {
guest_name = "win10"
disk_store = "datastore2"
guestos = "windows9-64"
boot_disk_type = "thin"
boot_disk_size = "35"
memsize = "2048"
numvcpus = "2"
resource_pool_name = "/"
power = "on"
# clone_from_vm uses ovftool to clone an existing Guest on your esxi host. This example will clone a Guest VM named "centos7", located in the "Templates" r$
# ovf_source uses ovftool to produce a clone from an ovf or vmx image. (typically produced using the ovf_tool).
# Basically clone_from_vm clones from sources on the esxi host and ovf_source clones from sources on your local hard disk or a URL.
# These two options are mutually exclusive.
clone_from_vm = "Windows10"
network_interfaces {
virtual_network = "var.vm_network"
mac_address = "00:50:56:a1:b1:c2"
nic_type = "e1000"
}
network_interfaces {
virtual_network = "var.nat_network"
}
guest_startup_timeout = 45
guest_shutdown_timeout = 30
}

13
ESXi/outputs.tf Normal file
View File

@@ -0,0 +1,13 @@
output "dc_ips" {
value = esxi_guest.dc.network_interfaces
}
output "wef_ips" {
value = esxi_guest.wef.network_interfaces
}
output "win10_ips" {
value = esxi_guest.win10.network_interfaces
}

28
ESXi/variables.tf Normal file
View File

@@ -0,0 +1,28 @@
#
# See https://www.terraform.io/intro/getting-started/variables.html for more details.
#
# Change these defaults to fit your needs!
variable "esxi_hostname" {
default = ""
}
variable "esxi_hostport" {
default = "22"
}
variable "esxi_username" {
default = "root"
}
variable "esxi_password" { # Unspecified will prompt
}
variable "vm_network" {
default = "VM Network"
}
variable "nat_network" {
default = "NAT Network"
}

106
ESXi/windows_10_esxi.json Normal file
View File

@@ -0,0 +1,106 @@
{
"builders": [
{
"vnc_disable_password": true,
"keep_registered": true,
"remote_datastore": "<fill_me_in>",
"remote_host": "<fill_me_in>",
"remote_username": "<fill_me_in>",
"remote_password": "<fill_me_in>",
"remote_type": "esx5",
"type": "vmware-iso",
"vm_name":"Windows10",
"communicator": "winrm",
"iso_url": "{{user `iso_url`}}",
"iso_checksum_type": "{{user `iso_checksum_type`}}",
"iso_checksum": "{{user `iso_checksum`}}",
"headless": false,
"boot_wait": "6m",
"boot_command": "",
"winrm_username": "vagrant",
"winrm_password": "vagrant",
"winrm_timeout": "4h",
"shutdown_timeout": "2h",
"shutdown_command": "a:/sysprep.bat",
"guest_os_type": "windows9-64",
"disk_size": "{{user `disk_size`}}",
"vnc_port_min": 5900,
"vnc_port_max": 5980,
"version": 11,
"floppy_files": [
"{{user `autounattend`}}",
"../Packer/floppy/WindowsPowershell.lnk",
"../Packer/floppy/PinTo10.exe",
"../Packer/scripts/fixnetwork.ps1",
"../Packer/scripts/rearm-windows.ps1",
"../Packer/scripts/disable-screensaver.ps1",
"../Packer/scripts/disable-winrm.ps1",
"../Packer/scripts/enable-winrm.ps1",
"../Packer/scripts/microsoft-updates.bat",
"../Packer/scripts/win-updates.ps1",
"../Packer/scripts/unattend.xml",
"../Packer/scripts/sysprep.bat"
],
"vmx_data": {
"ethernet0.networkName": "VM Network",
"memsize": "2048",
"numvcpus": "2",
"scsi0.virtualDev": "lsisas1068"
}
}
],
"provisioners": [
{
"type": "windows-shell",
"remote_path": "/tmp/script.bat",
"execute_command": "{{ .Vars }} cmd /c \"{{ .Path }}\"",
"scripts": [
"../Packer/scripts/vm-guest-tools.bat",
"../Packer/scripts/enable-rdp.bat"
]
},
{
"type": "powershell",
"scripts": [
"../Packer/scripts/debloat-windows.ps1"
]
},
{
"type": "windows-restart"
},
{
"type": "powershell",
"scripts": [
"../Packer/scripts/set-powerplan.ps1",
"../Packer/scripts/docker/disable-windows-defender.ps1"
]
},
{
"type": "windows-shell",
"remote_path": "/tmp/script.bat",
"execute_command": "{{ .Vars }} cmd /c \"{{ .Path }}\"",
"scripts": [
"../Packer/scripts/pin-powershell.bat",
"../Packer/scripts/compile-dotnet-assemblies.bat",
"../Packer/scripts/set-winrm-automatic.bat",
"../Packer/scripts/dis-updates.bat"
]
}
],
"post-processors": [
{
"type": "vagrant",
"keep_input_artifact": false,
"output": "windows_10_{{.Provider}}.box",
"vagrantfile_template": "vagrantfile-windows_10.template"
}
],
"variables": {
"iso_checksum": "ab4862ba7d1644c27f27516d24cb21e6b39234eb3301e5f1fb365a78b22f79b3",
"iso_checksum_type": "sha256",
"iso_url": "https://software-download.microsoft.com/download/pr/18362.30.190401-1528.19h1_release_svc_refresh_CLIENTENTERPRISEEVAL_OEMRET_x64FRE_en-us.iso",
"autounattend": "../Packer/answer_files/10/Autounattend.xml",
"disk_size": "61440"
}
}

View File

@@ -0,0 +1,92 @@
{
"builders": [
{
"vnc_disable_password": true,
"keep_registered": true,
"remote_datastore": "<fill_me_in>",
"remote_host": "<fill_me_in>",
"remote_username": "<fill_me_in>",
"remote_password": "<fill_me_in>",
"remote_type": "esx5",
"vm_name":"WindowsServer2016",
"type": "vmware-iso",
"communicator": "winrm",
"iso_url": "{{user `iso_url`}}",
"iso_checksum_type": "{{user `iso_checksum_type`}}",
"iso_checksum": "{{user `iso_checksum`}}",
"headless": false,
"boot_wait": "2m",
"winrm_username": "vagrant",
"winrm_password": "vagrant",
"winrm_timeout": "4h",
"shutdown_timeout": "2h",
"shutdown_command": "a:/sysprep.bat",
"guest_os_type": "windows8srv-64",
"disk_size": 61440,
"version": 11,
"floppy_files": [
"{{user `autounattend`}}",
"../Packer/floppy/WindowsPowershell.lnk",
"../Packer/floppy/PinTo10.exe",
"../Packer/scripts/unattend.xml",
"../Packer/scripts/sysprep.bat",
"../Packer/scripts/disable-screensaver.ps1",
"../Packer/scripts/disable-winrm.ps1",
"../Packer/scripts/enable-winrm.ps1",
"../Packer/scripts/microsoft-updates.bat",
"../Packer/scripts/win-updates.ps1"
],
"vmx_data": {
"ethernet0.networkName": "VM Network",
"memsize": "2048",
"numvcpus": "2",
"scsi0.virtualDev": "lsisas1068"
}
}
],
"provisioners": [
{
"type": "windows-shell",
"execute_command": "{{ .Vars }} cmd /c \"{{ .Path }}\"",
"scripts": [
"../Packer/scripts/vm-guest-tools.bat",
"../Packer/scripts/enable-rdp.bat"
]
},
{
"type": "powershell",
"scripts": [
"../Packer/scripts/debloat-windows.ps1"
]
},
{
"type": "windows-restart"
},
{
"type": "windows-shell",
"execute_command": "{{ .Vars }} cmd /c \"{{ .Path }}\"",
"scripts": [
"../Packer/scripts/pin-powershell.bat",
"../Packer/scripts/set-winrm-automatic.bat",
"../Packer/scripts/compile-dotnet-assemblies.bat",
"../Packer/scripts/uac-enable.bat",
"../Packer/scripts/compact.bat"
]
}
],
"post-processors": [
{
"type": "vagrant",
"keep_input_artifact": false,
"output": "windows_2016_{{.Provider}}.box",
"vagrantfile_template": "vagrantfile-windows_2016.template"
}
],
"variables": {
"iso_url": "https://software-download.microsoft.com/download/pr/Windows_Server_2016_Datacenter_EVAL_en-us_14393_refresh.ISO",
"iso_checksum_type": "md5",
"iso_checksum": "70721288BBCDFE3239D8F8C0FAE55F1F",
"autounattend": "../Packer/answer_files/2016/Autounattend.xml"
}
}