10 std::map<ULONGLONG, std::set<ExportedFunc>>::const_iterator itr;
14 stream << std::hex << itr->first <<
" :\n";
16 std::set<ExportedFunc>::const_iterator itr2;
17 const std::set<ExportedFunc> &funcs = itr->second;
19 for (itr2 = funcs.begin(); itr2 != funcs.end(); ++itr2) {
20 stream <<
"\t" << itr2->toString() <<
"\n";
27 std::map<ExportedFunc, ULONGLONG>::const_iterator itr;
29 stream << itr->first.toString() <<
" : "
30 << std::hex << itr->second <<
"\n";
34ULONGLONG
rebase_va(ULONGLONG va, ULONGLONG currBase, ULONGLONG targetBase)
36 if (currBase == targetBase) {
39 ULONGLONG rva = va - (ULONGLONG) currBase;
40 return rva + targetBase;
43size_t ExportsMapper::make_ord_lookup_tables(
46 std::map<PDWORD, DWORD> &va_to_ord
50 if (exp == NULL)
return 0;
52 SIZE_T functCount = exp->NumberOfFunctions;
53 DWORD funcsListRVA = exp->AddressOfFunctions;
54 DWORD ordBase = exp->Base;
57 for (DWORD i = 0; i < functCount; i++) {
58 DWORD* recordRVA = (DWORD*)(funcsListRVA + (BYTE*) modulePtr + i *
sizeof(DWORD));
62 if (*recordRVA == 0) {
63 LOG_INFO(
"Skipping 0 function address at RVA: 0x%llx (ord).", (
unsigned long long)((BYTE*)recordRVA - (BYTE*)modulePtr));
67 DWORD ordinal = ordBase + i;
68 va_to_ord[recordRVA] = ordinal;
73size_t ExportsMapper::resolve_forwarders(
const ULONGLONG va,
ExportedFunc &currFunc)
77 std::map<ExportedFunc, std::set<ExportedFunc>>::iterator fItr =
forwarders_lookup.find(currFunc);
79 std::set<ExportedFunc>::iterator sItr;
80 for (sItr = fItr->second.begin(); sItr != fItr->second.end(); ++sItr) {
88bool ExportsMapper::add_forwarded(
ExportedFunc &currFunc, DWORD callRVA, PBYTE modulePtr,
size_t moduleSize)
90 PBYTE fPtr = modulePtr + callRVA;
98 if (forwardedFunc.length() == 0) {
102 ExportedFunc forwarder(forwardedFunc);
103 if (!forwarder.isValid()) {
104 LOG_INFO(
"Skipped invalid forwarder.");
116DWORD
get_ordinal(PDWORD recordPtr, std::map<PDWORD, DWORD> &va_to_ord)
118 std::map<PDWORD, DWORD>::iterator ord_itr = va_to_ord.find(recordPtr);
119 if (ord_itr == va_to_ord.end()) {
123 DWORD ordinal = ord_itr->second;
124 va_to_ord.erase(ord_itr);
128bool ExportsMapper::add_to_maps(ULONGLONG va,
ExportedFunc &currFunc)
131 resolve_forwarders(va, currFunc);
137 if (exp ==
nullptr)
return false;
139 const SIZE_T namesCount = exp->NumberOfNames;
140 const SIZE_T funcCount = exp->NumberOfFunctions;
142 const DWORD funcsListRVA = exp->AddressOfFunctions;
143 const DWORD funcNamesListRVA = exp->AddressOfNames;
144 const DWORD namesOrdsListRVA = exp->AddressOfNameOrdinals;
146 for (DWORD i = 0; i < funcCount; i++) {
147 DWORD* recordRVA = (DWORD*)(funcsListRVA + (BYTE*)modulePtr + i *
sizeof(DWORD));
153 for (SIZE_T i = 0; i < namesCount; i++) {
154 DWORD* nameRVA = (DWORD*)(funcNamesListRVA + (BYTE*)modulePtr + i *
sizeof(DWORD));
155 WORD* nameIndex = (WORD*)(namesOrdsListRVA + (BYTE*)modulePtr + i *
sizeof(WORD));
161 DWORD* funcRVA = (DWORD*)(funcsListRVA + (BYTE*)modulePtr + (*nameIndex) *
sizeof(DWORD));
170ExportsMapper::ADD_FUNC_RES ExportsMapper::add_function_to_lookup(HMODULE modulePtr, ULONGLONG moduleBase,
size_t moduleSize,
ExportedFunc &currFunc, DWORD callRVA)
172 if (add_forwarded(currFunc, callRVA, (BYTE*)modulePtr, moduleSize)) {
173 LOG_DEBUG(
"FWD %s -> %p.", currFunc.
toString().c_str(), (
char*)modulePtr + callRVA);
174 return ExportsMapper::RES_FORWARDED;
177 ULONGLONG callVa = callRVA + moduleBase;
181 return ExportsMapper::RES_INVALID;
184 add_to_maps(callVa, currFunc);
185 return ExportsMapper::RES_MAPPED;
195 if (module_size == 0) {
197 if (module_size == 0)
return 0;
202 const bool is64b =
peconv::is64bit(
reinterpret_cast<const PBYTE
>(modulePtr));
203 DllInfo info(moduleBase, module_size, is64b, moduleName);
206 const std::string dllName = info.
shortName;
209 std::map<PDWORD, DWORD> va_to_ord;
210 size_t functCount = make_ord_lookup_tables(modulePtr, module_size, va_to_ord);
214 size_t forwarded_ctr = 0;
215 SIZE_T namesCount = exp->NumberOfNames;
217 DWORD funcsListRVA = exp->AddressOfFunctions;
218 DWORD funcNamesListRVA = exp->AddressOfNames;
219 DWORD namesOrdsListRVA = exp->AddressOfNameOrdinals;
221 size_t mapped_ctr = 0;
223 for (SIZE_T i = 0; i < namesCount; i++) {
224 DWORD* nameRVA = (DWORD*)(funcNamesListRVA + (BYTE*) modulePtr + i *
sizeof(DWORD));
225 WORD* nameIndex = (WORD*)(namesOrdsListRVA + (BYTE*) modulePtr + i *
sizeof(WORD));
226 DWORD* funcRVA = (DWORD*)(funcsListRVA + (BYTE*) modulePtr + (*nameIndex) *
sizeof(DWORD));
228 LOG_DEBUG(
"Skipping 0 function address at RVA: 0x%llx (name).", (
unsigned long long)((BYTE*)funcRVA - (BYTE*)modulePtr));
233 LPSTR name = (LPSTR)(*nameRVA + (BYTE*) modulePtr);
237 DWORD callRVA = *funcRVA;
240 int res = add_function_to_lookup(modulePtr, moduleBase, module_size, currFunc, callRVA);
241 if (res == ExportsMapper::RES_FORWARDED) forwarded_ctr++;
242 if (res == ExportsMapper::RES_MAPPED) mapped_ctr++;
245 std::map<PDWORD, DWORD>::iterator ord_itr = va_to_ord.begin();
246 for (;ord_itr != va_to_ord.end(); ++ord_itr) {
248 DWORD* funcRVA = ord_itr->first;
249 DWORD callRVA = *funcRVA;
252 int res = add_function_to_lookup(modulePtr, moduleBase, module_size, currFunc, callRVA);
253 if (res == ExportsMapper::RES_FORWARDED) forwarded_ctr++;
254 if (res == ExportsMapper::RES_MAPPED) mapped_ctr++;
256 LOG_DEBUG(
"Finished exports parsing, mapped: %zu forwarded: %zu.", mapped_ctr, forwarded_ctr);
262 std::map<std::string, std::set<ULONGLONG>>::const_iterator itr = this->
dll_shortname_to_base.find(short_name);
267 const std::set<ULONGLONG>& bases = itr->second;
268 std::set<ULONGLONG>::const_iterator bItr;
269 for (bItr = bases.begin(); bItr != bases.end(); ++bItr) {
270 ULONGLONG base = *bItr;
280 std::map<std::string, std::set<ULONGLONG>>::const_iterator itr = this->
dll_shortname_to_base.find(short_name);
284 const std::set<ULONGLONG>& bases = itr->second;
285 std::set<ULONGLONG>::const_iterator bItr;
286 for (bItr = bases.begin(); bItr != bases.end(); ++bItr) {
287 ULONGLONG base = *bItr;
std::string toString() const
void print_va_to_func(std::stringstream &stream) 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
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)
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
std::map< ExportedFunc, std::set< ExportedFunc > > forwarders_lookup
std::map< ULONGLONG, std::set< ExportedFunc > > va_to_func
ULONGLONG rebase_va(ULONGLONG va, ULONGLONG currBase, ULONGLONG targetBase)
bool is_valid_export_table(IMAGE_EXPORT_DIRECTORY *exp, HMODULE modulePtr, const size_t module_size)
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...
#define LOG_DEBUG(fmt,...)
#define LOG_INFO(fmt,...)
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)