USB detection using WMI script

USB flash drives are very common and can be foundSet wmi = GetObject("winmgmts:\\" &
in almost every computerized environment for storingstrComputer & "\root\cimv2")
and transferring data between computers. These USBSet wmiEvent = wmi.ExecNotificationQuery("select *
devices make it really easy for potential attacker tofrom __InstanceOperationEvent within 1 where
exploit unprotected computers with malicious virus andTargetInstance ISA 'Win32_PnPEntity' and
Trojan software and provide a gateway to theTargetInstance.Description='USB Mass Storage
network for manipulating sensitive data.Device'")
Detecting USB storage devicesWhile True
There are some nice tools that can be found on theSet usb = wmiEvent.NextEvent()
net that will notify about USB devices on local andSelect Case usb.Path_.Class
remote windows platforms. But most of them are notCase "__InstanceCreationEvent" WScript.Echo("USB
free and will require an installation of an agent on thedevice found")
remote windows platforms. Using the preinstalledCase "__InstanceDeletionEvent" WScript.Echo("USB
Windows Management Instrumentation (WMI) ondevice removed")
windows platforms is free and will not require anyCase "__InstanceModificationEvent"
remote agent. It will only require a simple script that canWScript.Echo("USB device modified")
be run manually from a privileged user account or fromEnd Select
another network monitoring software like SecurityWend
Center: IDS IPS Network Access Protection andJScript (should be copied and saved as .js
Switch Protector: NAC Network Access Controlfile):strComputer = "."; //(Any computer name or
Monitoring network security scanners.address)var wmi = GetObject("winmgmts:\\\\" +
WMI notification event scriptstrComputer + "\\root\\cimv2");var wmiEvent =
The following USB notification event script will send anwmi.ExecNotificationQuery("select * from
event message in response to any operation of USB__InstanceOperationEvent within 1 where
device on local or remote windows platform. ForTargetInstance ISA 'Win32_PnPEntity' and
simplicity, the script is using a temporary eventTargetInstance.Description='USB Mass Storage
subscription, which exists only as long as the script isDevice'");while(true) {var usb =
running. Some modifications will be needed for awmiEvent.NextEvent();switch (usb.Path_.Class) {case
permanent event subscription that will not require a"__InstanceCreationEvent": {WScript.Echo("USB
perpetually running script:device found"); break;}case "__InstanceDeletionEvent":
VBScript (should be copied and saved as .vbs{WScript.Echo("USB device removed"); break;}case
file):strComputer = "." '(Any computer name or"__InstanceModificationEvent": {WScript.
address)