SecurityUtils.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. namespace DiskAccessLibrary
  12. {
  13. public enum SecurityPrivilegeName
  14. {
  15. SeRestorePrivilege,
  16. SeBackupPrivilege,
  17. SeManageVolumePrivilege,
  18. }
  19. public class SecurityUtils
  20. {
  21. public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
  22. public const int TOKEN_QUERY = 0x00000008;
  23. public const int SE_PRIVILEGE_ENABLED = 0x00000002;
  24. [StructLayout(LayoutKind.Sequential)]
  25. public struct LUID
  26. {
  27. public int LowPart;
  28. public int HighPart;
  29. }
  30. [StructLayout(LayoutKind.Sequential)]
  31. public struct TOKEN_PRIVILEGES
  32. {
  33. public int PrivilegeCount;
  34. public LUID Luid;
  35. public int Attributes;
  36. }
  37. [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
  38. public static extern int OpenProcessToken(int ProcessHandle, int DesiredAccess,
  39. ref int tokenhandle);
  40. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  41. public static extern int GetCurrentProcess();
  42. [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
  43. public static extern int LookupPrivilegeValue(string lpsystemname, string lpname,
  44. [MarshalAs(UnmanagedType.Struct)] ref LUID lpLuid);
  45. [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  46. public static extern int AdjustTokenPrivileges(int tokenhandle, int disableprivs, [MarshalAs(UnmanagedType.Struct)]ref TOKEN_PRIVILEGES Newstate,
  47. int bufferlength, int PreivousState, int Returnlength);
  48. public static bool ObtainManageVolumePrivilege()
  49. {
  50. return ObtainPrivilege(SecurityPrivilegeName.SeManageVolumePrivilege);
  51. }
  52. public static bool ObtainPrivilege(SecurityPrivilegeName privilegeName)
  53. {
  54. int tokenHandle = 0;
  55. int retval = 0;
  56. TOKEN_PRIVILEGES tokenPrivileges = new TOKEN_PRIVILEGES();
  57. LUID privilegeLuid = new LUID();
  58. retval = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref tokenHandle);
  59. if (retval == 0) //If the function succeeds, the return value is nonzero
  60. {
  61. return false;
  62. }
  63. retval = LookupPrivilegeValue(null, privilegeName.ToString(), ref privilegeLuid);
  64. if (retval == 0) //If the function succeeds, the return value is nonzero
  65. {
  66. return false;
  67. }
  68. tokenPrivileges.PrivilegeCount = 1;
  69. tokenPrivileges.Attributes = SE_PRIVILEGE_ENABLED;
  70. tokenPrivileges.Luid = privilegeLuid;
  71. retval = AdjustTokenPrivileges(tokenHandle, 0, ref tokenPrivileges, 0, 0, 0);
  72. if (retval == 0) // If the function succeeds, the return value is nonzero
  73. {
  74. return false;
  75. }
  76. else
  77. {
  78. // http://msdn.microsoft.com/en-us/library/windows/desktop/aa375202%28v=vs.85%29.aspx
  79. // GetLastError returns one of the following values when the function succeeds:
  80. // ERROR_SUCCESS, ERROR_NOT_ALL_ASSIGNED
  81. int errorCode = Marshal.GetLastWin32Error();
  82. return (errorCode == (int)Win32Error.ERROR_SUCCESS);
  83. }
  84. }
  85. }
  86. }