473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

GetTokenInformation

Hi,

I need to check the SE_TCB_NAME previlige for the current user using
GetTokenInformation api call.i used the api call and get the
privilege,but i am unable to go thru each previlege to check whether
SE_TCB_NAME is enabled or not.my code is below[C#}

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;

namespace gettoken
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
enum TOKEN_INFORMATION_CLASS
{
TokenUser = 1,
TokenGroups,
TokenPrivileges,
TokenOwner,
TokenPrimaryGroup,
TokenDefaultDacl,
TokenSource,
TokenType,
TokenImpersonationLevel,
TokenStatistics,
TokenRestrictedSids,
TokenSessionId,
TokenGroupsAndPrivileges,
TokenSessionReference,
TokenSandBoxInert,
TokenAuditPolicy,
TokenOrigin
}
[StructLayout(LayoutKind.Sequential)]
public struct LUID
{
public int LowPart;
public int HighPart;
}

[StructLayout(LayoutKind.Sequential)]
public struct TOKEN_PRIVILEGES
{
public LUID Luid;
public int Attributes;
public int PrivilegeCount;
}
struct LUID_AND_ATTRIBUTES
{
public LUID Luid;
public int Attributes;
}
[DllImport("kernel32.dll")]
static extern IntPtr LocalFree(IntPtr hMem);

[DllImport("advapi32.dll", SetLastError=true)]
static extern bool GetTokenInformation(
IntPtr TokenHandle,
TOKEN_INFORMATION_CLASS TokenInformationClass,
IntPtr TokenInformation,
int TokenInformationLength,
out int ReturnLength);
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(48, 136);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(200, 48);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
int TokenInfLength = 0 ;
bool Result ;

// first call gets lenght of TokenInformation
Result = GetTokenInformation( WindowsIdentity.GetCurrent().Token ,
TOKEN_INFORMATION_CLASS.TokenPrivileges , IntPtr.Zero , TokenInfLength
, out TokenInfLength );

IntPtr TokenInformation = Marshal.AllocHGlobal( TokenInfLength ) ;

Result = GetTokenInformation( WindowsIdentity.GetCurrent().Token ,
TOKEN_INFORMATION_CLASS.TokenPrivileges , TokenInformation ,
TokenInfLength , out TokenInfLength ) ;

if( Result )
{

TOKEN_PRIVILEGES
TokenPrivilege=(TOKEN_PRIVILEGES)Marshal.PtrToStru cture(
TokenInformation , typeof( TOKEN_PRIVILEGES ));
LUID_AND_ATTRIBUTES
luid=(LUID_AND_ATTRIBUTES)Marshal.PtrToStructure(T okenInformation ,
typeof( LUID_AND_ATTRIBUTES ));
IntPtr pstr = IntPtr.Zero;

MessageBox.Show(TokenPrivilege.PrivilegeCount.ToSt ring());

LocalFree(pstr);
}
}
}
}

Please help me to solve the problem of checking SE_TCB_NAME Privilege

Regards,
Mani

Jan 4 '06 #1
5 8742


<pl**********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi,

I need to check the SE_TCB_NAME previlige for the current user using
GetTokenInformation api call.i used the api call and get the
privilege,but i am unable to go thru each previlege to check whether
SE_TCB_NAME is enabled or not.my code is below[C#}

Please help me to solve the problem of checking SE_TCB_NAME Privilege

Regards,
Mani

I realy don't know why you need this, after all only "localsystem" should
have this TCB privilege. You don't grant this privilege to all of your users
don't you?

Anyway, you need to call LookupPrivilegeValue, followed by PrivilegeCheck.
Here is a sample...
[StructLayout(LayoutKind.Sequential)]

public struct _PRIVILEGE_SET
{
public uint PrivilegeCount;
public uint Control;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public LUID_AND_ATTRIBUTES[] Privilege_1;
}

[StructLayout(LayoutKind.Sequential)]
public struct LUID_AND_ATTRIBUTES
{
public LUID Luid;
public uint Attributes;
}

[StructLayout(LayoutKind.Sequential)]
public struct LUID
{
public uint LowPart;
public uint HighPart;
}

class Tester
{

[DllImport("advapi32.dll", SetLastError=true)]
public static extern bool PrivilegeCheck( IntPtr ClientToken, IntPtr
RequiredPrivileges, ref int pfResult);

[DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public extern static bool LookupPrivilegeValue(string lpSystemName, string
lpName, IntPtr pLuid);

static void Main()
{
int result = 0;
IntPtr hToken;
string privilege = "SeTcbPrivilege";
_PRIVILEGE_SET ps = new _PRIVILEGE_SET();

ps.PrivilegeCount = 1;
ps.Privilege_1 = new LUID_AND_ATTRIBUTES[1];
int privSize = Marshal.SizeOf(ps.Privilege_1[0]);
IntPtr ptr = IntPtr.Zero;
ptr = Marshal.AllocHGlobal(privSize * (int)ps.PrivilegeCount);

Marshal.StructureToPtr(ps.Privilege_1[0], ptr, false);
if (!LookupPrivilegeValue(null, privilege, ptr))
Console.WriteLine("<LookupPrivilegeValue> Win32 Error {0}",
Marshal.GetLastWin32Error());
ps.Privilege_1[0] =
(LUID_AND_ATTRIBUTES)Marshal.PtrToStructure(ptr,ty peof(LUID_AND_ATTRIBUTES));

Marshal.FreeHGlobal(ptr); // Free alocated mem

IntPtr ptrPs = IntPtr.Zero;
ptrPs = Marshal.AllocHGlobal(64); // 64 byte buffer
Marshal.WriteInt32(ptrPs,(int)ps.PrivilegeCount);
Marshal.WriteInt32((IntPtr)((int)ptrPs + 4),(int)ps.Control); // Hard
coded offset!!!
Marshal.StructureToPtr(ps.Privilege_1[0],(IntPtr)((int) ptrPs + 8),
false);
WindowsIdentity.Impersonate(WindowsIdentity.GetCur rent().Token);
hToken = WindowsIdentity.GetCurrent().Token;
Console.WriteLine("Token used: " + hToken);
if(!PrivilegeCheck(hToken, ptrPs, ref result))
Console.WriteLine("<PrivilegeCheck> Win32 Error {0}",
Marshal.GetLastWin32Error());
Marshal.FreeHGlobal( ptrPs ); // Free allocated mem
Console.WriteLine("Privilege's set '{0}'", Convert.ToBoolean(result));
}
}

Willy.
Jan 4 '06 #2
Hi,
Thanks for your example code to check SE_TCB_NAME previlige.when i run
the example code in windows XP[service pack 2],windows2000
professional[service pack 4].it is always giving false for SE_TCB_NAME
previlige.but in windows xp SE_TCB_NAME previlige is set to true
default.i am running this example as administrator user account.
i need to check username,password supplied by user matches with windows
username,password,for this i used logonuser api .Logonuser api won't
works in windows 2000 due to SE_TCB_NAME previlige,so for windows 2000
i used NetUserChangePassword.Now i need to check whether SE_TCB_NAME
previlige is enabled,if enabled means use logonuser api ,otherwise use
NetUserChangePassword.
Whether my approach is right or wrong.Please help me out to solve
problems.
Regards,
Mani

Willy Denoyette [MVP] wrote:
<pl**********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi,

I need to check the SE_TCB_NAME previlige for the current user using
GetTokenInformation api call.i used the api call and get the
privilege,but i am unable to go thru each previlege to check whether
SE_TCB_NAME is enabled or not.my code is below[C#}

Please help me to solve the problem of checking SE_TCB_NAME Privilege

Regards,
Mani

I realy don't know why you need this, after all only "localsystem" should
have this TCB privilege. You don't grant this privilege to all of your users
don't you?

Anyway, you need to call LookupPrivilegeValue, followed by PrivilegeCheck.
Here is a sample...
[StructLayout(LayoutKind.Sequential)]

public struct _PRIVILEGE_SET
{
public uint PrivilegeCount;
public uint Control;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public LUID_AND_ATTRIBUTES[] Privilege_1;
}

[StructLayout(LayoutKind.Sequential)]
public struct LUID_AND_ATTRIBUTES
{
public LUID Luid;
public uint Attributes;
}

[StructLayout(LayoutKind.Sequential)]
public struct LUID
{
public uint LowPart;
public uint HighPart;
}

class Tester
{

[DllImport("advapi32.dll", SetLastError=true)]
public static extern bool PrivilegeCheck( IntPtr ClientToken, IntPtr
RequiredPrivileges, ref int pfResult);

[DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public extern static bool LookupPrivilegeValue(string lpSystemName, string
lpName, IntPtr pLuid);

static void Main()
{
int result = 0;
IntPtr hToken;
string privilege = "SeTcbPrivilege";
_PRIVILEGE_SET ps = new _PRIVILEGE_SET();

ps.PrivilegeCount = 1;
ps.Privilege_1 = new LUID_AND_ATTRIBUTES[1];
int privSize = Marshal.SizeOf(ps.Privilege_1[0]);
IntPtr ptr = IntPtr.Zero;
ptr = Marshal.AllocHGlobal(privSize * (int)ps.PrivilegeCount);

Marshal.StructureToPtr(ps.Privilege_1[0], ptr, false);
if (!LookupPrivilegeValue(null, privilege, ptr))
Console.WriteLine("<LookupPrivilegeValue> Win32 Error {0}",
Marshal.GetLastWin32Error());
ps.Privilege_1[0] =
(LUID_AND_ATTRIBUTES)Marshal.PtrToStructure(ptr,ty peof(LUID_AND_ATTRIBUTES));

Marshal.FreeHGlobal(ptr); // Free alocated mem

IntPtr ptrPs = IntPtr.Zero;
ptrPs = Marshal.AllocHGlobal(64); // 64 byte buffer
Marshal.WriteInt32(ptrPs,(int)ps.PrivilegeCount);
Marshal.WriteInt32((IntPtr)((int)ptrPs + 4),(int)ps.Control); // Hard
coded offset!!!
Marshal.StructureToPtr(ps.Privilege_1[0],(IntPtr)((int) ptrPs + 8),
false);
WindowsIdentity.Impersonate(WindowsIdentity.GetCur rent().Token);
hToken = WindowsIdentity.GetCurrent().Token;
Console.WriteLine("Token used: " + hToken);
if(!PrivilegeCheck(hToken, ptrPs, ref result))
Console.WriteLine("<PrivilegeCheck> Win32 Error {0}",
Marshal.GetLastWin32Error());
Marshal.FreeHGlobal( ptrPs ); // Free allocated mem
Console.WriteLine("Privilege's set '{0}'", Convert.ToBoolean(result));
}
}

Willy.


Jan 5 '06 #3
On XP and higher, you don't need TCB privileges to call LogonUser, only W2K
has this requirement, so only thing you need to do is check the OS version
and act accordingly.

[pseudo code]
If(OSVersion == W2K)
call NetUserChangePassword
else
call LogonUser
End If

Note that by default, only localsystem (SYSTEM) has TCB privileges (all OS).
That means that 'administrator' is included in the list of tcb privileged
users when it returns 'true' on XP.

Willy.

<pl**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,
Thanks for your example code to check SE_TCB_NAME previlige.when i run
the example code in windows XP[service pack 2],windows2000
professional[service pack 4].it is always giving false for SE_TCB_NAME
previlige.but in windows xp SE_TCB_NAME previlige is set to true
default.i am running this example as administrator user account.
i need to check username,password supplied by user matches with windows
username,password,for this i used logonuser api .Logonuser api won't
works in windows 2000 due to SE_TCB_NAME previlige,so for windows 2000
i used NetUserChangePassword.Now i need to check whether SE_TCB_NAME
previlige is enabled,if enabled means use logonuser api ,otherwise use
NetUserChangePassword.
Whether my approach is right or wrong.Please help me out to solve
problems.
Regards,
Mani

Willy Denoyette [MVP] wrote:
<pl**********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
> Hi,
>
> I need to check the SE_TCB_NAME previlige for the current user using
> GetTokenInformation api call.i used the api call and get the
> privilege,but i am unable to go thru each previlege to check whether
> SE_TCB_NAME is enabled or not.my code is below[C#}
>
> Please help me to solve the problem of checking SE_TCB_NAME Privilege
>
> Regards,
> Mani
>

I realy don't know why you need this, after all only "localsystem" should
have this TCB privilege. You don't grant this privilege to all of your
users
don't you?

Anyway, you need to call LookupPrivilegeValue, followed by
PrivilegeCheck.
Here is a sample...
[StructLayout(LayoutKind.Sequential)]

public struct _PRIVILEGE_SET
{
public uint PrivilegeCount;
public uint Control;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public LUID_AND_ATTRIBUTES[] Privilege_1;
}

[StructLayout(LayoutKind.Sequential)]
public struct LUID_AND_ATTRIBUTES
{
public LUID Luid;
public uint Attributes;
}

[StructLayout(LayoutKind.Sequential)]
public struct LUID
{
public uint LowPart;
public uint HighPart;
}

class Tester
{

[DllImport("advapi32.dll", SetLastError=true)]
public static extern bool PrivilegeCheck( IntPtr ClientToken, IntPtr
RequiredPrivileges, ref int pfResult);

[DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public extern static bool LookupPrivilegeValue(string lpSystemName,
string
lpName, IntPtr pLuid);

static void Main()
{
int result = 0;
IntPtr hToken;
string privilege = "SeTcbPrivilege";
_PRIVILEGE_SET ps = new _PRIVILEGE_SET();

ps.PrivilegeCount = 1;
ps.Privilege_1 = new LUID_AND_ATTRIBUTES[1];
int privSize = Marshal.SizeOf(ps.Privilege_1[0]);
IntPtr ptr = IntPtr.Zero;
ptr = Marshal.AllocHGlobal(privSize * (int)ps.PrivilegeCount);

Marshal.StructureToPtr(ps.Privilege_1[0], ptr, false);
if (!LookupPrivilegeValue(null, privilege, ptr))
Console.WriteLine("<LookupPrivilegeValue> Win32 Error {0}",
Marshal.GetLastWin32Error());
ps.Privilege_1[0] =
(LUID_AND_ATTRIBUTES)Marshal.PtrToStructure(ptr,ty peof(LUID_AND_ATTRIBUTES));

Marshal.FreeHGlobal(ptr); // Free alocated mem

IntPtr ptrPs = IntPtr.Zero;
ptrPs = Marshal.AllocHGlobal(64); // 64 byte buffer
Marshal.WriteInt32(ptrPs,(int)ps.PrivilegeCount);
Marshal.WriteInt32((IntPtr)((int)ptrPs + 4),(int)ps.Control); // Hard
coded offset!!!
Marshal.StructureToPtr(ps.Privilege_1[0],(IntPtr)((int) ptrPs + 8),
false);
WindowsIdentity.Impersonate(WindowsIdentity.GetCur rent().Token);
hToken = WindowsIdentity.GetCurrent().Token;
Console.WriteLine("Token used: " + hToken);
if(!PrivilegeCheck(hToken, ptrPs, ref result))
Console.WriteLine("<PrivilegeCheck> Win32 Error {0}",
Marshal.GetLastWin32Error());
Marshal.FreeHGlobal( ptrPs ); // Free allocated mem
Console.WriteLine("Privilege's set '{0}'", Convert.ToBoolean(result));
}
}

Willy.

Jan 5 '06 #4
Hi,
Thanks for your reply. i have already done the coding by finding the os
version as you mentioned like
f(OSVersion == W2K)
call NetUserChangePassword
else
call LogonUser
End If

I think if the SE_TCB_NAME privilege is enabled in windows 2000,then
why we have to use NetUserChangePassword ,for this reason only i
checked SE_TCB_NAME privilege.Is it correct or not.is SE_TCB_NAME
privilege is enabled default to windows2000 or not?.why it is always
returning true when i check SE_TCB_NAME privilege for windows
2000[service pack 4] under administrative account

Regards,
Mani

Jan 5 '06 #5

<pl**********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi,
Thanks for your reply. i have already done the coding by finding the os
version as you mentioned like
f(OSVersion == W2K)
call NetUserChangePassword
else
call LogonUser
End If

I think if the SE_TCB_NAME privilege is enabled in windows 2000,then
why we have to use NetUserChangePassword ,for this reason only i
checked SE_TCB_NAME privilege.Is it correct or not.is SE_TCB_NAME
privilege is enabled default to windows2000 or not?.why it is always
returning true when i check SE_TCB_NAME privilege for windows
2000[service pack 4] under administrative account

Regards,
Mani


It's possible that 'Administrators' are also members of the TCB, but that's
not the issue. Point is that on W2K a caller needs to be a TCB member to be
able to call LogonUser, on other OS's most users can call LogonUser by
default.
So if you run on W2K you call NetUserChangePassword, or supplementary you
check for TCK privileges, like so:

If(OSVersion == W2K)
If TCB enabled
call LogonUser
Else
call NetUserChangePassword
End If
else
call LogonUser
End If
Willy.
Jan 5 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Peter Schmiedeskamp | last post by:
Hello, I'm writing a python program (in Windows) from which I would like to determine whether or not either: 1. The user has administrative access on the current PC. or (better) 2. The currently...
4
by: Pete Fong | last post by:
Dear all, I am a beginner with Python. I want to write a program as "runas" in Windows XP. But I have got the following error: File...
7
by: Vincent Nguyen | last post by:
Hi, Does anyone know how call Win32 native API GetTokenInformation() by using C#? Any sample code would be helpful. Thanks! Vincent
1
by: dhornyak | last post by:
I have been banging my head against the wall for a while now, and can't seem to id the problem. I've been through a ton of posts and the code doesn't seem any different. Can anybody see it? When...
5
by: Prisy | last post by:
In asp.net(C#) we can get the Process Name of a process using the following code: System.Diagnostics.Process item; item.ProcessName.ToString(); Is there a way to get the User Name of a...
0
by: jlofgren111 | last post by:
I have been trying to get the list of user tokens using VB.Net. I have been searching through the news groups and found some information, but I am having issues getting that working. If I try the...
2
by: eliang | last post by:
after calls to logonuser() impersonateloggedouuser() the call to openprocess() fails with Access is denied. what i need to do to get the access right back to call OpenProcess success.
3
by: =?Utf-8?B?Um9iS2lubmV5MQ==?= | last post by:
Hello, This could be REAL simple, but I cannot find any info on it anywhere after 2 hours of searching. How can I programmatically determine if the UAC is turned in Vista using C#? I need...
33
by: JamesB | last post by:
I am writing a service that monitors when a particular app is started. Works, but I need to get the user who is currently logged in, and of course Environment.UserName returns the service logon...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.