Querying access to a UNC path on a remote machine

2019-07-24 02:27发布

问题:

I want to find out if the remote host has r/w access to a network share. To start out I wanted to see if I could query the target host's ability to query the UNC path for info, ala

var query = string.Format("select * from CIM_Directory where name = '{0}'", path);

This works fine for local files, e.g.

var path = @"c:\\Windows";

However, I can't figure out an appropriate way of querying a UNC path (e.g. \\foo\bar). The query always returns a blank set. I saw a related question about executing remote files and the solution for that one ended up being PsExec. I was hoping to ideally solve this problem entirely using WMI without having to rely on 3rd party execs, or uploading my own tool to the remote host.

Cheers

Here's a little usage sample of what I am trying to do right now (var values taken out):

using System;
using System.Linq;
using System.Management;

namespace netie
{
    class Program
    {
        static void Main()
        {
            var connection = new ConnectionOptions
            {
                Username = "user",
                Password = "pass",
                Authority = "domain",
                Impersonation = ImpersonationLevel.Impersonate,
                EnablePrivileges = true
            };

            var scope = new ManagementScope("\\\\remote\\root\\CIMV2", connection);
            scope.Connect();

            var path = @"\\\\foo\\bar\\";
            var queryString = string.Format("select * from CIM_Directory where name = '{0}'", path);
            try
            {
                var query = new ObjectQuery(queryString);
                var searcher = new ManagementObjectSearcher(scope, query);

                foreach (var queryObj in searcher.Get().Cast<ManagementObject>())
                {
                    Console.WriteLine("Number of properties: {0}", queryObj.Properties.Count);
                    foreach (var prop in queryObj.Properties)
                    {
                        Console.WriteLine("{0}: {1}", prop.Name, prop.Value);
                    }
                    Console.WriteLine();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            Console.ReadLine();
        }
    }
}

回答1:

So it looks like this is basically impossible as WMI locks you out of network access for security reasons. Looks like your best bet is WinRM or PsExec for one-offs. You can potentially enable WinRM through WMI if that's your only path of access, but I imagine that ability can be blocked by group policies. The third option is to write your own Windows Service that will respond to requests and installing that through WMI if you have the access.

In short: the answer to my question is a No. Use WinRm, PsExec, or a custom win-service solution.



回答2:

I know this is an old question, but for anyone looking to do this, the following code works. (I know that it's not WMI. Given the OP's answer I didn't even try it with WMI, but I shudder to think that people may write a service for something like this.)

if (System.IO.Directory.Exists(@"[SOME UNC PATH]"))
{
    System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(@"[SOME UNC PATH]");
    var securityInfo = info.GetAccessControl();
    var rules = securityInfo.GetAccessRules(
                   true, 
                   true,
                   typeof(System.Security.Principal.SecurityIdentifier));

    foreach (var rule in rules)
    {
        var fileSystemRule = rule as System.Security.AccessControl.FileSystemAccessRule;
        if (ruleastype != null)
        {
            string user = fileSystemRule.IdentityReference.Translate(
                    typeof(System.Security.Principal.NTAccount)).Value;

            System.Diagnostics.Debug.Print("{0} User: {1} Permissions: {2}",
                fileSystemRule.AccessControlType.ToString(),
                user,
                fileSystemRule.FileSystemRights.ToString());
        }
    }
}

When run it produces the following output:

Allow User: Everyone Permissions: ReadAndExecute, Synchronize
Allow User: CREATOR OWNER Permissions: FullControl
Allow User: NT AUTHORITY\SYSTEM Permissions: FullControl
Allow User: BUILTIN\Administrators Permissions: FullControl
Allow User: BUILTIN\Users Permissions: ReadAndExecute, Synchronize


标签: c# wmi unc wql