/// <summary> /// Enables or disables the privilege with given key for this process /// </summary> /// <param name="key">The privilege key</param> /// <param name="enable">Whether to enable or disable the privilege</param> public void SetPrivilege(String key, Boolean enable) { AdvApi32.Luid luid = new AdvApi32.Luid(); if (!AdvApi32.LookupPrivilegeValue(null, key, ref luid)) { throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not perform lookup of privilege value"); } IntPtr handle = IntPtr.Zero; if (!AdvApi32.OpenProcessToken(Handle.DangerousGetHandle(), TokenAccessLevels.AdjustPrivileges, ref handle)) { throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not open process token"); } using (SafeAccessTokenHandle tokenHandle = new SafeAccessTokenHandle(handle)) { AdvApi32.TokenPrivileges privileges = new AdvApi32.TokenPrivileges(); privileges.PrivilegeCount = 1; privileges.Luid = luid; privileges.State = enable ? PrivilegeState.Enabled : PrivilegeState.Removed; if (!AdvApi32.AdjustTokenPrivileges(tokenHandle.DangerousGetHandle(), false, ref privileges, 0, IntPtr.Zero, IntPtr.Zero)) { throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not adjust token privileges"); } } }
public static void Exit(ShutdownFlags Flags = ShutdownFlags.Shutdown, int Reason = 0) { var tp = new TokPriv1Luid(1, 0, AdvApi32.SE_PRIVILEGE_ENABLED); IntPtr hproc = Kernel32.CurrentProcess, htok = IntPtr.Zero; AdvApi32.OpenProcessToken(hproc, AdvApi32.TOKEN_ADJUST_PRIVILEGES | AdvApi32.TOKEN_QUERY, ref htok); AdvApi32.LookupPrivilegeValue(null, "SeShutdownPrivilege", ref tp.Luid); AdvApi32.AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero); User32.ExitWindows(Flags, 0); }
public static void AdjustPrivileges(this AdvApi32.SafeTokenHandle hObj, SafeCoTaskMemHandle privileges) { if (privileges == null || privileges.IsInvalid) { return; } uint retLen = 0; if (!AdvApi32.AdjustTokenPrivileges(hObj, false, privileges, (uint)privileges.Size, SafeCoTaskMemHandle.Null, ref retLen)) { throw new Win32Exception(); } }
public static void AdjustPrivileges(this AdvApi32.SafeTokenHandle hObj, AdvApi32.PTOKEN_PRIVILEGES privileges) { if (privileges == null) { return; } uint retLen = 0; if (!AdvApi32.AdjustTokenPrivileges(hObj, false, privileges, (uint)privileges.SizeInBytes, null, ref retLen)) { throw new Win32Exception(); } }
public static SafeCoTaskMemHandle AdjustPrivilege(this AdvApi32.SafeTokenHandle hObj, SystemPrivilege priv, AdvApi32.PrivilegeAttributes attr) { var newState = new AdvApi32.PTOKEN_PRIVILEGES(priv.GetLUID(), attr); var prevState = AdvApi32.PTOKEN_PRIVILEGES.GetAllocatedAndEmptyInstance(); var retLen = (uint)prevState.Size; if (!AdvApi32.AdjustTokenPrivileges(hObj, false, newState, newState.SizeInBytes, prevState, ref retLen)) { throw new Win32Exception(); } prevState.Resize((int)retLen); return(prevState); }
private static SafeFileHandle OpenReparsePoint(string path, bool writeAccess) { using (Process currentProcess = Process.GetCurrentProcess()) { IntPtr tokenHandle = IntPtr.Zero; try { tokenHandle = AdvApi32.OpenProcessToken(currentProcess.Handle, TokenAccessLevels.AdjustPrivileges | TokenAccessLevels.Query); Luid luid = AdvApi32.LookupPrivilegeValue(writeAccess ? PrivilegeName.Restore : PrivilegeName.Backup); var privileges = new TokenPrivileges { PrivilegeCount = 1, Privileges = new [] { new Privilege() { Luid = luid, Attributes = Privilege.EnabledAttribute, } } }; AdvApi32.AdjustTokenPrivileges(tokenHandle, privileges); } finally { if (tokenHandle != IntPtr.Zero) { Kernel32.CloseHandle(tokenHandle); } } } GenericAccessRights access = GenericAccessRights.Read; if (writeAccess) { access |= GenericAccessRights.Write; } SafeFileHandle handle = Kernel32.CreateFile(path, access, FileShare.None, FileMode.Open, FileAttributes.Normal, FileFlags.OpenReparsePoint | FileFlags.BackupSemantics); return(handle); }
public static SafeCoTaskMemHandle AdjustPrivileges(this AdvApi32.SafeTokenHandle hObj, params ExtensionMethods.PrivilegeAndAttributes[] privileges) { if (privileges == null || privileges.Length == 0) { return(SafeCoTaskMemHandle.Null); } var newState = new AdvApi32.PTOKEN_PRIVILEGES(privileges.Select(pa => new AdvApi32.LUID_AND_ATTRIBUTES(pa.Privilege.GetLUID(), pa.Attributes)).ToArray()); var prevState = AdvApi32.PTOKEN_PRIVILEGES.GetAllocatedAndEmptyInstance(); var retLen = (uint)prevState.Size; if (!AdvApi32.AdjustTokenPrivileges(hObj, false, newState, newState.SizeInBytes, prevState, ref retLen)) { throw new Win32Exception(); } prevState.Resize((int)retLen); return(prevState); }
public static void AcquireSystemPrivilege(string name) { AdvApi32.TOKEN_PRIVILEGES tkp; IntPtr token; tkp.Privileges = new AdvApi32.LUID_AND_ATTRIBUTES[1]; AdvApi32.LookupPrivilegeValue( IntPtr.Zero, name, out tkp.Privileges[0].Luid ); tkp.PrivilegeCount = 1; tkp.Privileges[0].Attributes = (uint)AdvApi32.Se_Privilege.ENABLED; if (!AdvApi32.OpenProcessToken( Process.GetCurrentProcess().Handle, (uint)(AdvApi32.Token.ADJUST_PRIVILEGES | AdvApi32.Token.QUERY), out token)) { Win32Error.Set("OpenProcessToken"); throw new Exception(Win32Error.GetFullErrMsg()); } if (!AdvApi32.AdjustTokenPrivileges( token, false, ref tkp, 0, IntPtr.Zero, IntPtr.Zero)) { Win32Error.Set("AdjustTokenPrivileges"); throw new Exception(Win32Error.GetFullErrMsg()); } }