Unattended Kali Linux installation

This post is all about setting up an automatic Kali Linux installation with packer and/or vagrant

General Information

The following article assumes the use of Kali Linux & Virtualbox. Vmware is not included, so changes for this have to be done manually.

Setting up a Vagrantfile as configuration for your pentesting environment is particularly useful to set up a machine on the get-go. When a machine is compromised, multiple pentesting environments with a default configuration are needed or you just want to be able to spawn new ready-to-go pentesting machines on other devices, you can just copy the Configuration file(s), run (packer &) vagrant and you're all set.

When no configuration for the installation process of the Kali image is necessary you may as well proceed right to the Vagrantfile configuration step

Technically you could do all the configuration when creating a base image with packer, but this article focuses on creating a base image with custom installation options and then modifying the system afterwards with vagrant. This allows you to have a custom base installation, while being able to automate the configuration with vagrant afterwards. So if you have to change some configuration, but need the same base installation, you don't have to run through the whole installation process again.

vagrant: tool to import boxes from either the official repositories or self-made

packer: tool to create self-made boxes (from an iso image)

Creating a custom Kali Linux image with packer

This template will be useful in order to pass custom instructions to the installation of Kali Linux, which will be automatically executed when the vm starts.

The following configuration will create a basic Kali Linux installation, you can adjust the settings as needed. Just refer to the mentioned template and add the options accordingly.

packer.json: configuration for the packer binary

preseed.cfg: configuration for the installer (here: Kali Linux)

After using packer to create a box, note that the vagrant user will have sudo permissions without requiring a password. This will be revoked when using the pre-configured Vagrantfile from this article. However if another Vagrantfile is used, you should take into account that you'll have to revoke this permission in order to have a save system.

Folder structure

The folder structure matters when the installer trys to load the preseed.cfg from the localhosts http server (that will be created automatically).

According to packer when working with a preseed file the structure should be as follows:

Packer will be execute from project_dir.

preseed.cfg

packer.json

Consider changing the following options:

  • iso_urls => which kali image should be used (local path is also possible); images can be found here

  • iso_checksum => checksum of the iso file (e.g. sha1, sha256 etc.)

  • disk_size => size of the virtual disk to be created

  • vboxmanage => hardware configuration for virtualbox (e.g. vram, ram, cpus etc.)

Building the box

With the configuration provided here, this will create a new directory and export the installed image to this directory (which can be found at builds/virtualbox-kali.box)

Configure the Kali Linux image with vagrant

Configuration options for Vagrantfiles can be found here. Something I find particularly useful are triggers, which allow you for example to execute commands before and after vagrant up.

Vagrantfile

The following configuration pulls the latest official vagrant-kali image, upgrades it and installs some packages that can be defined in the $installsoftware variable.

Consider changing the following option:

  • config.vm.provider "virtualbox" => hardware configuration for virtualbox (e.g. vram, ram, cpus etc.)

Installation

In order to have a "save" installation the default password of user vagrant has to be changed.

Installing an already existing box

When using the configuration from here, this will create a pre-configured kali linux image from the official website.

The Vagrantfile has to be in the current directory or any subdirectory starting at VAGRANT_CWD

Importing a self-made box and installing it

This will be necessary when a custom box has been created at this step

change the name of the box in the Vagrantfile

add and install the box

Automating the process

The following script is a quick & dirty solution to automate the installation process based on the files in the current directory and the boxes already imported into vagrant

  • if overwrite yes => run packer (and overwrite base image $boxpath (file) and $boxname (vagrant import) )

  • if $vagrantfile exists => run vagrant

    • if there's already an image in virtualbox with the same name/id, vagrant will most likely fail here

References

Last updated

Was this helpful?