USB Manufacturer

Just starting out? Need help? Post your questions and find answers here.
Ludo
User
User
Posts: 40
Joined: Thu Oct 16, 2008 12:55 pm
Location: Belgium-Tiegem

USB Manufacturer

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

Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Re: USB Manufacturer

Post by Num3 »

Interesting!

Thanks for the code.
Mr Coder
User
User
Posts: 54
Joined: Tue Apr 13, 2010 8:02 am

Re: USB Manufacturer

Post by Mr Coder »

You know Windows has a setting for admins where you can just deny access to removable drives, right? ;)
Ludo
User
User
Posts: 40
Joined: Thu Oct 16, 2008 12:55 pm
Location: Belgium-Tiegem

Re: USB Manufacturer

Post 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
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: USB Manufacturer

Post 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

The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
Post Reply