| 4 | using namespace WinSys; |
| 5 | |
| 6 | uint32_t WinSys::KernelModuleTracker::EnumModules() { |
| 7 | _modules.clear(); |
| 8 | DWORD size = 1 << 18; |
| 9 | wil::unique_virtualalloc_ptr<> buffer(::VirtualAlloc(nullptr, size,MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE)); |
| 10 | |
| 11 | NTSTATUS status; |
| 12 | status = ::NtQuerySystemInformation(SystemModuleInformationEx, buffer.get(), size, nullptr); |
| 13 | if (!NT_SUCCESS(status)) |
| 14 | return 0; |
| 15 | |
| 16 | if (_modules.empty()) { |
| 17 | _modules.reserve(256); |
| 18 | } |
| 19 | |
| 20 | auto p = (RTL_PROCESS_MODULE_INFORMATION_EX*)buffer.get(); |
| 21 | CHAR winDir[MAX_PATH]; |
| 22 | ::GetWindowsDirectoryA(winDir, _countof(winDir)); |
| 23 | static const std::string root("\\SystemRoot\\"); |
| 24 | static const std::string global("\\??\\"); |
| 25 | |
| 26 | for (;;) { |
| 27 | if (p->BaseInfo.ImageBase == 0) |
| 28 | break; |
| 29 | auto m = std::make_shared<KernelModuleInfo>(); |
| 30 | m->Flags = p->BaseInfo.Flags; |
| 31 | m->FullPath = (const char*)p->BaseInfo.FullPathName; |
| 32 | if (m->FullPath.find(global) == 0) |
| 33 | m->FullPath = m->FullPath.substr(global.size()); |
| 34 | if (m->FullPath.find(root) == 0) |
| 35 | m->FullPath = winDir + m->FullPath.substr(root.size() - 1); |
| 36 | m->ImageBase = p->BaseInfo.ImageBase; |
| 37 | m->MappedBase = p->BaseInfo.MappedBase; |
| 38 | m->ImageSize = p->BaseInfo.ImageSize; |
| 39 | m->InitOrderIndex = p->BaseInfo.InitOrderIndex; |
| 40 | m->LoadOrderIndex = p->BaseInfo.LoadOrderIndex; |
| 41 | m->LoadCount = p->BaseInfo.LoadCount; |
| 42 | m->hSection = p->BaseInfo.Section; |
| 43 | m->DefaultBase = p->DefaultBase; |
| 44 | m->ImageChecksum = p->ImageChecksum; |
| 45 | m->TimeDateStamp = p->TimeDateStamp; |
| 46 | m->Name = std::string((PCSTR)((BYTE*)p->BaseInfo.FullPathName + p->BaseInfo.OffsetToFileName)); |
| 47 | |
| 48 | _modules.push_back(std::move(m)); |
| 49 | |
| 50 | if (p->NextOffset == 0) |
| 51 | break; |
| 52 | p = (RTL_PROCESS_MODULE_INFORMATION_EX*)((BYTE*)p + p->NextOffset); |
| 53 | } |
| 54 | |
| 55 | return uint32_t(_modules.size()); |
| 56 | } |
| 57 | |
| 58 | const std::vector<std::shared_ptr<KernelModuleInfo>>& WinSys::KernelModuleTracker::GetModules() const { |
| 59 | return _modules; |
no test coverage detected