You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

94 lines
4.1 KiB

  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using System;
  22. using System.Diagnostics.CodeAnalysis;
  23. using System.Runtime.InteropServices;
  24. using System.Security;
  25. using System.Security.Principal;
  26. namespace Alphaleonis.Win32.Security
  27. {
  28. /// <summary>
  29. /// This object is used to enable a specific privilege for the currently running process during its lifetime.
  30. /// It should be disposed as soon as the elevated privilege is no longer needed.
  31. /// For more information see the documentation on AdjustTokenPrivileges on MSDN.
  32. /// </summary>
  33. internal sealed class InternalPrivilegeEnabler : IDisposable
  34. {
  35. /// <summary>Initializes a new instance of the <see cref="PrivilegeEnabler"/> class and enabling the specified privilege for the currently running process.</summary>
  36. /// <param name="privilegeName">The name of the privilege.</param>
  37. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  38. [SecurityCritical]
  39. public InternalPrivilegeEnabler(Privilege privilegeName)
  40. {
  41. if (privilegeName == null)
  42. throw new ArgumentNullException("privilegeName");
  43. EnabledPrivilege = privilegeName;
  44. AdjustPrivilege(true);
  45. }
  46. /// <summary>
  47. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  48. /// In this case the privilege previously enabled will be disabled.
  49. /// </summary>
  50. public void Dispose()
  51. {
  52. try
  53. {
  54. if (EnabledPrivilege != null)
  55. AdjustPrivilege(false);
  56. }
  57. finally
  58. {
  59. EnabledPrivilege = null;
  60. }
  61. }
  62. /// <summary>Adjusts the privilege.</summary>
  63. /// <param name="enable"><see langword="true"/> the privilege will be enabled, otherwise disabled.</param>
  64. [SecurityCritical]
  65. private void AdjustPrivilege(bool enable)
  66. {
  67. using (WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent(TokenAccessLevels.Query | TokenAccessLevels.AdjustPrivileges))
  68. {
  69. IntPtr hToken = currentIdentity.Token;
  70. TokenPrivileges newPrivilege = new TokenPrivileges();
  71. TokenPrivileges mOldPrivilege = new TokenPrivileges();
  72. newPrivilege.PrivilegeCount = 1;
  73. newPrivilege.Luid = Filesystem.NativeMethods.LongToLuid(EnabledPrivilege.LookupLuid());
  74. newPrivilege.Attributes = (uint)(enable ? 2 : 0); // 2 = SePrivilegeEnabled;
  75. uint length;
  76. if (!NativeMethods.AdjustTokenPrivileges(hToken, false, ref newPrivilege, (uint) Marshal.SizeOf(mOldPrivilege), out mOldPrivilege, out length))
  77. NativeError.ThrowException(Marshal.GetLastWin32Error());
  78. // If no privilege was changed, we don't want to reset it.
  79. if (mOldPrivilege.PrivilegeCount == 0)
  80. EnabledPrivilege = null;
  81. }
  82. }
  83. public Privilege EnabledPrivilege { get; private set; }
  84. }
  85. }