legend

Lock script to USB drive

1 post in this topic

#1 ·  Posted (edited)

;----------------------------------------------------------------------------------

   $Drivelabel = DriveGetLabel(StringLeft(@ScriptDir, 2))

;----------------------------------------------------------------------------------

   $FullPath = @ScriptDir
   $drive = StringLeft(@ScriptDir, 2)
   $id = _GetPNPDeviceID($drive)
   ConsoleWrite("ID: " & $id & @CRLF & "Labelname: " & $Drivelabel & @CRLF)

;----------------------------------------------------------------------------------

    If $id = ("24WCG18A3CZAEI41&0") and $Drivelabel = ("myusb") Then
    MsgBox("","","Access has been granted - Correct USB Device.")
    Else
    MsgBox("","","Access has been denied - Wrong USB Device.")
    EndIf

    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

Change this line, to the requires information from your usb stick:

If $id = ("24WCG18A3CZAEI41&0") and $Drivelabel = ("myusb") Then

Let me know if anybody is having suggestions for making it even more secure.

Edited by legend

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