Search code examples
vb.netactive-directorybitlocker

VB.net get bitlocker Password ID from Active Directory


I have a VB.net program that I am trying to add a bitlocker lookup tool that will search active directory for the machine name, and display the "Password ID" as well as the "Recovery Password"

So far my script/code works flawlessly for the lookup and displaying the Recovery Password, but I cannot get it to display the Password ID.

I've tried:

Item.Properties("msFVE-RecoveryGuid")(0)

Which returns the error "System.InvalidCastException: Conversion from type 'Byte()' to type 'String' is not valid."

Item.Properties("msFVE-RecoveryGuid")(0).ToString

Which returns "System.Byte[]"

Item.Properties("msFVE-RecoveryGuid").ToString

Which returns "System.DirectoryServices.ResultPropertyValueCollection"

So far in my searching I've only seen C# examples, and I haven't been able to translate.

The same for Recovery Password works however:

(Item.Properties("msFVE-RecoveryPassword")(0))

Here is the larger snippet of what I have for context:

    Dim RootDSE As New DirectoryEntry("LDAP://RootDSE")
    Dim DomainDN As String = RootDSE.Properties("DefaultNamingContext").Value
    Dim ADsearch As New DirectorySearcher("LDAP://" & DomainDN)

    ADsearch.Filter = ("(&(objectClass=computer)(name=" & MachineName & "))")

    Dim ADresult As SearchResult = ADsearch.FindOne
    Dim ADpath As String = ADresult.Path

    Dim BTsearch As New DirectorySearcher()

    BTsearch.SearchRoot = New DirectoryEntry(ADpath)
    BTsearch.Filter = "(&(objectClass=msFVE-RecoveryInformation))"

    Dim BitLockers As SearchResultCollection = BTsearch.FindAll()



    Dim Item As SearchResult

    Dim longTempstring As String = ""

    For Each Item In BitLockers
        If Item.Properties.Contains("msFVE-RecoveryGuid") Then

            Dim tempstring As String = Item.Properties("msFVE-RecoveryGuid")(0).ToString

            longTempstring = longTempstring & tempstring & vbNewLine
            'ListBox2.Items.Add(Item.Properties("msFVE-RecoveryGuid")(0))

        End If
        If Item.Properties.Contains("msFVE-RecoveryPassword") Then

            ListBox1.Items.Add(Item.Properties("msFVE-RecoveryPassword")(0))

        End If
    Next

    MsgBox(longTempstring)

Solution

  • So I figured out that I needed to convert the bytes to hex in order to get them to match what is viewed in the Microsoft Management Console. Once I began doing that the only problem I ran into is that I discovered the indexing of the byte arrays are not in the same order as they are in Active Directory. -- so instead of looping I had to list out each index of the Byte array and sort them to their proper positions so that they match how they show up in AD.

    My end function is:

    Function bitread(ByVal GUID As Byte())
        Dim tempVar As String
        tempVar = GUID(3).ToString("X02") & GUID(2).ToString("X02") _
            & GUID(1).ToString("X02") & GUID(0).ToString("X02") & "-" _
            & GUID(5).ToString("X02") & GUID(4).ToString("X02") & "-" _
            & GUID(7).ToString("X02") & GUID(6).ToString("X02") & "-" _
            & GUID(8).ToString("X02") & GUID(9).ToString("X02") & "-" _
            & GUID(10).ToString("X02") & GUID(11).ToString("X02") _
            & GUID(12).ToString("X02") & GUID(13).ToString("X02") _
            & GUID(14).ToString("X02") & GUID(15).ToString("X02")
        Return tempVar
    End Function
    

    Called with:

    bitread(Item.Properties("msFVE-RecoveryGUID")(0))