简体   繁体   English

使用Java的AclFileAttributeView获取通用文件夹权限(如GENERIC_ALL)

[英]Get generic folder permissions (like GENERIC_ALL) using Java's AclFileAttributeView

I use the AclFileAttributeView from Java7 to read the folder permissions of a Windows directory. 我使用Java7的AclFileAttributeView来读取Windows目录的文件夹权限。 The problem is that I'm not able to get a complete overview because the AclFileAttributeView doesn't return generic permissions like GENERIC_ALL, GENERIC_WRITE, GENERIC_READ and GENERIC_EXECUTE (the four high-order bits in the access mask). 问题是我无法获得完整的概述,因为AclFileAttributeView不返回GENERIC_ALL,GENERIC_WRITE,GENERIC_READ和GENERIC_EXECUTE(访问掩码中的四个高位)等通用权限。 In fact, when it comes to generic permissions it gives me wrong information about other AclEntries for the same member. 实际上,当涉及到通用权限时,它会向我提供有关同一成员的其​​他AclEntries的错误信息。 Let me give an example: 让我举个例子吧:

When I use a tool like AccessChk to list the AclEntries of c:\\windows for the System account I get the following: 当我使用像AccessChk这样的工具列出系统帐户的c:\\ windows的AclEntries时,我得到以下内容:

[2] ACCESS_ALLOWED_ACE_TYPE: NT AUTHORITY\SYSTEM
  FILE_ADD_FILE
  FILE_ADD_SUBDIRECTORY
  FILE_LIST_DIRECTORY
  FILE_READ_ATTRIBUTES
  FILE_READ_EA
  FILE_TRAVERSE
  FILE_WRITE_ATTRIBUTES
  FILE_WRITE_EA
  DELETE
  SYNCHRONIZE
  READ_CONTROL
[3] ACCESS_ALLOWED_ACE_TYPE: NT AUTHORITY\SYSTEM
    [OBJECT_INHERIT_ACE]
    [CONTAINER_INHERIT_ACE]
    [INHERIT_ONLY_ACE]
  GENERIC_ALL

As you can see the first AclEntry only applies to the folder itself and has not the special permissions WRITE_ACL and WRITE_OWNER. 如您所见,第一个AclEntry仅适用于文件夹本身,并且没有特殊权限WRITE_ACL和WRITE_OWNER。 The second AclEntry applies only to subfolders and files and contains the generic permission GENERIC_ALL. 第二个AclEntry仅适用于子文件夹和文件,并包含通用权限GENERIC_ALL。 This is exactly how I see it in the Security tab of Windows Explorer. 这正是我在Windows资源管理器的“安全”选项卡中看到的。 Two records for the System account, one applies only to the folder (with a subset of permissions) and one applies to subfolders/files with Full control. 系统帐户的两个记录,一个仅适用于文件夹(具有权限子集),一个适用于具有完全控制的子文件夹/文件。

Now I run my java program using the following code: 现在我使用以下代码运行我的java程序:

AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class);
System.out.println(view.getAcl());

This gives my the following results for the System account: 这为我的系统帐户提供了以下结果:

  • NT AUTHORITY\\SYSTEM:READ_DATA/WRITE_DATA/APPEND_DATA/READ_NAMED_ATTRS/WRITE_NAMED_ATTRS/EXECUTE/DELETE_CHILD/READ_ATTRIBUTES/WRITE_ATTRIBUTES/DELETE/READ_ACL/WRITE_ACL/WRITE_OWNER/SYNCHRONIZE:ALLOW NT AUTHORITY \\ SYSTEM:READ_DATA / WRITE_DATA / APPEND_DATA / READ_NAMED_ATTRS / WRITE_NAMED_ATTRS / EXECUTE / DELETE_CHILD / READ_ATTRIBUTES / WRITE_ATTRIBUTES / DELETE / READ_ACL / WRITE_ACL / WRITE_OWNER / SYNCHRONIZE:ALLOW
  • NT AUTHORITY\\SYSTEM:FILE_INHERIT/DIRECTORY_INHERIT/INHERIT_ONLY:ALLOW NT AUTHORITY \\ SYSTEM:FILE_INHERIT / DIRECTORY_INHERIT / INHERIT_ONLY:ALLOW

The first AclEntry applies only to the folder itself and contains all the special permissions, including WRITE_ACL and WRITE_OWNER, which is not correct! 第一个AclEntry仅适用于文件夹本身,并包含所有特殊权限,包括WRITE_ACL和WRITE_OWNER,这是不正确的! The second AclEntry doesn't show any permissions, because it has GENERIC_ALL on it! 第二个AclEntry没有显示任何权限,因为它上面有GENERIC_ALL!

I'm not sure where this goes wrong, it seems the JRE just decodes the ACE bitmask given by the OS (sun.nio.fs.WindowsSecurityDescriptor.decode). 我不确定这出错的地方,似乎JRE只是解码操作系统给出的ACE位掩码(sun.nio.fs.WindowsSecurityDescriptor.decode)。

Has anyone experienced these same issues? 有没有人遇到过同样的问题? I will try some other JRE's, perhaps that makes a difference. 我会尝试其他一些JRE,也许这会有所作为。

I too ran into this same issue. 我也遇到了同样的问题。 As it turns out the spec for AclFileAttributeView states it is designed to be compatible with RFC 3530: Network File System (NFS) version 4 Protocol . 事实证明,AclFileAttributeView规范表明它与RFC 3530兼容:网络文件系统(NFS)版本4协议 In RFC 3530, there is no support for GENERIC_* values. 在RFC 3530中,不支持GENERIC_ *值。 I also looked at the JDK code source (From OpenJDK project downloaded from here ) which runs the JVM. 我还查看了运行JVM的JDK代码源(从这里下载的OpenJDK项目 )。 While it seems to me that there would be a way to make the JVM compatible by mapping from the appropriate RFC 3530 flags to and from GENERIC_*, the maintainers clearly have not. 虽然在我看来,有一种方法可以通过从适当的RFC 3530标志映射到GENERIC_ *和从GENERIC_ *映射来使JVM兼容,但维护者显然没有。 This is the reason for your empty entry NT AUTHORITY\\SYSTEM:FILE_INHERIT/DIRECTORY_INHERIT/INHERIT_ONLY:ALLOW 这是你的空入口NT AUTHORITY \\ SYSTEM:FILE_INHERIT / DIRECTORY_INHERIT / INHERIT_ONLY:ALLOW的原因

Worse yet, the JVM does NOT support the SE_DACL_PROTECTED and SE_SACL_PROTECTED flags from Windows Security descriptors (which are set via the meta flags (UN)PROTECTED_(S/D)ACL_SECURITY_INFORMATION as indicated in comments here . If you use the tool AccessChk , it actually shows you the effective ACE list not the actual ACE list, which AclFileAttributeView and other Windows tools do (look at FileTest .) This is the reason why the number of ACEs are different. In my case, I had 9 ACEs but AccessChk showed 5. 4 out of those 9 where really duplicates on the SID (the user or group) value where the 1st ACE for a given SID had permissions and the 2nd ACE for a given SID had NO permissions, but just the SE_DACL_PROTECTED set or not set. 更糟糕的是,在JVM不支持从Windows安全描述符SE_DACL_PROTECTED和SE_SACL_PROTECTED标志(这是通过元标记(UN)PROTECTED_(S / d)ACL_SECURITY_INFORMATION在评论表示设置在这里 。如果你使用的工具的AccessChk,它实际上向您显示有效的 ACE列表,而不是实际的 ACE列表,AclFileAttributeView和其他Windows工具(请查看FileTest 。)这就是ACE数量不同的原因。在我的情况下,我有9个ACE但AccessChk显示5个。这些9中的4个在SID(用户或组)值上真正重复,其中给定SID的第一个ACE具有权限,而给定SID的第二个ACE没有权限,但只有SE_DACL_PROTECTED设置或未设置。

I'm not entirely sure what it is you wanted to DO with these ACLs, but I may have a solution for you depending upon your intent. 我不完全确定你想用这些ACL做什么,但我可能会根据你的意图为你找到解决方案。 I went ahead and made changes to the JNA project to start allowing one to modify Windows Security Descriptors in a more direct way. 我继续对JNA项目进行了更改,以便开始允许用户以更直接的方式修改Windows安全描述符。 You can see my merged pull request here . 您可以在此处查看我的合并拉取请求。 I don't know how often they publish versions to the Maven public repo, so you may have to download and compile the source directly to get those changes. 我不知道他们多久经常向Maven公共仓库发布版本,因此您可能必须直接下载并编译源代码才能获得这些更改。 See the GetNamedSecurityInfo for how to get the SD of an object. 有关如何获取对象的SD的信息,请参阅GetNamedSecurityInfo。 Then, you can use the help APIs in AdvApi32Util to maninuplate the SECURITY_DESCRIPTOR objects. 然后,您可以使用AdvApi32Util中的帮助API来编写SECURITY_DESCRIPTOR对象。 See public static SECURITY_DESCRIPTOR_RELATIVE getFileSecurityDescriptor(File file, boolean getSACL) for a way to get the SD into self relative format and then that object allows you to walk the ACEs in the ACL. 请参阅public static SECURITY_DESCRIPTOR_RELATIVE getFileSecurityDescriptor(File file, boolean getSACL)以获取将SD转换为自相对格式的方法,然后该对象允许您在ACL中遍历ACE。

I ended up using reflection to get the ACE's access mask (using sun.nio.fs.WindowsNativeDispatcher.GetAce). 我最终使用反射来获取ACE的访问掩码(使用sun.nio.fs.WindowsNativeDispatcher.GetAce)。 With the access mask I check the four high order bits to determine the generic permissions. 使用访问掩码,我检查四个高位,以确定通用权限。

It also allows me to get the INHERITED_ACE bit, which is not available in the JRE spec. 它还允许我获得INHERITED_ACE位,这在JRE规范中是不可用的。 I get the 'raw' ACL information, which is a lot more then the Windows Security tab. 我得到了'原始'ACL信息,这比Windows安全选项卡要多得多。 For example, my c:\\ drive gives me two ACE's for the BUILT-IN\\Administrators, one with GENERIC_ALL permissions and propagation to subfolders and files (not the container itself), and one with the applied special permissions having no flags at all (only applied on the current container). 例如,我的c:\\驱动器为BUILT-IN \\ Administrators提供了两个ACE,一个具有GENERIC_ALL权限并传播到子文件夹和文件(不是容器本身),另一个具有应用的特殊权限,根本没有标记(仅适用于当前容器)。 That's how Windows uses these Generic permissions. 这就是Windows使用这些通用权限的方式。 The same applies for the subfolders, like the Program Files directory, Generic permissions are propagated and every sub container has a separate ACE for the actual special permissions that apply only to the container itself. 这同样适用于子文件夹,如Program Files目录,传播通用权限,每个子容器都有一个单独的ACE,用于仅适用于容器本身的实际特殊权限。

About the tool AccessChck, are you sure you use the -l parameter? 关于AccessChck工具,您确定使用-l参数吗? This gives a lot more information and the 'raw' ACL information. 这提供了更多信息和“原始”ACL信息。

I already use the JNA Project to retrieve local groups from a server. 我已经使用JNA Project从服务器检索本地组。 Thanks for the tip about the AdvApi32Util! 感谢关于AdvApi32Util的提示! I will look into it. 我会仔细看看的。 How was your experience with the setACL method in the JRE? 您对JRE中setACL方法的体验如何?

I use all this to release a tool that combines group membership from a LDAP with the ACL information found in the directories and files. 我使用这一切来发布一个工具,它将来自LDAP的组成员资格与目录和文件中的ACL信息相结合。 All this information is saved in a local database (or external) and can be used to create overviews. 所有这些信息都保存在本地数据库(或外部)中,可用于创建概视图。 This overview offers a lot of filter options and displays permission info for a specific user or a group of users (including nested group membership). 此概述提供了大量筛选选项,并显示特定用户或一组用户的权限信息(包括嵌套组成员身份)。 Because everything is saved in a database, it gives you overviews in seconds instead of scanning the whole network every time. 因为所有内容都保存在数据库中,所以它可以在几秒钟内为您提供概述,而不是每次都扫描整个网络。 You can also trace permissions, it displays where permissions come from, from what group membership or from what folder, etc. It will contain a feature to modify a single ACE, but the focus is on viewing permissions. 您还可以跟踪权限,它显示权限来自何处,来自哪些组成员身份或来自哪个文件夹等。它将包含修改单个ACE的功能,但重点是查看权限。

The tool is ready for testing ;) Let me know if you're interested... The tool won't be for free because it took me a hell of a lot time to write, but let me know if you're interested. 该工具已准备好进行测试;)如果您有兴趣,请告诉我...该工具不是免费的,因为我花了很多时间写作,但如果您有兴趣,请告诉我。 I can get you a license. 我可以给你许可证。 See the following links for a quick impression. 请参阅以下链接以获得快速印象。 Don't mind the website, it still mentions an old version of the tool. 不介意网站,它仍然提到该工具的旧版本。

Scan screenshot 扫描截图
Overview screenshot 概述截图

Permission Analyzer 64bit 权限分析器64位
Permission Analyzer 32bit 权限分析器32位
Permission Analyzer 64bit with embedded JRE 具有嵌入式JRE的权限分析器64位
Permission Analyzer 32bit with embedded JRE 具有嵌入式JRE的权限分析器32位

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM