Results 1 to 5 of 5

Thread: [RESOLVED] help with LsaRetrievePrivateData

  1. #1

    Thread Starter
    New Member Rodent2008's Avatar
    Join Date
    Jul 2008
    Posts
    10

    Resolved [RESOLVED] help with LsaRetrievePrivateData

    Hi,

    I am having trouble getting data from LsaRetrievePrivateData.

    here is the code
    Code:
    <DllImport("advapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True, PreserveSig:=True)> _
        Private Shared Function LsaRetrievePrivateData(ByVal policyHandle As IntPtr, ByRef KeyName As LSA_UNICODE_STRING, ByRef PrivateData As Long) As UInteger
        End Function
     
    <DllImport("advapi32.dll", PreserveSig:=True)> _
        Private Shared Function LsaOpenPolicy(ByRef SystemName As LSA_UNICODE_STRING, ByRef ObjectAttributes As LSA_OBJECT_ATTRIBUTES, ByVal DesiredAccess As Int32, ByRef PolicyHandle As IntPtr) As UInt32
        End Function
     
    Private Function GetSecret() As String
        Dim lusSecretData As New LSA_UNICODE_STRING()
        Dim PrivateData As Long
        Dim Value As String
     
        Dim LsaPolicyHandle As IntPtr = GetLsaPolicy(LSA_AccessPolicy.POLICY_GET_PRIVATE_INFORMATION)
        Dim result As UInteger = LsaRetrievePrivateData(LsaPolicyHandle, secretName, PrivateData)
        ReleaseLsaPolicy(LsaPolicyHandle)
     
        If result <> 0 Then
            Dim lastError As String = "RetrievePrivateData failed: " & Marshal.GetLastWin32Error & " " & New System.ComponentModel.Win32Exception().Message
            Throw New Exception(lastError)
        End If
     
        lusSecretData = DirectCast(Marshal.PtrToStructure(PrivateData, GetType(LSA_UNICODE_STRING)), LSA_UNICODE_STRING)
        Value = Marshal.PtrToStringUni(lusSecretData.Buffer)
    
        'lusSecretData = Marshal.PtrToStructure(PrivateData, GetType(LSA_UNICODE_STRING))
        'Value = Marshal.PtrToStringAuto(lusSecretData.Buffer).Substring(0, lusSecretData.Length / 2)
    
        Return Value
    End Function
    GetLsaPolicy is a function that returns the LsaPolicyHandle
    ReleaseLsaPolicy is a function that closes the LsaPolicyHandle

    I have a function that Set the Secret and that all works fine.

    What isn't working is the bold sections to get the value back, is there anyone that can guide me in the right direction.

    Thanks
    Rodney

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: help with LsaRetrievePrivateData

    When you say its "not working" what exactly do you mean? What happens when this code runs? Do you get an exception? If so, what is it? etc etc
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: help with LsaRetrievePrivateData

    I have a function that Set the Secret and that all works fine.
    If you can't retrieve the value how do you know that it worked?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    New Member Rodent2008's Avatar
    Join Date
    Jul 2008
    Posts
    10

    Re: help with LsaRetrievePrivateData

    Ok, I have got it working.

    Changed
    Code:
    <DllImport("advapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True, PreserveSig:=True)> _
        Private Shared Function LsaRetrievePrivateData(ByVal policyHandle As IntPtr, ByRef KeyName As LSA_UNICODE_STRING, ByRef PrivateData As Long) As UInteger
        End Function
    To
    Code:
    <DllImport("advapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True, PreserveSig:=True)> _
        Private Shared Function LsaRetrievePrivateData(ByVal policyHandle As IntPtr, ByRef KeyName As LSA_UNICODE_STRING, ByRef PrivateData As LSA_UNICODE_STRING) As UInteger
        End Function
    and the function looks like this.
    Code:
        Private Function GetSecret() As String
            Dim lusSecretData As New LSA_UNICODE_STRING()
            Dim PrivateData As IntPtr
            Dim Value As String = ""
    
            Dim LsaPolicyHandle As IntPtr = GetLsaPolicy(LSA_AccessPolicy.POLICY_GET_PRIVATE_INFORMATION)
            Dim result As UInteger = LsaRetrievePrivateData(LsaPolicyHandle, secretName, PrivateData)
            ReleaseLsaPolicy(LsaPolicyHandle)
    
            If result <> 0 Then
                Dim lastError As String = "RetrievePrivateData failed: " & Marshal.GetLastWin32Error & " " & New System.ComponentModel.Win32Exception().Message
                Throw New Exception(lastError)
            End If
    
            lusSecretData = Marshal.PtrToStructure(PrivateData, GetType(LSA_UNICODE_STRING))
            If lusSecretData.Length > 0 Then
                Value = Marshal.PtrToStringAuto(lusSecretData.Buffer).Substring(0, lusSecretData.Length / 2)
            End If
            LsaFreeMemory(PrivateData)
    
            Return Value
        End Function
    This is now resolved.

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] help with LsaRetrievePrivateData

    Ah yeah that would make sense, as that's how MSDN defines it (no idea why you had it as a Long originally really) http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    Also, MSDN does say "Do not use the LSA private data functions. Instead, use the CryptProtectData and CryptUnprotectData functions."
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width