25 std::cout <<
"Found name:" << found_name << std::endl;
27 LPSTR name_ptr = (LPSTR)((ULONGLONG)
modulePtr + lib_desc->Name);
28 size_t full_name_len = found_name.length() + 1;
36 memcpy(name_ptr, found_name.c_str(), full_name_len);
68 const FIELD_T ordinal_flag,
69 std::map<ULONGLONG, std::set<ExportedFunc>> &addr_to_func
72 if (call_via_ptr == NULL ||
modulePtr == NULL || lib_desc == NULL) {
76 if (!importsDir)
return false;
78 const DWORD impAddr = importsDir->VirtualAddress;
80 FIELD_T *call_via_val = (FIELD_T*)call_via_ptr;
81 if (*call_via_val == 0) {
85 ULONGLONG searchedAddr = ULONGLONG(*call_via_val);
86 bool is_name_saved =
false;
88 FIELD_T lastOrdinal = 0;
89 std::set<ExportedFunc>::iterator funcname_itr = addr_to_func[searchedAddr].begin();
91 for (funcname_itr = addr_to_func[searchedAddr].begin();
92 funcname_itr != addr_to_func[searchedAddr].end();
98 const char* names_start = ((
const char*)
modulePtr + impAddr);
106 const ULONGLONG name_offset = (ULONGLONG)found_ptr - (ULONGLONG)
modulePtr;
109 if (funcname_itr != addr_to_func[searchedAddr].begin()) {
110 std::cout <<
">[*][" << std::hex << searchedAddr <<
"] " << found_func.
toString() << std::endl;
112 std::cout <<
"[+] Found the name at: " << std::hex << name_offset << std::endl;
114 PIMAGE_IMPORT_BY_NAME imp_field =
reinterpret_cast<PIMAGE_IMPORT_BY_NAME
>(name_offset -
sizeof(WORD));
116 memcpy(thunk_ptr, &imp_field,
sizeof(FIELD_T));
118 std::cout <<
"[+] Wrote found to offset: " << std::hex << call_via_ptr << std::endl;
120 is_name_saved =
true;
124 if (!is_name_saved && lastOrdinal != 0) {
126 std::cout <<
"[+] Filling ordinal: " << lastOrdinal << std::endl;
128 FIELD_T ord_thunk = lastOrdinal | ordinal_flag;
129 memcpy(thunk_ptr, &ord_thunk,
sizeof(FIELD_T));
130 is_name_saved =
true;
132 return is_name_saved;
139 FIELD_T ordinal = foundFunc.
funcOrdinal | ordinal_flag;
140 FIELD_T* by_ord = (FIELD_T*) desc;
143 std::cout <<
"[+] Saved ordinal" << std::endl;
148 PIMAGE_IMPORT_BY_NAME by_name = (PIMAGE_IMPORT_BY_NAME) ((ULONGLONG)
modulePtr + desc->u1.AddressOfData);
150 LPSTR func_name_ptr =
reinterpret_cast<LPSTR
>(by_name->Name);
151 std::string found_name = foundFunc.
funcName;
154 if (is_nameptr_valid) {
156 memcpy(func_name_ptr, found_name.c_str(), found_name.length() + 1);
158 std::cout <<
"[+] Saved name" << std::endl;
167 IN OUT IMAGE_IMPORT_DESCRIPTOR* lib_desc,
168 IN
const FIELD_T ordinal_flag,
169 IN std::map<ULONGLONG, std::set<ExportedFunc>> &addr_to_func,
173 if (lib_desc == NULL)
return false;
175 FIELD_T call_via = lib_desc->FirstThunk;
176 if (call_via == NULL)
return false;
178 size_t processed_imps = 0;
179 size_t recovered_imps = 0;
181 FIELD_T thunk_addr = lib_desc->OriginalFirstThunk;
182 if (thunk_addr == NULL) {
183 thunk_addr = call_via;
186 BYTE* call_via_ptr = (BYTE*)((ULONGLONG)
modulePtr + call_via);
187 BYTE* thunk_ptr = (BYTE*)((ULONGLONG)
modulePtr + thunk_addr);
189 call_via_ptr != NULL && thunk_ptr != NULL;
190 call_via_ptr +=
sizeof(FIELD_T), thunk_ptr +=
sizeof(FIELD_T)
193 FIELD_T *thunk_val = (FIELD_T*)thunk_ptr;
194 FIELD_T *call_via_val = (FIELD_T*)call_via_ptr;
195 if (*call_via_val == 0) {
199 IMAGE_THUNK_DATA_T* desc = (IMAGE_THUNK_DATA_T*)thunk_ptr;
200 if (desc->u1.Function == NULL) {
203 ULONGLONG searchedAddr = ULONGLONG(*call_via_val);
204 std::map<ULONGLONG,std::set<ExportedFunc>>::const_iterator found_itr = addr_to_func.find(searchedAddr);
205 if (found_itr == addr_to_func.end() || found_itr->second.size() == 0) {
209 notCovered->insert(thunkRVA, searchedAddr);
213 std::set<ExportedFunc>::const_iterator funcname_itr = found_itr->second.begin();
217 std::cout <<
"[*][" << std::hex << searchedAddr <<
"] " << funcname_itr->
toString() << std::endl;
220 if (!is_name_saved) {
224 if (is_name_saved) recovered_imps++;
227 return (recovered_imps == processed_imps);
233 bool is_filled =
false;
240 std::cerr <<
"[-] Could not fill some import names!" << std::endl;
bool findNameInBinaryAndFill(IMAGE_IMPORT_DESCRIPTOR *lib_desc, LPVOID call_via_ptr, LPVOID thunk_ptr, const FIELD_T ordinal_flag, std::map< ULONGLONG, std::set< ExportedFunc > > &addr_to_func)