首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >WMI列出Windows中某个目录的用户权限

WMI列出Windows中某个目录的用户权限

作者头像
Java架构师必看
发布于 2021-03-22 15:35:42
发布于 2021-03-22 15:35:42
2.6K0
举报
文章被收录于专栏:Java架构师必看Java架构师必看

using System;

using System.Management;

using System.Collections;

class Tester

{

public static void Main()

{

try

{

ManagementPath path = new ManagementPath( );

path.Server = ".";

path.NamespacePath = @"root/cimv2";

path.RelativePath = @"Win32_LogicalFileSecuritySetting.Path='c://test'"; // using tmp as folder name

ManagementObject lfs = new ManagementObject(path);

// Dump all trustees (this includes owner)

foreach (ManagementBaseObject b in lfs.GetRelated())

Console.WriteLine("Trustee: {0} /t SID [{1}]", b["AccountName"], b["SID"]);

// Get the security descriptor for this object

ManagementBaseObject outParams = lfs.InvokeMethod("GetSecurityDescriptor", null, null);

if (((uint)(outParams.Properties["ReturnValue"].Value)) == 0)

{

ManagementBaseObject Descriptor = ((ManagementBaseObject)(outParams.Properties["Descriptor"].Value));

ManagementBaseObject[] DaclObject = ((ManagementBaseObject[])(Descriptor.Properties["Dacl"].Value));

DumpACEs(DaclObject);

ManagementBaseObject OwnerObject = ((ManagementBaseObject)(Descriptor.Properties["Owner"].Value));

DumpOwnerProperties(OwnerObject.Properties); // Show owner properies

}

}

catch(Exception e)

{

Console.WriteLine(e);

Console.ReadLine();

}

}

static void DumpACEs(ManagementBaseObject[] DaclObject)

{

// ACE masks see: winnt.h

string[] filedesc = {"FILE_READ_DATA", "FILE_WRITE_DATA", "FILE_APPEND_DATA", "FILE_READ_EA",

"FILE_WRITE_EA", "FILE_EXECUTE", "FILE_DELETE_CHILD", "FILE_READ_ATTRIBUTES",

"FILE_WRITE_ATTRIBUTES", " ", " ", " ",

" ", " ", " ", " ",

"DELETE ", "READ_CONTROL", "WRITE_DAC", "WRITE_OWNER",

"SYNCHRONIZE ", " ", " "," ",

"ACCESS_SYSTEM_SECURITY", "MAXIMUM_ALLOWED", " "," ",

"GENERIC_ALL", "GENERIC_EXECUTE", "GENERIC_WRITE","GENERIC_READ"};

foreach(ManagementBaseObject mbo in DaclObject)

{

Console.WriteLine("-------------------------------------------------");

Console.WriteLine("mask: {0:X} - aceflags: {1} - acetype: {2}", mbo["AccessMask"], mbo["AceFlags"], mbo["AceType"]);

// Access allowed/denied ACE

if(mbo["AceType"].ToString() == "1")

Console.WriteLine("DENIED ACE TYPE");

else

Console.WriteLine("ALLOWED ACE TYPE");

// Dump trustees

ManagementBaseObject Trustee = ((ManagementBaseObject)(mbo["Trustee"]));

Console.WriteLine("Name: {0} - Domain: {1} - SID {2}/n",

Trustee.Properties["Name"].Value,

Trustee.Properties["Domain"].Value,

Trustee.Properties["SIDString"].Value);

// Dump ACE mask in readable form

UInt32 mask = (UInt32)mbo["AccessMask"];

int[] m = {(int)mask};

BitArray ba = new BitArray(m);

int i = 0;

IEnumerator baEnum = ba.GetEnumerator();

while ( baEnum.MoveNext() )

{

if((bool)baEnum.Current)

Console.WriteLine( "/t[{0}]", filedesc[i]);

i++;

}

}

}

static void DumpOwnerProperties(PropertyDataCollection Owner)

{

Console.WriteLine("=============== Owner Properties ========================");

Console.WriteLine();

Console.WriteLine("Domain {0} /tName {1}",Owner["Domain"].Value, Owner["Name"].Value);

Console.WriteLine("SID /t{0}",Owner["SidString"].Value);

Console.ReadLine();

}

}

// 

本文由来源 21aspnet,由 javajgs_com 整理编辑,其版权均为 21aspnet 所有,文章内容系作者个人观点,不代表 Java架构师必看 对观点赞同或支持。如需转载,请注明文章来源。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档