Click here to Skip to main content
15,913,685 members
Home / Discussions / Visual Basic
   

Visual Basic

 
QuestionCreating a file directory VB Pin
New_Coder6-Oct-08 14:36
New_Coder6-Oct-08 14:36 
AnswerRe: Creating a file directory VB Pin
Dave Kreskowiak6-Oct-08 16:04
mveDave Kreskowiak6-Oct-08 16:04 
Questionvb.net to vb script converter Pin
lostboy2416-Oct-08 9:13
lostboy2416-Oct-08 9:13 
AnswerRe: vb.net to vb script converter Pin
Paul Conrad6-Oct-08 9:48
professionalPaul Conrad6-Oct-08 9:48 
AnswerRe: vb.net to vb script converter Pin
Dave Kreskowiak6-Oct-08 10:38
mveDave Kreskowiak6-Oct-08 10:38 
GeneralRe: vb.net to vb script converter Pin
lostboy2416-Oct-08 14:01
lostboy2416-Oct-08 14:01 
QuestionRight fax issue with email ID format Pin
lostboy2416-Oct-08 4:25
lostboy2416-Oct-08 4:25 
QuestionLSA Functions Problems Pin
cyberhiker6-Oct-08 4:21
cyberhiker6-Oct-08 4:21 
I am trying to write my own System Policy Auditing application for a variety of reasons. At present, I am trying to detect how a system audit logs are configured on a given system. I chose VB.Net because it is what I am most comfortable with. I have tried to do this with RSOP, WMI, ADSI, et al. and the LSA function calls are the only thing that will allow for what I am trying to do.

I am new to Marshaling and copying memory. Below is my code. The problem comes out when I call the LsaQueryInformationPolicy, I get a False result, on a system that I know is configured to audit.

Thanks for any help you can provide!

Imports System.Text
Imports System.Runtime.InteropServices

Public Class LsaUtils
    ' Import the LSA functions

    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

    Public Shared Function LsaQueryInformationPolicy(ByRef PolicyHandle As UInt32, _
        ByRef PolicyInformationClass As UIntPtr, ByRef Buffer As Int32) As UInt32
    End Function

    Private Shared Function LsaNtStatusToWinError(ByVal NTStatus As UInt32) As UInt32
    End Function

    Private Shared Function LsaClose(ByVal ObjectHandle As IntPtr) As Long
    End Function

    Private Shared Function GetLastError() As Long
    End Function

    ' Define the structures
    Private Structure LSA_UNICODE_STRING
        Public Length As UInt16
        Public MaximumLength As UInt16
        Public Buffer As IntPtr
    End Structure

    Private Structure LSA_OBJECT_ATTRIBUTES
        Public Length As Int32
        Public RootDirectory As IntPtr
        Public ObjectName As LSA_UNICODE_STRING
        Public Attributes As UInt32
        Public SecurityDescriptor As IntPtr
        Public SecurityQualityOfService As IntPtr
    End Structure

    Private Structure POLICY_AUDIT_EVENTS_INFO
        Public AuditingMode As Boolean
        Public EventAuditingOptions As POLICY_AUDIT_EVENT_TYPE
        Public MaximumAuditEventCount As UInt32
    End Structure

    ' Enum all policies
    Private Enum LSA_AccessPolicy As Long
        POLICY_VIEW_LOCAL_INFORMATION = &H1L
        POLICY_VIEW_AUDIT_INFORMATION = &H2L
        POLICY_GET_PRIVATE_INFORMATION = &H4L
        POLICY_TRUST_ADMIN = &H8L
        POLICY_CREATE_ACCOUNT = &H10L
        POLICY_CREATE_SECRET = &H20L
        POLICY_CREATE_PRIVILEGE = &H40L
        POLICY_SET_DEFAULT_QUOTA_LIMITS = &H80L
        POLICY_SET_AUDIT_REQUIREMENTS = &H100L
        POLICY_AUDIT_LOG_ADMIN = &H200L
        POLICY_SERVER_ADMIN = &H400L
        POLICY_LOOKUP_NAMES = &H800L
        POLICY_NOTIFICATION = &H1000L
    End Enum

    Private Enum POLICY_INFORMATION_CLASS As Long
        PolicyAuditLogInformation = &H1L
        PolicyAuditEventsInformation = &H2L
        PolicyPrimaryDomainInformation = &H4L
        PolicyPdAccountInformation
        PolicyAccountDomainInformation
        PolicyLsaServerRoleInformation
        PolicyReplicaSourceInformation
        PolicyDefaultQuotaInformation
        PolicyModificationInformation
        PolicyAuditFullSetInformation
        PolicyAuditFullQueryInformation
        PolicyDnsDomainInformation
    End Enum

    Private Enum POLICY_AUDIT_EVENT_TYPE As ULong
        AuditCategorySystem
        AuditCategoryLogon
        AuditCategoryObjectAccess
        AuditCategoryPrivilegeUse
        AuditCategoryDetailedTracking
        AuditCategoryPolicyChange
        AuditCategoryAccountManagement
        AuditCategoryDirectoryServiceAccess
        AuditCategoryAccountLogon
    End Enum

    Public Shared Function OpenHandle(ByVal strSystem As String) As Long
        Dim winErrorCode As Long = 0 ' contains the last error

        ' initialize an empty unicode-string
        Dim systemName As LSA_UNICODE_STRING = New LSA_UNICODE_STRING
        systemName.Buffer = Marshal.StringToHGlobalUni(strSystem)

        ' Combine policies required to grant/deny privileges
        Dim access As Int32 = CInt(LSA_AccessPolicy.POLICY_VIEW_AUDIT_INFORMATION)

        ' initialize a pointer for the policy handle
        Dim policyHandle As IntPtr = IntPtr.Zero
        Dim myBuff As IntPtr = IntPtr.Zero

        ' these attributes are not used, but LsaOpenPolicy wants them to exists
        Dim ObjectAttributes As LSA_OBJECT_ATTRIBUTES = New LSA_OBJECT_ATTRIBUTES
        ObjectAttributes.Length = 0
        ObjectAttributes.RootDirectory = IntPtr.Zero
        ObjectAttributes.Attributes = UInt32.Parse("0")
        ObjectAttributes.SecurityDescriptor = IntPtr.Zero
        ObjectAttributes.SecurityQualityOfService = IntPtr.Zero

        ' get a policy handle
        Dim resultPolicy As UInt32 = LsaOpenPolicy(systemName, ObjectAttributes, access, policyHandle)

        'MessageBox.Show(resultPolicy)
        If Not resultPolicy.ToString = "0" Then
            MsgBox("OpenPolicy failed: " & resultPolicy.ToString, "ServiceUtils")
        Else
            resultPolicy = LsaQueryInformationPolicy(policyHandle, POLICY_INFORMATION_CLASS.PolicyAuditEventsInformation, _
                myBuff)

            Debug.Print("Result = " & LsaNtStatusToWinError(resultPolicy))

            If resultPolicy = 0 Then
                Dim polInfo As POLICY_AUDIT_EVENTS_INFO
                polInfo = Marshal.PtrToStructure(myBuff, GetType(POLICY_AUDIT_EVENTS_INFO))

                MsgBox(polInfo.AuditingMode & " " & polInfo.MaximumAuditEventCount)

                Dim audPolicy As POLICY_AUDIT_EVENT_TYPE = Marshal.PtrToStringAuto(polInfo.EventAuditingOptions)
                Dim audRestartShutdown As String = polInfo.EventAuditingOptions
                'Dim audLogonLogoff As String
                'Dim audObjectAccess = Test(4)
                'Dim audUserRights = Test(6)
                'Dim audProcessTrack = Test(8)
                'Dim audPolicyChanges = Test(10)
                'Dim audUserGroupManagement = Test(12)

                'MsgBox(audRestartShutdown.ToString)
                'MsgBox(audLogonLogoff)
                'MsgBox(audObjectAccess)
                'MsgBox(audUserRights)
                'MsgBox(audProcessTrack)
                'MsgBox(audPolicyChanges)
                'MsgBox(audUserGroupManagement)
            Else
                MsgBox("Don't Know")

            End If
        End If


        Dim Help
        Help = LsaClose(policyHandle)
        MessageBox.Show("Help " & Help)

        Return winErrorCode
    End Function
End Class

QuestionPrimary key problem from .csv file ODBC Pin
cstrader2326-Oct-08 2:20
cstrader2326-Oct-08 2:20 
AnswerRe: Primary key problem from .csv file ODBC Pin
Dave Kreskowiak6-Oct-08 6:25
mveDave Kreskowiak6-Oct-08 6:25 
GeneralRe: Primary key problem from .csv file ODBC Pin
cstrader2327-Oct-08 1:49
cstrader2327-Oct-08 1:49 
QuestionProject help Pin
sweetlover7546-Oct-08 0:13
sweetlover7546-Oct-08 0:13 
AnswerRe: Project help Pin
Ashfield6-Oct-08 0:50
Ashfield6-Oct-08 0:50 
GeneralRe: Project help Pin
sweetlover7546-Oct-08 4:43
sweetlover7546-Oct-08 4:43 
GeneralRe: Project help Pin
Dave Kreskowiak6-Oct-08 6:18
mveDave Kreskowiak6-Oct-08 6:18 
GeneralRe: Project help Pin
Paul Conrad6-Oct-08 7:31
professionalPaul Conrad6-Oct-08 7:31 
GeneralRe: Project help Pin
Ashfield6-Oct-08 19:54
Ashfield6-Oct-08 19:54 
AnswerRe: Project help Pin
leckey6-Oct-08 13:20
leckey6-Oct-08 13:20 
QuestionHow to use single MDI Child Form for all Crystal reports Pin
kedarrkulkarni5-Oct-08 22:40
kedarrkulkarni5-Oct-08 22:40 
AnswerRe: How to use single MDI Child Form for all Crystal reports Pin
Mycroft Holmes5-Oct-08 22:55
professionalMycroft Holmes5-Oct-08 22:55 
Questionincluding line/scatter chart in VB.net Pin
Amanjot5-Oct-08 13:42
Amanjot5-Oct-08 13:42 
AnswerRe: including line/scatter chart in VB.net Pin
Rupesh Kumar Swami5-Oct-08 20:41
Rupesh Kumar Swami5-Oct-08 20:41 
AnswerRe: including line/scatter chart in VB.net Pin
Thomas Stockwell6-Oct-08 6:14
professionalThomas Stockwell6-Oct-08 6:14 
AnswerRe: including line/scatter chart in VB.net Pin
Dave Kreskowiak6-Oct-08 6:20
mveDave Kreskowiak6-Oct-08 6:20 
GeneralRe: including line/scatter chart in VB.net Pin
Amanjot6-Oct-08 16:29
Amanjot6-Oct-08 16:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.