MCPcopy Create free account
hub / github.com/NoMercy-ac/NoMercy / CheckModulePEHeaderFill

Function CheckModulePEHeaderFill

Source/Client/NM_Engine/IModuleScanner.cpp:36–171  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34 return true;
35}
36bool CheckModulePEHeaderFill(HANDLE hProcess, const std::string & szModuleName, PVOID pModuleDump, std::size_t cbHeaderSize)
37{
38 // Disables file system redirection for the calling thread.
39 PVOID OldValue = NULL;
40 if (!g_nmApp->DirFunctionsInstance()->ManageFsRedirection(true, nullptr, &OldValue))
41 return true;
42
43 auto hFile = g_winapiApiTable->CreateFileA(szModuleName.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
44 auto dwLastError = g_winapiApiTable->GetLastError();
45
46 // Restore file system redirection for the calling thread.
47 if (!g_nmApp->DirFunctionsInstance()->ManageFsRedirection(false, OldValue, nullptr))
48 return true;
49
50 if (!IS_VALID_HANDLE(hFile))
51 {
52 SCANNER_LOG(LL_ERR, "CreateFileA fail! File: '%s' Error: %u", szModuleName.c_str(), dwLastError);
53
54 if (dwLastError != ERROR_ACCESS_DENIED && dwLastError != ERROR_SHARING_VIOLATION)
55 return false;
56 return true;
57 }
58
59 LARGE_INTEGER liFileSize;
60 if (!GetFileSizeEx(hFile, &liFileSize))
61 {
62 SCANNER_LOG(LL_ERR, "GetFileSizeEx fail! Error: %u", g_winapiApiTable->GetLastError());
63
64 g_winapiApiTable->CloseHandle(hFile);
65 return false;
66 }
67
68 auto pFileBuffer = g_nmApp->DynamicWinapiInstance()->NTHelper()->Alloc(liFileSize.LowPart, __FUNCTION__);
69 if (!pFileBuffer)
70 {
71 SCANNER_LOG(LL_ERR, "Memory can NOT allocated! Error: %u", g_winapiApiTable->GetLastError());
72
73 g_winapiApiTable->CloseHandle(hFile);
74 return false;
75 }
76
77 DWORD dwBytesRead;
78 if (!ReadFile(hFile, pFileBuffer, liFileSize.LowPart, &dwBytesRead, NULL) || dwBytesRead != liFileSize.LowPart)
79 {
80 SCANNER_LOG(LL_ERR, "ReadFile fail! Error: %u", g_winapiApiTable->GetLastError());
81
82 g_nmApp->DynamicWinapiInstance()->NTHelper()->Free(pFileBuffer);
83 g_winapiApiTable->CloseHandle(hFile);
84 return false;
85 }
86
87 auto pFileIDH = (IMAGE_DOS_HEADER*)pFileBuffer;
88 if (pFileIDH->e_magic != IMAGE_DOS_SIGNATURE)
89 {
90 SCANNER_LOG(LL_ERR, "File dos signature check fail!");
91
92 g_nmApp->DynamicWinapiInstance()->NTHelper()->Free(pFileBuffer);
93 g_winapiApiTable->CloseHandle(hFile);

Callers 1

ScanModuleHeaderFunction · 0.85

Calls 8

ManageFsRedirectionMethod · 0.80
DirFunctionsInstanceMethod · 0.80
CloseHandleMethod · 0.80
NTHelperMethod · 0.80
DynamicWinapiInstanceMethod · 0.80
SafeCloseHandleMethod · 0.80
AllocMethod · 0.45
FreeMethod · 0.45

Tested by

no test coverage detected