Automate Active Directory(Installation(Packer)+Provisioning(Vagrant))

n00🔑
5 min readApr 28, 2022

Hi Readers, Here we will be looking into automation of ad deployment. This challenge is part of Auror Project initiative by Zscaler’s Sudarshan Pisupati 🙏

Prerequisites:

  1. Packer
  2. Vagrant
  3. Virtualbox/Vmware Workstation(i have used VirtualBox for demonstration)
  4. Curiosity/Laziness

The Challenge/Goal:

Let’s create two machines, Machine A and Machine B.

Machine A (Domain controller)-

  1. Machine A is the domain controller
  2. Domain Name is “auror.local”
  3. Has DNS role
  4. Create a user “Adam” with password “Pass@123”

Machine B-

  1. Machine B is the machine to join to domain auror.local
  2. Machine B should have Chrome installed
  3. User Adam is configured as an administrator
  4. Firewall should be off
  5. Machine A and Machine B must be in the same subnet.

For example:

Machine A : 10.0.0.9, Machine B: 10.0.0.19

Test Cases

  • RDP into Machine B with user “Adam” should be successful
  • From Machine B as user Adam, the command “net use \\auror.local” should result in command completed successfully
  • Run script Powerview.ps1 function “Get-DomainUser” from Machine B should show Adam as a user
  • Chrome should be installed on Machine B

Let’s Start….

Brief introduction about packer(HCL2 format templates) and vagrant. Official definition-

Packer is an open source tool that enables you to create identical machine images for multiple platforms from a single source template. A common use case is creating “golden images” that teams across an organization can use in cloud infrastructure. in short it is similar to create docker images which can be uploaded to repositories, packer creates .box image files which is supported by different virtualization tools like vmware, vagrant etc.

Core components-

a) Source defines the initial image to use to create your customized image. Any defined source is reusable within build blocks.

b) Builders are responsible for creating machines from the base image, customizing the image as defined, and then creating a resulting image.

c) Provisioners use built-in and third-party integrations to install packages and configure the machine image.

  • Default one’s are file(The file Packer provisioner uploads files to machines built by Packer) and shell(as name suggests allows you to run os commands)
  • 3rd party- ansible ,chef, powershell, puppet

d) Post Processors- Post-processors run after the image is built by the builder and provisioned by the provisioner(s). These are optional, and they can be used to upload artifacts, re-package, or more. They are also defined in build block.

In our case our post processor block is -

post-processor "vagrant" {
keep_input_artifact = false //artifact will be deleted
output = "windows_2022_{{.Provider}}.box"
vagrantfile_template = "vagrantfile-windows_2016.template"
}

e) Variables(as the name suggests)

Some commands-

packer build <json/hcl conifg file>
packer build -debug <json/hcl conifg file>
packer -var <variable=value>
#Packer format
packer fmt <unformatted hcl config>
packer validate <config file>
packer hcl2_upgrade <json config>#packer env vars
PACKER_LOG #enables packer logs
PACKER_LOG_PATH #spcifes file path for storing log file

Vagrant is a Virtual machine orchestration tool. It supports various vurtualization tools like vmware workstation, vmware esxi, virtualbox, Mictrosoft hyper-V etc. It supports different pulgins for these.

Refer to its docs for more-https://www.vagrantup.com/docs

Some commands-

vagrant statusvagrant box list
vagrant box outdated
vagrant box update
vagrant box remove <box>
vagrant box add <box_path>
vagrant plugin list
vagrant plugin update
vagrant plugin uninstall <pluin>
vagrant package --output <centos.box> # run this after making changes, this is similar to creating images in docker
vagrant package --base <vm_name>
VAGRANT_HOME=F:\\.vagrant.d
VAGRANT_DEFAULT_PROVIDER=vmware_desktop
#Provision
vagrant provision
vagrant powershell

Now let’s solve our challenge-

Step 1- Creating a vagrant box using packer

note- Make sure to clone this repo before proceeding(https://github.com/pswalia2u/TheAurorProject)

Your directory tree should look like this

cd packer_project
packer build apni.json.pkr.hcl
packer build <packer_config_file>

Make sure you have these files before running packer build-

Windows_SERVER_2022_EVAL_x64FRE_en-us.iso or any other windows server (≥server 2016) iso

Autounattend.xml

vagrantfile-windows_2016.template

scripts

Now for next step we configure this .box file in vagrant config file.

Step 2- Provisioning using vagrant

Make sure you have these files before running packer build-

Vagrantfile(https://raw.githubusercontent.com/pswalia2u/TheAurorProject/main/Challenge_1/vagrant_project/Vagrantfile)

cd Challenge_1/vagrant_project
vagrant up

Wait for vagrant to provision both machines…

Finally we have 2 auto configured windows machines.

Running Testcases-

  • RDP into Machine B with user “Adam” should be successful
  • From Machine B as user Adam, the command “net use \\auror.local” should result in command completed successfully
  • Run script Powerview.ps1 function “Get-DomainUser” from Machine B should show Adam as a user
powershell.exe -exec Bypass -C "IEX (New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/PowerShellMafia/PowerSploit/dev/Recon/PowerView.ps1');Get-DomainUser | Select-Object samaccountname"
  • Chrome should be installed on Machine B

Whole challenge is pushed to https://github.com/pswalia2u/TheAurorProject/

References-

https://www.vagrantup.com/docs

https://www.packer.io/docs

https://github.com/StefanScherer/packer-windows

https://github.com/pswalia2u/TheAurorProject

Thanks for reading!!!

--

--