GetTokenInformation

This is a discussion on GetTokenInformation within the Windows Programming forums, part of the Platform Specific Boards category; Hi, hopefully somebody here can help me with this question. I am trying to build a client/server program where the ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Champaign, Illinois, United States
    Posts
    27

    GetTokenInformation

    Hi, hopefully somebody here can help me with this question. I am trying to build a client/server program where the client asks the server to serve a file. Its really a lab assignment where we are supposed to learn about secure programming in windows. Anyway, we are supposed to get the server processes' privilege information, log it, and then change it. I am running into the problem trying to get the information. Here is the relevant bit of code:
    Code:
    	HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, TRUE, GetCurrentProcessId());
    	HANDLE hToken;
    	if(OpenProcessToken(
    		hProc,
    		TOKEN_QUERY,
    		&hToken) == 0)
    	{
    		printf("I was unable to execute OpenProcessToken\r\nErrorCode: %d\r\n", GetLastError());
    		return 1;
    	}
    
    
        DWORD SizeReturned = 0;
    
    	// Pass NULL pointer to return the size needed
    	if(!GetTokenInformation(&hToken, TokenPrivileges, NULL, SizeReturned, &SizeReturned))
    	{
    		printf("1 GetTokenInformation failed. We needed a buffer this big: %d.\r\n", SizeReturned);
    		printf("Error Code: %d\r\n", GetLastError());
    		return 1;
    	}
    
    	TOKEN_PRIVILEGES TokenPrivilegesType = *(PTOKEN_PRIVILEGES)malloc(SizeReturned);
    
    	if(!GetTokenInformation(&hToken, TokenPrivileges, &TokenPrivilegesType, SizeReturned, &SizeReturned))
    	{
    		printf("2 GetTokenInformation failed. We needed a buffer this big: %d. But ours was this big: %d\r\n", SizeReturned, sizeof(TokenPrivilegesType));
    		printf("Error Code: %d\r\n", GetLastError());
    		return 1;
    	}
    The first time that I call GetTokenInformation is where it fails. GetLastError() returns a value of 6, which if I am not mistaken means an invalid handle. I think I understand the process that I am supposed to do but I must be having a hard time understanding the API. Any suggestions? I can provide more information if you need. Thanks in advance for the help!

  2. #2
    Registered User
    Join Date
    Jul 2011
    Location
    Champaign, Illinois, United States
    Posts
    27
    OK, I looked at the documentation again. The first call I have to GetTokenInformation() will always fail because of the arguments that I am passing in. Every function call up to that point I know is working, or at least does not return a fail value. My next question is: How do I allocate the TOKEN_PRIVILEGES data structure in order to be sure that I have enough space for everything? I want to get access to all of the privileges. I also changed the error checking to check for == 0 rather than just !GetTokenInformation(). Still fails.
    Last edited by breimer273; 02-02-2012 at 06:40 AM. Reason: Mistake

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    You've almost got it.
    Remove the & from the &hToken in both GetTokenInformation (that's where error 6 comes from as you're inadvertantly passing the wrong thing, the API takes HANDLE not a HANDLE*)
    Remove the * from the malloc line and make TokenPrivilegesType a pointer
    Remove the & on TokenPrivilegesType from the second GetTokenInformation
    And you're good to go

    Unless you're creating child processes and want the handle to be inherited, you can simplify
    HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, TRUE, GetCurrentProcessId());
    to
    HANDLE hProc = GetCurrentProcess();

    Just in case, don't forget to CloseHandle hToken when you're finished with it

  4. #4
    Registered User
    Join Date
    Jul 2011
    Location
    Champaign, Illinois, United States
    Posts
    27
    Quote Originally Posted by adeyblue View Post
    You've almost got it.
    Remove the & from the &hToken in both GetTokenInformation (that's where error 6 comes from as you're inadvertantly passing the wrong thing, the API takes HANDLE not a HANDLE*)
    Remove the * from the malloc line and make TokenPrivilegesType a pointer
    Remove the & on TokenPrivilegesType from the second GetTokenInformation
    And you're good to go

    Unless you're creating child processes and want the handle to be inherited, you can simplify
    HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, TRUE, GetCurrentProcessId());
    to
    HANDLE hProc = GetCurrentProcess();

    Just in case, don't forget to CloseHandle hToken when you're finished with it
    Wow.... THANKS!! I love and hate how its the mistakes like these that keep me up at night.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GetTokenInformation() confusion
    By Necrofear in forum Windows Programming
    Replies: 16
    Last Post: 12-14-2009, 11:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21