VoidsShadow

CS_UACHelper

Jun 12th, 2021
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. [Edit for https://stackoverflow.com/a/4497572/14894786]
  2. [Summary: Jam in the changes from Revision 3 of https://stackoverflow.com/a/17492949 e.g. disposal of instanced resources, support for Domain admins, suggested SizeOf fix]
  3.  
  4. Try this out:
  5.  
  6. ```CS
  7. using Microsoft.Win32;
  8. using System;
  9. using System.Diagnostics;
  10. using System.Runtime.InteropServices;
  11. using System.Security.Principal;
  12.  
  13. public static class UacHelper
  14. {
  15. private const string uacRegistryKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
  16. private const string uacRegistryValue = "EnableLUA";
  17.  
  18. private static uint STANDARD_RIGHTS_READ = 0x00020000;
  19. private static uint TOKEN_QUERY = 0x0008;
  20. private static uint TOKEN_READ = (STANDARD_RIGHTS_READ | TOKEN_QUERY);
  21.  
  22. [DllImport("advapi32.dll", SetLastError = true)]
  23. [return: MarshalAs(UnmanagedType.Bool)]
  24. static extern bool OpenProcessToken(IntPtr ProcessHandle, UInt32 DesiredAccess, out IntPtr TokenHandle);
  25.  
  26. [DllImport("kernel32.dll", SetLastError = true)]
  27. [return: MarshalAs(UnmanagedType.Bool)]
  28. static extern bool CloseHandle(IntPtr hObject);
  29.  
  30. [DllImport("advapi32.dll", SetLastError = true)]
  31. public static extern bool GetTokenInformation(IntPtr TokenHandle,
  32. TOKEN_INFORMATION_CLASS TokenInformationClass,
  33. IntPtr TokenInformation,
  34. uint TokenInformationLength,
  35. out uint ReturnLength);
  36.  
  37. public enum TOKEN_INFORMATION_CLASS
  38. {
  39. TokenUser = 1,
  40. TokenGroups,
  41. TokenPrivileges,
  42. TokenOwner,
  43. TokenPrimaryGroup,
  44. TokenDefaultDacl,
  45. TokenSource,
  46. TokenType,
  47. TokenImpersonationLevel,
  48. TokenStatistics,
  49. TokenRestrictedSids,
  50. TokenSessionId,
  51. TokenGroupsAndPrivileges,
  52. TokenSessionReference,
  53. TokenSandBoxInert,
  54. TokenAuditPolicy,
  55. TokenOrigin,
  56. TokenElevationType,
  57. TokenLinkedToken,
  58. TokenElevation,
  59. TokenHasRestrictions,
  60. TokenAccessInformation,
  61. TokenVirtualizationAllowed,
  62. TokenVirtualizationEnabled,
  63. TokenIntegrityLevel,
  64. TokenUIAccess,
  65. TokenMandatoryPolicy,
  66. TokenLogonSid,
  67. MaxTokenInfoClass
  68. }
  69.  
  70. public enum TOKEN_ELEVATION_TYPE
  71. {
  72. TokenElevationTypeDefault = 1,
  73. TokenElevationTypeFull,
  74. TokenElevationTypeLimited
  75. }
  76.  
  77. public static bool IsUacEnabled
  78. {
  79. get
  80. {
  81. using (RegistryKey uacKey = Registry.LocalMachine.OpenSubKey(uacRegistryKey, false))
  82. {
  83. bool result = uacKey.GetValue(uacRegistryValue).Equals(1);
  84. return result;
  85. }
  86. }
  87. }
  88.  
  89. public static bool IsProcessElevated
  90. {
  91. get
  92. {
  93. if (IsUacEnabled)
  94. {
  95. IntPtr tokenHandle = IntPtr.Zero;
  96. if (!OpenProcessToken(Process.GetCurrentProcess().Handle, TOKEN_READ, out tokenHandle))
  97. {
  98. throw new ApplicationException("Could not get process token. Win32 Error Code: " +
  99. Marshal.GetLastWin32Error());
  100. }
  101.  
  102. try
  103. {
  104. TOKEN_ELEVATION_TYPE elevationResult = TOKEN_ELEVATION_TYPE.TokenElevationTypeDefault;
  105.  
  106. int elevationResultSize = Marshal.SizeOf(typeof((int)TOKEN_ELEVATION_TYPE));
  107. uint returnedSize = 0;
  108. IntPtr elevationTypePtr = Marshal.AllocHGlobal(elevationResultSize);
  109.  
  110. try
  111. {
  112. bool success = GetTokenInformation(tokenHandle, TOKEN_INFORMATION_CLASS.TokenElevationType,
  113. elevationTypePtr, (uint) elevationResultSize,
  114. out returnedSize);
  115. if (success)
  116. {
  117. elevationResult = (TOKEN_ELEVATION_TYPE) Marshal.ReadInt32(elevationTypePtr);
  118. bool isProcessAdmin = elevationResult == TOKEN_ELEVATION_TYPE.TokenElevationTypeFull;
  119. return isProcessAdmin;
  120. }
  121. else
  122. {
  123. throw new ApplicationException("Unable to determine the current elevation.");
  124. }
  125. }
  126. finally
  127. {
  128. if (elevationTypePtr != IntPtr.Zero)
  129. Marshal.FreeHGlobal(elevationTypePtr);
  130. }
  131. }
  132. finally
  133. {
  134. if (tokenHandle != IntPtr.Zero)
  135. CloseHandle(tokenHandle);
  136. }
  137. }
  138. else
  139. {
  140. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  141. WindowsPrincipal principal = new WindowsPrincipal(identity);
  142. bool result = principal.IsInRole(WindowsBuiltInRole.Administrator)
  143. || principal.IsInRole(0x200); //Domain Administrator
  144. return result;
  145. }
  146. }
  147. }
  148. }
  149.  
  150. ```
  151.  
  152.  
Advertisement
Add Comment
Please, Sign In to add comment