[英]How can I get effective file permissions?
我正在嘗試獲取文件有效權限 。 最好的方法是什么?
我正在嘗試使用win32security
但GetEffectiveRightsFromAcl(trustee)
函數需要PyTRUSTEE
參數。 而且我不知道如何正確設置它。
結果,我需要獲得與在PowerShell中調用Get-EffectiveAccess相同的權限。
我們嘗試使用Authz.h
但是在那種情況下,我們在Windows Event Viewer中遇到了Audit失敗。
我們還嘗試使用Aclapi.h
GetEffectiveRightsFromAcl
,但是如果我們有很多文件,這可能會成為服務器掛起的原因。
蟒蛇:
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)
阿克拉皮
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
刪除
READ_CONTROL
WRITE_DAC
WRITE_OWNER
同步
使用@eryksun幫助我得到了一些結果。 謝謝。 我也發現了這個有用的例子 。
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
因為它包含在ace中。
當我嘗試使用權限常量創建令牌時,我也遇到了相同的Audit Failure
(在系統帳戶的情況下)。 因此,在這兩種情況下(系統帳戶和管理員)(PowerShell除外),我都沒有找到沒有Audit Faulire
任何結果都找不到。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.