Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

InternalPrivilegeEnabler.cs 4.2 KiB

8 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. _mPrivilege = 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 (_mPrivilege != null)
  55. AdjustPrivilege(false);
  56. }
  57. finally
  58. {
  59. _mPrivilege = 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. if (currentIdentity != null)
  70. {
  71. IntPtr hToken = currentIdentity.Token;
  72. TokenPrivileges newPrivilege = new TokenPrivileges();
  73. TokenPrivileges mOldPrivilege = new TokenPrivileges();
  74. newPrivilege.PrivilegeCount = 1;
  75. newPrivilege.Luid = Filesystem.NativeMethods.LongToLuid(_mPrivilege.LookupLuid());
  76. newPrivilege.Attributes = (uint)(enable ? 2 : 0); // 2 = SePrivilegeEnabled;
  77. uint length;
  78. if (!NativeMethods.AdjustTokenPrivileges(hToken, false, ref newPrivilege, (uint) Marshal.SizeOf(mOldPrivilege), out mOldPrivilege, out length))
  79. NativeError.ThrowException(Marshal.GetLastWin32Error());
  80. // If no privilege was changed, we don't want to reset it.
  81. if (mOldPrivilege.PrivilegeCount == 0)
  82. _mPrivilege = null;
  83. }
  84. }
  85. }
  86. public Privilege EnabledPrivilege
  87. {
  88. get { return _mPrivilege; }
  89. }
  90. private Privilege _mPrivilege;
  91. }
  92. }