Automate your VirtualBox configuration

Original address: medium.com/disruptive-…

Original author: medium.com/@0xbharath

Published: June 7, 2017-4 minutes to read

We often come across some little-known but very convenient features in some software. VirtualBox has one such feature, the command line.

VBoxManage is a VirtualBox command line interface. With it, you can completely control VirtualBox from the command line of your host operating system. VBoxManage supports everything the GUI gives you, but it does a lot more. It exposes all the capabilities of the virtualization engine, even those that are not (yet) accessible from the GUI.

Fortunately, VBoxManage has plenty of documentation to make life easy. It covers all the options available in VBoxManage. If you find yourself using VBoxManage, the documentation is your reference.

VBoxManage document

Rather than something already covered extensively in the official documentation that makes this another tutorial on VBoxManage, it’s a problem I’ve recently solved using the VirtualBox command line.

The problem is

I started running workshops in an open safe community. The great thing about hosting workshops in an open community is that it’s hard to predict what software/hardware people will walk in with. You must be aware of software dependencies and hardware requirements.

We plan to hold a seminar on cyber spying. As part of the lab setup, we face a number of challenges.

  • We wanted the audience to run several complete virtual machines as part of the lab to demonstrate remote OS detection techniques that take advantage of differences in kernel networking stack implementations, so using containers was not possible.
  • We wanted to run “ReactOS “to avoid confusing Windows license terms. Running ReactOS means that using Vagrant or containers is not an easy option.
  • Laptops carried by spectators have a variety of host operating systems. Windows doesn’t have a native SSH client, so Vagrant wasn’t a viable option.
  • We wanted the setup of the lab to be as automated as possible, rather than having the viewer click on every step. A simple VirtualBox GUI would not suffice.

VBoxManage FTW!

At this point, we didn’t have much choice but to turn to the old VirtualBox, which IS when I seriously considered the VirtualBox command line.

  • VirtualBox can run almost any * NIx machine as well as ReactOS.
  • VBoxManage supports full automation of lab Settings (Vagrant actually uses VBoxManage behind the scenes).
  • VBoxManage is available on all platforms with VirtualBox installed.

Steps of the solution

Creating lab Settings

We created a bunch of virtual machines. Some of these virtual machines are the victims, and one virtual machine is the attacker in the lab. We used the VirtualBox GUI to export the VIRTUAL machine in OVA format from our Linux machine. At this point we have a directory with all the lab virtual machines in OVA format.

Import laboratory Settings

  • We provide*nixA bash script was created to create a batch file for Windows to automatically import lab Settings using VBoxManage.
  • The problem with Windows is that VBoxManage can only be used as a command in the VirtualBox installation directory, so we had to adjust the batch file.
  • The following script imports all required OVA files and lists all available virtual machines on the host to check whether the OVA was successfully imported.

The bash script was imported to the lab VM.

#! /usr/bin/env bash
VBoxManage import "victim1.ova"
VBoxManage import "victim2.ova"
VBoxManage import "attacker.ova"
printf "\n\nList of all the VMs\n------------------------\n"
VBoxManage list vms
Copy the code

Batch script imports laboratory VMS.

PATH=%PATH%; C:\Program Files\Oracle\VirtualBox vboxmanage import "victim1.ova" vboxmanage import "victim2.ova" vboxmanage import "attacker.ova"echo. &echo 'List of all VMs' && echo. &echo '-------------------------'
vboxmanage list vms
cmd \k
Copy the code

Start lab

  • We created one*nixBash scripts and Windows batch files to run the lab.
  • We want to run the victim in the background, showing only the attacker. VirtualBox headless mode runs a virtual machine in the background.
  • We exported the OVA from a Linux machine and the VirtualBox host private network adapter name on Windows was not the same as Linux, so we had to use vboxManage ModifyVM to change the adapter name.
  • The following script runs all the victims in the background and displays the attacker’s virtual machine. This script lists all the running virtual machines to check that everything is running.

The bash script starts the lab.

#! /usr/bin/env bash
vboxmanage startvm "victim1" --type headless
vboxmanage startvm "victim2" --type headless
vboxmanage startvm "attacker"
printf "\n\nList of all the VMs running\n------------------------------\n"
vboxmanage list runningvms
Copy the code

Batch script starts the lab.

PATH=%PATH%; C:\Program Files\Oracle\VirtualBox vboxmanage modifyvm "victim1" --nic1 hostonly --hostonlyadapter1 "VirtualBox Host-Only Ethernet Adapter" vboxmanage modifyvm "victim2" --nic1 hostonly --hostonlyadapter1 "VirtualBox Host-Only Ethernet Adapter" vboxmanage modifyvm "attacker" --nic1 hostonly --hostonlyadapter1 "VirtualBox Host-Only Ethernet Adapter" vboxmanage startvm "victim1" --type headless
vboxmanage startvm "victim2" --type headless
vboxmanage startvm "attacker"
echo. &echo 'List of all the running VMs' && echo. &echo '-------------------------'
vboxmanage list runningvms
cmd \k
Copy the code

Stop lab

  • To create the*nixBash scripts and Windows batch files to gracefully shut down LABS.
  • vboxmanage controlvmThere is an option to shut down a running virtual machine.
  • The following script shuts down all running lab virtual machines and lists the currently running virtual machines to check that everything is shut down properly.

The bash script stops the lab.

#! /usr/bin/env bash
vboxmanage controlvm "victim1" poweroff
vboxmanage controlvm "victim2" poweroff
vboxmanage controlvm "attacker" poweroff
printf "\n\nList of all the VMs running\n------------------------------\n"
vboxmanage list runningvms
Copy the code

Batch files to stop the lab.

PATH=%PATH%; C:\Program Files\Oracle\VirtualBox vboxmanage controlvm "victim1" poweroff vboxmanage controlvm "victim2" poweroff vboxmanage controlvm "attacker" poweroffecho. &echo 'List of all the running VMs' && echo. &echo '-------------------------'
vboxmanage list runningvms
cmd \k
Copy the code

conclusion

VBoxManage is a very clean and powerful interface provided by VirtualBox. It comes in handy when you’re trying to automate your virtual environment, especially when you’re trying to distribute your environment. This article explores a solution to such a problem, which is not the tip of the iceberg, find a problem/challenge, read the VBoxManage documentation and have fun!


Translation via www.DeepL.com/Translator (free version)