10 size_t fetch_callbacks_list(IN PVOID modulePtr, IN
size_t moduleSize, IN DWORD callbacks_rva, OUT std::vector<ULONGLONG> &tls_callbacks)
12 FIELD_T* callbacks_list_ptr = (FIELD_T*)(callbacks_rva + (BYTE*)modulePtr);
13 if (!
validate_ptr(modulePtr, moduleSize, callbacks_list_ptr,
sizeof(FIELD_T))) {
17 const ULONGLONG img_base = (ULONGLONG)modulePtr;
20 for (FIELD_T *next_callback = callbacks_list_ptr;
21 validate_ptr(modulePtr, moduleSize, next_callback,
sizeof(FIELD_T));
24 FIELD_T value = *next_callback;
25 if (value == 0)
break;
27 tls_callbacks.push_back(value);
37 if (!img_size || !callback_addr)
return false;
40 if (callback_addr >= img_base && callback_addr < (img_base + img_size)) {
44 if (callback_addr < img_size) {
54 const ULONGLONG img_base = (ULONGLONG)modulePtr;
56 if (!img_size)
return 0;
58 if (moduleSize == 0) {
59 moduleSize = img_size;
62 if (!tls_dir)
return 0;
64 ULONGLONG callbacks_addr = tls_dir->AddressOfCallBacks;
65 if (!callbacks_addr)
return 0;
67 std::cout <<
"TLS Callbacks Table: " << std::hex << callbacks_addr << std::endl;
69 DWORD callbacks_rva = 0;
72 std::cout <<
"TLS Callbacks RVA: " << std::hex << callbacks_rva << std::endl;
76 counter = fetch_callbacks_list<ULONGLONG>(modulePtr, moduleSize, callbacks_rva, tls_callbacks);
79 counter = fetch_callbacks_list<DWORD>(modulePtr, moduleSize, callbacks_rva, tls_callbacks);
87 if (moduleSize == 0) {
88 moduleSize = img_size;
90 std::vector<ULONGLONG> tls_callbacks;
94 std::vector<ULONGLONG>::iterator itr;
96 for (itr = tls_callbacks.begin(); itr != tls_callbacks.end(); ++itr, i++) {
97 ULONGLONG callback_addr = *itr;
105 std::cout << std::hex <<
"TLS RVA:" << rva << std::endl;
107 ULONG_PTR callback_va = rva + (ULONG_PTR)modulePtr;
108 if (!
validate_ptr(modulePtr, moduleSize, (BYTE*)callback_va,
sizeof(BYTE))) {
112 void(NTAPI *callback_func)(PVOID DllHandle, DWORD dwReason, PVOID) = (
void(NTAPI *)(PVOID, DWORD, PVOID)) (callback_va);
114 std::cout <<
"Calling TLS callback[" << i <<
"]:" << std::endl;
116 callback_func(modulePtr, dwReason, NULL);