From 3fe0992ff721dfcf36ee3a89ceb38ddcf27499df Mon Sep 17 00:00:00 2001 From: wj32 Date: Fri, 6 Feb 2009 05:38:49 +0000 Subject: [PATCH] * finally refactored the token privileges code * removed icon in options tooltips git-svn-id: svn://svn.code.sf.net/p/processhacker/code@609 21ef857c-d57f-4fe0-8362-d861dc6d29cd --- .../Components/TokenProperties.cs | 11 ++-- .../Forms/OptionsWindow.Designer.cs | 24 ++++----- trunk/ProcessHacker/Forms/OptionsWindow.resx | 9 ---- trunk/ProcessHacker/Program.cs | 13 ++--- .../Win32/Handles/TokenHandle.cs | 54 +++++++++++++++++++ trunk/ProcessHacker/Win32/Win32.cs | 51 ------------------ 6 files changed, 74 insertions(+), 88 deletions(-) diff --git a/trunk/ProcessHacker/Components/TokenProperties.cs b/trunk/ProcessHacker/Components/TokenProperties.cs index d24b73014..c5531dd9e 100644 --- a/trunk/ProcessHacker/Components/TokenProperties.cs +++ b/trunk/ProcessHacker/Components/TokenProperties.cs @@ -154,7 +154,7 @@ namespace ProcessHacker try { - Win32.TOKEN_PRIVILEGES privileges = Win32.ReadTokenPrivileges(token); + Win32.TOKEN_PRIVILEGES privileges = token.GetPrivileges(); for (int i = 0; i < privileges.PrivilegeCount; i++) { @@ -262,8 +262,7 @@ namespace ProcessHacker { try { - Win32.WriteTokenPrivilege( - _object.GetToken(Win32.TOKEN_RIGHTS.TOKEN_ADJUST_PRIVILEGES), + _object.GetToken(Win32.TOKEN_RIGHTS.TOKEN_ADJUST_PRIVILEGES).SetPrivilege( item.Text, Win32.SE_PRIVILEGE_ATTRIBUTES.SE_PRIVILEGE_ENABLED); if (item.SubItems[1].Text != "Default Enabled") @@ -296,8 +295,7 @@ namespace ProcessHacker try { - Win32.WriteTokenPrivilege( - _object.GetToken(Win32.TOKEN_RIGHTS.TOKEN_ADJUST_PRIVILEGES), + _object.GetToken(Win32.TOKEN_RIGHTS.TOKEN_ADJUST_PRIVILEGES).SetPrivilege( item.Text, Win32.SE_PRIVILEGE_ATTRIBUTES.SE_PRIVILEGE_DISABLED); item.BackColor = GetAttributeColor(Win32.SE_PRIVILEGE_ATTRIBUTES.SE_PRIVILEGE_DISABLED); @@ -321,8 +319,7 @@ namespace ProcessHacker { try { - Win32.WriteTokenPrivilege( - _object.GetToken(Win32.TOKEN_RIGHTS.TOKEN_ADJUST_PRIVILEGES), + _object.GetToken(Win32.TOKEN_RIGHTS.TOKEN_ADJUST_PRIVILEGES).SetPrivilege( item.Text, Win32.SE_PRIVILEGE_ATTRIBUTES.SE_PRIVILEGE_REMOVED); item.Remove(); diff --git a/trunk/ProcessHacker/Forms/OptionsWindow.Designer.cs b/trunk/ProcessHacker/Forms/OptionsWindow.Designer.cs index fa5534b47..7445367c3 100644 --- a/trunk/ProcessHacker/Forms/OptionsWindow.Designer.cs +++ b/trunk/ProcessHacker/Forms/OptionsWindow.Designer.cs @@ -50,7 +50,6 @@ this.checkShowTrayIcon = new System.Windows.Forms.CheckBox(); this.checkVerifySignatures = new System.Windows.Forms.CheckBox(); this.tabHighlighting = new System.Windows.Forms.TabPage(); - this.label22 = new System.Windows.Forms.Label(); this.label20 = new System.Windows.Forms.Label(); this.label19 = new System.Windows.Forms.Label(); this.label11 = new System.Windows.Forms.Label(); @@ -87,6 +86,7 @@ this.colorMemoryPB = new ProcessHacker.Components.ColorModifier(); this.colorCPUUT = new ProcessHacker.Components.ColorModifier(); this.colorCPUKT = new ProcessHacker.Components.ColorModifier(); + this.label22 = new System.Windows.Forms.Label(); this.toolTipProvider = new System.Windows.Forms.ToolTip(this.components); ((System.ComponentModel.ISupportInitialize)(this.textUpdateInterval)).BeginInit(); this.tabControl.SuspendLayout(); @@ -398,16 +398,6 @@ this.tabHighlighting.Text = "Highlighting"; this.tabHighlighting.UseVisualStyleBackColor = true; // - // label22 - // - this.label22.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.label22.AutoSize = true; - this.label22.Location = new System.Drawing.Point(12, 387); - this.label22.Name = "label22"; - this.label22.Size = new System.Drawing.Size(300, 13); - this.label22.TabIndex = 10; - this.label22.Text = "Tip: Hover over an item to get a more detailed description of it."; - // // label20 // this.label20.Location = new System.Drawing.Point(214, 133); @@ -760,6 +750,16 @@ this.colorCPUKT.Size = new System.Drawing.Size(40, 20); this.colorCPUKT.TabIndex = 11; // + // label22 + // + this.label22.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.label22.AutoSize = true; + this.label22.Location = new System.Drawing.Point(12, 387); + this.label22.Name = "label22"; + this.label22.Size = new System.Drawing.Size(300, 13); + this.label22.TabIndex = 10; + this.label22.Text = "Tip: Hover over an item to get a more detailed description of it."; + // // toolTipProvider // this.toolTipProvider.AutomaticDelay = 250; @@ -767,8 +767,6 @@ this.toolTipProvider.InitialDelay = 250; this.toolTipProvider.IsBalloon = true; this.toolTipProvider.ReshowDelay = 50; - this.toolTipProvider.ToolTipIcon = System.Windows.Forms.ToolTipIcon.Info; - this.toolTipProvider.ToolTipTitle = "Description"; // // OptionsWindow // diff --git a/trunk/ProcessHacker/Forms/OptionsWindow.resx b/trunk/ProcessHacker/Forms/OptionsWindow.resx index d443dcf18..fb4813b39 100644 --- a/trunk/ProcessHacker/Forms/OptionsWindow.resx +++ b/trunk/ProcessHacker/Forms/OptionsWindow.resx @@ -120,13 +120,4 @@ 17, 17 - - 17, 17 - - - 17, 17 - - - 17, 17 - \ No newline at end of file diff --git a/trunk/ProcessHacker/Program.cs b/trunk/ProcessHacker/Program.cs index 189fbee0b..7ae8df67b 100644 --- a/trunk/ProcessHacker/Program.cs +++ b/trunk/ProcessHacker/Program.cs @@ -129,14 +129,11 @@ namespace ProcessHacker try { - Win32.WriteTokenPrivilege( - (new Win32.ProcessHandle(System.Diagnostics.Process.GetCurrentProcess().Id, - Win32.PROCESS_RIGHTS.PROCESS_QUERY_INFORMATION)).GetToken(), - "SeDebugPrivilege", Win32.SE_PRIVILEGE_ATTRIBUTES.SE_PRIVILEGE_ENABLED); - Win32.WriteTokenPrivilege( - (new Win32.ProcessHandle(System.Diagnostics.Process.GetCurrentProcess().Id, - Win32.PROCESS_RIGHTS.PROCESS_QUERY_INFORMATION)).GetToken(), - "SeLoadDriverPrivilege", Win32.SE_PRIVILEGE_ATTRIBUTES.SE_PRIVILEGE_ENABLED); + using (var thandle = new Win32.ProcessHandle(System.Diagnostics.Process.GetCurrentProcess().Id, + Win32.PROCESS_RIGHTS.PROCESS_QUERY_INFORMATION).GetToken()) + { + thandle.SetPrivilege("SeDebugPrivilege", Win32.SE_PRIVILEGE_ATTRIBUTES.SE_PRIVILEGE_ENABLED); + } } catch { } diff --git a/trunk/ProcessHacker/Win32/Handles/TokenHandle.cs b/trunk/ProcessHacker/Win32/Handles/TokenHandle.cs index e476435e9..dec7e77dc 100644 --- a/trunk/ProcessHacker/Win32/Handles/TokenHandle.cs +++ b/trunk/ProcessHacker/Win32/Handles/TokenHandle.cs @@ -182,6 +182,37 @@ namespace ProcessHacker return new WindowsSID(data.ReadInt32(0)); } } + + /// + /// Gets the token's privileges. + /// + /// A TOKEN_PRIVILEGES structure. + public TOKEN_PRIVILEGES GetPrivileges() + { + int retLen; + + GetTokenInformation(this, TOKEN_INFORMATION_CLASS.TokenPrivileges, IntPtr.Zero, 0, out retLen); + + using (MemoryAlloc data = new MemoryAlloc(retLen)) + { + if (!GetTokenInformation(this, TOKEN_INFORMATION_CLASS.TokenPrivileges, data.Memory, + data.Size, out retLen)) + ThrowLastWin32Error(); + + uint number = data.ReadUInt32(0); + TOKEN_PRIVILEGES privileges = new TOKEN_PRIVILEGES(); + + privileges.PrivilegeCount = number; + privileges.Privileges = new LUID_AND_ATTRIBUTES[number]; + + for (int i = 0; i < number; i++) + { + privileges.Privileges[i] = data.ReadStruct(4, i); + } + + return privileges; + } + } /// /// Gets the restricted token's restricting SIDs. @@ -303,6 +334,29 @@ namespace ProcessHacker return value != 0; } + + /// + /// Sets a privilege's attributes. + /// + /// The name of the privilege. + /// The new attributes of the privilege. + public void SetPrivilege(string privilegeName, SE_PRIVILEGE_ATTRIBUTES attributes) + { + TOKEN_PRIVILEGES tkp = new TOKEN_PRIVILEGES(); + + tkp.Privileges = new LUID_AND_ATTRIBUTES[1]; + + if (!LookupPrivilegeValue(null, privilegeName, ref tkp.Privileges[0].Luid)) + throw new Exception("Invalid privilege name '" + privilegeName + "'."); + + tkp.PrivilegeCount = 1; + tkp.Privileges[0].Attributes = attributes; + + AdjustTokenPrivileges(this, 0, ref tkp, 0, 0, 0); + + if (Marshal.GetLastWin32Error() != 0) + ThrowLastWin32Error(); + } } } } diff --git a/trunk/ProcessHacker/Win32/Win32.cs b/trunk/ProcessHacker/Win32/Win32.cs index ec7a9ae6e..d6c16d273 100644 --- a/trunk/ProcessHacker/Win32/Win32.cs +++ b/trunk/ProcessHacker/Win32/Win32.cs @@ -1114,57 +1114,6 @@ namespace ProcessHacker return sb.ToString(); } - public static TOKEN_PRIVILEGES ReadTokenPrivileges(TokenHandle TokenHandle) - { - int retLen = 0; - - GetTokenInformation(TokenHandle.Handle, TOKEN_INFORMATION_CLASS.TokenPrivileges, IntPtr.Zero, 0, out retLen); - - using (MemoryAlloc data = new MemoryAlloc(retLen)) - { - if (!GetTokenInformation(TokenHandle.Handle, TOKEN_INFORMATION_CLASS.TokenPrivileges, data.Memory, - data.Size, out retLen)) - ThrowLastWin32Error(); - - uint number = data.ReadUInt32(0); - TOKEN_PRIVILEGES privileges = new TOKEN_PRIVILEGES(); - - privileges.PrivilegeCount = number; - privileges.Privileges = new LUID_AND_ATTRIBUTES[number]; - - for (int i = 0; i < number; i++) - { - privileges.Privileges[i] = data.ReadStruct(4, i); - } - - return privileges; - } - } - - public static void WriteTokenPrivilege(string PrivilegeName, SE_PRIVILEGE_ATTRIBUTES Attributes) - { - WriteTokenPrivilege( - ProcessHandle.FromHandle(Program.CurrentProcess).GetToken(), PrivilegeName, Attributes); - } - - public static void WriteTokenPrivilege(TokenHandle TokenHandle, string PrivilegeName, SE_PRIVILEGE_ATTRIBUTES Attributes) - { - TOKEN_PRIVILEGES tkp = new TOKEN_PRIVILEGES(); - - tkp.Privileges = new LUID_AND_ATTRIBUTES[1]; - - if (!LookupPrivilegeValue(null, PrivilegeName, ref tkp.Privileges[0].Luid)) - throw new Exception("Invalid privilege name '" + PrivilegeName + "'."); - - tkp.PrivilegeCount = 1; - tkp.Privileges[0].Attributes = Attributes; - - AdjustTokenPrivileges(TokenHandle.Handle, 0, ref tkp, 0, 0, 0); - - if (Marshal.GetLastWin32Error() != 0) - ThrowLastWin32Error(); - } - #endregion #region Services