ModifyTLSCallbackPtr - changes the program TLS callback at runtime by modifying the data directory ptr (IMAGE_DIRECTORY_ENTRY_TLS) returns true on success */
| 543 | returns true on success |
| 544 | */ |
| 545 | bool Process::ModifyTLSCallbackPtr(__in const uintptr_t NewTLSFunction) |
| 546 | { |
| 547 | HMODULE hModule = GetModuleHandle(NULL); |
| 548 | IMAGE_DOS_HEADER* dosHeader = (IMAGE_DOS_HEADER*)hModule; |
| 549 | IMAGE_NT_HEADERS* ntHeader = (IMAGE_NT_HEADERS*)((BYTE*)dosHeader + dosHeader->e_lfanew); |
| 550 | |
| 551 | IMAGE_TLS_DIRECTORY* tlsDir = (IMAGE_TLS_DIRECTORY*)((BYTE*)hModule + ntHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].VirtualAddress); |
| 552 | |
| 553 | if (tlsDir == nullptr) |
| 554 | return false; |
| 555 | |
| 556 | DWORD dwOldProt = 0; |
| 557 | if (VirtualProtect((LPVOID)tlsDir->AddressOfCallBacks, sizeof(uintptr_t), PAGE_EXECUTE_READWRITE, &dwOldProt)) |
| 558 | { |
| 559 | __try |
| 560 | { |
| 561 | memcpy((void*)(tlsDir->AddressOfCallBacks), (const void*)&NewTLSFunction, sizeof(uintptr_t)); |
| 562 | return true; |
| 563 | } |
| 564 | __except (EXCEPTION_EXECUTE_HANDLER) |
| 565 | { |
| 566 | Logger::logf(Err, "Failed to write TLS callback ptr @ Process::ModifyTLSCallbackPtr"); |
| 567 | return false; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | return false; |
| 572 | } |
| 573 | |
| 574 | /* |
| 575 | _GetProcAddress - Attempt to retrieve address of function of `Module`, given `lpProcName` |
nothing calls this directly
no outgoing calls
no test coverage detected