|
| 1 | +#include <fstream> |
| 2 | +#include <sstream> |
| 3 | +#include <unordered_map> |
| 4 | + |
| 5 | +#include "NativeCore.hpp" |
| 6 | + |
| 7 | +inline bool operator&(SectionProtection& lhs, SectionProtection rhs) |
| 8 | +{ |
| 9 | + using T = std::underlying_type_t<SectionProtection>; |
| 10 | + |
| 11 | + return (static_cast<T>(lhs) & static_cast<T>(rhs)) == static_cast<T>(rhs); |
| 12 | +} |
| 13 | + |
| 14 | +template<typename T> |
| 15 | +inline std::istream& skip(std::istream& s) |
| 16 | +{ |
| 17 | + auto f = s.flags(); |
| 18 | + s >> std::noskipws; |
| 19 | + |
| 20 | + T t; |
| 21 | + s >> t; |
| 22 | + |
| 23 | + s.flags(f); |
| 24 | + |
| 25 | + return s; |
| 26 | +} |
| 27 | + |
| 28 | +std::istream& operator >> (std::istream& s, SectionProtection& protection) |
| 29 | +{ |
| 30 | + protection = SectionProtection::NoAccess; |
| 31 | + |
| 32 | + if (s.get() == 'r') protection |= SectionProtection::Read; |
| 33 | + if (s.get() == 'w') protection |= SectionProtection::Write; |
| 34 | + if (s.get() == 'x') protection |= SectionProtection::Execute; |
| 35 | + |
| 36 | + return s; |
| 37 | +} |
| 38 | + |
| 39 | +extern "C" void EnumerateRemoteSectionsAndModules(RC_Pointer handle, EnumerateRemoteSectionsCallback callbackSection, EnumerateRemoteModulesCallback callbackModule) |
| 40 | +{ |
| 41 | + if (callbackSection == nullptr && callbackModule == nullptr) |
| 42 | + { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + struct ModuleInfo |
| 47 | + { |
| 48 | + intptr_t Start = 0; |
| 49 | + intptr_t End = 0; |
| 50 | + RC_UnicodeChar Path[PATH_MAXIMUM_LENGTH] = {}; |
| 51 | + }; |
| 52 | + |
| 53 | + std::ifstream input(((std::stringstream&)(std::stringstream() << "/proc/" << (intptr_t)handle << "/maps")).str()); |
| 54 | + |
| 55 | + std::unordered_map<int, ModuleInfo> modules; |
| 56 | + |
| 57 | + std::string line; |
| 58 | + while (std::getline(input, line)) |
| 59 | + { |
| 60 | + std::stringstream ss(line); |
| 61 | + |
| 62 | + intptr_t start; |
| 63 | + intptr_t end; |
| 64 | + SectionProtection protection; |
| 65 | + intptr_t offset; |
| 66 | + int dev1, dev2; |
| 67 | + int inode; |
| 68 | + std::string path; |
| 69 | + ss >> std::hex >> start >> skip<char> >> end >> skip<char> >> protection >> skip<char> >> offset >> dev1 >> skip<char> >> dev2 >> std::dec >> inode >> std::skipws >> path; |
| 70 | + |
| 71 | + EnumerateRemoteSectionData section = {}; |
| 72 | + section.BaseAddress = (RC_Pointer)start; |
| 73 | + section.Size = end - start; |
| 74 | + section.Protection = protection; |
| 75 | + |
| 76 | + section.Category = SectionCategory::Unknown; |
| 77 | + section.Type = SectionType::Unknown; |
| 78 | + if (inode != 0) |
| 79 | + { |
| 80 | + section.Type = SectionType::Image; |
| 81 | + |
| 82 | + if (protection & SectionProtection::Read && protection & SectionProtection::Execute) |
| 83 | + { |
| 84 | + section.Category = SectionCategory::CODE; |
| 85 | + } |
| 86 | + else if (protection & SectionProtection::Read && protection & SectionProtection::Write) |
| 87 | + { |
| 88 | + section.Category = SectionCategory::DATA; |
| 89 | + } |
| 90 | + |
| 91 | + MultiByteToUnicode(path.c_str(), section.ModulePath, PATH_MAXIMUM_LENGTH); |
| 92 | + |
| 93 | + auto& module = modules[inode]; |
| 94 | + module.Start = module.Start != 0 ? std::min(module.Start, start) : start; |
| 95 | + module.End = module.End != 0 ? std::max(module.End, end) : end; |
| 96 | + if (module.Path[0] == 0) |
| 97 | + { |
| 98 | + std::memcpy(module.Path, section.ModulePath, PATH_MAXIMUM_LENGTH); |
| 99 | + } |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + section.Type = SectionType::Mapped; |
| 104 | + |
| 105 | + if (protection & SectionProtection::Read || protection & SectionProtection::Write) |
| 106 | + { |
| 107 | + section.Category = SectionCategory::HEAP; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if (callbackSection != nullptr) |
| 112 | + { |
| 113 | + callbackSection(§ion); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if (callbackModule != nullptr) |
| 118 | + { |
| 119 | + for (auto&& kv : modules) |
| 120 | + { |
| 121 | + EnumerateRemoteModuleData module = {}; |
| 122 | + module.BaseAddress = (RC_Pointer)kv.second.Start; |
| 123 | + module.Size = kv.second.End - kv.second.Start; |
| 124 | + std::memcpy(module.Path, kv.second.Path, PATH_MAXIMUM_LENGTH); |
| 125 | + |
| 126 | + callbackModule(&module); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments