From 4abb473f0be3ebb3d3a356f6f9e36ca61589bf3f Mon Sep 17 00:00:00 2001 From: wj32 Date: Fri, 18 Sep 2009 07:43:43 +0000 Subject: [PATCH] username and privilege lookups are now done through the internal LSA API git-svn-id: svn://svn.code.sf.net/p/processhacker/code@1894 21ef857c-d57f-4fe0-8362-d861dc6d29cd --- .../Objects/LsaPolicyHandle.cs | 35 ++++++++++++++ .../Security/Privilege.cs | 32 ++----------- trunk/ProcessHacker.Native/Security/Sid.cs | 46 ++++++++----------- trunk/ProcessHacker/Program/Program.cs | 4 ++ 4 files changed, 61 insertions(+), 56 deletions(-) diff --git a/trunk/ProcessHacker.Native/Objects/LsaPolicyHandle.cs b/trunk/ProcessHacker.Native/Objects/LsaPolicyHandle.cs index b8a15d035..3450c0c52 100644 --- a/trunk/ProcessHacker.Native/Objects/LsaPolicyHandle.cs +++ b/trunk/ProcessHacker.Native/Objects/LsaPolicyHandle.cs @@ -22,6 +22,7 @@ using System; using System.Collections.Generic; +using ProcessHacker.Common; using ProcessHacker.Native.Api; using ProcessHacker.Native.Security; @@ -32,6 +33,40 @@ namespace ProcessHacker.Native.Objects /// public sealed class LsaPolicyHandle : LsaHandle { + private static WeakReference _lookupPolicyHandle; + private static int _lookupPolicyHandleMisses = 0; + + public static LsaPolicyHandle LookupPolicyHandle + { + get + { + WeakReference weakRef = _lookupPolicyHandle; + LsaPolicyHandle policyHandle = null; + + if (weakRef != null) + { + policyHandle = weakRef.Target; + } + + if (policyHandle == null) + { + System.Threading.Interlocked.Increment(ref _lookupPolicyHandleMisses); + + policyHandle = new LsaPolicyHandle(LsaPolicyAccess.LookupNames); + + if (policyHandle != null) + _lookupPolicyHandle = new WeakReference(policyHandle); + } + + return policyHandle; + } + } + + public static int LookupPolicyHandleMisses + { + get { return _lookupPolicyHandleMisses; } + } + public delegate bool EnumAccountsDelegate(Sid sid); public delegate bool EnumPrivilegesDelegate(Privilege privilege); diff --git a/trunk/ProcessHacker.Native/Security/Privilege.cs b/trunk/ProcessHacker.Native/Security/Privilege.cs index c90a0bf81..3b15299b3 100644 --- a/trunk/ProcessHacker.Native/Security/Privilege.cs +++ b/trunk/ProcessHacker.Native/Security/Privilege.cs @@ -97,10 +97,9 @@ namespace ProcessHacker.Native.Security if (!hasLuid) { if (_name == null) - throw new Exception("You must specify either a LUID or a name."); + throw new ArgumentException("You must specify either a LUID or a name."); - if (!Win32.LookupPrivilegeValue(null, _name, out _luid)) - throw new Exception("Invalid privilege name '" + _name + "'."); + _luid = LsaPolicyHandle.LookupPolicyHandle.LookupPrivilegeValue(_name); } else { @@ -138,19 +137,7 @@ namespace ProcessHacker.Native.Security { if (_displayName == null) { - StringBuilder sb = new StringBuilder(0x100); - int size = sb.Capacity; - int languageId = 0; - - if (!Win32.LookupPrivilegeDisplayName(null, this.Name, sb, ref size, out languageId)) - { - sb.EnsureCapacity(size); - - if (!Win32.LookupPrivilegeDisplayName(null, this.Name, sb, ref size, out languageId)) - Win32.ThrowLastError(); - } - - _displayName = sb.ToString(); + _displayName = LsaPolicyHandle.LookupPolicyHandle.LookupPrivilegeDisplayName(this.Name); } return _displayName; @@ -194,18 +181,7 @@ namespace ProcessHacker.Native.Security { if (_name == null) { - StringBuilder sb = new StringBuilder(0x100); - int size = sb.Capacity; - - if (!Win32.LookupPrivilegeName(null, ref _luid, sb, ref size)) - { - sb.EnsureCapacity(size); - - if (!Win32.LookupPrivilegeName(null, ref _luid, sb, ref size)) - Win32.ThrowLastError(); - } - - _name = sb.ToString(); + _name = LsaPolicyHandle.LookupPolicyHandle.LookupPrivilegeName(_luid); } return _name; diff --git a/trunk/ProcessHacker.Native/Security/Sid.cs b/trunk/ProcessHacker.Native/Security/Sid.cs index df5f8685d..be92cad5a 100644 --- a/trunk/ProcessHacker.Native/Security/Sid.cs +++ b/trunk/ProcessHacker.Native/Security/Sid.cs @@ -25,6 +25,7 @@ using System.Text; using ProcessHacker.Common; using ProcessHacker.Common.Objects; using ProcessHacker.Native.Api; +using ProcessHacker.Native.Objects; namespace ProcessHacker.Native.Security { @@ -41,21 +42,9 @@ namespace ProcessHacker.Native.Security private static readonly byte[] _ntAuthority = { 0, 0, 0, 0, 0, 5 }; private static readonly byte[] _resourceManagerAuthority = { 0, 0, 0, 0, 0, 9 }; - public static Sid FromAccountName(string accountName) + public static Sid FromName(string name) { - using (MemoryAlloc memory = new MemoryAlloc(Win32.SecurityMaxSidSize)) - { - int memorySize = memory.Size; - StringBuilder domainSb = new StringBuilder(0x400); - int domainSbSize = domainSb.Capacity; - SidNameUse nameUse; - - if (!Win32.LookupAccountName(null, accountName, memory, ref memorySize, - domainSb, ref domainSbSize, out nameUse)) - Win32.ThrowLastError(); - - return new Sid(memory); - } + return LsaPolicyHandle.LookupPolicyHandle.LookupSid(name); } public static Sid FromPointer(IntPtr sid) @@ -356,25 +345,26 @@ namespace ProcessHacker.Native.Security } } - private void GetNameAndUse(out string domain, out string name, out SidNameUse nameUse) + public override int GetHashCode() { - StringBuilder nameSb = new StringBuilder(256); - StringBuilder domainSb = new StringBuilder(256); - int nameLen = 256; - int domainLen = 256; + int hashCode = 0x12345678; + byte[] identifierAuthority = this.IdentifierAuthority; + int[] subAuthorities = this.SubAuthorities; - if (!Win32.LookupAccountSid(_systemName, this, nameSb, ref nameLen, domainSb, ref domainLen, out nameUse)) + for (int i = 0; i < subAuthorities.Length; i++) { - // if the name is longer than 256 characters, increase the capacity. - nameSb.EnsureCapacity(nameLen); - domainSb.EnsureCapacity(domainLen); - - if (!Win32.LookupAccountSid(_systemName, this, nameSb, ref nameLen, domainSb, ref domainLen, out nameUse)) - Win32.ThrowLastError(); + hashCode ^= identifierAuthority[(uint)hashCode % identifierAuthority.Length]; + // Reverse and XOR. + hashCode ^= (hashCode >> 24) | ((hashCode >> 16) << 8) | ((hashCode >> 24) << 16) | (hashCode << 24); + hashCode ^= subAuthorities[(uint)hashCode % subAuthorities.Length]; } - domain = domainSb.ToString(); - name = nameSb.ToString(); + return hashCode; + } + + private void GetNameAndUse(out string domain, out string name, out SidNameUse nameUse) + { + name = LsaPolicyHandle.LookupPolicyHandle.LookupName(this, out nameUse, out domain); } public WellKnownSidIdentifierAuthority GetWellKnownIdentifierAuthority() diff --git a/trunk/ProcessHacker/Program/Program.cs b/trunk/ProcessHacker/Program/Program.cs index d5a0705c8..c43855576 100644 --- a/trunk/ProcessHacker/Program/Program.cs +++ b/trunk/ProcessHacker/Program/Program.cs @@ -827,6 +827,10 @@ namespace ProcessHacker else info.AppendLine("KProcessHacker: " + KProcessHacker.Instance.Features.ToString()); + info.AppendLine(); + info.AppendLine("PERFORMANCE COUNTERS"); + info.AppendLine("LSA lookup policy handle misses: " + LsaPolicyHandle.LookupPolicyHandleMisses.ToString()); + info.AppendLine(); info.AppendLine("OBJECTS"); info.AppendLine("Created: " + BaseObject.CreatedCount.ToString());