2014-06-18 64 views
2

我想知道遠程主機是否有r/w訪問網絡共享的權限。要開始了,我想看看我是否能查詢目標主機來查詢信息的UNC路徑的能力,ALA查詢通過WMI訪問遠程計算機上的UNC路徑

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

這正常工作對本地文件,例如

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

但是,我找不出一個合適的查詢UNC路徑(例如\\ foo \ bar)的方法。查詢總是返回一個空白集。我看到一個關於執行遠程文件的相關問題,該解決方案最終成爲PsExec。我希望完全用WMI解決這個問題,而不必依靠第三方執行者,或者將我自己的工具上傳到遠程主機。

乾杯

這裏是什麼,我試圖做的,現在(取出VAR值)有點用法示例:

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(); 
     } 
    } 
} 
+0

根據客戶端主機不能控制對共享的訪問。共享權限和Windows文件系統ACL是爲每個用戶帳戶設置的,所以您的問題沒有任何意義。您可以詢問某個特定用戶帳戶是否有權訪問特定的共享。 –

+0

這正是我想要做的。我需要驗證具有給定主機上的給定一組憑證的用戶是否可以訪問UNC路徑。 – Zoinks

回答

0

所以看起來這基本上是不可能的,因爲WMI鎖定你出去出於安全原因的網絡訪問。看起來你最好的選擇是WinRM或PsExec一次性。如果這是您的唯一訪問路徑,您可以通過WMI啓用WinRM,但我認爲該功能可以被組策略阻止。第三種選擇是編寫自己的Windows服務,如果您有訪問權限,它將響應請求並通過WMI安裝它。

總之:我的問題的答案是否定的。使用WinRm,PsExec或定製的雙贏服務解決方案。

0

我知道這是一個老問題,但對於任何想要這樣做的人來說,下面的代碼都可以工作。 (我知道,這不是WMI。鑑於OP的答案,我甚至沒有與WMI的嘗試,但我不敢想像,人們可以寫一個服務這樣的事情。)

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()); 
     } 
    } 
} 

在運行時它產生以下輸出:

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 
相關問題