The following example uses the OpenProcessToken and GetTokenInformation functions to get the group memberships in an access token.
The GetTokenInformation function retrieves a specified type of information about an access token. The calling process must have appropriate access rights to obtain the information.
参考:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa379554%28v=vs.85%29.aspx
The OpenProcessToken function opens the access token associated with a process.
参考:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa379295%28v=vs.85%29.aspx
参数如下:
<span>BOOL WINAPI GetTokenInformation(
_In_ HANDLE TokenHandle,
_In_ TOKEN_INFORMATION_CLASS TokenInformationClass,
_Out_opt_ LPVOID TokenInformation,
_In_ DWORD TokenInformationLength,
_Out_ PDWORD ReturnLength
);</span>
The AllocateAndInitializeSid function allocates and initializes a security identifier (SID) with up to eight subauthorities.
参考:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa375213%28v=vs.85%29.aspx
参数如下:
pIdentifierAuthority [<span>in</span><span>]
A pointer to a SID_IDENTIFIER_AUTHORITY structure. This structure provides the top</span>-level identifier authority value to <span>set</span><span>in</span><span> the SID.
nSubAuthorityCount [</span><span>in</span><span>]
Specifies the number of subauthorities to place </span><span>in</span> the SID. This parameter also identifies how many of the subauthority parameters have meaningful values. This parameter must contain a value <span>from</span><span>1</span> to <span>8</span><span>.
For example, a value of </span><span>3</span><span> indicates that the subauthority values specified by the dwSubAuthority0, dwSubAuthority1, and dwSubAuthority2 parameters have meaningful values and to ignore the remainder.
dwSubAuthority0 [</span><span>in</span><span>]
Subauthority value to place </span><span>in</span><span> the SID.
pSid [</span><span>out</span><span>]
A pointer to a variable that receives the pointer to the allocated and initialized SID structure.</span>
access token含义:
参考:
An access token contains the security information for a logon session. The system creates an access token when a user logs on, and every process executed on behalf of the user has a copy of the token. The token identifies the user, the user‘s groups, and the user‘s privileges. The system uses the token to control access to securable objects and to control the ability of the user to perform various system-related operations on the local computer. There are two kinds of access token, primary and impersonation.
SID含义:
The system uses the SID in the access token to identify the user in all subsequent interactions with Windows security.
参考:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa379571%28v=vs.85%29.aspx
#include <windows.h><span>
#include </span><stdio.h>
<span>#pragma</span> comment(lib, "advapi32.lib")
<span>#define</span> MAX_NAME 256
<span>using</span><span>namespace</span><span> std;
</span><span>int</span><span> main()
{
DWORD i, dwSize </span>= <span>0</span>, dwResult = <span>0</span><span>;
HANDLE hToken;
PTOKEN_GROUPS pGroupInfo;
PSID pSID </span>=<span> NULL;
SID_IDENTIFIER_AUTHORITY SIDAuth </span>=<span> SECURITY_NT_AUTHORITY;
</span><span>char</span><span> lpName[MAX_NAME];
</span><span>char</span><span> lpDomain[MAX_NAME];
SID_NAME_USE SidType;
</span><span>//</span><span> Open a handle to the access token for the calling process.
</span><span>//</span><span>TOKEN_QUERY:Required to query an access token.
</span><span>//</span><span>GetCurrentProcess()返回进程句柄
</span><span>//</span><span>[out]hToken是access token的句柄</span><span>if</span> (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &<span>hToken))
{
printf(</span><span>"</span><span>OpenProcessToken Error %u
</span><span>"</span><span>, GetLastError());
</span><span>return</span><span> FALSE;
}
</span><span>//</span><span>前后两次调用GetTokenInformation的目的不同
</span><span>//</span><span> Call GetTokenInformation to get the buffer size.
</span><span>//</span><span>The TOKEN_GROUPS structure contains information about the group security identifiers (SIDs) in an access token.</span><span>if</span> (!GetTokenInformation(hToken, TokenGroups, NULL, dwSize, &<span>dwSize))
{
dwResult </span>=<span> GetLastError();
</span><span>if</span> (dwResult !=<span> ERROR_INSUFFICIENT_BUFFER) {
printf(</span><span>"</span><span>GetTokenInformation Error %u
</span><span>"</span><span>, dwResult);
</span><span>return</span><span> FALSE;
}
}
</span><span>//</span><span> Allocate the buffer.</span>
pGroupInfo =<span> (PTOKEN_GROUPS)GlobalAlloc(GPTR, dwSize);
</span><span>//</span><span> Call GetTokenInformation again to get the group information.</span><span>if</span> (!<span>GetTokenInformation(hToken, TokenGroups, pGroupInfo,
dwSize, </span>&<span>dwSize))
{
printf(</span><span>"</span><span>GetTokenInformation Error %u
</span><span>"</span><span>, GetLastError());
</span><span>return</span><span> FALSE;
}
</span><span>//</span><span> Create a SID for the BUILTINAdministrators group.</span><span>if</span> (!AllocateAndInitializeSid(&SIDAuth, <span>2</span><span>,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
</span><span>0</span>, <span>0</span>, <span>0</span>, <span>0</span>, <span>0</span>, <span>0</span><span>,
</span>&<span>pSID))
{
printf(</span><span>"</span><span>AllocateAndInitializeSid Error %u
</span><span>"</span><span>, GetLastError());
</span><span>return</span><span> FALSE;
}
</span><span>//</span><span> Loop through the group SIDs looking for the administrator SID.
</span><span>//
</span><span>for</span> (i = <span>0</span>; i < pGroupInfo->GroupCount; i++<span>)
{
</span><span>if</span> (EqualSid(pSID, pGroupInfo-><span>Groups[i].Sid))
{
</span><span>//</span><span> Lookup the account name and print it.</span><span>
dwSize </span>=<span> MAX_NAME;
</span><span>if</span> (!LookupAccountSid(NULL, pGroupInfo-><span>Groups[i].Sid,
lpName, </span>&<span>dwSize, lpDomain,
</span>&dwSize, &<span>SidType))
{
dwResult </span>=<span> GetLastError();
</span><span>if</span> (dwResult ==<span> ERROR_NONE_MAPPED)
strcpy_s(lpName, dwSize, </span><span>"</span><span>NONE_MAPPED</span><span>"</span><span>);
</span><span>else</span><span>
{
printf(</span><span>"</span><span>LookupAccountSid Error %u
</span><span>"</span><span>, GetLastError());
</span><span>return</span><span> FALSE;
}
}
printf(</span><span>"</span><span>Current user is a member of the %s%s group
</span><span>"</span><span>,
lpDomain, lpName);
</span><span>//</span><span> Find out whether the SID is enabled in the token.</span><span>if</span> (pGroupInfo->Groups[i].Attributes &<span> SE_GROUP_ENABLED)
printf(</span><span>"</span><span>The group SID is enabled.
</span><span>"</span><span>);
</span><span>else</span><span>if</span> (pGroupInfo->Groups[i].Attributes &<span>
SE_GROUP_USE_FOR_DENY_ONLY)
printf(</span><span>"</span><span>The group SID is a deny-only SID.
</span><span>"</span><span>);
</span><span>else</span><span>
printf(</span><span>"</span><span>The group SID is not enabled.
</span><span>"</span><span>);
}
}
</span><span>if</span><span> (pSID)
FreeSid(pSID);
</span><span>if</span><span> (pGroupInfo)
GlobalFree(pGroupInfo);
system(</span><span>"</span><span>pause</span><span>"</span><span>);
</span><span>return</span><span>0</span><span>;
}</span>
整体流程:
OpenProcessToken:获取token句柄
GetTokenInformation:获取group information
for循环:在group中查找
原文:http://www.cnblogs.com/predator-wang/p/4786837.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!