* 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
This commit is contained in:
wj32
2009-02-06 05:38:49 +00:00
parent 3759bc7718
commit 3fe0992ff7
6 changed files with 74 additions and 88 deletions
@@ -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();
+11 -13
View File
@@ -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
//
@@ -120,13 +120,4 @@
<metadata name="toolTipProvider.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="toolTipProvider.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="toolTipProvider.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="toolTipProvider.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>
+5 -8
View File
@@ -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
{ }
@@ -182,6 +182,37 @@ namespace ProcessHacker
return new WindowsSID(data.ReadInt32(0));
}
}
/// <summary>
/// Gets the token's privileges.
/// </summary>
/// <returns>A TOKEN_PRIVILEGES structure.</returns>
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<LUID_AND_ATTRIBUTES>(4, i);
}
return privileges;
}
}
/// <summary>
/// Gets the restricted token's restricting SIDs.
@@ -303,6 +334,29 @@ namespace ProcessHacker
return value != 0;
}
/// <summary>
/// Sets a privilege's attributes.
/// </summary>
/// <param name="privilegeName">The name of the privilege.</param>
/// <param name="attributes">The new attributes of the privilege.</param>
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();
}
}
}
}
-51
View File
@@ -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<LUID_AND_ATTRIBUTES>(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