`
leonardleonard
  • 浏览: 823824 次
社区版块
存档分类
最新评论

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

阅读更多
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();

}

}

// 

 
分享到:
评论

相关推荐

    windows命令资源全集

    以上列出的命令覆盖了文件管理、系统管理、服务配置、任务调度等多个方面,是Windows系统用户日常管理和维护计算机的重要工具。通过掌握这些命令,用户可以更高效地完成任务,解决系统问题,以及进行系统级别的管理...

    任务管理器(仿windows)

    1. **进程查看**:仿制的C#任务管理器首先能够列出当前系统上运行的所有进程。这涉及到获取系统进程列表,可以通过Process.GetProcesses()方法实现。每个进程包括进程ID、进程名、CPU占用率、内存使用情况等信息,...

    C#windows任务管理器

    在C#编程环境中,开发一款类似Windows任务管理器的应用程序是一项复杂的挑战,涉及到多个系统级别的交互和用户界面设计。在本篇文章中,我们将深入探讨如何使用C#和Visual Studio 2008来实现这样的功能。 首先,让...

    Delphi实现禁用和启用网卡

    开发者可能已经列出了步骤,或者提供了特别注意事项,比如需要连接多块网卡来进行测试。 以上就是关于“Delphi实现禁用和启用网卡”项目中的主要知识点,涵盖了从API调用到用户界面交互的整个流程。理解并掌握这些...

    Windows命令使用大全.txt

    - **功能介绍**:设备管理器,列出并管理已安装的硬件设备。 - **应用场景**:查看硬件设备的状态,禁用或启用设备,更新驱动程序等。 #### 20. dfrg.msc - **功能介绍**:磁盘碎片整理工具,用于优化磁盘读写性能...

    Microsoft SQL Server 2005 Express Edition SP3

    4.3 WMI 提供程序和用户实例功能 4.4 联机丛书 4.5 获取 SQL Server Express 帮助 4.6 新闻组支持 4.7 提供有关 SQL Server Express 的反馈 5.0 数据库引擎 6.0 复制 [顶部] 1.0 简介 Microsoft SQL ...

    cmd操作命令和linux命令大全收集

    40. wmimgmt.msc----打开windows管理体系结构(WMI) 41. wupdmgr--------windows更新程序 42. wscript--------windows脚本宿主设置 43. write----------写字板 45. wiaacmgr-------扫描仪和照相机向导 46. ...

    基于python监控程序是否关闭

    这个例子展示了如何通过`GetObject`获取WMI实例,并执行查询来列出所有运行的进程: ```python from win32com.client import GetObject _wmi = GetObject('winmgmts:') processes = _wmi.ExecQuery('Select * from...

    List Information About the Binary Files Used by an Application

    ### 如何列出应用程序所使用的二进制文件信息 在IT领域中,经常需要对系统进行维护、调试或安全审查,这时候了解某个特定应用程序所依赖的所有二进制文件(包括但不限于可执行文件、图标、位图等)的具体信息就显得...

    cmd运行命令大全

    - **应用场景**: 在企业环境中非常有用,管理员可以通过它来配置各种系统级或用户级的安全策略。 #### 2. sndrec32 —— 录音机 - **功能**: 启动录音机程序,用于录制音频文件。 - **应用场景**: 适合快速录制简短...

Global site tag (gtag.js) - Google Analytics