由于与http://undocumented.ntinternals.net上的特定问题有关的大多数链接显然已失效,并且NtQueryInfoThread和相关的THREADINFOCLASSs已从Winternl.h中消失了。我现在正坐在这里努力寻找我知道要处理的进程的TEB。
我尝试从ntdll.dll加载该方法,这是另一个可行的解决方案,但可惜我仍然无法获得所需的地址。

typedef NTSTATUS(*ThreadInfoProc)(HANDLE, THREADINFOCLASS, PVOID, ULONG,  PULONG);
PVOID CProcessHelper::GetThreadStackTopAddress(HANDLE hThread)
{
HINSTANCE ntdllInstance;
ThreadInfoProc NtQueryInfoThread;

ntdllInstance = LoadLibrary("Ntdll.dll");

if (ntdllInstance != NULL)
{
    NtQueryInfoThread = (ThreadInfoProc)GetProcAddress(ntdllInstance, "NtQueryInformationThread");

    if (NtQueryInfoThread != NULL)
    {

        THREAD_BASIC_INFORMATION bi;
        NT_TIB tib;

        NTSTATUS ntstat = 0;
        NTSTATUS ntstat = (NtQueryInfoThread)(hThread, (THREADINFOCLASS)0, &bi, sizeof(THREAD_BASIC_INFORMATION),NULL);

        ReadProcessMemory(CurrentProcessHandle, bi.TebBaseAddress, &tib, sizeof(NT_TIB), 0);

        PrintHex(tib.StackBase); // output: CCCCCCCCCC
    }
}

return nullptr;
}

还有其他方法,也许使用公共(public)api调用来获取线程的TEB? (如MSDN所述,此方法不应再使用。)

最好的祝福,

亚历克斯

最佳答案

可以正常工作:S获得线程的TEB的唯一其他方法是使用以下方法读取它:
NT_TIB* tib = (NT_TIB*)__readfsdword(0x18);
并从中读取基址。

您的调用可能失败,因为您可能没有读取内存的正确权限。尝试使用VirtualProtect吗?

下面的作品,但我只在当前过程中对其进行了测试。

#include <iostream>
#include <windows.h>

typedef LONG NTSTATUS;
typedef DWORD KPRIORITY;
typedef WORD UWORD;

typedef struct _CLIENT_ID
{
    PVOID UniqueProcess;
    PVOID UniqueThread;
} CLIENT_ID, *PCLIENT_ID;

typedef struct _THREAD_BASIC_INFORMATION
{
    NTSTATUS                ExitStatus;
    PVOID                   TebBaseAddress;
    CLIENT_ID               ClientId;
    KAFFINITY               AffinityMask;
    KPRIORITY               Priority;
    KPRIORITY               BasePriority;
} THREAD_BASIC_INFORMATION, *PTHREAD_BASIC_INFORMATION;

enum THREADINFOCLASS
{
    ThreadBasicInformation,
};

void* GetThreadStackTopAddress(HANDLE hProcess, HANDLE hThread)
{
    bool loadedManually = false;
    HMODULE module = GetModuleHandle("ntdll.dll");

    if (!module)
    {
        module = LoadLibrary("ntdll.dll");
        loadedManually = true;
    }

    NTSTATUS (__stdcall *NtQueryInformationThread)(HANDLE ThreadHandle, THREADINFOCLASS ThreadInformationClass, PVOID ThreadInformation, ULONG ThreadInformationLength, PULONG ReturnLength);
    NtQueryInformationThread = reinterpret_cast<decltype(NtQueryInformationThread)>(GetProcAddress(module, "NtQueryInformationThread"));

    if (NtQueryInformationThread)
    {
        NT_TIB tib = {0};
        THREAD_BASIC_INFORMATION tbi = {0};

        NTSTATUS status = NtQueryInformationThread(hThread, ThreadBasicInformation, &tbi, sizeof(tbi), nullptr);
        if (status >= 0)
        {
            ReadProcessMemory(hProcess, tbi.TebBaseAddress, &tib, sizeof(tbi), nullptr);

            if (loadedManually)
            {
                FreeLibrary(module);
            }
            return tib.StackBase;
        }
    }


    if (loadedManually)
    {
        FreeLibrary(module);
    }

    return nullptr;
}

void __stdcall Test()
{
    for (int i = 0; i < 10; ++i)
    {
        printf("Hi. ");
        Sleep(500);
    }
}


int main()
{
    std::cout<<GetThreadStackTopAddress(GetCurrentProcess(), GetCurrentThread())<<"\n";

    DWORD threadID = 0;
    HANDLE hThread = CreateThread(nullptr, 0, reinterpret_cast<LPTHREAD_START_ROUTINE>(Test), nullptr, 0, &threadID);
    std::cout<<GetThreadStackTopAddress(GetCurrentProcess(), hThread)<<"\n\n";
    CloseHandle(hThread);
    Sleep(7000);

    return 0;
}

关于c++ - 通过线程的句柄获取线程的TIB/TEB(2015),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32297431/

10-10 12:53