为什么WinAPI函数:: NetUserGetLocalGroups()不返回所有组?

问题描述 投票:0回答:1

我有一个用户DOMAIN\User.Name,根据whoami /GROUPS有200多个Windows域组。

当我尝试使用WinAPI函数确定该用户的所有组:: NetUserGetLocalGroups()时,我只为该用户提供了27个组:

#include <windows.h> 
#include <lm.h>
#include <string>

int main()
{
  //query the domain server for the groups of the user
  LPGROUP_USERS_INFO_0 pBuf = NULL;
  DWORD dwLevel = 0;
  DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
  DWORD dwEntriesRead = 0;
  DWORD dwTotalEntries = 0;
  NET_API_STATUS nStatus = MAX_NERR;

  std::wstring serverName(L"\\\\MyDomainNameDns");
  std::wstring userName(L"DOMAIN\\user.name");

  nStatus = ::NetUserGetLocalGroups(serverName.c_str(),
    userName.c_str(),
    dwLevel,
    LG_INCLUDE_INDIRECT,
    reinterpret_cast<PBYTE*>(&pBuf),
    dwPrefMaxLen,
    &dwEntriesRead,
    &dwTotalEntries);
}

dwTotalEntries是27,nStatus是0,所以并不是我只获取所有组中的一部分。我也尝试使用::NetUserGetGroups(),但也没有成功。

注意:使用.NET功能System.Security.Principal.WindowsIdentity.GetCurrent().Groups确实给了我所有组。

NetUserGetLocalGroups不返回的原因可能是所有组都是whoami.NET / WindowsIdentity?返回/未返回的组之间可能有什么区别?

windows winapi usergroups
1个回答
0
投票

经过一番挖掘后,我在a windows user group discussion发现了以下声明:

实际上,NetUserGetLocalGroups / NetUserGetGroups不处理Windows 2000本机域中的嵌套组或通用组。从令牌枚举SID是唯一可靠的方法。

换句话说,如果你想要直接的团体会员资格,请使用::NetUserGetLocalGroups / NetUserGetGroups。如果您想知道群组所有权以确定用户是否有权做某事,请使用令牌/ SID方式(例如使用::OpenProcessToken::GetTokenInformation::LookupAccountSid)。

用户位于(域本地)组Employee中。所有员工都可以访问该建筑,因此Employe集团是HasBuildingAccess(域名本地)组的成员。

如果您想知道,用户是否(直接)在Employee组中,请使用::NetUserGetLocalGroups。它包含Employee组,但不包含HasBuildingAccess组。

但是,如果您想知道用户是否有权访问建筑物,请从令牌中枚举SID。它将包含HasBuildingAccess组。

© www.soinside.com 2019 - 2024. All rights reserved.