Home
Archives

Checking for Administrator Privileges in C++

In C++, just checking to see if your process is running under with administrator privileges can be somewhat cumbersome.


To check for admin priv's, you need to:
  • Get the token information for your process.
  • Create a domain admin SID.
  • Check to see if any of your token's groups are equal to the domain admin SID you just created.



// Get the current process's security token
OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_READ, &Token);
GetTokenInformation(Token, TokenGroups, pointer, MY_POINTER_LENGTH, &returnLength);

// Create a domain admin SID
if (!AllocateAndInitializeSid (&sepNTAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, 
   DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdminSID))
     return false;

// Check to see if any of your token's groups are equal
if (pointer) 
{
	 for (i = 0; i < pointer->GroupCount; i++)
	 {
		  if (EqualSid(pointer->Groups[i].Sid, AdminSID))
		  {			   
			   free(pointer);
			   FreeSid(AdminSID);
			   // Is an admin
			   return true;
		  }
	 }
	 free(pointer);
 }
FreeSid(AdminSID);

// not an admin
return false;



-JP