23#define TO_LOWERCASE(c1) c1 = (c1 <= 'Z' && c1 >= 'A') ? c1 = (c1 - 'A') + 'a': c1;
28 if (curr_name == NULL || wanted_name == NULL)
return false;
30 size_t wanted_name_len = strlen(wanted_name);
31 size_t curr_name_len = strlen(curr_name);
33 if (curr_name_len != wanted_name_len)
return false;
35 for (
size_t i = 0; i < wanted_name_len; i++) {
36 char c1 = curr_name[i];
37 char c2 = wanted_name[i];
40 if (c1 != c2)
return false;
45bool is_ordinal(IMAGE_EXPORT_DIRECTORY *exp, LPCSTR func_name)
47 ULONGLONG base = exp->Base;
48 ULONGLONG max_ord = base + exp->NumberOfFunctions;
49 ULONGLONG name_ptr_val = (ULONGLONG)func_name;
50 if (name_ptr_val >= base && name_ptr_val < max_ord) {
56FARPROC
get_export_by_ord(PVOID modulePtr, IMAGE_EXPORT_DIRECTORY* exp, DWORD wanted_ordinal)
58 SIZE_T functCount = exp->NumberOfFunctions;
59 DWORD funcsListRVA = exp->AddressOfFunctions;
60 DWORD ordBase = exp->Base;
63 for (DWORD i = 0; i < functCount; i++) {
64 DWORD* funcRVA = (DWORD*)(funcsListRVA + (BYTE*) modulePtr + i *
sizeof(DWORD));
65 BYTE* fPtr = (BYTE*) modulePtr + (*funcRVA);
66 DWORD ordinal = ordBase + i;
67 if (ordinal == wanted_ordinal) {
69 std::cerr <<
"[!] Forwarded function: ["<< wanted_ordinal <<
" -> "<< fPtr <<
"] cannot be resolved!" << std::endl;
72 return (FARPROC) fPtr;
81 if (exp == 0)
return 0;
83 SIZE_T namesCount = exp->NumberOfNames;
84 DWORD funcNamesListRVA = exp->AddressOfNames;
87 DWORD* nameRVAs = (DWORD*)(funcNamesListRVA + (ULONG_PTR)modulePtr);
89 for (i = 0; i < namesCount; i++) {
90 DWORD nameRVA = nameRVAs[i];
91 if (!nameRVA)
continue;
92 LPSTR name = (LPSTR)(nameRVA + (BYTE*) modulePtr);
94 names_list.push_back(name);
103 if (exp == NULL)
return NULL;
105 SIZE_T namesCount = exp->NumberOfNames;
107 DWORD funcsListRVA = exp->AddressOfFunctions;
108 DWORD funcNamesListRVA = exp->AddressOfNames;
109 DWORD namesOrdsListRVA = exp->AddressOfNameOrdinals;
113 std::cerr <<
"[*] Getting function by ordinal" << std::endl;
119 std::cerr <<
"[-] Invalid pointer to the name" << std::endl;
124 for (SIZE_T i = 0; i < namesCount; i++) {
125 DWORD* nameRVA = (DWORD*)(funcNamesListRVA + (BYTE*) modulePtr + i *
sizeof(DWORD));
126 WORD* nameIndex = (WORD*)(namesOrdsListRVA + (BYTE*) modulePtr + i *
sizeof(WORD));
127 DWORD* funcRVA = (DWORD*)(funcsListRVA + (BYTE*) modulePtr + (*nameIndex) *
sizeof(DWORD));
129 LPSTR name = (LPSTR)(*nameRVA + (BYTE*) modulePtr);
130 BYTE* fPtr = (BYTE*) modulePtr + (*funcRVA);
136 std::cerr <<
"[!] Forwarded function: ["<< name <<
" -> "<< fPtr <<
"] cannot be resolved!" << std::endl;
139 return (FARPROC) fPtr;
143 std::cerr <<
"Function not found!" << std::endl;
150 HMODULE libBasePtr = LoadLibraryA(lib_name);
151 if (libBasePtr == NULL) {
152 std::cerr <<
"Could not load the library!" << std::endl;
161 std::cerr <<
"[!] Cound not get the function: "<< func_name <<
" from exports!" << std::endl;
163 std::cerr <<
"[!] Cound not get the function: "<<
MASK_TO_DWORD((ULONG_PTR)func_name) <<
" from exports!" << std::endl;
165 std::cerr <<
"[!] Falling back to the default resolver..." <<std::endl;
169 std::cerr <<
"[-] Loading function from " << lib_name <<
" failed!" << std::endl;
174 if (hProc != defaultProc) {
175 std::cerr <<
"[-] Loaded proc is not matching the default one!" << std::endl;
187 LPSTR module_name = (
char*)((ULONGLONG)modulePtr + exp->Name);
#define MASK_TO_DWORD(val)
virtual FARPROC resolve_func(LPCSTR lib_name, LPCSTR func_name)
virtual FARPROC resolve_func(LPCSTR lib_name, LPCSTR func_name)
bool is_wanted_func(LPCSTR curr_name, LPCSTR wanted_name)
FARPROC get_export_by_ord(PVOID modulePtr, IMAGE_EXPORT_DIRECTORY *exp, DWORD wanted_ordinal)
bool is_ordinal(IMAGE_EXPORT_DIRECTORY *exp, LPCSTR func_name)
Searching specific functions in PE's Exports Table.
LPSTR read_dll_name(HMODULE modulePtr)
size_t forwarder_name_len(BYTE *fPtr)
size_t get_exported_names(PVOID modulePtr, std::vector< std::string > &names_list)
FARPROC get_exported_func(PVOID modulePtr, LPCSTR wanted_name)
bool is_bad_read_ptr(LPCVOID areaStart, SIZE_T areaSize)
IMAGE_EXPORT_DIRECTORY * get_export_directory(IN HMODULE modulePtr)
Miscellaneous utility functions.