枚举Windows上低权限的共享文件夹

发布于 2024-08-31 02:59:47 字数 859 浏览 4 评论 0原文

使用 C++ (VS2008),我需要能够枚举当前计算机上的所有共享文件夹并获取或构造本地和远程名称。

我们一直在使用 NetShareEnum 这相当成功,但遇到了一个问题,我们需要使用低权限的用户帐户运行。

要使用 NetShareEnum 获取本地路径,我们至少需要检索 SHARE_INFO_2 结构 - 但这需要“管理员、高级用户、打印操作员或服务器操作员组成员身份”。

我一直在尝试使用 WNetOpenEnumWNetEnumResource 相反,但我似乎也没有获得共享的本地名称。 AFAICS只是从外部角度列举了股份。

因此,我要么需要有关 WNetEnumResource 出错的地方的帮助,要么需要有关另一种方法的建议。非常感谢任何建议。

Using C++ (VS2008) I need to be able to enumerate all shared folders on the current machine and get or construct the local and remote names.

We've been using NetShareEnum for this fairly successfully, but have hit a problem where we need to run with a user account with low privileges.

To get the local path using NetShareEnum we need to retrieve at least SHARE_INFO_2 structures - but that requires "Administrator, Power User, Print Operator, or Server Operator group membership".

I've been trying to use WNetOpenEnum and WNetEnumResource instead but I don't seem to be getting the local name back for that for shares either. AFAICS it only enumerates the shares from an external perspective.

So I'd either like help on where I'm going wrong with WNetEnumResource, or a suggestion as to another way of doing this. Any suggestions are much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

峩卟喜欢 2024-09-07 02:59:47

如果您知道本地路径是什么,您可以通过测试来暴力破解它:(TODO:将所有内容设为 Unicode)

// Helper function to make a name unique.
std::string computername()
{
    static std::string name = []() {
        WCHAR buf[MAX_COMPUTERNAME_LENGTH];
        DWORD len = MAX_COMPUTERNAME_LENGTH;
        GetComputerNameEx(ComputerNameNetBIOS, buf, &len);
        return std::string(buf, buf + len);
    }();
    return name;
}

int main()
{
    std::string dir = "C:\\FindThisShare\\";
    // First, create marker
    std::string testfile = computername() + " 038D2B86-7459-417B-9179-90CEECC6EC9D.txt";
    std::ofstream test(dir + testfile);
    test << "This directory holds files from " << computername() 
         << std::endl;
    test.close();

    // Next, find the UNC path holding it.
    BYTE* buf;
    DWORD numEntries;
    NetShareEnum(nullptr, 1, &buf, MAX_PREFERRED_LENGTH, &numEntries,
                 &numEntries, nullptr);
    auto data = reinterpret_cast<SHARE_INFO_1*>(buf);
    std::string retval;
    for (int i = 0; i != numEntries; ++i)
    {
        auto const& entry = data[i];
        std::wstring tmp(entry.shi1_netname);
        std::string share(tmp.begin(), tmp.end());
        std::string uncdir = "\\\\" + computername() + "\\" + share + "\\";
        if (PathFileExistsA((uncdir + testfile).c_str()))
        {
            printf("Exists");
        }
    }
    remove((dir + testfile).c_str());
    NetApiBufferFree(buf);
}

If you have an idea what the local path would be, you can brute-force it by testing: (TODO: make everything Unicode)

// Helper function to make a name unique.
std::string computername()
{
    static std::string name = []() {
        WCHAR buf[MAX_COMPUTERNAME_LENGTH];
        DWORD len = MAX_COMPUTERNAME_LENGTH;
        GetComputerNameEx(ComputerNameNetBIOS, buf, &len);
        return std::string(buf, buf + len);
    }();
    return name;
}

int main()
{
    std::string dir = "C:\\FindThisShare\\";
    // First, create marker
    std::string testfile = computername() + " 038D2B86-7459-417B-9179-90CEECC6EC9D.txt";
    std::ofstream test(dir + testfile);
    test << "This directory holds files from " << computername() 
         << std::endl;
    test.close();

    // Next, find the UNC path holding it.
    BYTE* buf;
    DWORD numEntries;
    NetShareEnum(nullptr, 1, &buf, MAX_PREFERRED_LENGTH, &numEntries,
                 &numEntries, nullptr);
    auto data = reinterpret_cast<SHARE_INFO_1*>(buf);
    std::string retval;
    for (int i = 0; i != numEntries; ++i)
    {
        auto const& entry = data[i];
        std::wstring tmp(entry.shi1_netname);
        std::string share(tmp.begin(), tmp.end());
        std::string uncdir = "\\\\" + computername() + "\\" + share + "\\";
        if (PathFileExistsA((uncdir + testfile).c_str()))
        {
            printf("Exists");
        }
    }
    remove((dir + testfile).c_str());
    NetApiBufferFree(buf);
}
2024-09-07 02:59:47

听起来您正在为范围指定 RESOURCE_GLOBALNET ,但是在不查看代码的情况下很难判断出了什么问题。

如果不知道你尝试了什么以及你得到了什么,就很难提供帮助。例如,您期望什么本地名称以及返回什么?您是否尝试过将 NetShareEnum 与 SHARE_INFO_503 一起使用?

It sounds like you're specifying RESOURCE_GLOBALNET for the scope, but it's hard to tell what's wrong without seeing your code.

It's hard to help without knowing what you tried and what you got back. For example, what local name are you expecting and what is getting returned? Did you try NetShareEnum with SHARE_INFO_503?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文