private static AdjustPrivilegeResult AdjustPrivilege( AccessTokenHandle accessTokenHandle, Luid luid, PrivilegeAttributes privilegeAttributes) { TokenPrivilege newState = new TokenPrivilege { PrivilegeCount = 1, Privilege = new LuidAndAttributes { Attributes = privilegeAttributes, Luid = luid } }; TokenPrivilege previousState = new TokenPrivilege(); int returnLength = 0; if (!NativeMethods.AdjustTokenPrivileges( accessTokenHandle, false, ref newState, Marshal.SizeOf(previousState), ref previousState, ref returnLength)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return (AdjustPrivilegeResult)previousState.PrivilegeCount; }
internal static PrivilegeAndAttributesCollection GetPrivileges(AccessTokenHandle accessTokenHandle) { LuidAndAttributes[] luidAndAttributesArray = GetTokenPrivileges(accessTokenHandle); int length = luidAndAttributesArray.Length; List<PrivilegeAndAttributes> privilegeAndAttributes = new List<PrivilegeAndAttributes>(length); for (int i = 0; i < length; i++) { LuidAndAttributes luidAndAttributes = luidAndAttributesArray[i]; string name = GetPrivilegeName(luidAndAttributes.Luid); if (PrivilegesDictionary.ContainsKey(name)) { privilegeAndAttributes.Add(new PrivilegeAndAttributes( PrivilegesDictionary[name], luidAndAttributes.Attributes)); } } return new PrivilegeAndAttributesCollection(privilegeAndAttributes); }
public PrivilegeEnabler(Process process) { lock (AccessTokenHandles) { if (AccessTokenHandles.ContainsKey(process)) { _accessTokenHandle = AccessTokenHandles[process]; } else { _accessTokenHandle = process.GetAccessTokenHandle(TokenAccessRights.AdjustPrivileges | TokenAccessRights.Query); AccessTokenHandles.Add(process, _accessTokenHandle); _ownsHandle = true; } } _process = process; }
internal static AdjustPrivilegeResult EnablePrivilege(AccessTokenHandle accessTokenHandle, Privilege privilege) { return AdjustPrivilege(accessTokenHandle, privilege, PrivilegeAttributes.Enabled); }
private static LuidAndAttributes[] GetTokenPrivileges(AccessTokenHandle accessTokenHandle) { int tokenInformationLength = 0; int returnLength = 0; if (NativeMethods.GetTokenInformation( accessTokenHandle, TokenInformationClass.TokenPrivileges, IntPtr.Zero, tokenInformationLength, ref returnLength)) { return new LuidAndAttributes[0]; } int lastWin32Error = Marshal.GetLastWin32Error(); if (lastWin32Error != NativeMethods.ErrorInsufficientBuffer) { throw new Win32Exception(lastWin32Error); } tokenInformationLength = returnLength; returnLength = 0; using (AllocatedMemory allocatedMemory = new AllocatedMemory(tokenInformationLength)) { if (!NativeMethods.GetTokenInformation( accessTokenHandle, TokenInformationClass.TokenPrivileges, allocatedMemory.Pointer, tokenInformationLength, ref returnLength)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } int privilegeCount = Marshal.ReadInt32(allocatedMemory.Pointer); LuidAndAttributes[] luidAndAttributes = new LuidAndAttributes[privilegeCount]; long pointer = allocatedMemory.Pointer.ToInt64() + Marshal.SizeOf(privilegeCount); Type type = typeof(LuidAndAttributes); long size = Marshal.SizeOf(type); for (int i = 0; i < privilegeCount; i++) { luidAndAttributes[i] = (LuidAndAttributes)Marshal.PtrToStructure(new IntPtr(pointer), type); pointer += size; } return luidAndAttributes; } }
private static AdjustPrivilegeResult AdjustPrivilege( AccessTokenHandle accessTokenHandle, Privilege privilege, PrivilegeAttributes privilegeAttributes) { return AdjustPrivilege(accessTokenHandle, GetLuid(privilege), privilegeAttributes); }
private void InternalDispose() { if (!_disposed) { lock (SharedPrivileges) { var privileges = SharedPrivileges .Where(keyValuePair => keyValuePair.Value == this) .Select(keyValuePair => keyValuePair.Key) .ToArray(); foreach (var privilege in privileges) { _accessTokenHandle.DisablePrivilege(privilege); SharedPrivileges.Remove(privilege); } if (_ownsHandle) { _accessTokenHandle.Dispose(); lock (_accessTokenHandle) { AccessTokenHandles.Remove(_process); } } _accessTokenHandle = null; _ownsHandle = false; _process = null; _disposed = true; } } }
public static PrivilegeAttributes GetPrivilegeAttributes(this AccessTokenHandle accessTokenHandle, Privilege privilege) { return(Privileges.GetPrivilegeAttributes(privilege, GetPrivileges(accessTokenHandle))); }
public static AdjustPrivilegeResult EnablePrivilege(this AccessTokenHandle accessTokenHandle, Privilege privilege) { return(Privileges.EnablePrivilege(accessTokenHandle, privilege)); }