VirtualBox’s little secret: command-line

Automating your VirtualBox configurations

Bharath
Bharath
Jun 7, 2017 · 4 min read

We often run into features on some software that are little known but are very handy. VirtualBox has one such feature, the command-line.

VBoxManage is the command-line interface to VirtualBox. With it, you can completely control VirtualBox from the command line of your host operating system. VBoxManage supports all the features that the GUI gives you access to, but it supports a lot more than that. It exposes really all the features of the virtualization engine, even those that cannot (yet) be accessed from the GUI.

Fortunately, VBoxManage has extensive documentation which makes life easy. It covers every available option that there is in VBoxManage. If you ever find yourself using VBoxManage, the docs are your go-to reference.

VBoxManage documentation

Rather than going over what’s already covered in official documentation extensively and making this article an yet-another tutorial on VBoxManage, I’ll go over a problem that I solved using VirtualBox command-line recently.

The problem

I started conducting workshops at an open security community. The thing about running workshops at open communities is that it is hard to predict the kinda of software/hardware people walk-in with. You’ll have to be conscious about software dependencies and hardware requirements.

We planned to conduct a workshop on network reconnaissance. We faced a bunch of challenges as part of the lab setup:

  • We wanted audience to run couple of full blown VMs as part of labs to demonstrate remote OS detection techniques using differences in kernel networking stack implementations so using containers is not an option.
  • We wanted to run “ReactOS” to avoid the messy Windows licensing terms. Running ReactOS means, using Vagrant or containers is not an easy option.
  • Audience carry laptops with various host operating systems. Windows doesn’t have a native SSH client, so Vagrant is again not a viable option.
  • We wanted the lab setup to be as automated as possible rather than making audience click through every step, simple VirtualBox GUI won’t cut it.

VBoxManage FTW!

At this point, we were not left with many options and had to turn to good old VirtualBox, that’s when I gave a serious thought to VirtualBox command-line.

  • VirtualBox can run almost every *nix machines and also ReactOS.
  • VBoxManage supports full automation of lab setup(Infact Vagrant uses VBoxManage in the backend)
  • VBoxManage is available on all platforms that has VirtualBox installation.

Steps towards solution

Creating the lab setup
We created a bunch of virtual machines. Few of them act as victims and one VM acts as attacker in the labs. We exported the VMs in OVA format from our Linux machine using VirtualBox GUI. At this point we had a directory with all lab VMs in OVA format.

Importing lab setup

  • A bash script for *nix and a batch file for Windows were created to automate the lab setup importing using VBoxManage.
  • The problem with windows is that VBoxManage is available as a command only from VirtualBox installation directory, so we had to tweak the batch file.
  • The following script imports all the required OVA files and lists all the virtual machines available on the host to check if the OVAs are imported sucessfully.

bash script to import lab VMs:

batch file to import lab VMs:

Starting the labs

  • A bash script for *nix and batch file for Windows were created to run the labs.
  • We wanted to run the victims in the background and only display the attacker. VirtualBox headless mode runs a VM in the background.
  • We exported OVAs from a Linux machine and the VirtualBox host-only network adapter name on windows is not consistent with Linux so we had to use vboxmanage modifyvm to modify the adapter name.
  • The following script run all the victims in background and displays attacker VM. This script lists all running VMs to check if everything is running.

bash script to start the labs:

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

batch file to start the labs:

Stopping labs

  • A bash script for *nix and batch file for Windows were created to gracefully shutdown the labs.
  • vboxmanage controlvm has an option to poweroff running VMs.
  • The following script shuts down all the lab VMs running and lists current running VMs to check if everything shutdown properly.

bash script to stop the labs:

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

batch file to stop the labs:

Conclusion

VBoxManage is a pretty neat and powerful interface provided by VirtualBox. It comes handy when trying to automate your virtual environments especially when trying to distribute your environments. This article looks into one such solution to a problem, this isn’t even tip of the iceberg, find a problem/challenge, read through VBoxManage docs and have fun!

Disruptive Labs

Ramblings of a security antagonist

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store