Easily Access a USB Device from Within a Hyper-V Virtual Machine

Why I Needed to Access a USB Device from a VM

This article came about due to the fact that I needed a way to test how well various antivirus products block the execution of a script embedded in a USB device (which itself emulates a keyboard). The USB device in question is designed to inject keypresses into the Operating System (OS) upon device insertion. Of course, I didn't want to repeatedly install and then uninstall a variety of antivirus products on my own laptop in order to perform the tests.

As Hyper-V is included with Windows 10 Pro, I decided to use a Hyper-V Virtual Machine (VM) for the testing such that I could create a different VM instance for each antivirus product. However, I would need to be able to attach the physical USB device to the VM.

Not only did I need to attach a USB device (physically inserted into my laptop) to the running VM, ideally I needed the USB port to be attached to the VM prior to USB device insertion, such that the automatic injection of keypresses is directed to the VM, rather than the host OS.

Surprisingly, it turns out that this is really simple to achieve and I have shared the instructions below.

Note: There are alternative methods, but this one is simple and suits my use-case nicely.

Instructions

a) If your target Windows 10 VM is currently running in Hyper-V, shut it down.

b) In Hyper-V Manager, right-click on the name of the host machine and then select 'Hyper-V Settings...':

No alt text provided for this image

c) Within the 'Server' → 'Enhanced Session Mode Policy' section, ensure 'Allow enhanced session mode' is enabled:

No alt text provided for this image

d) Within the 'User' → 'Enhanced Session Mode' section, ensure 'Use enhanced session mode' is enabled, and then click 'OK' to save the changes:

No alt text provided for this image

e) The target VM should be off at this point, so start the VM running by right-clicking on the VM instance and selecting 'Start' (do not select 'Connect...'):

No alt text provided for this image

f) Shortly after the VM has started up, right-click on the VM instance and select 'Edit Session Settings...':

No alt text provided for this image

g) Click 'Show Options':

No alt text provided for this image

h) Select 'Local Resources' and then click 'More...':

No alt text provided for this image

i) Expand 'Other supported Plug and Play (PnP) devices' and then enable 'Devices that I plug in later', before clicking 'OK' to save the changes:

No alt text provided for this image

j) Select 'Display' and then enable 'Save my settings for future connections to this virtual machine' before clicking 'Connect':

No alt text provided for this image

That's it! From now on, if you have an active session running with the VM, any USB device that becomes attached to the physical machine will be seen has having become attached to the VM.

Chee Chong Tay

Senior Software Engineer

1 年

Hi Sean Burns, the information above are so useful, did you try it on Ubuntu VM too?

回复
Lance Harvie Bsc (Hons)

21k+ followers | Recruiting IoT, Electronics, FW/SW, (C/C++), FPGAs, Electrical, Mechanical, Avionics, ML/AI, FS Skill-Sets | 97% Placement Retention | Quantity ??

4 年

Nice one Sean Burns

要查看或添加评论,请登录

Sean Burns的更多文章

社区洞察

其他会员也浏览了