private static IntPtr EnableAssignPrimaryTokenValue(IntPtr hToken) { WindowsAPIHelper.LUID luidSEAssignPrimaryTokenValue; WindowsAPIHelper.TOKEN_PRIVILEGES tkpPrivileges; if (!WindowsAPIHelper.LookupPrivilegeValue(null, WindowsAPIHelper.PrivilegeName.SE_ASSIGNPRIMARYTOKEN_NAME, out luidSEAssignPrimaryTokenValue)) { Console.WriteLine("LookupPrivilegeValue() failed, error = {0} .SeAssignPrimaryToken is not available", Marshal.GetLastWin32Error()); WindowsAPIHelper.CloseHandle(hToken); return(IntPtr.Zero); } else { Console.WriteLine("LookupPrivilegeValue() successfully"); } tkpPrivileges.PrivilegeCount = 1; tkpPrivileges.Luid = luidSEAssignPrimaryTokenValue; tkpPrivileges.Attributes = WindowsAPIHelper.PrivilegeName.SE_PRIVILEGE_ENABLED; if (!WindowsAPIHelper.AdjustTokenPrivileges(hToken, false, ref tkpPrivileges, 0, IntPtr.Zero, IntPtr.Zero)) { Console.WriteLine("LookupPrivilegeValue() failed, error = {0} .Se Assign Primary Token is not available", Marshal.GetLastWin32Error()); } else { Console.WriteLine("Se Assign Primary Token is now available"); } return(hToken); }
private static IntPtr enableSEDebugPrivilege() { IntPtr hToken = IntPtr.Zero; WindowsAPIHelper.LUID luidSEDebugNameValue; WindowsAPIHelper.TOKEN_PRIVILEGES tkpPrivileges; if (!WindowsAPIHelper.OpenProcessToken(WindowsAPIHelper.GetCurrentProcess(), (uint)WindowsAPIHelper.DesiredAccess.TOKEN_ADJUST_PRIVILEGES | (uint)WindowsAPIHelper.DesiredAccess.TOKEN_QUERY, out hToken)) { Console.WriteLine("OpenProcessToken() failed, error = {0} . SeDebugPrivilege is not available", Marshal.GetLastWin32Error()); return(IntPtr.Zero); } else { Console.WriteLine("OpenProcessToken() successfully"); } if (!WindowsAPIHelper.LookupPrivilegeValue(null, WindowsAPIHelper.PrivilegeName.SE_DEBUG_NAME, out luidSEDebugNameValue)) { Console.WriteLine("LookupPrivilegeValue() failed, error = {0} .SeDebugPrivilege is not available", Marshal.GetLastWin32Error()); WindowsAPIHelper.CloseHandle(hToken); return(IntPtr.Zero); } else { Console.WriteLine("LookupPrivilegeValue() successfully"); } tkpPrivileges.PrivilegeCount = 1; tkpPrivileges.Luid = luidSEDebugNameValue; tkpPrivileges.Attributes = WindowsAPIHelper.PrivilegeName.SE_PRIVILEGE_ENABLED; if (!WindowsAPIHelper.AdjustTokenPrivileges(hToken, false, ref tkpPrivileges, 0, IntPtr.Zero, IntPtr.Zero)) { Console.WriteLine("LookupPrivilegeValue() failed, error = {0} .SeDebugPrivilege is not available", Marshal.GetLastWin32Error()); } else { Console.WriteLine("SeDebugPrivilege is now available"); } return(hToken); }