- January 30, 2018
- Posted by: Surender Kumar
- Category: Hyper-V, Windows PowerShell
Learning Ethical Hacking or Penetration Testing may require you to install Virtual Box on your Windows host OS. For those who don’t know about VirtualBox (or Hyper-V); these are virtualization software that you can use to run virtual machines inside your main operating system. Both of these software do same thing. So, they are not compatible with each other. What I mean is you can run either of these software but not both.
Most of Ethical Hacking tutorials will guide you to setup the lab environment using VirtualBox since this is an open source virtualization software available for all major platforms such as Microsoft Windows, Apple Macintosh and Linux totally free of cost. However, if you are on Windows (Education, Professional, or Enterprise) and using Microsoft’s proprietary virtualization software Hyper-V for something important and do not want to remove it. Since Hyper-V and VirtualBox both cannot work at the same time, you would most probably want to stick with Hyper-V for setting up your Ethical Hacking lab environment. This article will help you in such scenario.
In this test lab, we will setup two virtual machines. The first one will be the attacker machine (kali linux) and the second one will be victim machine (metasploitable2).
Enable Hyper-V Feature
If you have not already enabled Hyper-V, just enable it via single command. Just open an elevated Windows PowerShell console and enter the following command:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V –All
This will take a while and install all features related to Hyper-V. Restart your computer if prompted.
Create NAT Network
The first step is to create an Internal virtual switch. This switch will not be connected to a physical NIC of the host. Instead, the host OS will have a virtual NIC that will connect to the virtual switch. At the end, the virtual machines connected to the internal virtual switch will be able to talk to the host OS, but cannot talk to the external network the host OS is connected to. Open an elevated Windows PowerShell console and enter the following command:
New-VMSwitch -SwitchName “NATSwitch” -SwitchType Internal
The next step updates the virtual NIC that connects the host OS to the internal virtual switch. The following command will assign an IP address to this virtual NIC, and this IPv4 address will act as default gateway for NAT network that we are creating.
New-NetIPAddress -IPAddress 192.168.10.1 -PrefixLength 24 -InterfaceAlias "vEthernet (NATSwitch)"
The last step is to configure the network address of the NAT network that will run on the virtual switch. This essentially is the private address range used by the virtual machines connected to virtual switch. Make sure that the IPv4 address in the previous step must be in this range.
New-NetNat -Name "NATNetwork" -InternalIPInterfaceAddressPrefix 192.168.10.0/24
This is it. Any virtual machine that connects to virtual switch will use an IPv4 address in the 192.168.10.0/24 address range. The machines will route to the LAN via the host OS’s NIC and NAT exactly the same way that your laptop accesses the Internet via the router in your home.
One of the most important thing to note is that there is no DHCP functionality in the virtual switch like VirtualBox. If you want DHCP service so that all virtual machines connected to this virtual switch automatically obtains the IP configuration, then you must build a DHCP server virtual machine and connect it to the same virtual switch. Otherwise, if you do not worry about DHCP, you can assign static IP addresses to the virtual machines. In our Ethical Hacking lab setup, we will be using only two virtual machines and we can easily configure static IP on both of them.
Setup Kali Linux Virtual Machine
Firstly we will setup our Kali Linux virtual machine that will act as attacker device.
You need to download the virtual disk image for Hyper-V. You can download the Kali linux image that is ready to be used right out of box saving you lot of time from installing it from scratch. Visit the offensive security link and go to Kali Linux Hyper-V Images tab, then click the download link as shown in following image
After downloading, you will get a 7z archive. Just extract the archive to your preferred location on disk. I would like to keep mine in D:\VM\Hyper-V\kali-linux-2017.3.
UPDATE 18-March-2019: Since Offensive Security website no longer offers Kali Hyper-V image, I have uploaded Kali version 2017.3 image to my Google drive just in case someone want to download it. After downloading the zip file, extract it, and import the Kali VM.
Importing Kali VM
- Click Start button, type Hyper-V Manager. Launch the Hyper-V Manager from there.
- In the Action page, click Import Virtual Machine link. This will launch Import Virtual Machine wizard. Click Next.
- In Locate Folder screen click Browse and select the location where you extracted the downloaded file and click Next.
- Now select the name of virtual machine you want to import and click Next.
- On Choose Import Type screen, select Copy the virtual machine
- On Choose Destination screen, click the checkbox next to Store the virtual machine in a different location. Here you can select your preferred location for VM.
- Now choose storage folders to store Virtual hard disk of imported machine.
- In summary screen, verify your settings and click Finish button to start the import.
- After import is successful, you will see Kali linux virtual machine in Hyper-V Manager.
Connecting Kali VM to NAT Network
- In Hyper-V Manager, right click Kali virtual machine and select Settings from context menu.
- Select Network Adapter from the Hardware list in the left and click Remove button and then click Apply button but don’t click OK yet.
- Select Add Hardware option on top of left menu, select Legacy Network Adapter and click Add
- As soon as you click Add button, you will be taken to Legacy Network Adapter settings. Under Virtual Switch dropdown, select NATSwitch we created above, click Apply and then OK
Now, your kali machine is connected to NAT network and ready to be used.
Configuring Static IP in Kali VM
After booting Kali linux, login using following credentials:
After login, click on Network icon and select Wired Settings
Click on gear icon against Wired network and then select IPv4 tab. Here you can configure static IP as per network range you selected while creating NATNetwork and click Apply button on top right corner.
You will notice that your Kali machine can now connect to internet via NAT and you are good to go.
Setup Metasploitable 2 Linux Virtual Machine
Now that we have configured our attacker machine (kali linux), it is time to setup the victim machine (metasploitable).
Downloading Metasploitable VM
Metasploitable is a virtual machine running Ubuntu linux that is intentionally designed vulnerable to carry out attacks while learning penetration testing. It is available for download on Rapid7 website. You need to fill your basic information before you can download it. You will get a zip file after download gets finished. If you extract it, you will notice that the file format is .vmdk.
For those who don’t know, let me explain a bit about .vmdk file.
VMDK is a virtual machine disk file format which is natively used by VirtualBox and Vmware (another virtualization software). Since we are using Hyper-V (instead of VirtualBox and Vmware), we will have to convert this .vmdk file to .vhd or .vhdx (native file format used by Hyper-V). This is not that difficult. Fortunately, there is a free tool called Microsoft Virtual Machine Converter 3.0 which can convert these file formats to one-another. You can download if from Microsoft website.
Converting Metasploitable VMDK to VHD
After downloading and installing Microsoft Virtual Machine Converter, follow these steps to convert the file:
- Open an elevated PowerShell console
- Use the following command to import the MVMC module:
Import-Module "C:\Program Files\Microsoft Virtual Machine Converter\MvmcCmdlet.psd1"
- Prepare the source path with vmdk disk and the destination path. I extracted my Metasploitable VM on source path “D:\VM\Hyper-V\Metasploitable2\Metasploitable.vmdk” and I want to store the converted file in same location. So, my command would be something like shown below:
ConvertTo-MvmcVirtualHardDisk -SourceLiteralPath "D:\VM\Hyper-V\Metasploitable2\Metasploitable.vmdk" -DestinationLiteralPath "D:\VM\Hyper-V\Metasploitable2\" -VhdType DynamicHardDisk -VhdFormat Vhd
SourceLiteralPath: It is the location of the .vmdk file
DestinationLiteralPath: It is just the path to new .vhd (or .vhdx) disk
VhdType: You can use FixedHarddisk or DynamicHarddisk
VhdFormat: The format of disk can be vhd or vhdx
- Copy the above command and paste it in PowerShell console and hit enter. If everything goes fine, you will see progress bar that says Converting drive
- It will take some time for conversion and at the end you will see success message as shown in below image
Creating Metasploitable VM
- Open the Hyper-V Manager
- Go in the right Pane Actions and click New – – > Virtual Machine. A new virtual machine wizard will open up.
- On first screen, click Next.
- On next screen specify name and location of virtual machine
- On Specify Generation screen, leave it default (Generation 1) and click Next.
- On Assign memory screen, enter 512 to assign 512MB of memory, clear the checkbox against Use dynamic memory and click Next. Since Metasploitable is just command based VM, 512MB is enough for this.
- On Configure Networking screen, select NATSwitch (we created earlier) from dropdown list and click Next.
- This is the main step. On the Connect Virtual Hard Disk screen, check the radio button against Use an existing virtual hard disk and then browse to select the virtual hard disk file we just converted via PowerShell command and then click Next.
- On Summary screen, check your settings and click Finish to create the Metasploitable virtual machine.
Follow exactly the same process as mentioned in Kali Linux section. First remove the existing Network adapter and then add the Legacy Network Adapter.
Configuring Static IP in Metasploitable VM
After creating & connecting Metasploitable VM to NAT Network, start it and login via following credentials:
Since Metasploitable just works with command-line, you will need to set the static IP configuration using commands. Follow these steps:
- Use the following command to open main config file:
sudo nano /etc/network/interfaces
This will prompt for root password, just enter msfadmin and hit enter.
- When the file gets opened, change this:
auto eth0 iface eth0 inet dhcp
auto eth0 iface eth0 inet static address 192.168.10.11 netmask 255.255.255.0 network 192.168.10.0 broadcast 192.168.10.255 gateway 192.168.10.1 dns-nameservers 126.96.36.199
Make sure you adjust these numbers according to address range you selected while creating NATNetwork. At the end, your config file should look like this:
To save and close the File, press Ctrl + X keys. It will prompt you to save the modified file. Just type y and hit enter twice.
- Now you need to restart the networking services for new IP settings to take effect. You can do this by following command:
sudo /etc/init.d/networking restart
That is it. You can now verify the connectivity of both Kali VM and Metaspolitable VM by Ping.
Congratulations. You just finished setting your Lab environment for Ethical Hacking and Penetration testing on Windows 10 using Hyper-V.