在.NET 4.好的或坏的



文件中存在SECURITY_IDENTIFIER结构类型的对象。我需要从这个结构中获取所有者SID。为了做到这一点,我调用GetSecurityDescriptorOwner WinAPI函数并创建System.Security.Principal.SecurityIdentifier(它有以IntPtr为参数的重载)

问题是文件中的这个结构有时会被破坏,所以我从GetSecurityDescriptorOwner获得的指针无效。它不是IntPtr.Zero,它是无效的,所以当我创建类型为SecurityIdentifier的对象时,我会得到AccessViolationException,这是不可能用.NET 4通过简单的try-catch捕获的。

我知道允许捕获此类异常的属性,所以我暂时使用了它,但我不喜欢这个解决方案。不建议捕获损坏状态异常(CSE),但我看不到任何其他解决方案。这个WinAPI函数返回了无效的指针,我看不出有什么方法可以检查它的有效性。有什么想法吗?

更新

WinAPI

BOOL WINAPI GetSecurityDescriptorOwner(
  _In_   PSECURITY_DESCRIPTOR pSecurityDescriptor,
  _Out_  PSID *pOwner,
  _Out_  LPBOOL lpbOwnerDefaulted
);

外部定义

[DllImport("Advapi32.dll")]
static extern bool GetSecurityDescriptorOwner(
   IntPtr pSecurityDescriptor,
   out IntPtr owner,
   out bool defaulted);

更新

private static SecurityIdentifier GetSecurityIdentifier()
{
    // Allocate managed buffer for invalid security descriptor structure (20 bytes)
    int[] b = new int[5] {1, 1, 1, 1, 1};
    // Allocate unmanaged memory for security descriptor 
    IntPtr descriptorPtr = Marshal.AllocHGlobal(b.Length);
    // Copy invalid security descriptor structure to the unmanaged buffer
    Marshal.Copy(b, 0, descriptorPtr, b.Length);
    IntPtr ownerSid;
    bool defaulted;
    if (GetSecurityDescriptorGroup(descriptorPtr, out ownerSid, out defaulted))
    {
        // GetSecurityDescriptorGroup returns true, but `ownerSid` is `1`
        // Marshal.GetLastWin32Error returns 0 here
        return new SecurityIdentifier(ownerSid);
    }
    return null;
}

此代码有时会从SecurityIdentifier构造函数抛出CorruptedStateException。有什么解决方案吗?

您试过调用IsValidSecurityDescriptor吗?

[DllImport("Advapi32.dll")]
static extern bool IsValidSecurityDescriptor(IntPtr pSecurityDescriptor);

if (IsValidSecurityDescriptor(descriptorPtr) && 
    GetSecurityDescriptorOwner(descriptorPtr, out ownerSid, out defaulted))
{
     return new SecurityIdentifier(ownerSid);
}

更新:

试试这个:

    [DllImport("Advapi32.dll")]
    static extern bool GetSecurityDescriptorOwner(
       [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U4)]
       Int32[] securityDescriptor,
       [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U1)] 
       out Byte[] owner,
       out Boolean defaulted);

    [DllImport("Advapi32.dll")]
    static extern bool IsValidSecurityDescriptor(
        [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U4)] 
        Int32[] securityDescriptor);
    private static SecurityIdentifier GetSecurityIdentifier()
    {
        // Allocate managed buffer for invalid security descriptor structure (20 bytes)
        Int32[] b = new[] { 1, 1, 1, 1, 1 };
        Byte[] ownerSid;
        bool defaulted;
        if (IsValidSecurityDescriptor(b) &&
            GetSecurityDescriptorOwner(b, out ownerSid, out defaulted))
        {
            return new SecurityIdentifier(ownerSid, 0);
        }
        return null;
    }
    static void Main()
    {
        for (Int32 index = 0; index < 1000; index++)
        {
            SecurityIdentifier identifier = GetSecurityIdentifier();
            String text = identifier == null ? "(none)" : identifier.Value;
            Console.WriteLine(text);
        }
        Console.ReadKey();
    }

相关内容

  • 没有找到相关文章

最新更新