Service that responds to insertion / removal of USB devices. Reports them to log file and Event Log.
Can block certain devices based on Hardware ID.
- Log all USB device connect / disconnect events into Event Log
- Log such events into a file (path is specified in registry)
- Block connection of different devices based on Hardware ID
install
Install service *uninstall
Uninstall service *deny <HardwareID>
Add device to deny list *allow <HardwareID>
Remove device from deny list *denylist
Print deny listlogfile [path]
Get or set* absolute path (likeC:\log.txt
) for log fileh
,help
Print help message
* requires administrative privileges (Run as Admin)
- Install UsbMonitor service:
usbmon.exe install
* - (optional) Configure log file path:
usbmon.exe logfile "C:\path\to\log.txt"
* - Start service from Services menu or with
sc start UsbMonitor
* - Connect or disconnect some USB devices
- Open log file or EventVwr.exe (go to Windows Logs/Application)
- Notice Hardware ID value of format
USB\VID_041D&PID_C584&REV_0409
- (optional) To block device, use Hardware ID:
usbmon.exe deny "USB\VID_041D&PID_C584&REV_0409"
* - (optional) To unblock device, use Hardware ID:
usbmon.exe allow "USB\VID_041D&PID_C584&REV_0409"
* and enable it in Device Manager (if needed)
* requires administrative privileges (Run as Admin)
Service stores its config in registry. Base path is HKLM\SYSTEM\CurrentControlSet\Services\UsbMonitor\
.
Under this key:
Parameters\
LogFile
(REG_SZ) - Path to log file, likeC:\path\to\log.txt
DenyList\
USB_VID_041D&PID_C584&REV_0409
(DWORD, always 1) - if value exists, Hardware ID is in deny list- < ... >
If some keys don't exist, they are created automatically.
If LogFile
is not set, logs are sent to Event Log only.
Some Chinese flash drives may have strange Unicode hardware IDs, which can be confusing for Registry if set as a value name.
It can be solved by storing hex-values for hardware IDs (possibly MD5 of ID string) with corresponding user-friendly name of rule.
This scheme allows for quick dereference by hardware ID. Just calculate hash and check if it's there.
DenyList\
9d2cff9017b505e8beb206df9fd2efb3
(REG_SZ) =<rule_name>
- < ... >
This scheme allows for quick dereference by rule name. Also guarantees that rule names are unique, and it generally looks more natural.
DenyList\
<rule_name>
(REG_SZ) =9d2cff9017b505e8beb206df9fd2efb3
- < ... >
MD5 of Hardware IDs should appear in log file and Event Log instead of raw Hardware IDs.
usbmon.exe deny <name> <hwId_MD5>
usbmon.exe allow <name> <hwId_MD5>
I don't know if I implement this in near future, though I hope so ฅ ^•ﻌ•^ ฅ °。
-
Service template: https://learn.microsoft.com/en-us/windows/win32/services/svc-cpp
-
Device notifications: https://www.codeproject.com/Articles/15612/Receiving-Device-Event-Notification-in-Windows-Ser
- Note: do not set
DEVICE_NOTIFY_ALL_INTERFACE_CLASSES
, instead specifyclassguid
(seeSvcMain()
inservice.c
)
- Note: do not set
-
Working with registry: https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regqueryvalueexa
-
Handle USB events: https://learn.microsoft.com/en-us/answers/questions/985652/getting-instance-id-of-usb-drive-from-registerdevi
∩―――――――――――∩ . . || ∧ ヘ || . ゜ ゚. ゜ ✧ 。 || (* ´ ー`) ZZzz ° ★ ゚ ゜ ✧ |ノ^⌒⌒づ` ̄ ̄ \ ゚。 ゚ ★ 。゚ ( ノ ⌒ ヽ \ ゚ ★ 。 \ || ̄ ̄ ̄ ̄ ̄|| ゚ \,ノ|| ||