10template <
typename FIELD_T>
13 size_t addrCounter = 0;
15 LPVOID call_via_ptr = (LPVOID)((ULONGLONG)modulePtr + call_via);
16 if (call_via_ptr ==
nullptr)
break;
18 LPVOID thunk_ptr = (LPVOID)((ULONGLONG)modulePtr + thunk_addr);
19 if (thunk_ptr ==
nullptr)
break;
21 if (!
validate_ptr(modulePtr, moduleSize, thunk_ptr,
sizeof(FIELD_T))) {
24 if (!
validate_ptr(modulePtr, moduleSize, call_via_ptr,
sizeof(FIELD_T))) {
27 FIELD_T *thunk_val =
reinterpret_cast<FIELD_T*
>(thunk_ptr);
28 FIELD_T *call_via_val =
reinterpret_cast<FIELD_T*
>(call_via_ptr);
29 if (*call_via_val == 0) {
34 ULONGLONG searchedAddr = ULONGLONG(*call_via_val);
35 if (exportsMap.find_export_by_va(searchedAddr) !=
nullptr) {
36 addresses.insert(searchedAddr);
40 call_via +=
sizeof(FIELD_T);
41 thunk_addr +=
sizeof(FIELD_T);
49 std::set<std::string> currDllNames;
51 const std::set<ExportedFunc>* exports_for_va = exportsMap.
find_exports_by_va(func_addr);
52 if (!exports_for_va) {
53 std::cerr <<
"Cannot find any DLL exporting: " << std::hex << func_addr << std::endl;
57 for (std::set<ExportedFunc>::iterator strItr = exports_for_va->begin();
58 strItr != exports_for_va->end();
61 currDllNames.insert(strItr->libName);
66std::set<std::string>
get_dlls_intersection(
const std::set<std::string> &dllNames,
const std::set<std::string> &currDllNames)
68 std::set<std::string> resultSet;
69 std::set_intersection(dllNames.begin(), dllNames.end(),
70 currDllNames.begin(), currDllNames.end(),
71 std::inserter(resultSet, resultSet.begin())
79 std::set<std::string> mainDllsSet;
80 std::set<std::string> reserveDllSet;
85 std::set<ULONGLONG>::iterator addrItr;
87 for (addrItr = addresses.begin(); addrItr != addresses.end(); ++addrItr) {
88 ULONGLONG searchedAddr = *addrItr;
96 mainDllsSet = currDllNames;
102 if (resultSet.size() > 0) {
104 mainDllsSet = resultSet;
109 if (resultSet.size() > 0) {
111 reserveDllSet = mainDllsSet;
112 mainDllsSet = resultSet;
116 reserveDllSet = currDllNames;
118 if (mainDllsSet.size() > 0) {
119 const std::string main_dll = *(mainDllsSet.begin());
125bool ImportedDllCoverage::findCoveringDll()
128 if (found_name.length() == 0) {
130 std::cerr <<
"Cannot find a covering DLL" << std::endl;
136 std::cout <<
"[+] Found DLL name: " << found_name << std::endl;
142 IN
const std::string &chosenDll,
144 OUT std::map<ULONGLONG, std::set<ExportedFunc>> &addr_to_func,
145 OUT std::set<ULONGLONG> ¬_found
148 std::set<ULONGLONG> coveredAddresses;
149 std::set<ULONGLONG>::iterator addrItr;
150 for (addrItr = addresses.begin(); addrItr != addresses.end(); ++addrItr) {
152 ULONGLONG searchedAddr = *addrItr;
154 const std::set<ExportedFunc>* exports_for_va = exportsMap.find_exports_by_va(searchedAddr);
155 if (exports_for_va ==
nullptr) {
156 not_found.insert(searchedAddr);
158 std::cerr <<
"Cannot find any DLL exporting: " << std::hex << searchedAddr << std::endl;
163 for (std::set<ExportedFunc>::iterator strItr = exports_for_va->begin();
164 strItr != exports_for_va->end();
167 std::string dll_name = strItr->libName;
168 if (dll_name != chosenDll) {
172 addr_to_func[searchedAddr].insert(func);
173 coveredAddresses.insert(searchedAddr);
175 if (addr_to_func.find(searchedAddr) == addr_to_func.end()) {
176 const ExportedFunc* func = exportsMap.find_export_by_va(searchedAddr);
177 not_found.insert(searchedAddr);
179 std::cerr <<
"[WARNING] A function: " << func->
toString() <<
" not found in the covering DLL: " << chosenDll << std::endl;
183 return coveredAddresses.size();
198 std::cout <<
"[-] Not all addresses are covered! Not found: " << std::dec <<
notFound.size() << std::endl;
201 std::cout <<
"All covered!" << std::endl;
210 std::cerr <<
"[-] Function not recovered: [" << std::hex << searchedAddr <<
"] " << std::endl;
218 bool skip_bound =
false;
220 if (importsDir == NULL) {
224 DWORD maxSize = importsDir->Size;
225 DWORD impAddr = importsDir->VirtualAddress;
227 IMAGE_IMPORT_DESCRIPTOR* lib_desc = NULL;
228 DWORD parsedSize = 0;
230 printf(
"---IMP---\n");
233 while (parsedSize < maxSize) {
235 lib_desc = (IMAGE_IMPORT_DESCRIPTOR*)(impAddr + parsedSize + (ULONG_PTR) modulePtr);
236 if (!
validate_ptr(modulePtr, moduleSize, lib_desc,
sizeof(IMAGE_IMPORT_DESCRIPTOR))) {
237 printf(
"[-] Invalid descriptor pointer!\n");
240 parsedSize +=
sizeof(IMAGE_IMPORT_DESCRIPTOR);
241 if (lib_desc->OriginalFirstThunk == NULL && lib_desc->FirstThunk == NULL) {
244 const bool is_bound = (lib_desc->TimeDateStamp == (-1));
245 if (is_bound && skip_bound) {
249 printf(
"Imported Lib: %x : %x : %x\n", lib_desc->FirstThunk, lib_desc->OriginalFirstThunk, lib_desc->Name);
252 std::string lib_name =
"";
253 if (lib_desc->Name != 0) {
254 LPSTR name_ptr = (LPSTR)((ULONGLONG) modulePtr + lib_desc->Name);
256 lib_name = (LPSTR)((ULONGLONG) modulePtr + lib_desc->Name);
260 DWORD call_via = lib_desc->FirstThunk;
261 DWORD thunk_addr = lib_desc->OriginalFirstThunk;
262 std::set<ULONGLONG> addresses;
264 find_addresses_to_fill<DWORD>(call_via, thunk_addr, modulePtr, moduleSize, exportsMap, addresses);
266 find_addresses_to_fill<ULONGLONG>(call_via, thunk_addr, modulePtr, moduleSize, exportsMap, addresses);
270 bool is_lib_erased =
false;
274 if (lib_name.length() == 0) {
275 is_lib_erased =
true;
276 if (is_all_covered) {
278 lib_name = dllCoverage.
dllName;
281 if (lib_name.length() == 0) {
286 std::cout << lib_name << std::endl;
298 const std::string dll_with_ext = exportsMap.get_dll_fullname(dllCoverage.
dllName);
303 std::cout <<
"---------" << std::endl;
std::string toString() const
const std::set< ExportedFunc > * find_exports_by_va(ULONGLONG va) const
std::string mappedDllName
const peconv::ExportsMapper & exportsMap
size_t mapAddressesToFunctions(const std::string &_mappedDllName)
std::set< ULONGLONG > notFound
std::set< ULONGLONG > & addresses
std::map< ULONGLONG, std::set< ExportedFunc > > addrToFunc
bool uneraseDllName(IMAGE_IMPORT_DESCRIPTOR *lib_desc, const std::string &dll_name)
bool uneraseDllImports(IN OUT IMAGE_IMPORT_DESCRIPTOR *lib_desc, IN ImportedDllCoverage &dllCoverage, OUT OPTIONAL ImpsNotCovered *not_covered)
std::map< ULONGLONG, ULONGLONG > thunkToAddr
void insert(ULONGLONG thunk, ULONGLONG searchedAddr)
Functions related to operations on files. Wrappers for read/write.
std::string find_covering_dll(std::set< ULONGLONG > &addresses, const peconv::ExportsMapper &exportsMap)
size_t map_addresses_to_functions(std::set< ULONGLONG > &addresses, IN const std::string &chosenDll, IN const peconv::ExportsMapper &exportsMap, OUT std::map< ULONGLONG, std::set< ExportedFunc > > &addr_to_func, OUT std::set< ULONGLONG > ¬_found)
std::set< std::string > get_all_dlls_exporting_function(ULONGLONG func_addr, const peconv::ExportsMapper &exportsMap)
size_t find_addresses_to_fill(FIELD_T call_via, FIELD_T thunk_addr, LPVOID modulePtr, size_t moduleSize, IN const peconv::ExportsMapper &exportsMap, OUT std::set< ULONGLONG > &addresses)
std::set< std::string > get_dlls_intersection(const std::set< std::string > &dllNames, const std::set< std::string > &currDllNames)
Functions and classes responsible for fixing Import Table. A definition of ImportedDllCoverage class.
A definition of ImportsUneraser class - for recovery of a partialy erased Import Table.
bool validate_ptr(IN const void *buffer_bgn, IN SIZE_T buffer_size, IN const void *field_bgn, IN SIZE_T field_size)
bool fix_imports(IN OUT PVOID modulePtr, IN size_t moduleSize, IN const peconv::ExportsMapper &exportsMap, OUT OPTIONAL peconv::ImpsNotCovered *notCovered)
bool is64bit(IN const BYTE *pe_buffer)
IMAGE_DATA_DIRECTORY * get_directory_entry(IN const BYTE *pe_buffer, IN DWORD dir_id, IN bool allow_empty=false)
std::string get_dll_shortname(const std::string &str)