https://cocomelonc.github.io/malware/2023/04/08/malware-av-evasion-15.html custom implementation
| 66 | // https://cocomelonc.github.io/malware/2023/04/08/malware-av-evasion-15.html |
| 67 | // custom implementation |
| 68 | HMODULE myGetModuleHandle(LPCWSTR lModuleName) { |
| 69 | |
| 70 | // obtaining the offset of PPEB from the beginning of TEB |
| 71 | // PEB* pPeb = (PEB*)__readgsqword(0x60); |
| 72 | #ifdef _M_IX86 |
| 73 | PEB * pPeb = (PEB *) __readfsdword(0x30); |
| 74 | #else |
| 75 | PEB * pPeb = (PEB *)__readgsqword(0x60); |
| 76 | #endif |
| 77 | |
| 78 | // for x86 |
| 79 | // PEB* pPeb = (PEB*)__readgsqword(0x30); |
| 80 | |
| 81 | // obtaining the address of the head node in a linked list |
| 82 | // which represents all the models that are loaded into the process. |
| 83 | PEB_LDR_DATA* Ldr = pPeb->Ldr; |
| 84 | LIST_ENTRY* ModuleList = &Ldr->InMemoryOrderModuleList; |
| 85 | |
| 86 | // iterating to the next node. this will be our starting point. |
| 87 | LIST_ENTRY* pStartListEntry = ModuleList->Flink; |
| 88 | |
| 89 | // iterating through the linked list. |
| 90 | WCHAR mystr[MAX_PATH] = { 0 }; |
| 91 | WCHAR substr[MAX_PATH] = { 0 }; |
| 92 | for (LIST_ENTRY* pListEntry = pStartListEntry; pListEntry != ModuleList; pListEntry = pListEntry->Flink) { |
| 93 | |
| 94 | // getting the address of current LDR_DATA_TABLE_ENTRY (which represents the DLL). |
| 95 | LDR_DATA_TABLE_ENTRY* pEntry = (LDR_DATA_TABLE_ENTRY*)((BYTE*)pListEntry - sizeof(LIST_ENTRY)); |
| 96 | |
| 97 | // checking if this is the DLL we are looking for |
| 98 | memset(mystr, 0, MAX_PATH * sizeof(WCHAR)); |
| 99 | memset(substr, 0, MAX_PATH * sizeof(WCHAR)); |
| 100 | wcscpy_s(mystr, MAX_PATH, pEntry->FullDllName.Buffer); |
| 101 | wcscpy_s(substr, MAX_PATH, lModuleName); |
| 102 | if (cmpUnicodeStr(substr, mystr)) { |
| 103 | // returning the DLL base address. |
| 104 | return (HMODULE)pEntry->DllBase; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // the needed DLL wasn't found |
| 109 | PRINT_ERROR("failed to get a handle to %s\n", lModuleName); |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | VOID EnableConsoleColors() |
| 114 | { |