Jump to content

Lock unautorised usb stick


mary
 Share

Recommended Posts

Hi !

my goal is to lock unautorised usb stick to plug. Here the steps:

-Event: insertion of usb stick

-read the usb stick disk identifier (I imagine is a unique serial number in USB' ROM ?)

-if the identifier not exist in the whitelist (autorised.csv) then lock it (bloc pluging)

thanks for any help

Link to comment
Share on other sites

  • Moderators

mary,

Here is some code to get the unique ID of USB sticks/drives attached to your machine. It should help you get started on your project! :mellow:

; credit spudw2k

$aDrives = DriveGetDrive("Removable")

For $i = 1 To $aDrives[0]
    $id = _GetPNPDeviceID($aDrives[$i])
    MsgBox(0, $aDrives[$i], $id)
Next

Func _GetPNPDeviceID($drive, $fullid = 0)
    $objWMIService = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2")
    If Not IsObj($objWMIService) Then Return -1 ;Failed to Connect to WMI on Local Machine

    $colDevice = $objWMIService.ExecQuery("SELECT * from Win32_LogicalDiskToPartition")
    $var = ""
    For $objItem In $colDevice
        If StringInStr($objItem.Dependent, $drive) Then
            $var = StringTrimLeft($objItem.Antecedent, StringInStr($objItem.Antecedent, "="))
        EndIf
    Next
    If Not $var Then Return -2 ;Failed to Find Drive Letter

    $colDevice = $objWMIService.ExecQuery("SELECT * from Win32_DiskDriveToDiskPartition")
    $diskpartition = $var
    $var = ""
    For $objItem In $colDevice
        If StringInStr($objItem.Dependent, $diskpartition) Then
            $var = StringTrimLeft($objItem.Antecedent, StringInStr($objItem.Antecedent, "="))
        EndIf
    Next
    If Not $var Then Return -3 ;Failed to Find Physical Drive #

    $colDevice = $objWMIService.ExecQuery("SELECT * from Win32_DiskDrive")
    $physicaldrive = StringReplace(StringReplace($var, "\\", "\"), '"', "")
    $var = ""
    For $objItem In $colDevice
        If $objItem.DeviceID = $physicaldrive Then
            $var = $objItem.PNPDeviceID
        EndIf
    Next
    If Not $var Then Return -4 ;Failed to Find PNPDeviceID

    If Not $fullid Then $var = StringTrimLeft($var, StringInStr($var, "\", 0, -1)) ;Return Ugly Full PNPDeviceID
    Return $var
EndFunc   ;==>_GetPNPDeviceID

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

$strComputer = "localhost"
$objWMIServices = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$sink = ObjCreate("WbemScripting.SWbemSink")
ObjEvent($sink, "SINK_")
$objWMIServices.ExecNotificationQueryAsync($sink, "SELECT * FROM __InstanceOperationEvent WITHIN 5 WHERE " & "TargetInstance ISA 'Win32_LogicalDisk'")
;- End of setting up the WMI event ----------------
Sleep(111111)
;- The event that is called upon detecting a USB storage device.
Func sink_OnObjectReady($objObject, $objAsyncContext)
    Local $choice
    If $objObject.TargetInstance.DriveType = 2 Then ;- TargetIstance.DriveType = 2 means a Removable drive.
        Select
            Case $objObject.Path_.Class () = "__InstanceCreationEvent" ;- Storage Device is plugged in.
                ConsoleWrite("Drive " & $objObject.TargetInstance.DeviceId & "has been inserted." & @CR)
            Case $objObject.Path_.Class () = "__InstanceDeletionEvent" ;- Storage device has been removed.
                ConsoleWrite("Drive " & $objObject.TargetInstance.DeviceId & "has been removed." & @CR)
            Case Else 
                MsgBox(0,"","WTF?")
        EndSelect
    EndIf
EndFunc

This is how you detect when a usb drive is plugged .

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...