static void adjustToken() { IntPtr processhandle = Kernel32.GetCurrentProcess(); IntPtr tokenHandle; ADVAPI32.OpenProcessToken(processhandle, (TOKEN.ADJUST_PRIVILEGES | TOKEN.QUERY), out tokenHandle); var newState = new TOKEN_PRIVILEGES(); newState.Privileges.Attributes = SE.PRIVILEGE_ENABLED; newState.PrivilegeCount = 1; ADVAPI32.LookupPrivilegeValue(null, SE.SHUTDOWN_NAME, out newState.Privileges.Luid); ADVAPI32.AdjustTokenPrivileges(tokenHandle, false, ref newState, 0, IntPtr.Zero, IntPtr.Zero); Kernel32.CloseHandle(tokenHandle); }
public static bool ShutdownLocalhost(ShutdownEnum.ExitWindows options, ShutdownEnum.ShutdownReason reason) { TokPriv1Luid tp; IntPtr hproc = KERNEL32.getCurrentProcess(); IntPtr zeroPtr = IntPtr.Zero; ADVAPI32.OpenProcessToken(hproc, ADVAPI32.TOKEN_ADJUST_PRIVILEGES | ADVAPI32.TOKEN_QUERY, ref zeroPtr); tp.Count = 1; tp.Luid = 0; tp.Attr = ADVAPI32.SE_PRIVILEGE_ENABLED; ADVAPI32.LookupPrivilegeValue(null, ADVAPI32.SE_SHUTDOWN_NAME, ref tp.Luid); ADVAPI32.AdjustTokenPrivileges(zeroPtr, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero); return(USER32.exitWindowsEx(options, reason)); }
private void AdjustToken(IntPtr hToken, string privilege) { ADVAPI32.TokPriv1Luid tokenPriv1Luid = new ADVAPI32.TokPriv1Luid() { Count = 1, Attr = ADVAPI32.SE_PRIVILEGE_ENABLED, Luid = 0 }; bool lookupResult = ADVAPI32.LookupPrivilegeValue(null, privilege, ref tokenPriv1Luid.Luid); if (!lookupResult) { int win32Error = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); ThrowTerminatingError(new ErrorRecord(new InstanceNotFoundException(string.Format("Privilege lookup failed for '{1}': {0}", win32Error, privilege)), "100", ErrorCategory.ResourceUnavailable, null)); } bool adjustTokenResult = ADVAPI32.AdjustTokenPrivileges(hToken, false, ref tokenPriv1Luid, 12, IntPtr.Zero, IntPtr.Zero); if (!adjustTokenResult) { int win32Error = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); ThrowTerminatingError(new ErrorRecord(new InvalidOperationException(string.Format("Cannot adjust token privilege '{1}': {0}", win32Error, privilege)), "100", ErrorCategory.AuthenticationError, null)); } }