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());
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();
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))) {
238 std::cout <<
"[-] Invalid descriptor pointer!\n";
242 parsedSize +=
sizeof(IMAGE_IMPORT_DESCRIPTOR);
243 if (lib_desc->OriginalFirstThunk == NULL && lib_desc->FirstThunk == NULL) {
246 const bool is_bound = (lib_desc->TimeDateStamp == (-1));
247 if (is_bound && skip_bound) {
251 printf(
"Imported Lib: %x : %x : %x\n", lib_desc->FirstThunk, lib_desc->OriginalFirstThunk, lib_desc->Name);
254 std::string lib_name =
"";
255 if (lib_desc->Name != 0) {
256 LPSTR name_ptr = (LPSTR)((ULONGLONG) modulePtr + lib_desc->Name);
258 lib_name = (LPSTR)((ULONGLONG) modulePtr + lib_desc->Name);
262 DWORD call_via = lib_desc->FirstThunk;
263 DWORD thunk_addr = lib_desc->OriginalFirstThunk;
264 std::set<ULONGLONG> addresses;
272 bool is_lib_erased =
false;
276 if (lib_name.length() == 0) {
277 is_lib_erased =
true;
278 if (is_all_covered) {
280 lib_name = dllCoverage.
dllName;
283 if (lib_name.length() == 0) {
288 std::cout << lib_name << std::endl;
300 const std::string dll_with_ext = exportsMap.get_dll_fullname(dllCoverage.
dllName);
305 std::cout <<
"---------" << std::endl;
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)
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)