Does anyone know where LDR_MODULE.LoadCount is on windows 8?
The following code always prints 6 for the reference count :S I checked with a RemoteDLLTool and the baseaddress and all other information is correct. However, LoadCount is wrong as it is always 6. I read that if it is 6, it means the DLL is loaded dynamically and if it is -1, it is static.
Also is there a way I can just iterate the linked list without having to constantly ReadProcessMemory?
I need to figure out the reference count somehow.. Basically the code below on Windows 7 will tell me how many times a DLL is loaded.. aka the reference count to the 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 on Windows 8?