I have the following code snippet written in C which is supposed to retrieve token information about the program privileges:
char buffer[1000]; //Declaring the buffer where the token information will be stored
int size = sizeof(buffer); //Storing the size of the buffer
DWORD required_size; //A variable storing return information from the GetTokenInformation() method
HANDLE process = OpenProcess(SYNCHRONIZE, FALSE, GetCurrentProcessId()); //Opening the current process
HANDLE token; //Creating a handle for the token
OpenProcessToken(process, TOKEN_ADJUST_PRIVILEGES, &token); //Opening the process token
GetTokenInformation(token, TokenPrivileges, buffer, size, &required_size); //Obtaining the token information
printf("The following information was obtained with regards to the token privileges: \n\n");
printf("%s\n\n", buffer);
The main problem is that when I try to print the contents of ‘buffer’, only rubbish information is printed on the screen :s How can I solve this please?
It’s printing rubbish because the data you haven’t isn’t a C string, it’s just raw binary data. In the case of
TokenPrivileges, the data is really aTOKEN_PRIVILEGESstructure.So, you should cast the byte buffer into a pointer to a
TOKEN_PRIVILEGESstructure. Note that it ends with a flexible array member — thePrivilegesarray will contain a variable number of array elements, which is why you have to query the total size and use a large enough byte buffer instead of just allocating aTOKEN_PRIVILEGESon the stack, which wouldn’t be large enough to hold more than one entry.