如何获得有效的文件权限?

时间:2018-03-22 12:36:17

标签: python c windows file-permissions

我试图获取文件有效权限。最好的方法是什么?

我尝试使用win32securityGetEffectiveRightsFromAcl(trustee)函数需要PyTRUSTEE参数。而且我不知道如何正确设置它。

因此,我需要获得与在PowerShell中调用Get-EffectiveAccess相同的权限。

我们尝试使用Authz.h,但在这种情况下,我们在Windows事件查看器中遇到了审核失败。

我们还尝试使用GetEffectiveRightsFromAcl中的Aclapi.h,但如果我们有大量文件,它可能会成为服务器挂起的原因。

蟒:

dacl = win32security.GetNamedSecurityInfo( FILENAME,
 win32security.SE_FILE_OBJECT,
 win32security.DACL_SECURITY_INFORMATION).GetSecurityDescriptorDacl()
mask = dacl.GetEffectiveRightsFromAcl( ??? )

Authz.h

AuthzInitializeResourceManager(AUTHZ_RM_FLAG_NO_AUDIT, NULL, NULL, NULL, NULL, &hManager);
AuthzInitializeContextFromSid(0, psid, hManager, NULL, unusedId, NULL, &hAuthzClientContext);
AuthzFreeResourceManager(hManager);

accessRequest->DesiredAccess = MAXIMUM_ALLOWED;
accessRequest->PrincipalSelfSid = NULL;
accessRequest->ObjectTypeList = NULL;
accessRequest->ObjectTypeListLength = 0;
accessRequest->OptionalArguments = NULL;
RtlZeroMemory(Buffer, sizeof(Buffer));
accessReply->ResultListLength = 1;

accessReply->GrantedAccessMask = (PACCESS_MASK)LocalAlloc(LPTR, sizeof(ACCESS_MASK));
accessReply->Error = (PDWORD)(Buffer + sizeof(ACCESS_MASK));

AuthzAccessCheck(0, hAuthzClient, accessRequest, NULL, psd, NULL, 0, accessReply, NULL)

Aclapi.h

ACCESS_MASK accessRights;
TRUSTEE trustee;

BuildTrusteeWithName(&trustee, trav->user);
retcode = GetEffectiveRightsFromAcl( acl,&trustee,&accessRights);

我需要得到类似的东西:

FILE_READ_DATA
FILE_WRITE_DATA
FILE_APPEND_DATA
FILE_READ_EA
FILE_WRITE_EA
FILE_EXECUTE
FILE_DELETE_CHILD
FILE_READ_ATTRIBUTE
FILE_WRITE_ATTRIBUTE
DELETE
READ_CONTROL
WRITE_DAC
WRITE_OWNER
SYNCHRONIZE

1 个答案:

答案 0 :(得分:0)

我使用@eryksun帮助获得了一些结果。谢谢。 我还找到了this useful example

def print_permissions(mask):
print("PERMISSION:",
      1 if bool(mask & 0x00000001) else 0,
      1 if bool(mask & 0x00000002) else 0,
      1 if bool(mask & 0x00000004) else 0,
      1 if bool(mask & 0x00000008) else 0,
      1 if bool(mask & 0x00000010) else 0,
      1 if bool(mask & 0x00000020) else 0,
      1 if bool(mask & 0x00000040) else 0,
      1 if bool(mask & 0x00000080) else 0,
      1 if bool(mask & 0x00000100) else 0,
      1 if bool(mask & 0x00010000) else 0,
      1 if bool(mask & 0x00020000) else 0,
      1 if bool(mask & 0x00040000) else 0,
      1 if bool(mask & 0x00080000) else 0,
      1 if bool(mask & 0x00100000) else 0)

def get_permissions(dacl):
for n_ace in range(dacl.GetAceCount()):
    ace = dacl.GetAce(n_ace)
    (ace_type, ace_flags) = ace[0]
    if ace_type in CONVENTIONAL_ACES:
        mask, sid = ace[1:]
    else:
        mask, object_type, inherited_object_type, sid = ace[1:]
    name, domain, type = win32security.LookupAccountSid(None, sid)
    print("\nUSER:", name)
    print_permissions(mask)

for f in files:
    try:
        dacl = win32security.GetNamedSecurityInfo(
            f,
            win32security.SE_FILE_OBJECT,
            win32security.DACL_SECURITY_INFORMATION).GetSecurityDescriptorDacl()
    except BaseException as ex:
        winerror, funcname, strerror = ex.args
        print("Error: ", winerror,"\n")
    else:
        get_permissions(dacl)

我不使用GetEffectiveRightsFromAcl,因为它包含在王牌中。

当我尝试使用权限常量创建令牌时,我也得到了相同的Audit Failure(在系统帐户案例中)。因此,在两种情况下(系统帐户和管理员)(PowerShell除外),我都没有找到任何没有Audit Faulire的结果。