USB Manufacturer
Posted: Thu Jul 01, 2010 9:48 am
				
				Hi, 
USB-sticks, SD-cards et-all : a pain in the *beep* when it comes to securing your environment.
I'm trying to make a program that monitors the removable devices, and when the attached device is not acceptable, I simply eject them 
 
From code snippets on the forum, I made something that scans drives C .. Z, if it's a removable drive, I check for the existance of a file on the device and then eventually eject USB. A good starting point, but nothing more.
I would like to make it more clever like checking the manufacturer & type and/or checking serial numbers.
How to ???
Any comment & suggestions are welcomed.
Cheers,
Ludo
P.S. : quick and dirty code ... 
 
			USB-sticks, SD-cards et-all : a pain in the *beep* when it comes to securing your environment.
I'm trying to make a program that monitors the removable devices, and when the attached device is not acceptable, I simply eject them
 
 From code snippets on the forum, I made something that scans drives C .. Z, if it's a removable drive, I check for the existance of a file on the device and then eventually eject USB. A good starting point, but nothing more.
I would like to make it more clever like checking the manufacturer & type and/or checking serial numbers.
How to ???

Any comment & suggestions are welcomed.
Cheers,
Ludo
P.S. : quick and dirty code ...
 
 Code: Select all
OpenConsole()
#DRIVE_TYPE_UNDTERMINED               = 0
#DRIVE_ROOT_NOT_EXIST                 = 1
#DRIVE_REMOVABLE                      = 2
#DRIVE_FIXED                          = 3
#DRIVE_REMOTE                         = 4
#DRIVE_CDROM                          = 5
#DRIVE_RAMDISK                        = 6
#DIGCF_PRESENT = 2
#DIGCF_ALLCLASSES = 4
#SPDRP_SERVICE = 4
Structure SP_DEVINFO_DATA
  cbSize.l
  ClassGuid.GUID
  DevInst.l
  Reserved.l
EndStructure
Global IllegalDevice=#False
Global DevPointer.i=0
Global DeviceInfoData.SP_DEVINFO_DATA
OpenLibrary(1, "setupapi.dll")
*F1 = GetFunction(1, "SetupDiGetDeviceInstanceIdA")
*F2 = GetFunction(1, "SetupDiGetDeviceRegistryPropertyA")
*F3 = GetFunction(1, "CM_Locate_DevNodeA")
*F4 = GetFunction(1, "CM_Request_Device_Eject_ExA")
While 1=1
IllegalDevice=#False
DevPointer=0
For x = 2 To 25       ; start with C:
    NextDrive$ = Chr(x + 65)
    Drive$ = NextDrive$ + ":\"
    Drive_typ.i = GetDriveType_(Drive$)
      Select Drive_typ
        Case #DRIVE_TYPE_UNDTERMINED
          DriveType$ = "  has not been recognized and its type is undetermined."
        Case #DRIVE_ROOT_NOT_EXIST
          DriveType$ = "  specified drive doesn't exist."
        Case #DRIVE_CDROM
          DriveType$ = "  is a CD-ROM Or DVD drive."
        Case #DRIVE_FIXED
          DriveType$ = "  media cannot be removed i.e.. Hard Disk."
        Case #DRIVE_RAMDISK
          DriveType$ = "  is a RAM disk/drive using system memory."
        Case #DRIVE_REMOTE
          DriveType$ = "  is a remote drive (i.e..Network drive)."
        Case #DRIVE_REMOVABLE
          DriveType$ = "  media can be removed (i.e.. Floppy Disk or tape drive) or other removable media or is a device software installed drive for that device use."
      EndSelect
      
   If Drive_Typ<>#DRIVE_ROOT_NOT_EXIST
      PrintN(Nextdrive$+" "+Drivetype$)
   EndIf
   If Drive_Typ=#DRIVE_REMOVABLE
      file$=NextDrive$+":\"+"Authorized.doc"
Debug file$
Debug FileSize(file$)
      If FileSize(file$)=-1
         IllegalDevice=#True
      EndIf
   EndIf
Next
If IllegalDevice
Debug "Need to disconnect !"
   hDeviceInfoSet = SetupDiGetClassDevs_(0, 0, 0, #DIGCF_PRESENT | #DIGCF_ALLCLASSES)
   DeviceInfoData\cbSize = SizeOf(DeviceInfoData)
   *MemoryID = AllocateMemory(500)
   While SetupDiEnumDeviceInfo_(hDeviceInfoSet, DevPointer, @DeviceInfoData)
     DevPointer=DevPointer+1
     CallFunctionFast(*F2, hDeviceInfoSet, DeviceInfoData, #SPDRP_SERVICE, 0, *MemoryID, 500, 0)
     If UCase(PeekS(*MemoryID)) = "USBSTOR"
        CallFunctionFast(*F1, hDeviceInfoSet, DeviceInfoData, *MemoryID, 500, 0)
        If CallFunctionFast(*F3, @devinst.l, *MemoryID, 0) = 0
           CallFunctionFast(*F4, devinst, 0, 0, 0, 0, 0)
        EndIf
     EndIf
   Wend
   SetupDiDestroyDeviceInfoList_(hDeviceInfoSet)
FreeMemory(*MemoryID)
Else
   Debug "Devices are OK"
EndIf
Delay(2000)
Wend
CloseLibrary(1)
CloseConsole()
