12 std::map<ULONGLONG, std::set<ExportedFunc>>::const_iterator itr;
16 stream << std::hex << itr->first <<
" :\n";
18 std::set<ExportedFunc>::const_iterator itr2;
19 const std::set<ExportedFunc> &funcs = itr->second;
21 for (itr2 = funcs.begin(); itr2 != funcs.end(); ++itr2) {
22 stream <<
"\t" << itr2->toString() <<
"\n";
29 std::map<ExportedFunc, ULONGLONG>::const_iterator itr;
31 stream << itr->first.toString() <<
" : "
32 << std::hex << itr->second <<
"\n";
36size_t ExportsMapper::make_ord_lookup_tables(
39 std::map<PDWORD, DWORD> &va_to_ord
43 if (exp == NULL)
return 0;
45 SIZE_T functCount = exp->NumberOfFunctions;
46 DWORD funcsListRVA = exp->AddressOfFunctions;
47 DWORD ordBase = exp->Base;
50 for (DWORD i = 0; i < functCount; i++) {
51 DWORD* recordRVA = (DWORD*)(funcsListRVA + (BYTE*) modulePtr + i *
sizeof(DWORD));
55 if (*recordRVA == 0) {
56 LOG_INFO(
"Skipping 0 function address at RVA: 0x%llx (ord).", (
unsigned long long)((BYTE*)recordRVA - (BYTE*)modulePtr));
60 DWORD ordinal = ordBase + i;
61 va_to_ord[recordRVA] = ordinal;
66size_t ExportsMapper::resolve_forwarders(
const ULONGLONG va,
ExportedFunc &currFunc)
70 std::map<ExportedFunc, std::set<ExportedFunc>>::iterator fItr =
forwarders_lookup.find(currFunc);
72 std::set<ExportedFunc>::iterator sItr;
73 for (sItr = fItr->second.begin(); sItr != fItr->second.end(); ++sItr) {
81bool ExportsMapper::add_forwarded(
ExportedFunc &currFunc, DWORD callRVA, PBYTE modulePtr,
size_t moduleSize)
83 PBYTE fPtr = modulePtr + callRVA;
91 if (forwardedFunc.length() == 0) {
96 if (!forwarder.isValid()) {
97 LOG_INFO(
"Skipped invalid forwarder.");
102 const auto found =
func_to_va.find(forwarder);
103 if (found !=
func_to_va.end() && found->second != 0) {
104 const ULONGLONG va = found->second;
110DWORD
get_ordinal(PDWORD recordPtr, std::map<PDWORD, DWORD> &va_to_ord)
112 std::map<PDWORD, DWORD>::iterator ord_itr = va_to_ord.find(recordPtr);
113 if (ord_itr == va_to_ord.end()) {
117 DWORD ordinal = ord_itr->second;
118 va_to_ord.erase(ord_itr);
122bool ExportsMapper::add_to_maps(ULONGLONG va,
ExportedFunc &currFunc)
125 resolve_forwarders(va, currFunc);
130 bool is_valid_export_table(IMAGE_EXPORT_DIRECTORY* exp, HMODULE modulePtr,
const size_t module_size)
132 if (exp ==
nullptr)
return false;
134 const SIZE_T namesCount = exp->NumberOfNames;
135 const SIZE_T funcCount = exp->NumberOfFunctions;
137 const DWORD funcsListRVA = exp->AddressOfFunctions;
138 const DWORD funcNamesListRVA = exp->AddressOfNames;
139 const DWORD namesOrdsListRVA = exp->AddressOfNameOrdinals;
141 for (DWORD i = 0; i < funcCount; i++) {
142 DWORD* recordRVA = (DWORD*)(funcsListRVA + (BYTE*)modulePtr + i *
sizeof(DWORD));
148 for (SIZE_T i = 0; i < namesCount; i++) {
149 DWORD* nameRVA = (DWORD*)(funcNamesListRVA + (BYTE*)modulePtr + i *
sizeof(DWORD));
150 WORD* nameIndex = (WORD*)(namesOrdsListRVA + (BYTE*)modulePtr + i *
sizeof(WORD));
156 DWORD* funcRVA = (DWORD*)(funcsListRVA + (BYTE*)modulePtr + (*nameIndex) *
sizeof(DWORD));
166ExportsMapper::ADD_FUNC_RES ExportsMapper::add_function_to_lookup(HMODULE modulePtr, ULONGLONG moduleBase,
size_t moduleSize,
ExportedFunc &currFunc, DWORD callRVA)
168 if (add_forwarded(currFunc, callRVA, (BYTE*)modulePtr, moduleSize)) {
169 LOG_DEBUG(
"FWD %s -> %p.", currFunc.
toString().c_str(), (
char*)modulePtr + callRVA);
170 return ExportsMapper::RES_FORWARDED;
173 ULONGLONG callVa = callRVA + moduleBase;
177 return ExportsMapper::RES_INVALID;
180 add_to_maps(callVa, currFunc);
181 return ExportsMapper::RES_MAPPED;
191 if (module_size == 0) {
193 if (module_size == 0)
return 0;
195 if (!is_valid_export_table(exp, modulePtr, module_size)) {
198 const bool is64b =
peconv::is64bit(
reinterpret_cast<const PBYTE
>(modulePtr));
199 DllInfo info(moduleBase, module_size, is64b, moduleName);
202 const std::string dllName = info.
shortName;
205 std::map<PDWORD, DWORD> va_to_ord;
206 size_t functCount = make_ord_lookup_tables(modulePtr, module_size, va_to_ord);
210 size_t forwarded_ctr = 0;
211 SIZE_T namesCount = exp->NumberOfNames;
213 DWORD funcsListRVA = exp->AddressOfFunctions;
214 DWORD funcNamesListRVA = exp->AddressOfNames;
215 DWORD namesOrdsListRVA = exp->AddressOfNameOrdinals;
217 size_t mapped_ctr = 0;
219 for (SIZE_T i = 0; i < namesCount; i++) {
220 DWORD* nameRVA = (DWORD*)(funcNamesListRVA + (BYTE*) modulePtr + i *
sizeof(DWORD));
221 WORD* nameIndex = (WORD*)(namesOrdsListRVA + (BYTE*) modulePtr + i *
sizeof(WORD));
222 DWORD* funcRVA = (DWORD*)(funcsListRVA + (BYTE*) modulePtr + (*nameIndex) *
sizeof(DWORD));
224 LOG_DEBUG(
"Skipping 0 function address at RVA: 0x%llx (name).", (
unsigned long long)((BYTE*)funcRVA - (BYTE*)modulePtr));
229 const LPSTR name = (LPSTR)(*nameRVA + (BYTE*) modulePtr);
234 DWORD callRVA = *funcRVA;
237 int res = add_function_to_lookup(modulePtr, moduleBase, module_size, currFunc, callRVA);
238 if (res == ExportsMapper::RES_FORWARDED) forwarded_ctr++;
239 if (res == ExportsMapper::RES_MAPPED) mapped_ctr++;
242 std::map<PDWORD, DWORD>::iterator ord_itr = va_to_ord.begin();
243 for (;ord_itr != va_to_ord.end(); ++ord_itr) {
245 DWORD* funcRVA = ord_itr->first;
246 DWORD callRVA = *funcRVA;
249 int res = add_function_to_lookup(modulePtr, moduleBase, module_size, currFunc, callRVA);
250 if (res == ExportsMapper::RES_FORWARDED) forwarded_ctr++;
251 if (res == ExportsMapper::RES_MAPPED) mapped_ctr++;
253 LOG_DEBUG(
"Finished exports parsing, mapped: %zu forwarded: %zu.", mapped_ctr, forwarded_ctr);
259 std::map<std::string, std::set<ULONGLONG>>::const_iterator itr = this->
dll_shortname_to_base.find(short_name);
264 const std::set<ULONGLONG>& bases = itr->second;
265 std::set<ULONGLONG>::const_iterator bItr;
266 for (bItr = bases.begin(); bItr != bases.end(); ++bItr) {
267 ULONGLONG base = *bItr;
277 std::map<std::string, std::set<ULONGLONG>>::const_iterator itr = this->
dll_shortname_to_base.find(short_name);
281 const std::set<ULONGLONG>& bases = itr->second;
282 std::set<ULONGLONG>::const_iterator bItr;
283 for (bItr = bases.begin(); bItr != bases.end(); ++bItr) {
284 ULONGLONG base = *bItr;
std::string toString() const
void print_va_to_func(std::stringstream &stream) const
std::string get_dll_path(const ULONGLONG base) const
std::map< ULONGLONG, DllInfo > dll_base_to_info
std::map< std::string, std::set< ULONGLONG > > dll_shortname_to_base
void associateVaAndFunc(ULONGLONG va, const ExportedFunc &func)
std::map< ExportedFunc, ULONGLONG > func_to_va
void print_func_to_va(std::stringstream &stream) const
size_t get_dll_paths(IN const std::string &short_name, OUT std::set< std::string > &paths) const
size_t add_to_lookup(const std::string &moduleName, HMODULE modulePtr, size_t moduleSize, ULONGLONG moduleBase)
std::map< ExportedFunc, std::set< ExportedFunc > > forwarders_lookup
std::map< ULONGLONG, std::set< ExportedFunc > > va_to_func
DWORD get_ordinal(PDWORD recordPtr, std::map< PDWORD, DWORD > &va_to_ord)
A definition of ExportsMapper class. Creates a lookup of all the exported functions from the supplied...
Compile-time configurable logging macros for peconv.
#define LOG_DEBUG(fmt,...)
#define LOG_INFO(fmt,...)
bool is_valid_string(LPVOID modulePtr, const size_t moduleSize, const CHAR_T *name_ptr, const size_t max_len=260)
size_t forwarder_name_len(BYTE *fPtr)
bool validate_ptr(IN const void *buffer_bgn, IN size_t buffer_size, IN const void *field_bgn, IN size_t field_size)
DWORD get_image_size(IN const BYTE *payload)
bool is64bit(IN const BYTE *pe_buffer)
std::string format_dll_func(const std::string &str)
IMAGE_EXPORT_DIRECTORY * get_export_directory(IN HMODULE modulePtr)
Miscellaneous utility functions.