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));
59 if (*recordRVA == 0) {
61 std::cout <<
">>> Skipping 0 function address at RVA:" << std::hex << (BYTE*)recordRVA - (BYTE*)modulePtr<<
"(ord)\n";
69 DWORD ordinal = ordBase + i;
70 va_to_ord[recordRVA] = ordinal;
75size_t ExportsMapper::resolve_forwarders(
const ULONGLONG va,
ExportedFunc &currFunc)
79 std::map<ExportedFunc, std::set<ExportedFunc>>::iterator fItr =
forwarders_lookup.find(currFunc);
82 std::set<ExportedFunc>::iterator sItr;
83 for (sItr = fItr->second.begin(); sItr != fItr->second.end(); ++sItr) {
92bool ExportsMapper::add_forwarded(
ExportedFunc &currFunc, DWORD callRVA, PBYTE modulePtr,
size_t moduleSize)
94 PBYTE fPtr = modulePtr + callRVA;
102 if (forwardedFunc.length() == 0) {
107 if (!forwarder.isValid()) {
109 std::cerr <<
"Skipped invalid forwarder" << std::endl;
122DWORD
get_ordinal(PDWORD recordPtr, std::map<PDWORD, DWORD> &va_to_ord)
124 std::map<PDWORD, DWORD>::iterator ord_itr = va_to_ord.find(recordPtr);
125 if (ord_itr == va_to_ord.end()) {
129 DWORD ordinal = ord_itr->second;
130 va_to_ord.erase(ord_itr);
134bool ExportsMapper::add_to_maps(ULONGLONG va,
ExportedFunc &currFunc)
137 resolve_forwarders(va, currFunc);
143 if (exp ==
nullptr)
return false;
145 const SIZE_T namesCount = exp->NumberOfNames;
146 const SIZE_T funcCount = exp->NumberOfFunctions;
148 const DWORD funcsListRVA = exp->AddressOfFunctions;
149 const DWORD funcNamesListRVA = exp->AddressOfNames;
150 const DWORD namesOrdsListRVA = exp->AddressOfNameOrdinals;
152 for (DWORD i = 0; i < funcCount; i++) {
153 DWORD* recordRVA = (DWORD*)(funcsListRVA + (BYTE*)modulePtr + i *
sizeof(DWORD));
159 for (SIZE_T i = 0; i < namesCount; i++) {
160 DWORD* nameRVA = (DWORD*)(funcNamesListRVA + (BYTE*)modulePtr + i *
sizeof(DWORD));
161 WORD* nameIndex = (WORD*)(namesOrdsListRVA + (BYTE*)modulePtr + i *
sizeof(WORD));
167 DWORD* funcRVA = (DWORD*)(funcsListRVA + (BYTE*)modulePtr + (*nameIndex) *
sizeof(DWORD));
176ExportsMapper::ADD_FUNC_RES ExportsMapper::add_function_to_lookup(HMODULE modulePtr, ULONGLONG moduleBase,
size_t moduleSize,
ExportedFunc &currFunc, DWORD callRVA)
178 if (add_forwarded(currFunc, callRVA, (BYTE*)modulePtr, moduleSize)) {
180 char* fPtr = (
char*)modulePtr + callRVA;
181 std::cout <<
"FWD " << currFunc.
toString() <<
" -> " << fPtr <<
"\n";
183 return ExportsMapper::RES_FORWARDED;
186 ULONGLONG callVa = callRVA + moduleBase;
190 std::cout <<
"Validation failed: " << currFunc.
toString() <<
"\n";
192 return ExportsMapper::RES_INVALID;
195 add_to_maps(callVa, currFunc);
196 return ExportsMapper::RES_MAPPED;
206 if (module_size == 0) {
208 if (module_size == 0)
return 0;
213 const bool is64b =
peconv::is64bit(
reinterpret_cast<const PBYTE
>(modulePtr));
214 DllInfo info(moduleBase, module_size, is64b, moduleName);
217 const std::string dllName = info.
shortName;
220 std::map<PDWORD, DWORD> va_to_ord;
221 size_t functCount = make_ord_lookup_tables(modulePtr, module_size, va_to_ord);
225 size_t forwarded_ctr = 0;
226 SIZE_T namesCount = exp->NumberOfNames;
228 DWORD funcsListRVA = exp->AddressOfFunctions;
229 DWORD funcNamesListRVA = exp->AddressOfNames;
230 DWORD namesOrdsListRVA = exp->AddressOfNameOrdinals;
232 size_t mapped_ctr = 0;
234 for (SIZE_T i = 0; i < namesCount; i++) {
235 DWORD* nameRVA = (DWORD*)(funcNamesListRVA + (BYTE*) modulePtr + i *
sizeof(DWORD));
236 WORD* nameIndex = (WORD*)(namesOrdsListRVA + (BYTE*) modulePtr + i *
sizeof(WORD));
237 DWORD* funcRVA = (DWORD*)(funcsListRVA + (BYTE*) modulePtr + (*nameIndex) *
sizeof(DWORD));
240 std::cout <<
">>> Skipping 0 function address at RVA:" << std::hex << (BYTE*)funcRVA - (BYTE*)modulePtr <<
"(name)\n";
246 LPSTR name = (LPSTR)(*nameRVA + (BYTE*) modulePtr);
250 DWORD callRVA = *funcRVA;
253 int res = add_function_to_lookup(modulePtr, moduleBase, module_size, currFunc, callRVA);
254 if (res == ExportsMapper::RES_FORWARDED) forwarded_ctr++;
255 if (res == ExportsMapper::RES_MAPPED) mapped_ctr++;
258 std::map<PDWORD, DWORD>::iterator ord_itr = va_to_ord.begin();
259 for (;ord_itr != va_to_ord.end(); ++ord_itr) {
261 DWORD* funcRVA = ord_itr->first;
262 DWORD callRVA = *funcRVA;
265 int res = add_function_to_lookup(modulePtr, moduleBase, module_size, currFunc, callRVA);
266 if (res == ExportsMapper::RES_FORWARDED) forwarded_ctr++;
267 if (res == ExportsMapper::RES_MAPPED) mapped_ctr++;
270 std::cout <<
"Finished exports parsing, mapped: "<< mapped_ctr <<
" forwarded: " << forwarded_ctr << std::endl;
277 std::map<std::string, std::set<ULONGLONG>>::const_iterator itr = this->
dll_shortname_to_base.find(short_name);
282 const std::set<ULONGLONG>& bases = itr->second;
283 std::set<ULONGLONG>::const_iterator bItr;
284 for (bItr = bases.begin(); bItr != bases.end(); ++bItr) {
285 ULONGLONG base = *bItr;
295 std::map<std::string, std::set<ULONGLONG>>::const_iterator itr = this->
dll_shortname_to_base.find(short_name);
299 const std::set<ULONGLONG>& bases = itr->second;
300 std::set<ULONGLONG>::const_iterator bItr;
301 for (bItr = bases.begin(); bItr != bases.end(); ++bItr) {
302 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...
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)