GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — ReClass.NET-MemoryPipePlugin/PipeServer/MemoryHelper.cpp at master · ReClassNET/ReClass.NET-MemoryPipePlugin Expand file tree/ Copy pathMemoryHelper.cppMore file actions 243 lines (213 loc) · 7.06 KB / Copy pathMemoryHelper.cppFile metadata and controls243 lines (213 loc) · 7.06 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 #include <windows.h> #include <vector> #include <cstdint> #include <algorithm> #include <functional> #include <string> #include "ReClassNET_Plugin.hpp" bool IsValidMemoryRange(LPCVOID address, int length) { const auto endAddress = static_cast<const uint8_t*>(address) + length; do { MEMORY_BASIC_INFORMATION info; if (!VirtualQuery(address, &info, sizeof(MEMORY_BASIC_INFORMATION))) { return false; } if (info.State != MEM_COMMIT) { return false; } switch (info.Protect) { case PAGE_EXECUTE_READ: case PAGE_EXECUTE_READWRITE: case PAGE_EXECUTE_WRITECOPY: case PAGE_READONLY: case PAGE_READWRITE: case PAGE_WRITECOPY: break; default: return false; } address = static_cast<uint8_t*>(info.BaseAddress) + info.RegionSize; } while (endAddress > address); return true; } //--------------------------------------------------------------------------- bool ReadMemory(LPCVOID address, std::vector<uint8_t>& buffer) { if (!IsValidMemoryRange(address, static_cast<int>(buffer.size()))) { return false; } std::memcpy(buffer.data(), address, buffer.size()); return true; } //--------------------------------------------------------------------------- bool WriteMemory(LPVOID address, const std::vector<uint8_t>& buffer) { if (!IsValidMemoryRange(address, static_cast<int>(buffer.size()))) { return false; } DWORD oldProtect; if (VirtualProtect(address, buffer.size(), PAGE_EXECUTE_READWRITE, &oldProtect)) { std::memcpy(address, buffer.data(), buffer.size()); VirtualProtect(address, buffer.size(), oldProtect, nullptr); return true; } return false; } //--------------------------------------------------------------------------- void EnumerateRemoteSectionsAndModules(const std::function<void(RC_Pointer, RC_Pointer, std::wstring&&)>& moduleCallback, const std::function<void(RC_Pointer, RC_Pointer, SectionType, SectionCategory, SectionProtection, std::wstring&&, std::wstring&&)>& sectionCallback) { std::vector<EnumerateRemoteSectionData> sections; // First enumerate all memory sections. MEMORY_BASIC_INFORMATION memInfo = { }; memInfo.RegionSize = 0x1000; size_t address = 0; while (VirtualQuery(reinterpret_cast<LPCVOID>(address), &memInfo, sizeof(MEMORY_BASIC_INFORMATION)) != 0 && address + memInfo.RegionSize > address) { if (memInfo.State == MEM_COMMIT) { EnumerateRemoteSectionData section = {}; section.BaseAddress = memInfo.BaseAddress; section.Size = memInfo.RegionSize; switch (memInfo.Protect & 0xFF) { case PAGE_EXECUTE: section.Protection = SectionProtection::Execute; break; case PAGE_EXECUTE_READ: section.Protection = SectionProtection::Execute | SectionProtection::Read; break; case PAGE_EXECUTE_READWRITE: case PAGE_EXECUTE_WRITECOPY: section.Protection = SectionProtection::Execute | SectionProtection::Read | SectionProtection::Write; break; case PAGE_NOACCESS: section.Protection = SectionProtection::NoAccess; break; case PAGE_READONLY: section.Protection = SectionProtection::Read; break; case PAGE_READWRITE: case PAGE_WRITECOPY: section.Protection = SectionProtection::Read | SectionProtection::Write; break; } if ((memInfo.Protect & PAGE_GUARD) == PAGE_GUARD) { section.Protection |= SectionProtection::Guard; } switch (memInfo.Type) { case MEM_IMAGE: section.Type = SectionType::Image; break; case MEM_MAPPED: section.Type = SectionType::Mapped; break; case MEM_PRIVATE: section.Type = SectionType::Private; break; } section.Category = section.Type == SectionType::Private ? SectionCategory::HEAP : SectionCategory::Unknown; sections.push_back(std::move(section)); } address = reinterpret_cast<size_t>(memInfo.BaseAddress) + memInfo.RegionSize; } struct UNICODE_STRING { USHORT Length; USHORT MaximumLength; PWSTR Buffer; }; 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; }; struct PEB_LDR_DATA { ULONG Length; BOOLEAN Initialized; PVOID SsHandle; LIST_ENTRY InLoadOrderModuleList; LIST_ENTRY InMemoryOrderModuleList; LIST_ENTRY InInitializationOrderModuleList; }; struct PEB { BOOLEAN InheritedAddressSpace; BOOLEAN ReadImageFileExecOptions; BOOLEAN BeingDebugged; BOOLEAN Spare; HANDLE Mutant; PVOID ImageBaseAddress; PEB_LDR_DATA *LoaderData; }; // Second enumerate all modules. #ifdef _WIN64 const auto peb = reinterpret_cast<PEB*>(__readgsqword(0x60)); #else const auto peb = reinterpret_cast<PEB*>(__readfsdword(0x30)); #endif auto ldr = reinterpret_cast<LDR_MODULE*>(peb->LoaderData->InLoadOrderModuleList.Flink); while (ldr->BaseAddress != nullptr) { moduleCallback(static_cast<RC_Pointer>(ldr->BaseAddress), reinterpret_cast<RC_Pointer>(static_cast<intptr_t>(ldr->SizeOfImage)), ldr->FullDllName.Buffer); const auto it = std::lower_bound(std::begin(sections), std::end(sections), ldr->BaseAddress, [§ions](const auto& lhs, const LPVOID& rhs) { return lhs.BaseAddress < rhs; }); const auto dosHeader = reinterpret_cast<IMAGE_DOS_HEADER*>(ldr->BaseAddress); const auto ntHeader = reinterpret_cast<IMAGE_NT_HEADERS*>(reinterpret_cast<intptr_t>(ldr->BaseAddress) + dosHeader->e_lfanew); int i = 0; for (auto sectionHeader = IMAGE_FIRST_SECTION(ntHeader); i < ntHeader->FileHeader.NumberOfSections; i++, sectionHeader++) { const auto sectionAddress = reinterpret_cast<intptr_t>(ldr->BaseAddress) + static_cast<intptr_t>(sectionHeader->VirtualAddress); for (auto j = it; j != std::end(sections); ++j) { if (sectionAddress >= reinterpret_cast<intptr_t>(j->BaseAddress) && sectionAddress < reinterpret_cast<intptr_t>(j->BaseAddress) + static_cast<intptr_t>(j->Size)) { // Copy the name because it is not null padded. char buffer[IMAGE_SIZEOF_SHORT_NAME + 1] = { 0 }; std::memcpy(buffer, sectionHeader->Name, IMAGE_SIZEOF_SHORT_NAME); if (std::strcmp(buffer, ".text") == 0 || std::strcmp(buffer, "code") == 0) { j->Category = SectionCategory::CODE; } else if (std::strcmp(buffer, ".data") == 0 || std::strcmp(buffer, "data") == 0 || std::strcmp(buffer, ".rdata") == 0 || std::strcmp(buffer, ".idata") == 0) { j->Category = SectionCategory::DATA; } size_t convertedChars = 0; mbstowcs_s(&convertedChars, j->Name, IMAGE_SIZEOF_SHORT_NAME, buffer, _TRUNCATE); std::memcpy(j->ModulePath, ldr->FullDllName.Buffer, sizeof(EnumerateRemoteSectionData::ModulePath)); break; } } } ldr = reinterpret_cast<LDR_MODULE*>(ldr->InLoadOrderModuleList.Flink); } for (auto&& section : sections) { sectionCallback(section.BaseAddress, reinterpret_cast<RC_Pointer>(section.Size), section.Type, section.Category, section.Protection, section.Name, section.ModulePath); } } //--------------------------------------------------------------------------- |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |