Home >Backend Development >C++ >How Can I Accurately Determine if a Process Has Elevated Privileges in Windows?
Beyond Admin Role: Determine Privilege Elevation Status
The code originally used to detect administrator status was limited in its ability to identify privilege escalation because it did not take into account situations such as running as administrator but not elevating privileges. This makes it easy for administrators to perform sensitive operations without appropriate authorization.
To resolve this issue, a more comprehensive approach to determining admin status and actual elevation level is needed. The following code example provides a solution:
<code class="language-csharp">using Microsoft.Win32; using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Security.Principal; public static class UacHelper { // 用于确定UAC状态的UAC注册表项和值 private const string uacRegistryKey = "Software\Microsoft\Windows\CurrentVersion\Policies\System"; private const string uacRegistryValue = "EnableLUA"; // 令牌访问和查询常量 private static uint STANDARD_RIGHTS_READ = 0x00020000; private static uint TOKEN_QUERY = 0x0008; private static uint TOKEN_READ = (STANDARD_RIGHTS_READ | TOKEN_QUERY); // 用于打开具有所需访问权限的进程令牌的函数 [DllImport("advapi32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool OpenProcessToken(IntPtr ProcessHandle, UInt32 DesiredAccess, out IntPtr TokenHandle); // 用于获取令牌信息(例如提升级别)的函数 [DllImport("advapi32.dll", SetLastError = true)] public static extern bool GetTokenInformation(IntPtr TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass, IntPtr TokenInformation, uint TokenInformationLength, out uint ReturnLength); // 用于检索提升类型的令牌信息类 public enum TOKEN_INFORMATION_CLASS { TokenElevationType = 9 } // 可能的提升级别 public enum TOKEN_ELEVATION_TYPE { TokenElevationTypeDefault = 1, TokenElevationTypeFull, TokenElevationTypeLimited } // 检查UAC是否启用 public static bool IsUacEnabled { get { using (var uacKey = Registry.LocalMachine.OpenSubKey(uacRegistryKey, false)) { return uacKey.GetValue(uacRegistryValue).Equals(1); } } } // 检查当前进程是否已提升 public static bool IsProcessElevated { get { if (IsUacEnabled) { // 当前进程令牌的句柄 IntPtr tokenHandle; // 尝试以读取访问权限打开进程令牌 if (!OpenProcessToken(Process.GetCurrentProcess().Handle, TOKEN_READ, out tokenHandle)) { throw new ApplicationException("无法获取进程令牌。Win32错误代码:" + Marshal.GetLastWin32Error()); } // 分配内存以存储提升信息 int elevationResultSize = Marshal.SizeOf((int)TOKEN_ELEVATION_TYPE.TokenElevationTypeDefault); IntPtr elevationTypePtr = Marshal.AllocHGlobal(elevationResultSize); // 从令牌中检索提升类型 uint returnedSize; bool success = GetTokenInformation(tokenHandle, TOKEN_INFORMATION_CLASS.TokenElevationType, elevationTypePtr, (uint)elevationResultSize, out returnedSize); // 检查操作是否成功 if (success) { // 从已分配的内存中读取提升类型 var elevationResult = (TOKEN_ELEVATION_TYPE)Marshal.ReadInt32(elevationTypePtr); // 判断进程是否具有完全提升权限 return elevationResult == TOKEN_ELEVATION_TYPE.TokenElevationTypeFull; } else { throw new ApplicationException("无法确定当前提升级别。"); } } else { // UAC未启用,依靠WindowsPrincipal检查管理员角色 WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); return principal.IsInRole(WindowsBuiltInRole.Administrator); } } } }</code>
Usage:
To use this code, just call the IsProcessElevated
attribute:
<code class="language-csharp">bool isElevated = UacHelper.IsProcessElevated;</code>
Returns true
if the process was elevated with full privileges, false
otherwise. Additionally, you can check if UAC is enabled on your system using the IsUacEnabled
attribute.
The above is the detailed content of How Can I Accurately Determine if a Process Has Elevated Privileges in Windows?. For more information, please follow other related articles on the PHP Chinese website!