KVM Malware Lab Guide
This is my guide for setting up your very own malware lab using KVM.
Before you start, this guide assumes you have an AirVPN subscription or another equivalent one, which provides a .ovpn file.
NOTE: I like to have internet enabled on my analysis VMs and this comes with extra security considerations and potential risk if you do not perform the setup correctly. It is recommended that you have your KVM host machine on a DMZ. Most home routers do have DMZ settings, just consult your manual. This guide is not intended for beginners.
If you have used VirtualBox or VMWare Workstation in the past, Spice is just like the guest tools for these but for KVM, which allows you to copy/paste and copy samples to your VMs from your host machine.
NOTE: This is not a very stealthy setup as of yet, so do expect some malware to detect your VMs. I will continue to update this guide for stealth once I have a server or workstation. This requires forking the qemu code and modifying it.
Once this guide is completed, you should have networking that looks like this:
|
|
Install KVM
This section of the guide will show you how to install KVM on your Linux host.
- Verify you have virtualization enabled
|
|
- Enable Nested Virtualization (optional)
|
|
- If there is any output from the previous command then virtualization is enabled, otherwise, enable it in your bios and try again NOTE: Using nested virtualization will come with significant performance costs, it is much more advisable to use Linux with KVM on bare metal
- Install dependencies
|
|
- Verify that libvirtd service is running
|
|
- Add current user to the libvirt group
|
|
- Install modified version of SeaBios (modifies src/config.h to more realistic values)
|
|
- If you want virt-manager to be able to access your home directory, you may need to set upstream permissions (this step is optional)
|
|
- Once all has been completed do a reboot
|
|
Create VMS
This section will guide you through the creation of the malware lab with KVM using virt-manager.
Create Virtual Networks
- Edit -> Connection Details -> + -> vmbr0 Isolated (PFSense LAN)
- Disable both IPV4 and IPv6 (Provided by PFSense)
- Edit -> Connection Details -> + -> vmbr1 Isolated (Analysis LAN)
- Disable both IPV4 and IPv6 (Provided by Remnux)
Create PFSense VM
The primary purpose of the PFSense VM is to provide an easy way to manage firewall rules between your home or work network and the internet. I know most people disable their internet when analyzing malware. However, I like to use an anonymous VPN and with that comes additional risk and considerations.
- Download: https://www.pfsense.org/download/
- Create qcow2 image:
|
|
- Create a new virtual machine
- Import Existing Disk Image
- Select pfsense.qcow2 as the storage path
- Select FreeBSD as the OS
- Name: pfsense
- Customize configuration before install
- Set first NIC to NAT
- Add Hardware -> Network -> vmbr0 (PFSense Network)
- Add Hardware -> Network -> CDROM Device -> Manager -> PFSense ISO
- Boot Options -> Set CDROM device as first in boot order
- Start the VM
- Set LAN as 10.0.1.1/24 in PFSense setup
Create Remnux VM (Ubuntu 22.04)
The primary purpose of this VM is to allow you to perform static analysis of binaries using IDA, Ghidra or Cutter but also acts as the gateway for your entire analysis subnet. This gives you full control over the DNS and DHCP server for traffic interception and more.
NOTE: The official remnux distribution OS is quite outdated, to solve this it is recommended to use the latest LTS image from Ubuntu as the VM and instead use remnux within that VM as a container.
- Download https://releases.ubuntu.com/22.04/
- Create the disk image
|
|
- Create a new virtual machine
- Import existing disk image
- Select remnux.qcow2 as the storage path
- Select Ubuntu as the OS
- Name: remnux
- Customize configuration before install
- Set first NIC to vmbr0 (PFSense Network)
- Add Hardware -> Network -> vmbr1 (Analysis Network)
- Add Hardware -> Storage -> CDROM Device-> Manage -> Ubuntu ISO Image
- Boot Options -> Set CDROM device as first in boot order
- Start VM
- Install dependancies
|
|
- Create your remnux docker container
|
|
- Set up the interfaces
|
|
- Disable dnsmasq conflicts with systemd-resolved
|
|
- Setup the dhcp server
|
|
- Browse to https://10.0.1.1 and login with admin:pfsense and setup your own password for PFSense.
- Add firewall rules to block your local network, in my case it was to block everything from LAN net to 192.168.0.1/24.
- Apply the changes
- Setup internet forwarding
|
|
- Create a script to allow to you start your VPN
|
|
- Setup TLS Decryption with MITMProxy
|
|
Once completed, you should be able to reboot and run sudo ./vpn.sh to get connected to the internet. NOTE: The internet will not work on the Analysis LAN unless you have VPN enabled as we set the internet to be forwarded through tun0 for openvpn. This is a good thing, this means it will force you to use an anonymous VPN and reduce risk of leaking your actual IP address.
Create Windows VM
This VM will sit on the Analysis LAN, its main purpose is to dynamically analyze malware by executing it. This includes debugging, tracing API calls and more.
NOTE: With a Windows VM it is very important to ensure your boot drive is using the VirtIO drivers.
- Download VirtIO ISO
- Create disk image
|
|
- Create a new virtual machine
- Import existing disk image
- Select windows.qcow2 as the storage path
- Select Windows as the OS
- Name: windows
- Customize configuration before install
- Set first NIC to vmbr1 (Analysis LAN)
- Add Hardware -> Storage -> CDROM Device-> Manage -> Windows ISO Image
- Add Hardware -> Storage CDROM Device -> Manage -> VirtIO ISO
- Boot Options -> Set CDROM device for Windows ISO to boot first
- Remove the SATA Disk and replace with one for VirtIO pointing to windows.qcow2
- Start installation
- During the Windows install the VirtIO disk will not show unless you browse to the VirtIO ISO and select the folder with the drivers for your version of Windows. Once completed, you can proceed with the installation.
- Once Windows is installed, open Device Manager and identify all the drivers with question marks and update them using the VirtIO ISO CDROM.
- Download and install Spice Guest Tools
- Reboot the VM
If you are unable to boot using the VirtIO driver, try the following troubleshooting steps:
- Open an elevated command prompt and set the VM to boot into safe mode by typing
- bcdedit /set {current} safeboot minimal
- shut-down the VM and change the boot device type to virtio
- boot the VM. It will enter in safe mode.
- in the booted VM reset the bcdedit settings to allow the machine to boot into the Normal mode by typing (in an elevated command prompt again):
- bcdedit /deletevalue {current} safeboot
Reference: Windows 10 in KVM: change boot disk to Virtio
Once you have completed all necessary steps, it is recommended to remove the CDROM drive for the VirtIO drivers.
Installing TLS Decryption Certificates
This section of the guide will discuss how to get TLS decryption working on your Remnux VM so you can transparently decrypt TLS and capture secrets and pcaps from the analysis network as long as the devices have the root CA installed.
- On your Remnux machine do the following commands
|
|
- On your Windows VM open a browser and go to mitm.it then follow either the Manual or Automated install instructions
- Manual Install
- On the webpage download the certificate for Windows
- Double click the P12 file to start the import wizard
- Select a certificate store location. This determines who will trust the certificate – only the current Windows user or everyone on the machine. Click Next.
- Click Next again
- Leave Password blank and click Next
- Select Place all certificates in the following store, then click Browse, and select Trusted Root Certification Authorities. Click OK and Next
- Click Finish
- Click Yes to confirm the warning dialog.
- Automated Install
- certutil.exe -addstore root mitmproxy-ca-cert.cer
- In your browser visit https://example.com to verify a successful decryption in the mitmproxy interface
- Manual Install
If the above steps were successful, you can now use the tool mitmpcap to view traffic live with mitmproxy but also use it to capture TLS secrets and a pcap for analysis later.
|
|
Once you have finished your capture, press Q then Y to exit mitmproxy. You should then notice you have the files dump.pcap and secrets.txt. The following steps will show you how to use Wireshark to analyze your capture.
- Open dump.pcap with Wireshark
- Edit -> Preferences -> Protocols -> TLS -> (Pre)-Master-Secret log filename Browse -> select secrets.txt
- In the filter bar type http and press Enter
- You should now have decrypted TLS traffic
Once you have completed capturing your traffic and no longer wish to perform interception, disable the redirection by executing the following.
|
|
Install Malware Analysis Tools
Once you have completed the networking and VM creation, you can install your favorite malware analysis tools!
You can get a list of my favorite tools here.
Establishing a Clean Environment
Once you have completed installing your favorite tools, establish a clean environment by taking snapshots of every VM. This will ensure you can go back to those clean states after you have completed an analysis.
Sharing Files Between Host and Remnux VM
Sometimes we wish to move files that are safe from our Remnux VM to our host machine. To do this, we can use the file system pass-through in KVM. Please note, it is not recommended to have your host and guest tied together at all times, to reduce your risk only mount this when needed and unmount when done.
- Memory
- Enable Shared Memory (checkmark)
- Add Hardware
- Filesytem hostfs
- Driver -> virtiofs
- Source path -> Browse Local -> Select Source Folder to Share
- Target path -> hostfs
- XML Tab -> under the filesystem tag put accessmode=passthrough Example XML:
|
|
- Boot Remnux VM
- Mount the folder
|
|
Forwarding Ports
Using virt-manager
you can edit XML by enabling Edit->Preferences->General->Enable XML editing
.
Using this, ensure the <domain>
XML is <domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
.
Host to Guest
To forward ports from your host machine to your guest machine, you can use QEMU command-line with -netdev
and -device
.
The id
and netdev
options must be the same unique string.
The guestfwd
option by default uses the net
option of 10.0.2.0/24
for the subnet.
To change this we set our own net
option to 10.0.3.0/24
, using the IP address 10.0.3.1
.
We are forwarding, in this case, 11434 from the host machine, to 10.0.3.1:11434
on a guest interface.
The XML for this is provided below.
|
|
This example, uses the ollama
port for working with LLMs, now we can access the port by doing curl http://10.0.3.1:11434
.
This usecase is for those who may want to use LLMs for static reversing but do not have two available graphics cards for PCI passthrough.
NOTE: It is important to know that forwarding ports to your host machine can pose additional risks
Guest to Host
Similar to forwarding ports from the host machine to the guest machine, we use the netdev
option.
However, this time we are forwarding port 2222
on the host to 22
on the guest.
If SSH is enabled on the guest machine, from your host machine you can do ssh -p 2222 username@127.0.0.1
to get a shell.
|
|
NOTE: It is important to note that accessing your guest machines directly using additional applications on your host can pose additional risks.
Workflow
When working with malware it is important to establish a general workflow, please refer to my guide here.
Conclusion
Once you have the PFSense and Remnux VMs setup, you can create additional VMs on your Analysis LAN to suit your needs.
Creating a malware lab using KVM can be a daunting task for beginners compared to using VMWare Workstation or VirtualBox. However, it can benefit your lab in the following ways.
- Access to source code you can edit to make your VMs super stealthy
- CPU Emulated VMs (MIPS, ARM, etc.) for IoT malware
- PCI pass through of hardware
Again I cannot recommend KVM enough, it is great for malware analysis!