Question

J'essaie de lire l'accès des fichiers et des répertoires dans Windows à l'aide de ce code (à motifs après Patch proposé par Tim Golden sur Os.Access pour le faire lire des ACLS sous 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"))

Cependant, chaque fois que je cours, je reçois ceci:

WindowsError: [Error 1338] The security descriptor structure is invalid.

Comment suis-je censé transmettre le SecurityDescriptor à Accesscheck?

EDIT: Modification de DACL_SECURITY_INFORMATION en DACL_SECURITY_INFORMATION |Group_security_information |Propriétaire_security_information me donne ceci:

WindowsError: [Error 122] The data area passed to a system call is too small.

Était-ce utile?

La solution

Apparemment par "optionnel" Windows signifie "requis".Je l'ai réparé en allouant un tampon et en passant la taille de Privilege_set (20).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top