Sign in to follow this  
Followers 0
mdwerne

Looking for a better way to locate BitLocker Rec Key

4 posts in this topic

Hello,

Currently I'm using the following code to allow a user to select a BitLocker recovery key file, and then decrypt a drive.

Func GetKey()
    ; Create a constant variable in Local scope of the message to display in FileOpenDialog.
    Local Const $sMessage = "Select the BitLocker Key File."

    ; Display an open dialog to select a file.
    Local $sFileOpenDialog = FileOpenDialog($sMessage, @DesktopCommonDir & "\", "Bitlocker Key File (*.txt)", $FD_FILEMUSTEXIST)
    If @error Then
        ; Display the error message.
        MsgBox($MB_SYSTEMMODAL, "", "No file was selected.")
    Else
        Local $sFileRead = FileRead($sFileOpenDialog)
        ;MsgBox($MB_SYSTEMMODAL, "", $sFileRead)
        Local $iPosition = StringTrimLeft($sFileRead, "378")
        $iPosition = StringTrimRight($iPosition, "239")
        MsgBox($MB_SYSTEMMODAL, "", $iPosition)
        GUICtrlSetData($sRecoveryKey, $iPosition)
    EndIf
EndFunc   ;==>GetKey

I'd like to improve this code in a couple possible ways:

1) Use a REGEX "^[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}$" to locate the recovery key, no matter where it is in the file, and then set the input field with the matched key.

or maybe...

2) Use REGEX again, and create a combobox with all the numbers in the file that match the pattern...just in case there is more than one match.

The 48bit recovery key is always in this format: "000000-000000-000000-000000-000000-000000-000000-000000"

Any suggestions as to how I might proceed?

Thanks,

-Mike

Share this post


Link to post
Share on other sites



mdwerne,

I might do it like this...

; script requires 3.3.10+

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
#include <array.au3>
#include <GuiComboBox.au3>
#include <ComboConstants.au3>

_create_test_file()

Local $gui010 = GUICreate('BitLocker Key Test', 600, 200)
Local $sRecoveryKey = GUICtrlCreateCombo('', 20, 20, 330, 100, $cbs_dropdownlist)
_GUICtrlComboBox_SetCueBanner($sRecoveryKey, "Select a Recovery Key")
GUICtrlSetData($sRecoveryKey, GetKey())

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $sRecoveryKey
            ConsoleWrite('Recovery key selected = ' & GUICtrlRead($sRecoveryKey) & @LF)
    EndSwitch
WEnd

Func GetKey()

    ; Create a constant variable in Local scope of the message to display in FileOpenDialog.
    Local Const $sMessage = "Select the BitLocker Key File."

    ; Display an open dialog to select a file.
    Local $sFileOpenDialog = FileOpenDialog($sMessage, @ScriptDir & "\", "Bitlocker Key File (*.txt)", $FD_FILEMUSTEXIST)
    If @error Then
        ; Display the error message.
        MsgBox($MB_SYSTEMMODAL, "", "No file was selected.")
    EndIf

    Local $ret = StringRegExp(FileRead($sFileOpenDialog), '(?:\d{6}-){7}\d{6}', 3), $tstr

    Switch @error
        Case 0
            For $1 = 0 To UBound($ret) - 1
                $tstr &= '|' & $ret[$1]
            Next
            Return $tstr
        Case 1
            ConsoleWrite('No matches' & @LF)
            Exit
        Case 2
            ConsoleWrite('Bad pattern at offset ' & @extended & @LF)
            Exit
    EndSwitch

EndFunc   ;==>GetKey

; -----------------------------------------------------------------------------------------------------------------------------
;
; functions used to create test file
;
; -----------------------------------------------------------------------------------------------------------------------------

Func _create_test_file()
    Local $str, $FilePath = @ScriptDir & '\bitlocker.txt'
    For $1 = 1 To 10
        For $2 = 1 To 5
            $str &= (Mod($2, 2) = 0) ? _garbage() : _bitlockerpattern()
        Next
    Next
    FileDelete($FilePath)
    FileWrite($FilePath, $str)
EndFunc   ;==>_create_test_file

Func _garbage()
    Local $tstr = ''
    For $1 = 1 To Random(1, 50, 1)
        $tstr &= Chr(Random(1, 256, 1))
    Next
    Return $tstr
EndFunc   ;==>_garbage

Func _bitlockerpattern()
    Local $tstr = ''
    For $1 = 1 To 8
        For $2 = 1 To 6
            $tstr &= Random(0, 9, 1)
        Next
        $tstr &= '-'
    Next
    Return $tstr
EndFunc   ;==>_bitlockerpattern

The gui is rough and proof of concept only.

I dreamed up a test file consisting of junk with valid keys interspersed throughout since you did not provide an example.

kylomas

1 person likes this

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Well I searched and searched but sadly could not find an emoticon that depicted a jaw dropping to the floor...that is how I feel about your example...this is exactly what I was after. The extra step to create the test file shows me just how much I still have to learn, and why this application, the forum, and its members are the best!! :thumbsup:

Thanks kylomas for taking the time to help me out, I really appreciate your effort.

-Mike

Share this post


Link to post
Share on other sites

No problem, glad to help.

In the future please remember to provide as much pertinent data as possible.  SRE's require a complete sample of input data and expected results. 


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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