-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(loader): handle unexpected loading order
- Loading branch information
1 parent
8a40994
commit dcd82c6
Showing
5 changed files
with
117 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include "Win32Internals.hpp" | ||
|
||
#define ThreadQuerySetWin32StartAddress 9 | ||
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L) | ||
|
||
typedef NTSTATUS(WINAPI* pNtQueryInformationThread)( | ||
HANDLE ThreadHandle, | ||
LONG ThreadInformationClass, | ||
PVOID ThreadInformation, | ||
ULONG ThreadInformationLength, | ||
PULONG ReturnLength | ||
); | ||
|
||
namespace Win32Internals { | ||
uint64_t GetThreadStartAddress(uint64_t thread_id) { | ||
// Resolve the internal NtQueryInformationThread function (non-public). | ||
pNtQueryInformationThread NtQueryInformationThread = (pNtQueryInformationThread)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQueryInformationThread"); | ||
if (NtQueryInformationThread == NULL) { | ||
return 0; | ||
} | ||
|
||
// Get a duplicate thread query handle. | ||
HANDLE hCurrentProcess = GetCurrentProcess(); | ||
HANDLE hThreadQueryHandle = (HANDLE)0; | ||
if (!DuplicateHandle(hCurrentProcess, (HANDLE)thread_id, hCurrentProcess, &hThreadQueryHandle, THREAD_QUERY_INFORMATION, FALSE, 0)) { | ||
SetLastError(ERROR_ACCESS_DENIED); | ||
return 0; | ||
} | ||
|
||
// Query for the thread entry point address (ThreadQuerySetWin32StartAddress). | ||
DWORD_PTR dwStartAddress; | ||
NTSTATUS ntStatus = NtQueryInformationThread(hThreadQueryHandle, ThreadQuerySetWin32StartAddress, &dwStartAddress, sizeof(DWORD_PTR), NULL); | ||
CloseHandle(hThreadQueryHandle); | ||
|
||
if (ntStatus != STATUS_SUCCESS) { | ||
return 0; | ||
} | ||
|
||
return (uint64_t)dwStartAddress; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#pragma once | ||
#include <cstdint> | ||
#include <Windows.h> | ||
|
||
namespace Win32Internals { | ||
uint64_t GetThreadStartAddress(uint64_t thread_id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters