Security.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Security.Principal;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace SmartCoalApplication.SystemLayer
  11. {
  12. /// <summary>
  13. /// Security related static methods and properties.
  14. /// </summary>
  15. public static class Security
  16. {
  17. private static bool isAdmin = GetIsAdministrator();
  18. private static bool GetIsAdministrator()
  19. {
  20. AppDomain domain = Thread.GetDomain();
  21. domain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
  22. WindowsPrincipal principal = (WindowsPrincipal)Thread.CurrentPrincipal;
  23. return principal.IsInRole(WindowsBuiltInRole.Administrator);
  24. }
  25. /// <summary>
  26. /// Gets a flag indicating whether the user has administrator-level privileges.
  27. /// </summary>
  28. /// <remarks>
  29. /// This is used to control access to actions that require the user to be an administrator.
  30. /// An example is checking for and installing updates, actions which are not normally able
  31. /// to be performed by normal or "limited" users. A user must also be an administrator in
  32. /// order to write to any Settings.SystemWide entries.
  33. /// </remarks>
  34. public static bool IsAdministrator
  35. {
  36. get
  37. {
  38. return isAdmin;
  39. }
  40. }
  41. /// <summary>
  42. /// Gets a flag indicating whether the current user is able to elevate to obtain
  43. /// administrator-level privileges.
  44. /// </summary>
  45. /// <remarks>
  46. /// This flag has no meaning if IsAdministrator returns true.
  47. /// This flag indicates whether a new process may be spawned which has administrator
  48. /// privilege. It does not indicate the ability to elevate the current process to
  49. /// administrator privilege. For Windows this indicates that the user is running
  50. /// Vista and has UAC enabled. This property should be used instead of checking
  51. /// the OS version anytime this check must be performed.
  52. /// Note to implementors: This may be written to simply return false.
  53. /// </remarks>
  54. public static bool CanElevateToAdministrator
  55. {
  56. get
  57. {
  58. if (OS.IsVistaOrLater && !Security.IsAdministrator)
  59. {
  60. return IsUacEnabled;
  61. }
  62. else
  63. {
  64. return false;
  65. }
  66. }
  67. }
  68. private static bool IsUacEnabled
  69. {
  70. get
  71. {
  72. bool returnVal = false;
  73. const string keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System";
  74. const string valueName = "EnableLUA";
  75. try
  76. {
  77. if (Environment.OSVersion.Version >= OS.WindowsVista)
  78. {
  79. using (RegistryKey key = Registry.LocalMachine.OpenSubKey(keyName, false))
  80. {
  81. if (key != null)
  82. {
  83. RegistryValueKind valueKind = key.GetValueKind(valueName);
  84. if (valueKind == RegistryValueKind.DWord)
  85. {
  86. int value = unchecked((int)key.GetValue(valueName));
  87. returnVal = (value == 1);
  88. }
  89. }
  90. }
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. Tracing.Ping(ex.ToString());
  96. returnVal = false;
  97. }
  98. return returnVal;
  99. }
  100. }
  101. /// <summary>
  102. /// If IsAdministrator is true, this returns true if we can launch a process with limited privilege.
  103. /// </summary>
  104. /// <remarks>
  105. /// Here's the truth table for this:
  106. /// Windows XP + Admin User -> false
  107. /// Windows XP + Standard User -> true
  108. /// Windows Vista + Admin User + UAC Enabled -> true
  109. /// Windows Vista + Admin User + UAC Disabled -> false
  110. /// Windows Vista + Standard User -> true
  111. /// </remarks>
  112. public static bool CanLaunchNonAdminProcess
  113. {
  114. get
  115. {
  116. if (!Security.IsAdministrator)
  117. {
  118. return true;
  119. }
  120. else if (OS.IsVistaOrLater)
  121. {
  122. return Security.IsUacEnabled;
  123. }
  124. else
  125. {
  126. return false;
  127. }
  128. }
  129. }
  130. /// <summary>
  131. /// Verifies that a file has a valid digital signature.
  132. /// </summary>
  133. /// <param name="owner">The parent/owner window for any UI that may be shown.</param>
  134. /// <param name="fileName">The path to the file to be validate.</param>
  135. /// <param name="showNegativeUI">Whether or not to show a UI in the case that the signature can not be found or validated.</param>
  136. /// <param name="showPositiveUI">Whether or not to show a UI in the case that the signature is successfully found and validated.</param>
  137. /// <returns>true if the file has a digital signature that validates up to a trusted root, or false otherwise</returns>
  138. public static bool VerifySignedFile(IWin32Window owner, string fileName, bool showNegativeUI, bool showPositiveUI)
  139. {
  140. unsafe
  141. {
  142. fixed (char* szFileName = fileName)
  143. {
  144. Guid pgActionID = NativeConstants.WINTRUST_ACTION_GENERIC_VERIFY_V2;
  145. NativeStructs.WINTRUST_FILE_INFO fileInfo = new NativeStructs.WINTRUST_FILE_INFO();
  146. fileInfo.cbStruct = (uint)sizeof(NativeStructs.WINTRUST_FILE_INFO);
  147. fileInfo.pcwszFilePath = szFileName;
  148. NativeStructs.WINTRUST_DATA wintrustData = new NativeStructs.WINTRUST_DATA();
  149. wintrustData.cbStruct = (uint)sizeof(NativeStructs.WINTRUST_DATA);
  150. if (!showNegativeUI && !showPositiveUI)
  151. {
  152. wintrustData.dwUIChoice = NativeConstants.WTD_UI_NONE;
  153. }
  154. else if (!showNegativeUI && showPositiveUI)
  155. {
  156. wintrustData.dwUIChoice = NativeConstants.WTD_UI_NOBAD;
  157. }
  158. else if (showNegativeUI && !showPositiveUI)
  159. {
  160. wintrustData.dwUIChoice = NativeConstants.WTD_UI_NOGOOD;
  161. }
  162. else // if (showNegativeUI && showPositiveUI)
  163. {
  164. wintrustData.dwUIChoice = NativeConstants.WTD_UI_ALL;
  165. }
  166. wintrustData.fdwRevocationChecks = NativeConstants.WTD_REVOKE_WHOLECHAIN;
  167. wintrustData.dwUnionChoice = NativeConstants.WTD_CHOICE_FILE;
  168. wintrustData.pInfo = (void*)&fileInfo;
  169. IntPtr handle;
  170. if (owner == null)
  171. {
  172. handle = IntPtr.Zero;
  173. }
  174. else
  175. {
  176. handle = owner.Handle;
  177. }
  178. int result = NativeMethods.WinVerifyTrust(handle, ref pgActionID, ref wintrustData);
  179. GC.KeepAlive(owner);
  180. return result >= 0;
  181. }
  182. }
  183. }
  184. }
  185. }