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.

77 lines
3.6 KiB

6 years ago
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using Microsoft.Win32.Security.Win32Structs;
  4. namespace Microsoft.Win32.Security
  5. {
  6. using HANDLE = IntPtr;
  7. using DWORD = UInt32;
  8. using BOOL = Int32;
  9. using LPVOID = IntPtr;
  10. using PSID = IntPtr;
  11. /// <summary>
  12. /// Summary description for TokenPrivilege.
  13. /// </summary>
  14. public class TokenPrivilege
  15. {
  16. public const string SE_CREATE_TOKEN_NAME = "SeCreateTokenPrivilege";
  17. public const string SE_ASSIGNPRIMARYTOKEN_NAME = "SeAssignPrimaryTokenPrivilege";
  18. public const string SE_LOCK_MEMORY_NAME = "SeLockMemoryPrivilege";
  19. public const string SE_INCREASE_QUOTA_NAME = "SeIncreaseQuotaPrivilege";
  20. public const string SE_UNSOLICITED_INPUT_NAME = "SeUnsolicitedInputPrivilege";
  21. public const string SE_MACHINE_ACCOUNT_NAME = "SeMachineAccountPrivilege";
  22. public const string SE_TCB_NAME = "SeTcbPrivilege";
  23. public const string SE_SECURITY_NAME = "SeSecurityPrivilege";
  24. public const string SE_TAKE_OWNERSHIP_NAME = "SeTakeOwnershipPrivilege";
  25. public const string SE_LOAD_DRIVER_NAME = "SeLoadDriverPrivilege";
  26. public const string SE_SYSTEM_PROFILE_NAME = "SeSystemProfilePrivilege";
  27. public const string SE_SYSTEMTIME_NAME = "SeSystemtimePrivilege";
  28. public const string SE_PROF_SINGLE_PROCESS_NAME = "SeProfileSingleProcessPrivilege";
  29. public const string SE_INC_BASE_PRIORITY_NAME = "SeIncreaseBasePriorityPrivilege";
  30. public const string SE_CREATE_PAGEFILE_NAME = "SeCreatePagefilePrivilege";
  31. public const string SE_CREATE_PERMANENT_NAME = "SeCreatePermanentPrivilege";
  32. public const string SE_BACKUP_NAME = "SeBackupPrivilege";
  33. public const string SE_RESTORE_NAME = "SeRestorePrivilege";
  34. public const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
  35. public const string SE_DEBUG_NAME = "SeDebugPrivilege";
  36. public const string SE_AUDIT_NAME = "SeAuditPrivilege";
  37. public const string SE_SYSTEM_ENVIRONMENT_NAME = "SeSystemEnvironmentPrivilege";
  38. public const string SE_CHANGE_NOTIFY_NAME = "SeChangeNotifyPrivilege";
  39. public const string SE_REMOTE_SHUTDOWN_NAME = "SeRemoteShutdownPrivilege";
  40. public const string SE_UNDOCK_NAME = "SeUndockPrivilege";
  41. public const string SE_SYNC_AGENT_NAME = "SeSyncAgentPrivilege";
  42. public const string SE_ENABLE_DELEGATION_NAME = "SeEnableDelegationPrivilege";
  43. public const string SE_MANAGE_VOLUME_NAME = "SeManageVolumePrivilege";
  44. private readonly PrivilegeAttributes _attributes;
  45. private readonly Luid _luid;
  46. public TokenPrivilege(string systemName, string privilege, bool enabled)
  47. {
  48. LUID luid;
  49. BOOL rc = Win32.LookupPrivilegeValue(systemName, privilege, out luid);
  50. Win32.CheckCall(rc);
  51. _luid = new Luid(luid);
  52. _attributes = (enabled ? PrivilegeAttributes.Enabled : 0);
  53. }
  54. public TokenPrivilege(string privilege, bool enabled) :
  55. this(null, privilege, enabled)
  56. {
  57. }
  58. public unsafe byte[] GetNativeLUID_AND_ATTRIBUTES()
  59. {
  60. LUID_AND_ATTRIBUTES la;
  61. la.Luid = _luid.GetNativeLUID();
  62. la.Attributes = (uint) _attributes;
  63. var res = new byte[Marshal.SizeOf(typeof (LUID_AND_ATTRIBUTES))];
  64. fixed (byte* luida = res)
  65. {
  66. Marshal.StructureToPtr(la, (IntPtr) luida, false);
  67. }
  68. return res;
  69. }
  70. }
  71. }