#include <windows.h>
#include <stdio.h>
#pragma hdrstop

void main()
{
	HANDLE hToken;
	LUID setcbnameValue;
	TOKEN_PRIVILEGES tkp;
	DWORD errcod;
	LPVOID lpMsgBuf;
	LPCTSTR msgptr;

	UCHAR InfoBuffer[1000];
	PTOKEN_PRIVILEGES ptgPrivileges = (PTOKEN_PRIVILEGES) InfoBuffer;
	DWORD dwInfoBufferSize;
	DWORD dwPrivilegeNameSize;
	DWORD dwDisplayNameSize;
	UCHAR ucPrivilegeName[500];
	UCHAR ucDisplayName[500];
	DWORD dwLangId;
	UINT i;

	if ( ! OpenProcessToken( GetCurrentProcess(),
		TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) )
	{
		puts( "OpenProcessToken" );
		return;
	}

	// ---------------------------------------------------------------------
	// enumerate currently held privs (NOTE: not *enabled* privs, just the
	// ones you _could_ enable as in the last part)

	GetTokenInformation( hToken, TokenPrivileges, InfoBuffer,
		sizeof InfoBuffer, &dwInfoBufferSize);

	printf( "Account privileges: \n\n" );
	for( i = 0; i < ptgPrivileges->PrivilegeCount; i ++ )
	{
		dwPrivilegeNameSize = sizeof ucPrivilegeName;
		dwDisplayNameSize = sizeof ucDisplayName;
		LookupPrivilegeName( NULL, &ptgPrivileges->Privileges[i].Luid,
			ucPrivilegeName, &dwPrivilegeNameSize );
		LookupPrivilegeDisplayName( NULL, ucPrivilegeName,
			ucDisplayName, &dwDisplayNameSize, &dwLangId );
		printf( "%40s (%s)\n", ucDisplayName, ucPrivilegeName );
	}

	//----------------------------------------------------------------------
	// enable SeTcbPrivilege: lookup, adjust token privs

	if ( !LookupPrivilegeValue( NULL, SE_TCB_NAME, &setcbnameValue ) )
	{
		puts( "LookupPrivilegeValue" );
		return;
	}

	tkp.PrivilegeCount = 1;
	tkp.Privileges[0].Luid = setcbnameValue;
	tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

	AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp,
		NULL, NULL );

	errcod = GetLastError();
	if ( errcod != ERROR_SUCCESS )
	{
		FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
			FORMAT_MESSAGE_FROM_SYSTEM, NULL, errcod,
			MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
			(LPTSTR) &lpMsgBuf, 0, NULL );

		msgptr = (LPCTSTR) lpMsgBuf;
		printf( "err %d: %s\n", errcod, msgptr );
		return;
	}
}


