libPeConv
A library to load, manipulate, dump PE files.
Loading...
Searching...
No Matches
exports_mapper.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <windows.h>
9
10#include <string>
11#include <map>
12#include <set>
13#include <sstream>
14
15#include "pe_hdrs_helper.h"
16#include "pe_raw_to_virtual.h"
18#include "peconv/file_util.h"
19
20namespace peconv {
21
22 struct DllInfo {
24 : moduleBase(0), moduelSize(0), is64b(false)
25 {
26 }
27
28 DllInfo(ULONGLONG _moduleBase, size_t _moduelSize, bool _is64b, std::string _moduleName)
29 {
30 moduleBase = _moduleBase;
31 moduelSize = _moduelSize;
32 moduleName = _moduleName;
33 is64b = _is64b;
35 }
36
37 DllInfo(const DllInfo &other)
38 {
39 moduleBase = other.moduleBase;
40 moduelSize = other.moduelSize;
41 moduleName = other.moduleName;
42 shortName = other.shortName;
43 is64b = other.is64b;
44 }
45
46 bool operator<(const DllInfo &other) const
47 {
48 return this->moduleBase < other.moduleBase;
49 }
50
51 protected:
52 ULONGLONG moduleBase;
53 size_t moduelSize;
54 std::string moduleName;
55 std::string shortName;
56 bool is64b;
57
58 friend class ExportsMapper;
59 };
60
62
63 public:
64
72 size_t add_to_lookup(std::string moduleName, HMODULE modulePtr, size_t moduleSize, ULONGLONG moduleBase);
73
80 size_t add_to_lookup(std::string moduleName, HMODULE modulePtr, ULONGLONG moduleBase)
81 {
82 return add_to_lookup(moduleName, modulePtr, 0, moduleBase);
83 }
84
92 size_t add_to_lookup(std::string moduleName, HMODULE modulePtr)
93 {
94 return add_to_lookup(moduleName, modulePtr, reinterpret_cast<ULONGLONG>(modulePtr));
95 }
96
100 const std::set<ExportedFunc>* find_exports_by_va(ULONGLONG va) const
101 {
102 std::map<ULONGLONG, std::set<ExportedFunc>>::const_iterator itr = va_to_func.find(va);
103 if (itr != va_to_func.end()) {
104 const std::set<ExportedFunc> &fSet = itr->second;
105 return &fSet;
106 }
107 return NULL;
108 }
109
113 ULONGLONG find_dll_base_by_func_va(ULONGLONG func_rva) const
114 {
115 // the first element that is greater than the start address
116 std::map<ULONGLONG, DllInfo>::const_iterator firstGreater = dll_base_to_info.upper_bound(func_rva);
117
118 std::map<ULONGLONG, DllInfo>::const_iterator itr;
119 for (itr = dll_base_to_info.begin(); itr != firstGreater; ++itr) {
120 const DllInfo& module = itr->second;
121
122 if (func_rva >= module.moduleBase && func_rva <= (module.moduleBase + module.moduelSize)) {
123 // Address found in module:
124 return module.moduleBase;
125 }
126 }
127 return 0;
128 }
129
133 std::string get_dll_path(ULONGLONG base) const
134 {
135 std::map<ULONGLONG, DllInfo>::const_iterator found = this->dll_base_to_info.find(base);
136 if (found == this->dll_base_to_info.end()) { // no DLL found at this base
137 return "";
138 }
139 const DllInfo& info = found->second;
140 return info.moduleName;
141 }
142
146 std::string get_dll_path(std::string short_name) const;
147
151 size_t get_dll_paths(IN std::string short_name, OUT std::set<std::string>& paths) const;
152
156 std::string get_dll_fullname(std::string short_name) const
157 {
158 std::string dll_path = get_dll_path(short_name);
159 if (dll_path.length() == 0) return "";
160
161 return get_file_name(dll_path);
162 }
163
167 const ExportedFunc* find_export_by_va(ULONGLONG va) const
168 {
169 const std::set<ExportedFunc>* exp_set = find_exports_by_va(va);
170 if (exp_set == NULL) return NULL;
171
172 std::set<ExportedFunc>::iterator fItr = exp_set->begin();
173 const ExportedFunc* func = &(*fItr);
174 return func;
175 }
176
177 void print_va_to_func(std::stringstream &stream) const;
178 void print_func_to_va(std::stringstream &stream) const;
179
180
181 private:
182 enum ADD_FUNC_RES { RES_INVALID = 0, RES_MAPPED = 1, RES_FORWARDED = 2 };
183 ADD_FUNC_RES add_function_to_lookup(HMODULE modulePtr, ULONGLONG moduleBase, size_t moduleSize, ExportedFunc &currFunc, DWORD callRVA);
184
185 bool add_forwarded(ExportedFunc &currFunc, DWORD callRVA, PBYTE modulePtr, size_t moduleSize);
186 bool add_to_maps(ULONGLONG va, ExportedFunc &currFunc);
187
188 size_t resolve_forwarders(const ULONGLONG va, ExportedFunc &currFunc);
189 size_t make_ord_lookup_tables(PVOID modulePtr, size_t moduleSize, std::map<PDWORD, DWORD> &va_to_ord);
190
191 protected:
195 void associateVaAndFunc(ULONGLONG va, const ExportedFunc& func)
196 {
197 va_to_func[va].insert(func);
198 func_to_va[func] = va;
199 }
200
204 std::map<ULONGLONG, std::set<ExportedFunc>> va_to_func;
205
209 std::map<ExportedFunc, std::set<ExportedFunc>> forwarders_lookup;
210
214 std::map<ExportedFunc, ULONGLONG> func_to_va;
215
219 std::map<std::string, std::set<ULONGLONG>> dll_shortname_to_base;
220
221 std::map<ULONGLONG, DllInfo> dll_base_to_info;
222 };
223
224}; //namespace peconv
void print_va_to_func(std::stringstream &stream) const
ULONGLONG find_dll_base_by_func_va(ULONGLONG func_rva) const
size_t get_dll_paths(IN std::string short_name, OUT std::set< std::string > &paths) const
std::map< ULONGLONG, DllInfo > dll_base_to_info
size_t add_to_lookup(std::string moduleName, HMODULE modulePtr, ULONGLONG moduleBase)
std::map< std::string, std::set< ULONGLONG > > dll_shortname_to_base
size_t add_to_lookup(std::string moduleName, HMODULE modulePtr, size_t moduleSize, ULONGLONG moduleBase)
size_t add_to_lookup(std::string moduleName, HMODULE modulePtr)
void associateVaAndFunc(ULONGLONG va, const ExportedFunc &func)
std::string get_dll_path(ULONGLONG base) const
std::map< ExportedFunc, ULONGLONG > func_to_va
void print_func_to_va(std::stringstream &stream) const
const ExportedFunc * find_export_by_va(ULONGLONG va) const
std::string get_dll_fullname(std::string short_name) const
const std::set< ExportedFunc > * find_exports_by_va(ULONGLONG va) const
std::map< ExportedFunc, std::set< ExportedFunc > > forwarders_lookup
std::map< ULONGLONG, std::set< ExportedFunc > > va_to_func
A definition of ExportedFunc class - used for storing the details of the exported function....
Functions related to operations on files. Wrappers for read/write.
std::string get_dll_shortname(const std::string &str)
std::string get_file_name(IN const std::string full_path)
Wrappers over various fields in the PE header. Read, write, parse PE headers.
Converting PE from raw to virtual format.
DllInfo(ULONGLONG _moduleBase, size_t _moduelSize, bool _is64b, std::string _moduleName)
std::string moduleName
ULONGLONG moduleBase
std::string shortName
DllInfo(const DllInfo &other)
bool operator<(const DllInfo &other) const