Does anyone know where LDR_MODULE.LoadCount is located on Windows 8?
The following code always prints 6 for reference counting: S I checked with RemoteDLLTool and baseaddress, and all other information is correct. However, LoadCount is wrong, since it is always 6. I read that if it is 6, it means that the DLL loads dynamically, and if it is -1, it is static.
There is also a way that I can simply iterate over a linked list without having a constant ReadProcessMemory?
I need to somehow calculate the reference count. Basically, the code below in Windows 7 will tell me how many times a DLL loads. aka link refers to a DLL.
#include <winternl.h> typedef struct _LDR_MODULE { LIST_ENTRY InLoadOrderModuleList; LIST_ENTRY InMemoryOrderModuleList; LIST_ENTRY InInitializationOrderModuleList; PVOID BaseAddress; PVOID EntryPoint; ULONG SizeOfImage; UNICODE_STRING FullDllName; UNICODE_STRING BaseDllName; ULONG Flags; SHORT LoadCount; SHORT TlsIndex; LIST_ENTRY HashTableEntry; ULONG TimeDateStamp; } LDR_MODULE, *PLDR_MODULE; int GetModuleLoadCount() { DWORD dwBytesRead = 0; PROCESS_BASIC_INFORMATION PBI = {0}; HANDLE ProcessHandle = GetCurrentProcess(); if (NT_SUCCESS(NtQueryInformationProcess(ProcessHandle, ProcessBasicInformation, &PBI, sizeof(PBI), &dwBytesRead))) { PEB_LDR_DATA LdrData; LDR_MODULE LdrModule; PPEB_LDR_DATA pLdrData = nullptr; PLDR_MODULE pLdrModule = nullptr; char* LdrDataOffset = reinterpret_cast<char*>(PBI.PebBaseAddress) + offsetof(PEB, Ldr); ReadProcessMemory(ProcessHandle, LdrDataOffset, &pLdrData, sizeof(pLdrData), &dwBytesRead); ReadProcessMemory(ProcessHandle, pLdrData, &LdrData, sizeof(LdrData), &dwBytesRead); LIST_ENTRY* Head = LdrData.InMemoryOrderModuleList.Flink; LIST_ENTRY* Next = Head; do { LDR_DATA_TABLE_ENTRY LdrEntry; LDR_DATA_TABLE_ENTRY* Base = CONTAINING_RECORD(Head, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks); if (ReadProcessMemory(ProcessHandle, Base, &LdrEntry, sizeof(LdrEntry), &dwBytesRead)) { char* pLdrModuleOffset = reinterpret_cast<char*>(Head) - sizeof(LIST_ENTRY); ReadProcessMemory(ProcessHandle, pLdrModuleOffset, &pLdrModule, sizeof(pLdrModule), &dwBytesRead); ReadProcessMemory(ProcessHandle, pLdrModule, &LdrModule, sizeof(LdrModule), &dwBytesRead); if (LdrEntry.DllBase) { std::cout<<"BaseAddress: "<< LdrModule.BaseAddress<<std::endl; std::cout<<"Reference Count: "<< LdrModule.LoadCount<<std::endl; } Head = LdrEntry.InMemoryOrderLinks.Flink; } } while (Head != Next); } CloseHandle(ProcessHandle); return 0; }
Any ideas on how to do the same in Windows 8?