使用python检查文件夹/文件ntfs权限

发布于 2024-07-21 13:46:22 字数 542 浏览 1 评论 0原文

正如问题标题可能暗示的那样,我非常想知道如何检查给定文件或文件夹的 ntfs 权限(提示:这些是您在“安全”选项卡中看到的权限)。 基本上,我需要的是获取文件或目录的路径(在本地计算机上,或者最好在远程计算机上的共享上)并获取用户/组的列表以及该文件/文件夹的相应权限。 最终,应用程序将遍历目录树,读取每个对象的权限并进行相应的处理。

现在,我可以想到多种方法来做到这一点:

  • 解析 cacls.exe 输出 - 很容易完成,但是,除非我错过了某些东西,否则 cacls.exe 只以 R|W|C|F 的形式提供权限(读/写/更改/完整),这是不够的(我需要获得“列出文件夹内容”等权限,还需要扩展权限)
  • xcacls.exe 或 xcacls.vbs 输出 - 是的,它们给了我所需的所有权限,但它们的工作速度非常慢,xcacls.vbs 大约需要一秒钟才能获得本地系统文件的权限。 这样的速度是不可接受的
  • win32security(它环绕winapi,对吧?)——我确信它可以像这样处理,但我不想重新发明轮子

还有什么我在这里遗漏的吗?

As the question title might suggest, I would very much like to know of the way to check the ntfs permissions of the given file or folder (hint: those are the ones you see in the "security" tab). Basically, what I need is to take a path to a file or directory (on a local machine, or, preferrably, on a share on a remote machine) and get the list of users/groups and the corresponding permissions for this file/folder. Ultimately, the application is going to traverse a directory tree, reading permissions for each object and processing them accordingly.

Now, I can think of a number of ways to do that:

  • parse cacls.exe output -- easily done, BUT, unless im missing something, cacls.exe only gives the permissions in the form of R|W|C|F (read/write/change/full), which is insufficient (I need to get the permissions like "List folder contents", extended permissions too)
  • xcacls.exe or xcacls.vbs output -- yes, they give me all the permissions I need, but they work dreadfully slow, it takes xcacls.vbs about ONE SECOND to get permissions on a local system file. Such speed is unacceptable
  • win32security (it wraps around winapi, right?) -- I am sure it can be handled like this, but I'd rather not reinvent the wheel

Is there anything else I am missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稳稳的幸福 2024-07-28 13:46:22

除非您喜欢自己动手,否则 win32security 是您的最佳选择。 这里是一个示例的开始:

http:// /timgolden.me.uk/python/win32_how_do_i/get-the-owner-of-a-file.html

如果你想生活得有点危险(!)我正在进行的 winsys 包的设计目的正是你在追赶。 您可以在此处获取开发版本的 MSI:

http: //timgolden.me.uk/python/downloads/WinSys-0.4.win32-py2.6.msi

或者您可以直接签出 svn trunk:

svn co <​​a href="http://winsys.googlecode .com/svn/trunk" rel="noreferrer">http://winsys.googlecode.com/svn/trunk winsys

要执行您所描述的操作(稍微猜测确切的要求),您可以这样做:

import codecs
from winsys import fs

base = "c:/temp"
with codecs.open ("permissions.log", "wb", encoding="utf8") as log:
  for f in fs.flat (base):
  log.write ("\n" + f.filepath.relative_to (base) + "\n")
  for ace in f.security ().dacl:
    access_flags = fs.FILE_ACCESS.names_from_value (ace.access)
    log.write (u"  %s => %s\n" % (ace.trustee, ", ".join (access_flags)))

TJG

Unless you fancy rolling your own, win32security is the way to go. There's the beginnings of an example here:

http://timgolden.me.uk/python/win32_how_do_i/get-the-owner-of-a-file.html

If you want to live slightly dangerously (!) my in-progress winsys package is designed to do exactly what you're after. You can get an MSI of the dev version here:

http://timgolden.me.uk/python/downloads/WinSys-0.4.win32-py2.6.msi

or you can just checkout the svn trunk:

svn co http://winsys.googlecode.com/svn/trunk winsys

To do what you describe (guessing slightly at the exact requirements) you could do this:

import codecs
from winsys import fs

base = "c:/temp"
with codecs.open ("permissions.log", "wb", encoding="utf8") as log:
  for f in fs.flat (base):
  log.write ("\n" + f.filepath.relative_to (base) + "\n")
  for ace in f.security ().dacl:
    access_flags = fs.FILE_ACCESS.names_from_value (ace.access)
    log.write (u"  %s => %s\n" % (ace.trustee, ", ".join (access_flags)))

TJG

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文