Exemplo n.º 1
0
        /// <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");
                }
            }
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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());
            }
        }