Page 1 of 1

USB Manufacturer

Posted: Thu Jul 01, 2010 9:48 am
by Ludo
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 :twisted:

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 ... :mrgreen:

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()


Re: USB Manufacturer

Posted: Thu Jul 01, 2010 10:13 am
by Num3
Interesting!

Thanks for the code.

Re: USB Manufacturer

Posted: Thu Jul 01, 2010 10:37 am
by Mr Coder
You know Windows has a setting for admins where you can just deny access to removable drives, right? ;)

Re: USB Manufacturer

Posted: Thu Jul 01, 2010 11:03 am
by Ludo
@Coder : yeps ! But the point is : users ARE allowed to use USB for it is handy too. But I want them to only be able to use the sticks and cards that I provide. Not their own stuff they bring from home .

Ludo

Re: USB Manufacturer

Posted: Thu Jul 01, 2010 5:42 pm
by SFSxOI
GetVolumeInformation API might give you some ideas, there are also some other API's which will give you some info > http://msdn.microsoft.com/en-us/library ... S.85).aspx

Personally, I use WMI through srod's Comate, for this type of information. Here is an example:

Code: Select all


XIncludeFile "COMatePLUS.pbi"

Procedure.s GetFileSystemType(drvspec$)

Define.COMateObject objFSO, DrvSys
objFSO = COMate_CreateObject("Scripting.FileSystemObject")

If objFSO
  DrvSys = objFSO\GetObjectProperty("GetDrive('" + drvspec$ + "')")
  FileSystem$ = DrvSys\GetStringProperty("FileSystem")
EndIf

objFSO\Release()
ProcedureReturn FileSystem$
EndProcedure

; where: drvspec$ = drive letter