是什么让这个安全描述符变坏了?

Stu*_*ley 5 python ctypes pywin32 advapi32

我正在尝试使用此代码读取Windows中文件和目录的访问权限(在Tim Golden建议的os.access补丁之后进行模式化以使其从Windows上的ACL读取):

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"))
Run Code Online (Sandbox Code Playgroud)

但是,每次我运行它,我得到这个:

WindowsError: [Error 1338] The security descriptor structure is invalid.
Run Code Online (Sandbox Code Playgroud)

我怎么能把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.
Run Code Online (Sandbox Code Playgroud)

Stu*_*ley 5

显然,"可选"Windows意味着"必需".我通过分配缓冲区并传递PRIVILEGE_SET(20)的大小来修复它.

  • 这是我一年前做过的一次实习,我没有接受我的代码,但是从阅读我在这里说的话我认为诀窍是(通过这里写的例子)替换`privsetlength = wintypes .DWORD(0)``privsetlength = wintypes.DWORD(20)`和`c_void_p(0)`类似`create_string_buffer(20)`(我过去一年半的时间里一直在高兴地忘记了我曾经做过的一切确实涉及Python ctypes模块). (2认同)