Sign in to follow this  
Followers 0
mary

Lock unautorised usb stick

5 posts in this topic

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

Share this post


Link to post
Share on other sites



Maybe something like this could be of use. Usb modem, but its USB never the less.

http://www.autoitscript.com/forum/index.php?showtopic=108798&st=0&p=766458&hl=USB&fromsearch=1&#entry766458


They call me MrRegExpMan

Share this post


Link to post
Share on other sites

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
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 .

Share this post


Link to post
Share on other sites

Thanks Melba23 and Juvigy, very helpfull ! :mellow:

Now, I will just find how to lock usb stick...and protect autorised.csv file to prevent modifying

Share this post


Link to post
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
Sign in to follow this  
Followers 0