Build the base image
Every target starts from the same machine. It is a clean Ubuntu 22.04.1 install. Your host must be able to connect to it with SSH. You do this step one time. In Step 02, you add the shared tools and make a snapshot.
- Time
- ~20 min
- Difficulty
- Easy
- Runs on
- Host machine
- Ends with
- Ubuntu + SSH +
fresh-ossnapshot
Requirements
Everything runs on your own computer. The host does the setup. The guest VM is only for tests. You can delete it and start again at any time.
- VirtualBox 7.0 or later. Install the matching Extension Pack.
- 8 GB of host RAM or more. The base VM uses 2 GB. Some playbooks need 4 GB.
- 25 GB of free disk. The base image uses about 9 GB. Snapshots use more.
- Ansible 2.15 or later and
sshpasson the host. macOS and Linux run them directly. On Windows, use WSL2. - The Ubuntu 22.04.1 LTS server ISO. Newer releases have different package versions. The playbooks need the old versions.
Run these commands to check your host.
VBoxManage --version # expect 7.0.x or later ansible --version | head -1 # expect core 2.15+ which sshpass
Use this exact ISO. Download ubuntu-22.04.1-live-server-amd64.iso. Do not use a newer release. The playbooks check the package versions. They stop if the versions are different.
Create the virtual machine
Use the command line to create the VM. Then the settings are correct for the playbooks. The host-only network lets Ansible connect to the guest. The guest does not join your home network.
VBoxManage createvm --name thehackingmachine --ostype Ubuntu22_LTS_64 --register VBoxManage modifyvm thehackingmachine --memory 2048 --cpus 2 --vram 16 VBoxManage modifyvm thehackingmachine --nic1 nat --nic2 hostonly --hostonlyadapter2 vboxnet0 VBoxManage createmedium disk --filename ~/VirtualBox\ VMs/thehackingmachine/disk.vdi \ --size 25600 --format VDI VBoxManage storagectl thehackingmachine --name SATA --add sata --controller IntelAhci VBoxManage storageattach thehackingmachine --storagectl SATA \ --port 0 --device 0 --type hdd --medium ~/VirtualBox\ VMs/thehackingmachine/disk.vdi
The VM has two network adapters. This is on purpose. The nat adapter gives the guest
internet access. The guest uses it to install packages. The hostonly adapter gives the
guest a private address. You can reach this address from the host. No other device on your network
can reach the guest.
Now attach the ISO and start the VM.
VBoxManage storageattach thehackingmachine --storagectl SATA \ --port 1 --device 0 --type dvddrive --medium ~/iso/ubuntu-22.04.1-live-server-amd64.iso VBoxManage startvm thehackingmachine
Install Ubuntu
Do the steps in the server installer. Keep the default answers. Change only these four answers. The playbooks need them.
- Hostname:
thehackingmachine - Username:
lab. Password:lab. The password is weak on purpose. This machine never goes online. - Storage: use the whole disk. Do not use LVM. LVM makes a rollback harder for some kernel playbooks.
- Snap selection: install only OpenSSH server. Ansible installs everything else later.
When the install is complete, restart the VM. Log in one time at the console. This shows that the VM starts correctly. Then find the host-only address and write it down.
ip -4 addr show enp0s8 | grep inet
You must see 10.0.x.x on enp0s8. If the address is different,
change the DHCP reservation in VirtualBox. The next steps use this address.
Remove the ISO
The install is done, so the VM does not need the ISO anymore. If you leave it attached, the VM can boot the installer again. Shut the VM down and detach the ISO.
sudo poweroff
Wait until the VM window closes. Then run these commands on the host.
VBoxManage storageattach thehackingmachine --storagectl SATA --port 1 --device 0 --medium emptydrive VBoxManage startvm thehackingmachine
The first command empties the virtual DVD drive. The ISO file stays on your disk. The second command starts the VM.
Open SSH from the host
Ansible uses SSH to control the guest. Add your SSH key now. Then the playbooks do not ask for a password.
ssh-keygen -t ed25519 -f ~/.ssh/thehackingmachine -N "" ssh-copy-id -o IdentitiesOnly=yes -i ~/.ssh/thehackingmachine.pub lab@10.0.x.x ssh -o IdentitiesOnly=yes -i ~/.ssh/thehackingmachine lab@10.0.x.x "echo connected"
The last command must show connected.
Take the first snapshot
The VM is clean now. Ubuntu is installed, SSH works, and nothing else is on it. Save this state
as a snapshot named fresh-os. If a later step breaks the VM, you can come back here
without installing Ubuntu again.
Shut the VM down first. A snapshot of a stopped VM is smaller and safer.
VBoxManage controlvm thehackingmachine acpipowerbutton
Wait until the VM window closes. Then take the snapshot and start the VM again.
VBoxManage snapshot thehackingmachine take fresh-os --description "Ubuntu 22.04.1 + SSH" VBoxManage snapshot thehackingmachine list VBoxManage startvm thehackingmachine
The list must show fresh-os.