首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从AuditQuerySystemPolicy调用C# () (advapi32.dll)返回“参数不正确”

从AuditQuerySystemPolicy调用C# () (advapi32.dll)返回“参数不正确”
EN

Stack Overflow用户
提问于 2010-06-10 17:17:44
回答 1查看 1.7K关注 0票数 3

顺序如下:

使用categories;

  • For (未示出)

  • Call LsaQueryInformationPolicy()打开策略句柄以获取每个类别的GUID数:将枚举值转换为GUID的
  • Call AuditLookupCategoryGuidFromCategoryId()H 211H 112调用AuditEnumerateSubCategories()以获得所有subcategories;
  • Call AuditQuerySystemPolicy()的GUID列表,以获取subcategories.

的审计策略。

所有这些工作和返回预期,合理的价值,除了最后。调用AuditQuerySystemPolicy()会得到一个“参数不正确”的错误。我想一定有一些微妙的解封问题。我可能误解了AuditEnumerateSubCategories()到底返回了什么,但我很困惑。

您将看到(注释),我尝试将返回指针从AuditEnumerateSubCategories()取消为指针。做或不做都会产生同样的结果。

代码:

代码语言:javascript
复制
#region LSA types
public enum POLICY_INFORMATION_CLASS
{
    PolicyAuditLogInformation = 1,
    PolicyAuditEventsInformation,
    PolicyPrimaryDomainInformation,
    PolicyPdAccountInformation,
    PolicyAccountDomainInformation,
    PolicyLsaServerRoleInformation,
    PolicyReplicaSourceInformation,
    PolicyDefaultQuotaInformation,
    PolicyModificationInformation,
    PolicyAuditFullSetInformation,
    PolicyAuditFullQueryInformation,
    PolicyDnsDomainInformation
}

public enum POLICY_AUDIT_EVENT_TYPE
{
    AuditCategorySystem,
    AuditCategoryLogon,
    AuditCategoryObjectAccess,
    AuditCategoryPrivilegeUse,
    AuditCategoryDetailedTracking,
    AuditCategoryPolicyChange,
    AuditCategoryAccountManagement,
    AuditCategoryDirectoryServiceAccess,
    AuditCategoryAccountLogon 
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct POLICY_AUDIT_EVENTS_INFO
{
    public bool AuditingMode;
    public IntPtr EventAuditingOptions;
    public UInt32 MaximumAuditEventCount;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct GUID
{
    public UInt32 Data1;
    public UInt16 Data2;
    public UInt16 Data3;
    public Byte Data4a;
    public Byte Data4b;
    public Byte Data4c;
    public Byte Data4d;
    public Byte Data4e;
    public Byte Data4f;
    public Byte Data4g;
    public Byte Data4h;

    public override string ToString()
    {
        return Data1.ToString("x8") + "-" + Data2.ToString("x4") + "-" + Data3.ToString("x4") + "-"
              + Data4a.ToString("x2") + Data4b.ToString("x2") + "-"
              + Data4c.ToString("x2") + Data4d.ToString("x2") + Data4e.ToString("x2") + Data4f.ToString("x2") + Data4g.ToString("x2") + Data4h.ToString("x2");
    }
}
#endregion

#region LSA Imports
[DllImport("kernel32.dll")]
extern static int GetLastError();

[DllImport("advapi32.dll", CharSet = CharSet.Unicode, PreserveSig = true)]
public static extern UInt32 LsaNtStatusToWinError(
    long Status);

[DllImport("advapi32.dll", CharSet = CharSet.Unicode, PreserveSig = true)]
public static extern long LsaOpenPolicy(
    ref LSA_UNICODE_STRING SystemName,
    ref LSA_OBJECT_ATTRIBUTES ObjectAttributes,
    Int32 DesiredAccess,
    out IntPtr PolicyHandle );

[DllImport("advapi32.dll", CharSet = CharSet.Unicode, PreserveSig = true)]
public static extern long LsaClose(IntPtr PolicyHandle);

[DllImport("advapi32.dll", CharSet = CharSet.Unicode, PreserveSig = true)]
public static extern long LsaFreeMemory(IntPtr Buffer);

[DllImport("advapi32.dll", CharSet = CharSet.Unicode, PreserveSig = true)]
public static extern void AuditFree(IntPtr Buffer);

[DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
public static extern long LsaQueryInformationPolicy(
    IntPtr PolicyHandle, POLICY_INFORMATION_CLASS InformationClass,
    out IntPtr Buffer);

[DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
public static extern bool AuditLookupCategoryGuidFromCategoryId(
    POLICY_AUDIT_EVENT_TYPE AuditCategoryId,
    IntPtr pAuditCategoryGuid);

[DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
public static extern bool AuditEnumerateSubCategories(
    IntPtr pAuditCategoryGuid,
    bool bRetrieveAllSubCategories,
    out IntPtr ppAuditSubCategoriesArray,
    out ulong pCountReturned);

[DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
public static extern bool AuditQuerySystemPolicy(
    IntPtr pSubCategoryGuids,
    ulong PolicyCount,
    out IntPtr ppAuditPolicy);
#endregion

Dictionary<string, UInt32> retList = new Dictionary<string, UInt32>();
long lretVal;
uint retVal;

IntPtr pAuditEventsInfo;
lretVal = LsaQueryInformationPolicy(policyHandle, POLICY_INFORMATION_CLASS.PolicyAuditEventsInformation, out pAuditEventsInfo);
retVal = LsaNtStatusToWinError(lretVal);
if (retVal != 0)
{
    LsaClose(policyHandle);
    throw new System.ComponentModel.Win32Exception((int)retVal);
}

POLICY_AUDIT_EVENTS_INFO myAuditEventsInfo = new POLICY_AUDIT_EVENTS_INFO();
myAuditEventsInfo = (POLICY_AUDIT_EVENTS_INFO)Marshal.PtrToStructure(pAuditEventsInfo, myAuditEventsInfo.GetType());

IntPtr subCats = IntPtr.Zero;
ulong nSubCats = 0;

for (int audCat = 0; audCat < myAuditEventsInfo.MaximumAuditEventCount; audCat++)
{
    GUID audCatGuid = new GUID();
    if (!AuditLookupCategoryGuidFromCategoryId((POLICY_AUDIT_EVENT_TYPE)audCat, new IntPtr(&audCatGuid)))
    {
        int causingError = GetLastError();
        LsaFreeMemory(pAuditEventsInfo);
        LsaClose(policyHandle);
        throw new System.ComponentModel.Win32Exception(causingError);
    }

    if (!AuditEnumerateSubCategories(new IntPtr(&audCatGuid), true, out subCats, out nSubCats))
    {
        int causingError = GetLastError();
        LsaFreeMemory(pAuditEventsInfo);
        LsaClose(policyHandle);
        throw new System.ComponentModel.Win32Exception(causingError);
    }

    // Dereference the first pointer-to-pointer to point to the first subcategory
    // subCats = (IntPtr)Marshal.PtrToStructure(subCats, subCats.GetType());

    if (nSubCats > 0)
    {
        IntPtr audPolicies = IntPtr.Zero;
        if (!AuditQuerySystemPolicy(subCats, nSubCats, out audPolicies))
        {
            int causingError = GetLastError();
            if (subCats != IntPtr.Zero)
                AuditFree(subCats);
            LsaFreeMemory(pAuditEventsInfo);
            LsaClose(policyHandle);
            throw new System.ComponentModel.Win32Exception(causingError);
        }

        AUDIT_POLICY_INFORMATION myAudPol = new AUDIT_POLICY_INFORMATION();
        for (ulong audSubCat = 0; audSubCat < nSubCats; audSubCat++)
        {
            // Process audPolicies[audSubCat], turn GUIDs into names, fill retList.
            // http://msdn.microsoft.com/en-us/library/aa373931%28VS.85%29.aspx
            // http://msdn.microsoft.com/en-us/library/bb648638%28VS.85%29.aspx

            IntPtr itemAddr = IntPtr.Zero;
            IntPtr itemAddrAddr = new IntPtr(audPolicies.ToInt64() + (long)(audSubCat * (ulong)Marshal.SizeOf(itemAddr)));
            itemAddr = (IntPtr)Marshal.PtrToStructure(itemAddrAddr, itemAddr.GetType());
            myAudPol = (AUDIT_POLICY_INFORMATION)Marshal.PtrToStructure(itemAddr, myAudPol.GetType());
            retList[myAudPol.AuditSubCategoryGuid.ToString()] = myAudPol.AuditingInformation;
        }

        if (audPolicies != IntPtr.Zero)
            AuditFree(audPolicies);
    }

    if (subCats != IntPtr.Zero)
        AuditFree(subCats);

    subCats = IntPtr.Zero;
    nSubCats = 0;
}

lretVal = LsaFreeMemory(pAuditEventsInfo);
retVal = LsaNtStatusToWinError(lretVal);
if (retVal != 0)
    throw new System.ComponentModel.Win32Exception((int)retVal);

lretVal = LsaClose(policyHandle);
retVal = LsaNtStatusToWinError(lretVal);
if (retVal != 0)
    throw new System.ComponentModel.Win32Exception((int)retVal);
EN

回答 1

Stack Overflow用户

发布于 2010-06-11 23:02:58

首先你发布的不是完整的代码,所以我不能编译它。没有使用policyHandle函数打开LsaOpenPolicy的代码。AUDIT_POLICY_INFORMATIONLSA_OBJECT_ATTRIBUTESLSA_UNICODE_STRING等一些结构的声明也不存在。

不过,我在您的代码中至少发现了一个错误。使用AuditLookupCategoryGuidFromCategoryId的最后一个参数似乎是错误的。函数AuditLookupCategoryGuidFromCategoryId有原型

代码语言:javascript
复制
BOOLEAN WINAPI AuditLookupCategoryGuidFromCategoryId(
  __in   POLICY_AUDIT_EVENT_TYPE AuditCategoryId,
  __out  GUID *pAuditCategoryGuid
);

这意味着,您必须分配非托管内存来保存GUID并获得指向AuditLookupCategoryGuidFromCategoryId的指针。内存将由AuditLookupCategoryGuidFromCategoryId填充。所以而不是

代码语言:javascript
复制
GUID audCatGuid = new GUID();
if (!AuditLookupCategoryGuidFromCategoryId((POLICY_AUDIT_EVENT_TYPE)audCat,
                                           new IntPtr(&audCatGuid)))
// ...
if (!AuditEnumerateSubCategories(new IntPtr(&audCatGuid), true, out subCats,
                                 out nSubCats))
// ...

似乎我纠正了以下几点

代码语言:javascript
复制
IntPtr pAuditCatGuid = Marshal.AllocHGlobal (Marshal.SizeOf(GUID));
if (!AuditLookupCategoryGuidFromCategoryId((POLICY_AUDIT_EVENT_TYPE)audCat,
                                           pAuditCatGuid))
// ...
if (!AuditEnumerateSubCategories(pAuditCatGuid, true, out subCats,
                                 out nSubCats))
// ...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3016778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档