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!