python – 是什么让这个安全描述符变坏了?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 是什么让这个安全描述符变坏了?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1983字,纯文字阅读大概需要3分钟。
内容图文
我正在尝试使用此代码读取Windows中文件和目录的访问权限(在Tim Golden’s proposed patch to os.access to make it read from ACLs on Windows之后模式化):
from ctypes import(
windll,
wintypes,
c_char_p,
c_void_p,
byref
)
from win32api import GetCurrentThread
from win32security import (
GetFileSecurity,
DACL_SECURITY_INFORMATION,
ImpersonateSelf,
SecurityImpersonation,
OpenThreadToken,
TOKEN_ALL_ACCESS,
MapGenericMask
)
from ntsecuritycon import (
FILE_READ_DATA,
FILE_WRITE_DATA,
FILE_EXECUTE,
FILE_ALL_ACCESS
)
import pywintypes
import winnt
TRUE = 1
def CheckAccess(path,AccessDesired):
result = wintypes.BOOL()
granted = wintypes.DWORD(0)
privsetlength = wintypes.DWORD(0)
fileSD = GetFileSecurity(path, DACL_SECURITY_INFORMATION)
if not fileSD.IsValid():
raise Exception("Invalid security descriptor")
ImpersonateSelf(SecurityImpersonation)
token = OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, TRUE)
mapping = wintypes.DWORD(MapGenericMask(AccessDesired,
(FILE_READ_DATA, FILE_WRITE_DATA, FILE_EXECUTE, FILE_ALL_ACCESS)))
if not windll.advapi32.AccessCheck(
c_char_p(str(buffer(fileSD))),
wintypes.HANDLE(int(token)),
AccessDesired,
byref(mapping),
c_void_p(0), #privilege set, optional
byref(privsetlength), #size of optional privilege set
byref(granted),
byref(result)
):
code = GetLastError()
raise WindowsError(GetLastError(),FormatMessage(code))
return bool(result)
def HasReadAccess(path):
return CheckAccess(path,FILE_READ_DATA)
def HasWriteAccess(path):
return CheckAccess(path,FILE_WRITE_DATA)
if __name__ == "__main__":
print(HasReadAccess("C:/Python26"))
但是,每次我运行它,我得到这个:
WindowsError: [Error 1338] The security descriptor structure is invalid.
我怎么能把SecurityDescriptor传递给AccessCheck?
编辑:将DACL_SECURITY_INFORMATION更改为DACL_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION给了我这个:
WindowsError: [Error 122] The data area passed to a system call is too small.
解决方法:
显然,“可选”Windows意味着“必需”.我通过分配缓冲区并传递PRIVILEGE_SET(20)的大小来修复它.
内容总结
以上是互联网集市为您收集整理的python – 是什么让这个安全描述符变坏了?全部内容,希望文章能够帮你解决python – 是什么让这个安全描述符变坏了?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。