[英]Querying access to a UNC path on a remote machine via WMI
我想確定遠程主機是否具有對網絡共享的讀寫訪問權限。 首先,我想看看是否可以查詢目標主機查詢UNC路徑以獲取信息的能力,ala
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();
}
}
}
因此看起來這基本上是不可能的,因為出於安全原因WMI將您鎖定在網絡訪問之外。 看來,最好的選擇是一次性獲得WinRM或PsExec。 如果這是您唯一的訪問路徑,則可以通過WMI啟用WinRM,但是我想這種能力可以被組策略阻止。 第三種選擇是編寫自己的Windows服務,該服務將響應請求,並在有訪問權限的情況下通過WMI安裝該服務。
簡而言之:我的問題的答案是“否”。請使用WinRm,PsExec或自定義的win-service解決方案。
我知道這是一個老問題,但是對於希望這樣做的任何人,以下代碼都可以工作。 (我知道這不是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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.