接收用户所属的所有成员身份组的 SID?



我正在使用以下代码来检索进程所有者的 SID,这里一切都很好,但我该怎么做 可能至少检索进程所有者所属的任何(最多每个(成员身份 SID?

PSID g_pSID;
BOOL GetCurrentProcessSID()
{
DWORD dwSize = 0, dwError, dwResult = 0;
HANDLE hToken;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
{
printf("OpenProcessToken Error %un", GetLastError());
return FALSE;
}
// Call GetTokenInformation to get the buffer size.
TOKEN_USER tU;
if (!GetTokenInformation(hToken, TokenUser, &tU, 0, &dwSize))
{
dwError = GetLastError();
if (dwError != ERROR_INSUFFICIENT_BUFFER)
{
std::cout << "GetTokenInformation failed, error " << dwError;
CloseHandle(hToken);
return 0;
}
}
PTOKEN_OWNER to = (PTOKEN_OWNER)LocalAlloc(LPTR, dwSize);
if (!to)
{
dwError = GetLastError();
std::cout << "LocalAlloc failed, error " << dwError;
CloseHandle(hToken);
return 0;
}
if (!GetTokenInformation(hToken, TokenOwner, to, dwSize, &dwSize))
{
dwError = GetLastError();
std::cout << "GetTokenInformation failed, error " << dwError;
LocalFree(to);
CloseHandle(hToken);
return 0;
}
g_pSID = to->Owner;
return TRUE;
}

另外,除了使用全局变量之外,我在片段中是否有任何错误,有什么建议吗?

首先使用TokenUser获取TokenInformation的缓冲区大小,然后在第二个调用方中使用TokenOwner。不确定你真正想要哪个。 有一个很好的解释。

TokenOwner是令牌中确定默认值的部分 由令牌中运行的进程或线程创建的对象的所有者 安全上下文。TokenUser是令牌的用户 代表。

此外,应在函数返回之前调用LocalFree(to)

如果要获取与令牌关联的组帐户。您可以在拨打GetTokenInformationTokenGroups获得它。

#define MAX_NAME 256
BOOL RetriveGroupSid(VOID)
{
DWORD i, dwSize = 0, dwResult = 0;
HANDLE hToken;
PTOKEN_GROUPS pGroupInfo;
SID_NAME_USE SidType;
char lpName[MAX_NAME];
char lpDomain[MAX_NAME];
SID_IDENTIFIER_AUTHORITY SIDAuth = SECURITY_NT_AUTHORITY;
// Open a handle to the access token for the calling process.
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
{
printf("OpenProcessToken Error %un", GetLastError());
return FALSE;
}
// Call GetTokenInformation to get the buffer size.
if (!GetTokenInformation(hToken, TokenGroups, NULL, dwSize, &dwSize))
{
dwResult = GetLastError();
if (dwResult != ERROR_INSUFFICIENT_BUFFER) {
printf("GetTokenInformation Error %un", dwResult);
return FALSE;
}
}
// Allocate the buffer.
pGroupInfo = (PTOKEN_GROUPS)GlobalAlloc(GPTR, dwSize);
// Call GetTokenInformation again to get the group information.
if (!GetTokenInformation(hToken, TokenGroups, pGroupInfo,
dwSize, &dwSize))
{
printf("GetTokenInformation Error %un", GetLastError());
return FALSE;
}
for (i = 0; i < pGroupInfo->GroupCount; i++)
{
dwSize = MAX_NAME;
LPSTR sid;
if (!ConvertSidToStringSid(pGroupInfo->Groups[i].Sid, &sid))
{
printf("ConvertSidToStringSid Error %un", GetLastError());
return FALSE;
}
if (!LookupAccountSid(NULL, pGroupInfo->Groups[i].Sid,
lpName, &dwSize, lpDomain,
&dwSize, &SidType))
{
dwResult = GetLastError();
if (dwResult == ERROR_NONE_MAPPED)
strcpy_s(lpName, dwSize, "NONE_MAPPED");
else
{
printf("LookupAccountSid Error %un", GetLastError());
LocalFree(sid);
return FALSE;
}
}
printf("%s : %s\%s n", sid, lpDomain, lpName);

// Find out whether the SID is enabled in the token.
if (pGroupInfo->Groups[i].Attributes & SE_GROUP_ENABLED)
printf("The group SID is enabled.n");
else if (pGroupInfo->Groups[i].Attributes &
SE_GROUP_USE_FOR_DENY_ONLY)
printf("The group SID is a deny-only SID.n");
else
printf("The group SID is not enabled.n");
LocalFree(sid);
}
if (pGroupInfo)
GlobalFree(pGroupInfo);
return TRUE;
}

有关另一种方法,请参阅另请参阅检索用户所属的所有组...在C++

相关内容

  • 没有找到相关文章

最新更新