private void DumpDirectory(IEnumerable <TokenEntry> tokens, AccessMask access_rights, AccessMask dir_access_rights, NtFile file, FileOpenOptions options, int current_depth) { if (Stopping || current_depth <= 0) { return; } var parent_sd = GetSecurityDescriptor(file); if (Recurse) { using (var result = file.ReOpen(FileAccessRights.Synchronize | FileAccessRights.ReadData | FileAccessRights.ReadAttributes, FileShareMode.Read | FileShareMode.Delete, options | FileOpenOptions.DirectoryFile | FileOpenOptions.SynchronousIoNonAlert, GetAttributeFlags(), false)) { if (result.Status.IsSuccess()) { foreach (var entry in result.Result.QueryDirectoryInfo(Filter, FileTypeMask.All)) { if (CheckMode == FileCheckMode.DirectoriesOnly && !entry.IsDirectory) { continue; } NtFile base_file = result.Result; string filename = entry.FileName; if (filename.Contains(@"\")) { filename = base_file.FullPath + filename; base_file = null; } using (var new_file = OpenFile(filename, base_file, options)) { if (new_file.IsSuccess) { if (FollowPath(new_file.Result, GetFilePath)) { DumpFile(tokens, access_rights, dir_access_rights, parent_sd.IsSuccess ? parent_sd.Result : null, new_file.Result); if (IsDirectoryNoThrow(new_file.Result)) { DumpDirectory(tokens, access_rights, dir_access_rights, new_file.Result, options, current_depth - 1); } } } } } } } } }
private void CheckAccessUnderImpersonation(TokenEntry token, AccessMask access_rights, NtFile file) { using (var result = token.Token.RunUnderImpersonate(() => file.ReOpen(FileAccessRights.MaximumAllowed, FileShareMode.Read | FileShareMode.Delete, FileOpenOptions.None, false))) { if (result.Status.IsSuccess() && IsAccessGranted(result.Result.GrantedAccessMask, access_rights)) { WriteAccessCheckResult(file.FullPath, file.NtType.Name, result.Result.GrantedAccessMask, file.NtType.GenericMapping, String.Empty, IsDirectoryNoThrow(file) ? typeof(FileDirectoryAccessRights) : typeof(FileAccessRights), token.Information); } } }