/* * Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved. * * This file is part of System Informer. * * Authors: * * jxy-s 2020-2022 * dmex 2021-2026 * */ #include #include #include #include #define PH_IMGCOHERENCY_NORMAL_SCAN_LIMIT (40 * (1024 * 1024)) // 40Mib #define PH_IMGCOHERENCY_QUICK_SCAN_LIMIT (PAGE_SIZE * 2) #define PH_IMGCOHERENCY_MIN_ENTRY_INSPECT (PAGE_SIZE * 2) /** * Image coherency context, used during the calculation of the process * image coherency. */ typedef struct _PH_IMAGE_COHERENCY_CONTEXT { PH_IMAGE_COHERENCY_SCAN_TYPE Type; /**< Type of scan to perform */ SIZE_T CoherentBytes; /**< Updated during inspection, coherent bytes */ SIZE_T TotalBytes; /**< Updated during inspection, total bytes analyzed */ SIZE_T SkippedBytes; /**< Bytes skipped during inspection */ NTSTATUS MappedImageStatus; /**< Status of initializing MappedImage */ PH_MAPPED_IMAGE MappedImage; /**< On-disk image mapping */ PPH_HASHTABLE MappedImageReloc; /**< On-disk mapped image relocations table */ ULONG MappedImageBaseRva; /**< On-disk optional header image base RVA. */ ULONG MappedImageIatRva; /**< On-disk import address table RVA */ ULONG MappedImageIatSize; /**< On-disk import address table size */ PULONG MappedImageRelocRvas; /**< Sorted relocation starts for vector spans */ ULONG MappedImageRelocRvaCount; BOOLEAN MappedImageSkipIndexReady; NTSTATUS RemoteMappedImageStatus; /**< Status of initializing RemoteMappedImage */ PH_REMOTE_MAPPED_IMAGE RemoteMappedImage; /**< Remote image mapping */ PVOID RemoteImageBase; /**< Remote image base address */ SIZE_T RemoteImageSize; /**< Remote image size */ BOOLEAN ImageIsKernelModule; } PH_IMAGE_COHERENCY_CONTEXT, *PPH_IMAGE_COHERENCY_CONTEXT; /** * Inspection skip callback type. * * \param[in] Rva - Current rva in the range being inspected. * \param[in] Context - Context supplied to this callback. * \return Number of bytes to skip, 0 does not skip bytes. */ typedef _Function_class_(PH_IMGCOHERENCY_SKIP_BYTE_CALLBACK) ULONG CALLBACK PH_IMGCOHERENCY_SKIP_BYTE_CALLBACK( _In_ ULONG Rva, _In_opt_ PVOID Context ); typedef PH_IMGCOHERENCY_SKIP_BYTE_CALLBACK *PPH_IMGCOHERENCY_SKIP_BYTE_CALLBACK; _Function_class_(PH_IMGCOHERENCY_SKIP_BYTE_CALLBACK) ULONG CALLBACK PhpImgCoherencySkip( _In_ ULONG Rva, _In_ PVOID Context ); /** * qsort comparison callback for the sorted relocation start RVA array. * * \param[in] Left - Pointer to the first ULONG RVA. * \param[in] Right - Pointer to the second ULONG RVA. * \return Negative, zero or positive as Left is less than, equal to or greater than Right. */ static int __cdecl PhpCompareImageCoherencyRva( _In_ const void* Left, _In_ const void* Right ) { return uintcmp(*(PULONG)Left, *(PULONG)Right); } /** * Builds the sorted relocation start index used to find the spans between * skip callback hits, allowing those spans to be compared with vector * instructions instead of invoking the skip callback for every byte. * * The index is best-effort: if there are no relocations it stays empty and * only the ImageBase and IAT RVAs bound a span. Relocation keys that do not * fit in a ULONG are excluded because PhpImgCoherencySkip takes a ULONG RVA * and can never match them. * * \param[in,out] Context - Image coherency context. MappedImageReloc should be * populated before this is called. Sets MappedImageSkipIndexReady on return. */ static VOID PhpInitializeImageCoherencySkipIndex( _Inout_ PPH_IMAGE_COHERENCY_CONTEXT Context ) { PPH_KEY_VALUE_PAIR entry; ULONG enumerationKey = 0; if (Context->MappedImageReloc && Context->MappedImageReloc->Count) { Context->MappedImageRelocRvas = PhAllocate( (SIZE_T)Context->MappedImageReloc->Count * sizeof(ULONG) ); while (PhEnumHashtable(Context->MappedImageReloc, &entry, &enumerationKey)) { // The callback accepts a ULONG RVA. Keys above that range cannot // match it, and must not be truncated into a different skip start. if ((ULONG_PTR)entry->Key <= ULONG_MAX) Context->MappedImageRelocRvas[Context->MappedImageRelocRvaCount++] = PtrToUlong(entry->Key); } qsort(Context->MappedImageRelocRvas, Context->MappedImageRelocRvaCount, sizeof(ULONG), PhpCompareImageCoherencyRva); } Context->MappedImageSkipIndexReady = TRUE; } /** * Determines how many bytes, starting at Rva, are guaranteed not to be a skip * callback hit and can therefore be compared without consulting the callback. * * The span is bounded by the nearest of: the next relocation start (binary * search of the sorted index), the ImageBase field RVA, the IAT start RVA, and * the point where the ULONG callback RVA would wrap. Only starts are indexed; * merging overlapping ranges would change PhpImgCoherencySkip's skip semantics. * * \param[in] Context - Image coherency context with the skip index built. * \param[in] Rva - RVA of the first byte in the span. The caller must have * already established that PhpImgCoherencySkip returns 0 for this RVA. * \param[in] Length - Maximum span length (bytes remaining in the buffer). * \return Number of bytes, at most Length and at least 1, safe to compare. */ static ULONG PhpImageCoherencyCompareSpan( _In_ PPH_IMAGE_COHERENCY_CONTEXT Context, _In_ ULONG Rva, _In_ ULONG Length ) { ULONG low = 0; ULONG high = Context->MappedImageRelocRvaCount; while (low < high) { ULONG middle = low + (high - low) / 2; if (Context->MappedImageRelocRvas[middle] <= Rva) low = middle + 1; else high = middle; } if (low < Context->MappedImageRelocRvaCount) Length = min(Length, Context->MappedImageRelocRvas[low] - Rva); if (Context->MappedImageBaseRva > Rva) Length = min(Length, Context->MappedImageBaseRva - Rva); if (Context->MappedImageIatRva > Rva) Length = min(Length, Context->MappedImageIatRva - Rva); // Re-evaluate the index if the ULONG callback RVA wraps around. if (Rva) Length = min(Length, ULONG_MAX - Rva + 1); return Length; } /** * Retrieves the size of the section to scan given the scan type. * * \param[in] Type - Image coherency scan type. * \param[in] SectionHeader - Image section header. * \return Amount of the section to scan given the scan type. */ ULONG PhpGetSectionScanSize( _In_ PH_IMAGE_COHERENCY_SCAN_TYPE Type, _In_ PIMAGE_SECTION_HEADER SectionHeader ) { ULONG size; size = min(SectionHeader->SizeOfRawData, SectionHeader->Misc.VirtualSize); switch (Type) { case PhImageCoherencyQuick: { if (size > PH_IMGCOHERENCY_QUICK_SCAN_LIMIT) { size = PH_IMGCOHERENCY_QUICK_SCAN_LIMIT; } } break; case PhImageCoherencyNormal: { if (size > PH_IMGCOHERENCY_NORMAL_SCAN_LIMIT) { size = PH_IMGCOHERENCY_NORMAL_SCAN_LIMIT; } } break; case PhImageCoherencyFull: default: break; } return size; } /** * Determines if the section should be scanned given the scan type. * * \param[in] Type - Image coherency scan type. * \param[in] SectionHeader - Section header to inspect. * \return TRUE if the section should be scanned for the given scan type, FALSE otherwise. */ BOOLEAN PhpShouldScanSection( _In_ PH_IMAGE_COHERENCY_SCAN_TYPE Type, _In_ PIMAGE_SECTION_HEADER SectionHeader ) { switch (Type) { case PhImageCoherencyQuick: case PhImageCoherencyNormal: case PhImageCoherencyFull: { if ((SectionHeader->Characteristics & IMAGE_SCN_MEM_EXECUTE) != 0) { // // Anything marked execute. // return TRUE; } } break; } return FALSE; } /** * Frees the image coherency context. * * \param[in] Context - Context to free, may be NULL. */ VOID PhpFreeImageCoherencyContext( _In_opt_ PPH_IMAGE_COHERENCY_CONTEXT Context ) { if (Context) { PhUnloadMappedImage(&Context->MappedImage); PhUnloadRemoteMappedImage(&Context->RemoteMappedImage); if (Context->MappedImageReloc) PhDereferenceObject(Context->MappedImageReloc); if (Context->MappedImageRelocRvas) PhFree(Context->MappedImageRelocRvas); PhFree(Context); } } _Function_class_(PH_MAPPED_IMAGE_RELOC_CALLBACK) static NTSTATUS NTAPI PhImageCoherencyRelocationCallback( _In_ PPH_MAPPED_IMAGE MappedImage, _In_ PIMAGE_DATA_DIRECTORY DataDirectory, _In_ PIMAGE_BASE_RELOCATION RelocationDirectory, _In_ PIMAGE_RELOCATION_RECORD Relocations, _In_ ULONG RelocationCount, _In_ PPH_IMAGE_COHERENCY_CONTEXT Context ) { for (ULONG i = 0; i < RelocationCount; i++) { PIMAGE_RELOCATION_RECORD entry = &Relocations[i]; __try { PhMappedImageProbe(MappedImage, entry, sizeof(IMAGE_RELOCATION_RECORD)); } __except (EXCEPTION_EXECUTE_HANDLER) { return GetExceptionCode(); } if ( (entry->Type != IMAGE_REL_BASED_ABSOLUTE) && (entry->Type != IMAGE_REL_BASED_RESERVED) ) { PVOID rva = (PVOID)UInt32Add32To64(RelocationDirectory->VirtualAddress, entry->Offset); if (entry->Type == IMAGE_REL_BASED_DIR64) { PhAddItemSimpleHashtable(Context->MappedImageReloc, rva, UlongToPtr(8)); } else { // // For now, we're just going to do a 4 byte skip for // all other relocations. This could probably use some // work for higher accuracy. // PhAddItemSimpleHashtable(Context->MappedImageReloc, rva, UlongToPtr(4)); } } } return STATUS_SUCCESS; } _Function_class_(PH_MAPPED_IMAGE_DYNAMIC_RELOC_CALLBACK) static NTSTATUS NTAPI PhImageCoherencyDynamicRelocationCallback( _In_ PPH_MAPPED_IMAGE MappedImage, _In_ PPH_IMAGE_DYNAMIC_RELOC_ENTRY Entry, _In_opt_ PVOID Context ) { PPH_IMAGE_COHERENCY_CONTEXT context = Context; ULONG_PTR rva = 0; ULONG_PTR size = 0; if (Entry->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X) { rva = UInt32Add32To64(Entry->ARM64X.BlockRva, Entry->ARM64X.RecordFixup.Offset); switch (Entry->ARM64X.RecordFixup.Type) { case IMAGE_DVRT_ARM64X_FIXUP_TYPE_ZEROFILL: case IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE: size = (ULONG_PTR)(1ull << Entry->ARM64X.RecordFixup.Size); break; case IMAGE_DVRT_ARM64X_FIXUP_TYPE_DELTA: size = 4; break; } } else if (Entry->Symbol == IMAGE_DYNAMIC_RELOCATION_GUARD_IMPORT_CONTROL_TRANSFER) { rva = UInt32Add32To64(Entry->ImportControl.BlockRva, Entry->ImportControl.Record.PageRelativeOffset); // // 48 FF 15 XX XX XX XX call qword ptr [_imp_] // 0F 1F 44 00 00 nop // size = 12; } else if (Entry->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64_KERNEL_IMPORT_CALL_TRANSFER) { rva = UInt32Add32To64(Entry->ARM64ImportControl.BlockRva, (Entry->ARM64ImportControl.Record.PageRelativeOffset << 2)); // // ARM64 instructions are fixed 4 bytes // Either BR or BLR instruction for indirect call/jump // size = 4; } else if (Entry->Symbol == IMAGE_DYNAMIC_RELOCATION_GUARD_RF_PROLOGUE) { rva = Entry->RFPrologue.BlockRva; // // Prologue size is variable, specified in PrologueByteCount // size = Entry->RFPrologue.PrologueByteCount; } else if (Entry->Symbol == IMAGE_DYNAMIC_RELOCATION_GUARD_RF_EPILOGUE) { rva = Entry->RFEpilogue.BlockRva; // // Epilogue size is variable // Total size = EpilogueByteCount + (BranchDescriptorElementSize * BranchDescriptorCount) + bitmap // size = (ULONG_PTR)Entry->RFEpilogue.EpilogueByteCount + ((ULONG_PTR)Entry->RFEpilogue.BranchDescriptorElementSize * Entry->RFEpilogue.BranchDescriptorCount) + ((Entry->RFEpilogue.BranchDescriptorCount + 7) / 8); } else if (Entry->Symbol == IMAGE_DYNAMIC_RELOCATION_GUARD_INDIR_CONTROL_TRANSFER) { rva = UInt32Add32To64(Entry->IndirControl.BlockRva, Entry->IndirControl.Record.PageRelativeOffset); size = 12; } else if (Entry->Symbol == IMAGE_DYNAMIC_RELOCATION_GUARD_SWITCHTABLE_BRANCH) { rva = UInt32Add32To64(Entry->SwitchBranch.BlockRva, Entry->SwitchBranch.Record.PageRelativeOffset); // // FF D0 jmp rax // CC CC CC int 3 // size = 5; } else if (Entry->Symbol == IMAGE_DYNAMIC_RELOCATION_FUNCTION_OVERRIDE) { rva = UInt32Add32To64(Entry->FuncOverride.BlockRva, Entry->FuncOverride.Record.Offset); // // The loader patches a fixed width per override type (see RtlpParseFunctionOverrideRelocations): // X64 REL32 and ARM64 BRANCH26 rewrite 4 bytes; ARM64 THUNK rewrites 8. Use the record type // rather than the image magic so x64 REL32 sites don't over-skip trailing code bytes. // switch (Entry->FuncOverride.Record.Type) { case IMAGE_FUNCTION_OVERRIDE_X64_REL32: case IMAGE_FUNCTION_OVERRIDE_ARM64_BRANCH26: size = 4; break; case IMAGE_FUNCTION_OVERRIDE_ARM64_THUNK: size = 8; break; } } else { // // This should only be absolute, skipping others. // if (Entry->Other.Record.Type == IMAGE_REL_BASED_ABSOLUTE) { rva = UInt32Add32To64(Entry->Other.BlockRva, Entry->Other.Record.Offset); if (MappedImage->Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) size = 4; else if (MappedImage->Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) size = 8; } } if (rva && size) PhAddItemSimpleHashtable(context->MappedImageReloc, (PVOID)rva, (PVOID)size); return STATUS_SUCCESS; } _Function_class_(PH_READ_VIRTUAL_MEMORY_CALLBACK) static NTSTATUS NTAPI PhImageCoherencyReadVirtualMemoryCallback( _In_ HANDLE ProcessHandle, _In_ PVOID BaseAddress, _Out_writes_bytes_(BufferSize) PVOID Buffer, _In_ SIZE_T BufferSize, _Out_opt_ PSIZE_T NumberOfBytesRead, _In_opt_ PVOID Context ) { PPH_IMAGE_COHERENCY_CONTEXT context = (PPH_IMAGE_COHERENCY_CONTEXT)Context; if (!context) return STATUS_INVALID_PARAMETER_6; if (context->ImageIsKernelModule) return KphReadVirtualMemory(ProcessHandle, BaseAddress, Buffer, BufferSize, NumberOfBytesRead); return PhReadVirtualMemory(ProcessHandle, BaseAddress, Buffer, BufferSize, NumberOfBytesRead); } /** * Created the image coherency context. This is done best-effort and * the status is stored in the context. * * \param[in] Type - Image coherency scan type. * \param[in] FileName - Win32 file name of the image to inspect. * \param[in] ProcessHandle - Handle to process to inspect. Requires * PROCESS_QUERY_LIMITED_INFORMATION and PROCESS_VM_READ. * \param[in] RemoteImageBase - Remove image base address, optional. * \param[in] RemoteImageSize - Remove image size, optional. * \param[in] RemoteImageBaseStatus - If RemoteImageBase is null, this is stored * in the context instead of attempting to map the image. * \param[in] ReadVirtualMemoryCallback - Callback to use to read virtual memory. * \return Pointer to newly allocated image coherency context, or NULL on * allocation failure. The created context must be passed to * PhpFreeImageCoherencyContext to free. */ PPH_IMAGE_COHERENCY_CONTEXT PhpCreateImageCoherencyContext( _In_ PH_IMAGE_COHERENCY_SCAN_TYPE Type, _In_ PPH_STRING FileName, _In_ HANDLE ProcessHandle, _In_opt_ PVOID RemoteImageBase, _In_opt_ SIZE_T RemoteImageSize, _In_ NTSTATUS RemoteImageBaseStatus, _In_ BOOLEAN ImageIsKernelModule ) { PPH_IMAGE_COHERENCY_CONTEXT context; // // This is best-effort context creation, we don't fail if the mapping // fails - the caller of the API may make decisions based on success of // each // context = PhAllocateZero(sizeof(PH_IMAGE_COHERENCY_CONTEXT)); context->Type = Type; context->ImageIsKernelModule = ImageIsKernelModule; if (NT_SUCCESS(RemoteImageBaseStatus)) { context->RemoteImageBase = RemoteImageBase; context->RemoteImageSize = RemoteImageSize; // // Map the on-disk image // context->MappedImageStatus = PhLoadMappedImageEx( &FileName->sr, NULL, &context->MappedImage ); if (NT_SUCCESS(context->MappedImageStatus)) { PIMAGE_DATA_DIRECTORY directory; PhMappedImagePrefetch(&context->MappedImage); // // Build a hash table for the relocation entries to skip later. // This hash table will map the RVA to the number of bytes to skip. // context->MappedImageReloc = PhCreateSimpleHashtable(50); context->MappedImageStatus = PhMappedImageEnumerateRelocations( &context->MappedImage, PhImageCoherencyRelocationCallback, context ); PhMappedImageEnumerateDynamicRelocations( &context->MappedImage, PhImageCoherencyDynamicRelocationCallback, context ); if (NT_SUCCESS(PhGetMappedImageDataDirectory( &context->MappedImage, IMAGE_DIRECTORY_ENTRY_IAT, &directory ))) { context->MappedImageIatRva = directory->VirtualAddress; context->MappedImageIatSize = directory->Size; } if (context->MappedImage.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) { PVOID address; address = &context->MappedImage.NtHeaders32->OptionalHeader.ImageBase; context->MappedImageBaseRva = PtrToUlong(PTR_SUB_OFFSET(address, context->MappedImage.ViewBase)); } else if (context->MappedImage.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) { PVOID address; address = &context->MappedImage.NtHeaders->OptionalHeader.ImageBase; context->MappedImageBaseRva = PtrToUlong(PTR_SUB_OFFSET(address, context->MappedImage.ViewBase)); } else { context->MappedImageBaseRva = 0; } } // // Map the remote image // context->RemoteMappedImageStatus = PhInitializeRemoteMappedImage( &context->RemoteMappedImage, PhImageCoherencyReadVirtualMemoryCallback, context ); if (NT_SUCCESS(context->RemoteMappedImageStatus)) { context->RemoteMappedImageStatus = PhLoadRemoteMappedImage( &context->RemoteMappedImage, ProcessHandle, context->RemoteImageBase, context->RemoteImageSize ); } } else { context->RemoteMappedImageStatus = RemoteImageBaseStatus; } PhpInitializeImageCoherencySkipIndex(context); return context; } #ifndef PH_IMGCOHERENCY_LEGACY_INSPECT #define PH_IMGCOHERENCY_VECTOR_INSPECT #endif #ifdef PH_IMGCOHERENCY_VECTOR_INSPECT /** * Inspects two buffers and adds them to the coherency calculation. * * \param[in] LeftBuffer - First buffer to inspect. * \param[in] LeftCount - Number of bytes in the first buffer. * \param[in] RightBuffer - Second buffer to inspect. * \param[in] RightCount - Number of bytes in the second buffer. * \param[in,out] Context - Context to be updated during inspection. * \param[in] Rva - RVA from which the buffers were retrieved, informs skip callback. * \param[in] SkipCallback - Optional, if provided the skip callback is invoked * for each inspected byte, the callback may return any number of bytes to skip. * \param[in] SkipCallbackContext - Optional, callback context passed to the skip callback. * \return STATUS_SUCCESS, or the exception code if reading either buffer faulted. * * \remarks Three comparison paths are used: * - A custom skip callback is invoked for every byte (scalar). * - PhpImgCoherencySkip with its own context uses the skip index: the callback * is invoked once at the start of each span and the span is then compared with * vector instructions (AVX-512, AVX2 or SSE, falling back to scalar). * - No callback compares the whole range with vector instructions. * If a vector block faults, it is retried a byte at a time so the faulting byte * counts towards the total but not the coherent bytes, as in the scalar path. */ NTSTATUS PhpAnalyzeImageCoherencyInspect( _In_opt_ PBYTE LeftBuffer, _In_ ULONG LeftCount, _In_opt_ PBYTE RightBuffer, _In_ ULONG RightCount, _Inout_ PPH_IMAGE_COHERENCY_CONTEXT Context, _In_opt_ ULONG Rva, _In_opt_ PPH_IMGCOHERENCY_SKIP_BYTE_CALLBACK SkipCallback, _In_opt_ PVOID SkipCallbackContext ) { NTSTATUS status = STATUS_SUCCESS; // // For the minimum bytes between the buffers increment the coherent bytes // for each match. // if (LeftBuffer && RightBuffer) { ULONG length = min(LeftCount, RightCount); ULONG i = 0; BOOLEAN indexedSkip = SkipCallback == PhpImgCoherencySkip && SkipCallbackContext == Context && Context->MappedImageSkipIndexReady; if (SkipCallback && !indexedSkip) { // // The skip callback is defined per-byte and its RVA argument must be // presented for every byte in the range, so this path stays scalar. // while (i < length) { ULONG skip = SkipCallback(Rva + i, SkipCallbackContext); if (skip != 0) { ULONG remaining = length - i; ULONG effectiveSkip = (skip > remaining) ? remaining : skip; Context->CoherentBytes += effectiveSkip; Context->SkippedBytes += effectiveSkip; Context->TotalBytes += effectiveSkip; i += effectiveSkip; continue; } Context->TotalBytes++; __try { if (LeftBuffer[i] == RightBuffer[i]) { Context->CoherentBytes++; } } __except (EXCEPTION_EXECUTE_HANDLER) { status = GetExceptionCode(); break; } i++; } } else { // // Without a skip callback the range is a straight byte comparison, // which reduces to a vector compare plus a population count of the // resulting mask. SEH is per-block rather than per-byte; a block // that faults is retried a byte at a time so the accounting stays // identical to the scalar path (the faulting byte counts towards // the total but not towards the coherent bytes). // SIZE_T coherentBytes = 0; BOOLEAN scalarOnly = FALSE; ULONG spanRemaining = 0; while (i < length) { ULONG remaining = length - i; ULONG matched = 0; ULONG width; if (indexedSkip) { if (!spanRemaining) { ULONG skip = PhpImgCoherencySkip(Rva + i, Context); if (skip) { ULONG effectiveSkip = min(skip, remaining); coherentBytes += effectiveSkip; Context->SkippedBytes += effectiveSkip; Context->TotalBytes += effectiveSkip; i += effectiveSkip; continue; } spanRemaining = PhpImageCoherencyCompareSpan(Context, Rva + i, remaining); } remaining = min(remaining, spanRemaining); } if (scalarOnly) width = 1; #if defined(_WIN64) && !defined(_ARM64_) else if (PhHasAVX512 && remaining >= 256) width = 64; #endif else if (PhHasAVX && remaining >= 32) width = 32; else if (PhHasIntrinsics && remaining >= 16) width = 16; else width = 1; __try { switch (width) { #if defined(_WIN64) && !defined(_ARM64_) case 64: matched = (ULONG)PhPopulationCount64(_mm512_cmpeq_epi8_mask( _mm512_loadu_si512((void const*)(LeftBuffer + i)), _mm512_loadu_si512((void const*)(RightBuffer + i)) )); break; #endif case 32: matched = PhPopulationCount32(PhMoveMaskINT256by8(PhCompareEqINT256by8( PhLoadINT256U(LeftBuffer + i), PhLoadINT256U(RightBuffer + i) ))); break; case 16: matched = PhPopulationCount32(PhMoveMaskINT128by8(PhCompareEqINT128by8( PhLoadINT128U((PLONG)(LeftBuffer + i)), PhLoadINT128U((PLONG)(RightBuffer + i)) ))); break; default: matched = (LeftBuffer[i] == RightBuffer[i]) ? 1 : 0; break; } } __except (EXCEPTION_EXECUTE_HANDLER) { if (width != 1) { // // Retry this block a byte at a time to locate the exact // faulting byte, and stay scalar for the remainder. // scalarOnly = TRUE; continue; } status = GetExceptionCode(); Context->TotalBytes++; break; } coherentBytes += matched; Context->TotalBytes += width; i += width; if (indexedSkip) spanRemaining -= width; } Context->CoherentBytes += coherentBytes; if (PhHasAVX) PhZeroUpper(); } } // // Buffers of mismatched sizes are incoherent over mismatched range. // Include any diff in the total bytes. // if (LeftCount > RightCount) { Context->TotalBytes += ((SIZE_T)LeftCount - (SIZE_T)RightCount); } else if (LeftCount < RightCount) { Context->TotalBytes += ((SIZE_T)RightCount - (SIZE_T)LeftCount); } return status; } #else // Original scalar implementation (disabled by default). NTSTATUS PhpAnalyzeImageCoherencyInspect( _In_opt_ PBYTE LeftBuffer, _In_ ULONG LeftCount, _In_opt_ PBYTE RightBuffer, _In_ ULONG RightCount, _Inout_ PPH_IMAGE_COHERENCY_CONTEXT Context, _In_opt_ ULONG Rva, _In_opt_ PPH_IMGCOHERENCY_SKIP_BYTE_CALLBACK SkipCallback, _In_opt_ PVOID SkipCallbackContext ) { NTSTATUS status = STATUS_SUCCESS; // // For the minimum bytes between the buffers increment the coherent bytes // for each match. // if (LeftBuffer && RightBuffer) { ULONG length = min(LeftCount, RightCount); ULONG i = 0; while (i < length) { if (SkipCallback) { ULONG skip = SkipCallback(Rva + i, SkipCallbackContext); if (skip != 0) { ULONG remaining = length - i; ULONG effectiveSkip = (skip > remaining) ? remaining : skip; Context->CoherentBytes += effectiveSkip; Context->SkippedBytes += effectiveSkip; Context->TotalBytes += effectiveSkip; i += effectiveSkip; continue; } } Context->TotalBytes++; __try { if (LeftBuffer[i] == RightBuffer[i]) { Context->CoherentBytes++; } } __except (EXCEPTION_EXECUTE_HANDLER) { status = GetExceptionCode(); break; } i++; } } // // Buffers of mismatched sizes are incoherent over mismatched range. // Include any diff in the total bytes. // if (LeftCount > RightCount) { Context->TotalBytes += ((SIZE_T)LeftCount - (SIZE_T)RightCount); } else if (LeftCount < RightCount) { Context->TotalBytes += ((SIZE_T)RightCount - (SIZE_T)LeftCount); } return status; } #endif /** * Analyzes the image coherency as if it were a native application. * * \param[in] ProcessHandle - Handle to the process requires PROCESS_VM_READ. * \param[in] Rva - Relative virtual address to inspect. * \param[in] Size - Size of data to inspect from the RVA. * \param[in] Context - Image coherency context. * \param[in] SkipCallback - Optional skip callback used to skip analysis. * \param[in] SkipCallbackContext - Context passed to the skip callback. */ VOID PhpAnalyzeImageCoherencyCommonByRva( _In_ HANDLE ProcessHandle, _In_ ULONG Rva, _In_ ULONG Size, _In_ PPH_IMAGE_COHERENCY_CONTEXT Context, _In_opt_ PPH_IMGCOHERENCY_SKIP_BYTE_CALLBACK SkipCallback, _In_opt_ PVOID SkipCallbackContext ) { NTSTATUS status; BYTE buffer[PAGE_SIZE]; ULONG remainingBytes; ULONG chunk; PBYTE fileBytes = 0; SIZE_T bytesRead; SIZE_T remainingView; SIZE_T bytes; ULONG rva; remainingBytes = Size; rva = Rva; while (remainingBytes > 0) { chunk = PAGE_SIZE; if (chunk > remainingBytes) { chunk = remainingBytes; } // // Try to read the remote process // if (!NT_SUCCESS(PhImageCoherencyReadVirtualMemoryCallback( ProcessHandle, PTR_ADD_OFFSET(Context->RemoteImageBase, rva), buffer, chunk, &bytesRead, Context ))) { // // Force 0, we'll handle this below // bytesRead = 0; } if (NT_SUCCESS(PhMappedImageRvaToVa(&Context->MappedImage, rva, &fileBytes))) { // // Calculate the remaining view from the VA // remainingView = (SIZE_T)PTR_SUB_OFFSET( Context->MappedImage.ViewSize, PTR_SUB_OFFSET(fileBytes, Context->MappedImage.ViewBase) ); } else { // // Force 0, we'll handle this below // remainingView = 0; } // // We will cast to ULONG below, should never have over PAGE_SIZE here. // bytes = __min(bytesRead, remainingView); assert(bytes <= PAGE_SIZE); // // Do the inspection, clamp the bytes to the minimum // status = PhpAnalyzeImageCoherencyInspect( fileBytes, (ULONG)bytes, buffer, (ULONG)bytes, Context, rva, SkipCallback, SkipCallbackContext ); if (!NT_SUCCESS(status)) break; rva += chunk; remainingBytes -= chunk; } } /** * Analyzes the image coherency as if it were a native application. And expects * certain bytes over the range, used for code cave scanning. * * \param[in] ProcessHandle - Handle to the process requires PROCESS_VM_READ. * \param[in] Rva - Relative virtual address to inspect. * \param[in] Size - Size of data to inspect from the RVA. * \param[in] Context - Image coherency context. * \param[in] ExpectedByte - Expected byte in the entire range. */ VOID PhpAnalyzeImageCoherencyCommonByRvaExpectBytes( _In_ HANDLE ProcessHandle, _In_ ULONG Rva, _In_ ULONG Size, _In_ PPH_IMAGE_COHERENCY_CONTEXT Context, _In_ BYTE ExpectedByte ) { NTSTATUS status; BYTE buffer[PAGE_SIZE]; BYTE expected[PAGE_SIZE]; ULONG remainingBytes; ULONG chunk; SIZE_T bytesRead; ULONG rva; remainingBytes = Size; rva = Rva; RtlFillMemory(expected, PAGE_SIZE, ExpectedByte); while (remainingBytes > 0) { chunk = PAGE_SIZE; if (chunk > remainingBytes) { chunk = remainingBytes; } // // Try to read the remote process // if (NT_SUCCESS(PhImageCoherencyReadVirtualMemoryCallback( ProcessHandle, PTR_ADD_OFFSET(Context->RemoteImageBase, rva), buffer, chunk, &bytesRead, Context ))) { assert(bytesRead <= PAGE_SIZE); // // Do the inspection // status = PhpAnalyzeImageCoherencyInspect( expected, (ULONG)bytesRead, buffer, (ULONG)bytesRead, Context, rva, NULL, NULL ); if (!NT_SUCCESS(status)) break; } rva += chunk; remainingBytes -= chunk; } } /** * Skip bytes callback to skip bytes during inspection. * * \param[in] Rva - Current rva in the range being inspected. * \param[in] Context - Relocation skip context. * \return Number of bytes to skip, 0 otherwise. */ _Function_class_(PH_IMGCOHERENCY_SKIP_BYTE_CALLBACK) ULONG CALLBACK PhpImgCoherencySkip( _In_ ULONG Rva, _In_ PVOID Context ) { PPH_IMAGE_COHERENCY_CONTEXT context; PVOID* entry; context = (PPH_IMAGE_COHERENCY_CONTEXT)Context; if (context->MappedImageBaseRva && (context->MappedImageBaseRva == Rva)) { if (context->MappedImage.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) return RTL_FIELD_SIZE(IMAGE_OPTIONAL_HEADER32, ImageBase); else if (context->MappedImage.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) return RTL_FIELD_SIZE(IMAGE_OPTIONAL_HEADER64, ImageBase); else return 0; } if (context->MappedImageReloc) { // // Look up the RVA in our hash table, if we find one we will skip the // number of bytes stored in the hash table for that entry. // entry = PhFindItemSimpleHashtable(context->MappedImageReloc, PTR_ADD_OFFSET(NULL, Rva)); if (entry) return PtrToUlong(*entry); } if (context->MappedImageIatRva && (context->MappedImageIatRva == Rva)) { // // Skip over the import address table. // return context->MappedImageIatSize; } return 0; } /** * Analyzes the image coherency as if it were a native application. * * \param[in] ProcessHandle - Handle to the process requires PROCESS_VM_READ. * \param[in] Context - Image coherency context. */ VOID PhpAnalyzeImageCoherencyCommonAsNative( _In_ HANDLE ProcessHandle, _In_ PPH_IMAGE_COHERENCY_CONTEXT Context ) { ULONG addressOfEntry = 0; PIMAGE_SECTION_HEADER entrySection = NULL; switch (Context->MappedImage.Magic) { case IMAGE_NT_OPTIONAL_HDR32_MAGIC: addressOfEntry = Context->MappedImage.NtHeaders32->OptionalHeader.AddressOfEntryPoint; break; case IMAGE_NT_OPTIONAL_HDR64_MAGIC: addressOfEntry = Context->MappedImage.NtHeaders->OptionalHeader.AddressOfEntryPoint; break; default: break; } PhMappedImageRvaToSection(&Context->MappedImage, addressOfEntry, &entrySection); // // Here we will inspect each executable section. // for (USHORT i = 0; i < __max(Context->MappedImage.NumberOfSections, Context->RemoteMappedImage.NumberOfSections); i++) { if ((i < Context->MappedImage.NumberOfSections) && (i < Context->RemoteMappedImage.NumberOfSections)) { PIMAGE_SECTION_HEADER mappedSection; PIMAGE_SECTION_HEADER remoteMappedSection; mappedSection = &Context->MappedImage.Sections[i]; remoteMappedSection = &Context->RemoteMappedImage.Sections[i]; if (PhpShouldScanSection(Context->Type, mappedSection) || PhpShouldScanSection(Context->Type, remoteMappedSection)) { ULONG size; SIZE_T prevTotal; SIZE_T bytesInspected; SIZE_T prevSkipped; SIZE_T bytesSkipped; size = PhpGetSectionScanSize(Context->Type, mappedSection); prevTotal = Context->TotalBytes; prevSkipped = Context->SkippedBytes; PhpAnalyzeImageCoherencyCommonByRva( ProcessHandle, mappedSection->VirtualAddress, size, Context, PhpImgCoherencySkip, Context ); bytesInspected = (Context->TotalBytes - prevTotal); bytesSkipped = (Context->SkippedBytes - prevSkipped); if (entrySection == mappedSection) { ULONG length; // // Make sure we scanned enough of the entry point. // If not, force scan that part. // length = __min(entrySection->SizeOfRawData, entrySection->Misc.VirtualSize); length -= (addressOfEntry - entrySection->VirtualAddress); if (length > PH_IMGCOHERENCY_MIN_ENTRY_INSPECT) { length = PH_IMGCOHERENCY_MIN_ENTRY_INSPECT; } if ((bytesInspected + bytesSkipped) < (((ULONGLONG)addressOfEntry - entrySection->VirtualAddress) + length)) { PhpAnalyzeImageCoherencyCommonByRva( ProcessHandle, addressOfEntry, length, Context, PhpImgCoherencySkip, Context ); } } // // If we're doing a full scan, inspect for possible code caves // beyond on-disk content. // If VirtualAddress is greater than SizeOfRawData the loader // will zero initialize the bytes to extend the mapping. // If there is non-zero content here someone has written // into the code cave. // if ((Context->Type == PhImageCoherencyFull) && ((mappedSection->Characteristics & IMAGE_SCN_MEM_EXECUTE) != 0) && (mappedSection->Misc.VirtualSize > mappedSection->SizeOfRawData)) { PhpAnalyzeImageCoherencyCommonByRvaExpectBytes( ProcessHandle, mappedSection->VirtualAddress + mappedSection->SizeOfRawData, mappedSection->Misc.VirtualSize - mappedSection->SizeOfRawData, Context, 0x00 ); } } } else { // // There are a mismatched number of sections // Inflate the total bytes // if (i < Context->MappedImage.NumberOfSections) { Context->TotalBytes += __min(Context->MappedImage.Sections[i].SizeOfRawData, Context->MappedImage.Sections[i].Misc.VirtualSize); } else { Context->TotalBytes += __min(Context->RemoteMappedImage.Sections[i].SizeOfRawData, Context->RemoteMappedImage.Sections[i].Misc.VirtualSize); } } } } /** * Analyzes the image coherency as if it were a managed (.NET) application. * * \param[in] ProcessHandle - Handle to the process requires PROCESS_VM_READ. * \param[in] Context - Image coherency context. */ VOID PhpAnalyzeImageCoherencyCommonAsManaged( _In_ HANDLE ProcessHandle, _In_ PPH_IMAGE_COHERENCY_CONTEXT Context ) { PIMAGE_DATA_DIRECTORY dataDirectory; PIMAGE_COR20_HEADER dotNet; // // We will check for coherency across two blocks here // 1. The COR20 header // 2. The .NET Meta Data // // // Get the COM32 directory bytes // if (!NT_SUCCESS(PhGetMappedImageDataDirectory( &Context->MappedImage, IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &dataDirectory ))) { return; } // // Inspect the COR20 header // PhpAnalyzeImageCoherencyCommonByRva( ProcessHandle, dataDirectory->VirtualAddress, dataDirectory->Size, Context, NULL, NULL ); // // Get the .NET MetaData // if (!NT_SUCCESS(PhMappedImageRvaToVa( &Context->MappedImage, dataDirectory->VirtualAddress, &dotNet)) || (dotNet->MetaData.Size == 0) || !dotNet->MetaData.VirtualAddress) { return; } // // Inspect the .NET MetaData // PhpAnalyzeImageCoherencyCommonByRva( ProcessHandle, dotNet->MetaData.VirtualAddress, dotNet->MetaData.Size, Context, NULL, NULL ); } /** * Checks if the image is a .NET application. * * \param[in] Context - Image coherency context. * \return TRUE if the image is a .NET application, FALSE otherwise. */ BOOLEAN PhpAnalyzeImageCoherencyIsDotNet ( _In_ PPH_IMAGE_COHERENCY_CONTEXT Context ) { PIMAGE_DATA_DIRECTORY dataDirectory; PIMAGE_COR20_HEADER dotNet; PULONG dotNetMagic; // // Get the com descriptor directly, if it doesn't exist it isn't .NET // if (!NT_SUCCESS(PhGetMappedImageDataDirectory( &Context->MappedImage, IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &dataDirectory ))) { return FALSE; } // // Check for the COR20 header // if (!NT_SUCCESS(PhMappedImageRvaToVa( &Context->MappedImage, dataDirectory->VirtualAddress, &dotNet)) || (dotNet->cb != sizeof(IMAGE_COR20_HEADER))) { return FALSE; } if (!NT_SUCCESS(PhMappedImageRvaToVa( &Context->MappedImage, dotNet->MetaData.VirtualAddress, &dotNetMagic)) || !dotNetMagic) { return FALSE; } // // If we can locate the magic number and it equal the .NET magic then we // are reasonably confident it is .NET. // return (dotNetMagic && *dotNetMagic == 0x424A5342); } /** * Common analysis for image coherency. * * \param[in] ProcessHandle - Handle to the process requires PROCESS_VM_READ. * \param[in] Context - Image coherency context. */ VOID PhpAnalyzeImageCoherencyCommon( _In_ HANDLE ProcessHandle, _In_ PPH_IMAGE_COHERENCY_CONTEXT Context ) { // // Loop over the number of sections and include them in the calculation // for (USHORT i = 0; i < __max(Context->MappedImage.NumberOfSections, Context->RemoteMappedImage.NumberOfSections); i++) { if ((i < Context->MappedImage.NumberOfSections) && (i < Context->RemoteMappedImage.NumberOfSections)) { PhpAnalyzeImageCoherencyInspect( (PBYTE)&Context->MappedImage.Sections[i], IMAGE_SIZEOF_SECTION_HEADER, (PBYTE)&Context->RemoteMappedImage.Sections[i], IMAGE_SIZEOF_SECTION_HEADER, Context, 0, NULL, NULL ); } else { // // There are a mismatched number of sections // Inflate the total bytes // Context->TotalBytes += IMAGE_SIZEOF_SECTION_HEADER; } } // // If we detect the on-disk image is a .NET application use the managed // path, this will assume the remote image is a .NET app, if it isn't // we'll detect lots of incoherent bytes. // // Otherwise use the native path and inspect by the supplied RVAs // if (PhpAnalyzeImageCoherencyIsDotNet(Context)) PhpAnalyzeImageCoherencyCommonAsManaged(ProcessHandle, Context); else PhpAnalyzeImageCoherencyCommonAsNative(ProcessHandle, Context); } /** * Analyzes image coherency for 32 bit images. * * \param[in] ProcessHandle - Handle to the process requires PROCESS_VM_READ. * \param[in] Context - Image coherency context. * \return Success status or failure. */ NTSTATUS PhpAnalyzeImageCoherencyNt32( _In_ HANDLE ProcessHandle, _In_ PPH_IMAGE_COHERENCY_CONTEXT Context ) { PIMAGE_OPTIONAL_HEADER32 fileOptHeader; PIMAGE_OPTIONAL_HEADER32 procOptHeader; fileOptHeader = (PIMAGE_OPTIONAL_HEADER32)&Context->MappedImage.NtHeaders32->OptionalHeader; procOptHeader = (PIMAGE_OPTIONAL_HEADER32)&Context->RemoteMappedImage.NtHeaders32->OptionalHeader; // // Inspect the header // PhpAnalyzeImageCoherencyInspect( (PBYTE)Context->MappedImage.NtHeaders32, UFIELD_OFFSET(IMAGE_NT_HEADERS32, OptionalHeader), (PBYTE)Context->RemoteMappedImage.NtHeaders32, UFIELD_OFFSET(IMAGE_NT_HEADERS32, OptionalHeader), Context, PtrToUlong(PTR_SUB_OFFSET(Context->MappedImage.NtHeaders32, Context->MappedImage.ViewBase)), PhpImgCoherencySkip, Context ); // // Inspect the optional header // PhpAnalyzeImageCoherencyInspect( (PBYTE)fileOptHeader, sizeof(IMAGE_OPTIONAL_HEADER32), (PBYTE)procOptHeader, sizeof(IMAGE_OPTIONAL_HEADER32), Context, PtrToUlong(PTR_SUB_OFFSET(fileOptHeader, Context->MappedImage.ViewBase)), PhpImgCoherencySkip, Context ); // // Do the common inspection // PhpAnalyzeImageCoherencyCommon(ProcessHandle, Context); if (fileOptHeader->CheckSum != procOptHeader->CheckSum) { return STATUS_INVALID_IMAGE_HASH; } return STATUS_SUCCESS; } /** * Analyzes image coherency for 64 bit images. * * \param[in] ProcessHandle - Handle to the process requires PROCESS_VM_READ. * \param[in] Context - Image coherency context. * \return Success status or failure. */ NTSTATUS PhpAnalyzeImageCoherencyNt64( _In_ HANDLE ProcessHandle, _In_ PPH_IMAGE_COHERENCY_CONTEXT Context ) { PIMAGE_OPTIONAL_HEADER64 fileOptHeader; PIMAGE_OPTIONAL_HEADER64 procOptHeader; fileOptHeader = (PIMAGE_OPTIONAL_HEADER64)&Context->MappedImage.NtHeaders->OptionalHeader; procOptHeader = (PIMAGE_OPTIONAL_HEADER64)&Context->RemoteMappedImage.NtHeaders->OptionalHeader; // // Inspect the header // PhpAnalyzeImageCoherencyInspect( (PBYTE)Context->MappedImage.NtHeaders, UFIELD_OFFSET(IMAGE_NT_HEADERS64, OptionalHeader), (PBYTE)Context->RemoteMappedImage.NtHeaders, UFIELD_OFFSET(IMAGE_NT_HEADERS64, OptionalHeader), Context, PtrToUlong(PTR_SUB_OFFSET(Context->MappedImage.NtHeaders, Context->MappedImage.ViewBase)), PhpImgCoherencySkip, Context ); // // Inspect the optional header // PhpAnalyzeImageCoherencyInspect( (PBYTE)fileOptHeader, sizeof(IMAGE_OPTIONAL_HEADER64), (PBYTE)procOptHeader, sizeof(IMAGE_OPTIONAL_HEADER64), Context, PtrToUlong(PTR_SUB_OFFSET(fileOptHeader, Context->MappedImage.ViewBase)), PhpImgCoherencySkip, Context ); // // Do the common inspection // PhpAnalyzeImageCoherencyCommon(ProcessHandle, Context); if (fileOptHeader->CheckSum != procOptHeader->CheckSum) { // // The optional header checksum doesn't match the in memory checksum // return status to reflect that. We could inflate the TotalBytes here // but choose not. // return STATUS_INVALID_IMAGE_HASH; } return STATUS_SUCCESS; } /** * Inspects the process image coherency compared to the file on disk. * * \param[in] ProcessHandle - Handle to the process requires PROCESS_VM_READ. * \param[in] Context - Image coherency context. * \param[out] ImageCoherency Image coherency value between 0 and 1. This * indicates how similar the image on-disk is compared to what is mapped into * the process. A value of 1 means coherent while a value lower than 1 * indicates how incoherent the image is. * * \return Status indicating the coherency calculation, note errors may indicate * partial success. * STATUS_SUCCESS The coherency calculation was successful. * STATUS_INVALID_IMAGE_HASH The coherency calculation was successful or * partially successful and unusually incoherent. * STATUS_INVALID_IMAGE_FORMAT The coherency calculation was successful or * partially successful and unusually incoherent. * STATUS_SUBSYSTEM_NOT_PRESENT The coherency calculation was successful or * partially successful and unusually incoherent. * All other errors are failures to calculate. */ NTSTATUS PhpInspectForImageCoherency( _In_ HANDLE ProcessHandle, _In_ PPH_IMAGE_COHERENCY_CONTEXT Context, _Out_ PFLOAT ImageCoherency ) { NTSTATUS status; // // Creating the coherency context is best-effort and will store the status // in the context. We can infer some state if one fails, do so here. // if (!NT_SUCCESS(Context->MappedImageStatus) || !NT_SUCCESS(Context->RemoteMappedImageStatus)) { if (NT_SUCCESS(Context->RemoteMappedImageStatus) && ((Context->MappedImageStatus == STATUS_INVALID_IMAGE_FORMAT) || (Context->MappedImageStatus == STATUS_INVALID_IMAGE_HASH) || (Context->MappedImageStatus == STATUS_IMAGE_SUBSYSTEM_NOT_PRESENT))) { // // If we succeeded to map the remote image but the on-disk image // is inherently incoherent, return the status from the on-disk // mapping. // status = Context->MappedImageStatus; } else { // // If we failed for any other reason, return the error from the // remote mapping. // status = Context->RemoteMappedImageStatus; } goto CleanupExit; } if (Context->MappedImage.Magic != Context->RemoteMappedImage.Magic) { // // The image types don't match. This is incoherent but we have one more // case to check. .NET can change this in the loaded PE to run "AnyCPU" // in some cases, if it's a .NET mapping do the managed calculation. // if (PhpAnalyzeImageCoherencyIsDotNet(Context)) PhpAnalyzeImageCoherencyCommonAsManaged(ProcessHandle, Context); status = STATUS_INVALID_IMAGE_FORMAT; goto CleanupExit; } switch (Context->MappedImage.Magic) { case IMAGE_NT_OPTIONAL_HDR32_MAGIC: status = PhpAnalyzeImageCoherencyNt32(ProcessHandle, Context); break; case IMAGE_NT_OPTIONAL_HDR64_MAGIC: status = PhpAnalyzeImageCoherencyNt64(ProcessHandle, Context); break; default: { // // Not supporting ELF for WSL yet. Note however, if the image type // is mismatched above we will still reflect that // (e.g. ELF<->non-ELF). But we don't yet support coherency checks // for ELF<->ELF. The remote image mapping should be updated to // handle remote mapping ELF. // status = STATUS_NOT_IMPLEMENTED; } break; } CleanupExit: if (Context->TotalBytes) *ImageCoherency = (FLOAT)Context->CoherentBytes / (FLOAT)Context->TotalBytes; else *ImageCoherency = 0.0f; return status; } /** * Inspects a module image coherency compared to the file on disk. * * \param[in] FileName Win32 path to the image file on disk. * \param[in] ProcessHandle - Handle to the process where the module is mapped * requires PROCESS_VM_READ. * \param[in] RemoteImageBase - Base address of the image. * \param[in] RemoteImageSize - Size of the image. * \param[in] RemoteImageBaseStatus - If RemoteImageBase is null, this is stored * in the context instead of attempting to map the image. * \param[in] IsKernelModule - Notes if this is a kernel module. * \param[in] Type - Image coherency scan type. * \param[out] ImageCoherency Image coherency value between 0 and 1. This * indicates how similar the image on-disk is compared to what is mapped into * the process. A value of 1 means coherent while a value lower than 1 * indicates how incoherent the image is. * * \return Status indicating the coherency calculation, note errors may indicate * partial success. * STATUS_SUCCESS The coherency calculation was successful. * STATUS_INVALID_IMAGE_HASH The coherency calculation was successful or * partially successful and unusually incoherent. * STATUS_INVALID_IMAGE_FORMAT The coherency calculation was successful or * partially successful and unusually incoherent. * STATUS_SUBSYSTEM_NOT_PRESENT The coherency calculation was successful or * partially successful and unusually incoherent. * All other errors are failures to calculate. */ NTSTATUS PhpGetModuleCoherency( _In_ PPH_STRING FileName, _In_ HANDLE ProcessHandle, _In_opt_ PVOID RemoteImageBase, _In_opt_ SIZE_T RemoteImageSize, _In_ NTSTATUS RemoteImageBaseStatus, _In_ BOOLEAN IsKernelModule, _In_ PH_IMAGE_COHERENCY_SCAN_TYPE Type, _Out_ PFLOAT ImageCoherency ) { NTSTATUS status; *ImageCoherency = 0.0f; if (Type == PhImageCoherencySharedOriginal) { SIZE_T numberOfPages; SIZE_T numberOfTamperedPages; if (!RemoteImageBase || RemoteImageSize < PAGE_SIZE) return STATUS_INVALID_PARAMETER; status = PhCheckImagePagesForTampering( ProcessHandle, RemoteImageBase, RemoteImageSize, &numberOfPages, &numberOfTamperedPages ); if (NT_SUCCESS(status)) { *ImageCoherency = (FLOAT)(numberOfPages - numberOfTamperedPages) / (FLOAT)numberOfPages; } } else { PPH_IMAGE_COHERENCY_CONTEXT context; context = PhpCreateImageCoherencyContext( Type, FileName, ProcessHandle, RemoteImageBase, RemoteImageSize, RemoteImageBaseStatus, IsKernelModule ); status = PhpInspectForImageCoherency(ProcessHandle, context, ImageCoherency); PhpFreeImageCoherencyContext(context); } return status; } /** * Inspects the process image coherency compared to the file on disk. * * \param[in] FileName Win32 path to the image file on disk. * \param[in] ProcessId Process ID of the active process to compare to. * \param[in] Type - Image coherency scan type. * \param[out] ImageCoherency Image coherency value between 0 and 1. This * indicates how similar the image on-disk is compared to what is mapped into * the process. A value of 1 means coherent while a value lower than 1 * indicates how incoherent the image is. * * \return Status indicating the coherency calculation, note errors may indicate * partial success. * STATUS_SUCCESS The coherency calculation was successful. * STATUS_INVALID_IMAGE_HASH The coherency calculation was successful or * partially successful and unusually incoherent. * STATUS_INVALID_IMAGE_FORMAT The coherency calculation was successful or * partially successful and unusually incoherent. * STATUS_SUBSYSTEM_NOT_PRESENT The coherency calculation was successful or * partially successful and unusually incoherent. * All other errors are failures to calculate. */ NTSTATUS PhGetProcessImageCoherency( _In_ PPH_STRING FileName, _In_ HANDLE ProcessId, _In_ PH_IMAGE_COHERENCY_SCAN_TYPE Type, _Out_ PFLOAT ImageCoherency ) { NTSTATUS status; HANDLE processHandle; PVOID remoteImageBase; PVOID imageBase; SIZE_T imageSize; remoteImageBase = NULL; imageBase = NULL; imageSize = 0; *ImageCoherency = 0.0f; // Try to get a handle with query information + vm read access. if (!NT_SUCCESS(status = PhOpenProcess( &processHandle, PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, ProcessId ))) { // Try to get a handle with query limited information + vm read access. if (!NT_SUCCESS(status = PhOpenProcess( &processHandle, PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_VM_READ, ProcessId ))) { processHandle = NULL; goto CleanupExit; } } // // Get the remote image base // status = PhGetProcessImageBaseAddress(processHandle, &remoteImageBase); if (NT_SUCCESS(status)) { status = PhGetProcessMappedImageBaseFromAddress( processHandle, remoteImageBase, &imageBase, &imageSize ); } if (NT_SUCCESS(status)) { status = PhpGetModuleCoherency( FileName, processHandle, imageBase, imageSize, status, FALSE, Type, ImageCoherency ); } CleanupExit: if (processHandle) { NtClose(processHandle); } return status; } /** * Inspects a module image coherency compared to the file on disk. * * \param[in] FileName Win32 path to the image file on disk. * \param[in] ProcessHandle - Handle to the process where the module is mapped * requires PROCESS_VM_READ. * \param[in] ImageBaseAddress - Base address of the image. * \param[in] ImageSize - Size of the image. * \param[in] IsKernelModule - Notes if this is a kernel module. * \param[in] Type - Image coherency scan type. * \param[out] ImageCoherency Image coherency value between 0 and 1. This * indicates how similar the image on-disk is compared to what is mapped into * the process. A value of 1 means coherent while a value lower than 1 * indicates how incoherent the image is. * * \return Status indicating the coherency calculation, note errors may indicate * partial success. * STATUS_SUCCESS The coherency calculation was successful. * STATUS_INVALID_IMAGE_HASH The coherency calculation was successful or * partially successful and unusually incoherent. * STATUS_INVALID_IMAGE_FORMAT The coherency calculation was successful or * partially successful and unusually incoherent. * STATUS_SUBSYSTEM_NOT_PRESENT The coherency calculation was successful or * partially successful and unusually incoherent. * All other errors are failures to calculate. */ NTSTATUS PhGetProcessModuleImageCoherency( _In_ PPH_STRING FileName, _In_ HANDLE ProcessHandle, _In_ PVOID ImageBaseAddress, _In_ SIZE_T ImageSize, _In_ BOOLEAN IsKernelModule, _In_ PH_IMAGE_COHERENCY_SCAN_TYPE Type, _Out_ PFLOAT ImageCoherency ) { return PhpGetModuleCoherency( FileName, ProcessHandle, ImageBaseAddress, ImageSize, STATUS_SUCCESS, IsKernelModule, Type, ImageCoherency ); } /** * \brief Checks the image pages for tampering. * * \details Check out our blog for more info: * https://windows-internals.com/understanding-a-new-mitigation-module-tampering-protection/ * * \param[in] ProcessHandle - Handle to the process where the module is mapped. * \param[in] BaseAddress - Base address of the image pages to check. * \param[in] SizeOfImage - Size of the image to check. * \param[out] NumberOfPages - Number of pages checked. * \param[out] NumberOfTamperedPages - Number of tampered pages. * \return Successful or errant status. */ NTSTATUS PhCheckImagePagesForTampering( _In_ HANDLE ProcessHandle, _In_ PVOID BaseAddress, _In_ SIZE_T SizeOfImage, _Out_ PSIZE_T NumberOfPages, _Out_ PSIZE_T NumberOfTamperedPages ) { NTSTATUS status; SIZE_T numberOfPages; ULONG_PTR virtualAddress; MEMORY_WORKING_SET_EX_INFORMATION* info; SIZE_T i; *NumberOfPages = 0; *NumberOfTamperedPages = 0; numberOfPages = ADDRESS_AND_SIZE_TO_SPAN_PAGES(BaseAddress, SizeOfImage); virtualAddress = (ULONG_PTR)PAGE_ALIGN(BaseAddress); if (!numberOfPages) return STATUS_INVALID_PARAMETER; info = PhAllocatePage(numberOfPages * sizeof(MEMORY_WORKING_SET_EX_INFORMATION), NULL); if (!info) return STATUS_NO_MEMORY; for (i = 0; i < numberOfPages; i++) { info[i].VirtualAddress = (PVOID)virtualAddress; virtualAddress += PAGE_SIZE; } status = NtQueryVirtualMemory( ProcessHandle, NULL, MemoryWorkingSetExInformation, info, numberOfPages * sizeof(MEMORY_WORKING_SET_EX_INFORMATION), NULL ); if (NT_SUCCESS(status)) { *NumberOfPages = numberOfPages; for (i = 0; i < numberOfPages; i++) { PMEMORY_WORKING_SET_EX_BLOCK page = &info[i].VirtualAttributes; if (!page->SharedOriginal) { (*NumberOfTamperedPages)++; } } } PhFreePage(info); return status; }