@@ -12,23 +12,29 @@ typedef struct _MMP_FAKE_HANDLE_LIST_ENTRY {
1212 LIST_ENTRY InMmpFakeHandleList;
1313 HANDLE hObject;
1414 PVOID value;
15+ BOOL bImageMapping;
1516}MMP_FAKE_HANDLE_LIST_ENTRY, * PMMP_FAKE_HANDLE_LIST_ENTRY;
1617
1718static decltype (&CreateFileW) OriginCreateFileW = CreateFileW;
1819static decltype (&GetFileInformationByHandle) OriginGetFileInformationByHandle = GetFileInformationByHandle;
1920static decltype (&GetFileAttributesExW) OriginGetFileAttributesExW = GetFileAttributesExW;
21+ static decltype (&GetFileSize) OriginGetFileSize = GetFileSize;
22+ static decltype (&GetFileSizeEx) OriginGetFileSizeEx = GetFileSizeEx;
2023static decltype (&CreateFileMappingW) OriginCreateFileMappingW = CreateFileMappingW;
2124static decltype (&MapViewOfFileEx) OriginMapViewOfFileEx = MapViewOfFileEx;
25+ static decltype (&MapViewOfFile) OriginMapViewOfFile = MapViewOfFile;
2226static decltype (&UnmapViewOfFile)OriginUnmapViewOfFile = UnmapViewOfFile;
2327static decltype (&CloseHandle)OriginCloseHandle = CloseHandle;
24- static GetFileVersion_T OriginGetFileVersion = nullptr ;
28+ static GetFileVersion_T OriginGetFileVersion1 = nullptr ;
29+ static GetFileVersion_T OriginGetFileVersion2 = nullptr ;
2530
2631FILETIME AssemblyTimes;
2732
2833CRITICAL_SECTION MmpFakeHandleListLock;
2934LIST_ENTRY MmpFakeHandleListHead;
3035
31- static BOOL Initialized = FALSE ;
36+ static BOOLEAN PreHooked = FALSE ;
37+ static BOOLEAN Initialized = FALSE ;
3238
3339BOOL MmpIsMemoryModuleFileName (
3440 _In_ LPCWSTR lpFileName,
@@ -74,10 +80,12 @@ BOOL MmpIsMemoryModuleFileName(
7480
7581VOID MmpInsertHandleEntry (
7682 _In_ HANDLE hObject,
77- _In_ PVOID value) {
83+ _In_ PVOID value,
84+ _In_ BOOL bImageMapping = FALSE ) {
7885 auto entry = (PMMP_FAKE_HANDLE_LIST_ENTRY)RtlAllocateHeap (RtlProcessHeap (), 0 , sizeof (MMP_FAKE_HANDLE_LIST_ENTRY));
7986 entry->hObject = hObject;
8087 entry->value = value;
88+ entry->bImageMapping = bImageMapping;
8189
8290 EnterCriticalSection (&MmpFakeHandleListLock);
8391 InsertTailList (&MmpFakeHandleListHead, &entry->InMmpFakeHandleList );
@@ -193,6 +201,49 @@ BOOL WINAPI HookGetFileAttributesExW(
193201 );
194202}
195203
204+ DWORD WINAPI HookGetFileSize (
205+ _In_ HANDLE hFile,
206+ _Out_opt_ LPDWORD lpFileSizeHigh) {
207+
208+ auto iter = MmpFindHandleEntry (hFile);
209+ if (iter) {
210+ if (lpFileSizeHigh)*lpFileSizeHigh = 0 ;
211+
212+ auto entry = (PLDR_DATA_TABLE_ENTRY)iter->value ;
213+ auto module = MapMemoryModuleHandle ((HMEMORYMODULE)entry->DllBase );
214+
215+ return module ->dwImageFileSize ;
216+ }
217+ else {
218+ return OriginGetFileSize (
219+ hFile,
220+ lpFileSizeHigh
221+ );
222+ }
223+
224+ }
225+
226+ BOOL WINAPI HookGetFileSizeEx (
227+ _In_ HANDLE hFile,
228+ _Out_ PLARGE_INTEGER lpFileSize) {
229+
230+ auto iter = MmpFindHandleEntry (hFile);
231+ if (iter) {
232+ auto entry = (PLDR_DATA_TABLE_ENTRY)iter->value ;
233+ auto module = MapMemoryModuleHandle ((HMEMORYMODULE)entry->DllBase );
234+
235+ lpFileSize->QuadPart = module ->dwImageFileSize ;
236+ return TRUE ;
237+ }
238+ else {
239+ return OriginGetFileSizeEx (
240+ hFile,
241+ lpFileSize
242+ );
243+ }
244+
245+ }
246+
196247HANDLE WINAPI HookCreateFileMappingW (
197248 _In_ HANDLE hFile,
198249 _In_opt_ LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
@@ -205,7 +256,7 @@ HANDLE WINAPI HookCreateFileMappingW(
205256 if (iter) {
206257 HANDLE hEvent = CreateEventW (nullptr , TRUE , FALSE , nullptr );
207258
208- MmpInsertHandleEntry (hEvent, iter->value );
259+ MmpInsertHandleEntry (hEvent, iter->value , !!(flProtect & SEC_IMAGE) );
209260 return hEvent;
210261 }
211262
@@ -233,9 +284,13 @@ LPVOID WINAPI HookMapViewOfFileEx(
233284 auto entry = (PLDR_DATA_TABLE_ENTRY)iter->value ;
234285 auto pModule = MapMemoryModuleHandle ((HMEMORYMODULE)entry->DllBase );
235286 if (pModule) {
236- MemoryLoadLibrary (&hModule, pModule->lpReserved , pModule->dwImageFileSize );
237-
238- if (hModule) MmpInsertHandleEntry (hModule, hModule);
287+ if (iter->bImageMapping ) {
288+ MemoryLoadLibrary (&hModule, pModule->lpReserved , pModule->dwImageFileSize );
289+ if (hModule) MmpInsertHandleEntry (hModule, hModule);
290+ }
291+ else {
292+ return pModule->lpReserved ;
293+ }
239294 }
240295
241296 return hModule;
@@ -251,6 +306,24 @@ LPVOID WINAPI HookMapViewOfFileEx(
251306 );
252307}
253308
309+ LPVOID WINAPI HookMapViewOfFile (
310+ _In_ HANDLE hFileMappingObject,
311+ _In_ DWORD dwDesiredAccess,
312+ _In_ DWORD dwFileOffsetHigh,
313+ _In_ DWORD dwFileOffsetLow,
314+ _In_ SIZE_T dwNumberOfBytesToMap) {
315+
316+ return HookMapViewOfFileEx (
317+ hFileMappingObject,
318+ dwDesiredAccess,
319+ dwFileOffsetHigh,
320+ dwFileOffsetLow,
321+ dwNumberOfBytesToMap,
322+ nullptr
323+ );
324+
325+ }
326+
254327BOOL WINAPI HookUnmapViewOfFile (_In_ LPCVOID lpBaseAddress) {
255328 auto iter = MmpFindHandleEntry ((HANDLE)lpBaseAddress);
256329 if (iter) {
@@ -309,49 +382,81 @@ HRESULT WINAPI HookGetFileVersion(
309382
310383 }
311384
312- return OriginGetFileVersion (
385+ return OriginGetFileVersion1 (
313386 szFilename,
314387 szBuffer,
315388 cchBuffer,
316389 dwLength
317390 );
318391}
319392
393+ BOOL WINAPI MmpPreInitializeHooksForDotNet () {
394+
395+ EnterCriticalSection (NtCurrentPeb ()->FastPebLock );
396+
397+ if (!PreHooked) {
398+ HMODULE hModule = LoadLibraryW (L" mscoree.dll" );
399+ if (hModule) {
400+ OriginGetFileVersion2 = (GetFileVersion_T)GetProcAddress (hModule, " GetFileVersion" );
401+ if (OriginGetFileVersion2) {
402+
403+ GetSystemTimeAsFileTime (&AssemblyTimes);
404+
405+ InitializeCriticalSection (&MmpFakeHandleListLock);
406+ InitializeListHead (&MmpFakeHandleListHead);
407+
408+ DetourTransactionBegin ();
409+ DetourUpdateThread (NtCurrentThread ());
410+
411+ DetourAttach ((PVOID*)&OriginCreateFileW, HookCreateFileW);
412+ DetourAttach ((PVOID*)&OriginGetFileInformationByHandle, HookGetFileInformationByHandle);
413+ DetourAttach ((PVOID*)&OriginGetFileAttributesExW, HookGetFileAttributesExW);
414+ DetourAttach ((PVOID*)&OriginGetFileSize, HookGetFileSize);
415+ DetourAttach ((PVOID*)&OriginGetFileSizeEx, HookGetFileSizeEx);
416+ DetourAttach ((PVOID*)&OriginCreateFileMappingW, HookCreateFileMappingW);
417+ DetourAttach ((PVOID*)&OriginMapViewOfFileEx, HookMapViewOfFileEx);
418+ DetourAttach ((PVOID*)&OriginMapViewOfFile, HookMapViewOfFile);
419+ DetourAttach ((PVOID*)&OriginUnmapViewOfFile, HookUnmapViewOfFile);
420+ DetourAttach ((PVOID*)&OriginCloseHandle, HookCloseHandle);
421+ DetourAttach ((PVOID*)&OriginGetFileVersion2, HookGetFileVersion);
422+
423+ DetourTransactionCommit ();
424+
425+ PreHooked = TRUE ;
426+ }
427+ }
428+ }
429+
430+ LeaveCriticalSection (NtCurrentPeb ()->FastPebLock );
431+
432+ return PreHooked;
433+ }
434+
320435BOOL WINAPI MmpInitializeHooksForDotNet () {
321436 HMODULE hModule = GetModuleHandleW (L" mscoreei.dll" );
322- if (!hModule) {
323- RtlRaiseStatus (STATUS_NOT_SUPPORTED);
324- return FALSE ;
325- }
437+ if (hModule) {
438+ OriginGetFileVersion1 = (GetFileVersion_T)GetProcAddress (hModule, " GetFileVersion" );
439+ if (OriginGetFileVersion1) {
326440
327- OriginGetFileVersion = (GetFileVersion_T)GetProcAddress (hModule, " GetFileVersion" );
328- if (!OriginGetFileVersion) {
329- RtlRaiseStatus (STATUS_NOT_SUPPORTED);
330- return FALSE ;
331- }
441+ EnterCriticalSection (NtCurrentPeb ()->FastPebLock );
332442
333- GetSystemTimeAsFileTime (&AssemblyTimes);
443+ if (!PreHooked) {
444+ LeaveCriticalSection (NtCurrentPeb ()->FastPebLock );
445+ return FALSE ;
446+ }
334447
335- EnterCriticalSection (NtCurrentPeb ()->FastPebLock );
336- if (!Initialized) {
337-
338- InitializeCriticalSection (&MmpFakeHandleListLock);
339- InitializeListHead (&MmpFakeHandleListHead);
340-
341- DetourTransactionBegin ();
342- DetourUpdateThread (NtCurrentThread ());
343- DetourAttach ((PVOID*)&OriginCreateFileW, HookCreateFileW);
344- DetourAttach ((PVOID*)&OriginGetFileInformationByHandle, HookGetFileInformationByHandle);
345- DetourAttach ((PVOID*)&OriginGetFileAttributesExW, HookGetFileAttributesExW);
346- DetourAttach ((PVOID*)&OriginCreateFileMappingW, HookCreateFileMappingW);
347- DetourAttach ((PVOID*)&OriginMapViewOfFileEx, HookMapViewOfFileEx);
348- DetourAttach ((PVOID*)&OriginUnmapViewOfFile, HookUnmapViewOfFile);
349- DetourAttach ((PVOID*)&OriginCloseHandle, HookCloseHandle);
350- DetourAttach ((PVOID*)&OriginGetFileVersion, HookGetFileVersion);
351- DetourTransactionCommit ();
352- Initialized = TRUE ;
448+ if (!Initialized) {
449+ DetourTransactionBegin ();
450+ DetourUpdateThread (NtCurrentThread ());
451+ DetourAttach ((PVOID*)&OriginGetFileVersion1, HookGetFileVersion);
452+ DetourTransactionCommit ();
453+ Initialized = TRUE ;
454+ }
455+
456+ LeaveCriticalSection (NtCurrentPeb ()->FastPebLock );
457+ return TRUE ;
458+ }
353459 }
354- LeaveCriticalSection (NtCurrentPeb ()->FastPebLock );
355460
356- return TRUE ;
461+ return FALSE ;
357462}
0 commit comments