尝试使用NtQuerySystemInformation获取特定进程的线程起始地址

时间:2019-07-27 06:40:37

标签: c windows winapi kernel

我正在使用Windows 7 x86(并为Windows 7 x86编码)。

问题:尝试获取特定进程的线程启动列表(带有模块名称)

简单的方法,也就是NtQueryInformationThread,需要具有ThreadQueryInformation访问权限的线程句柄。

尝试使用Thread_Query_Information的OpenThread,始终返回0xc0000022。

因此,我查找了ProcessHacker的源代码,该代码通过NtQuerySystemInformation获取Process的线程列表。

问题是,在原始PH代码正常工作的情况下,当我尝试迭代时,大多数线程返回无效值。

我在这里做什么错了?

SeDebugPrivilege,已授予UAC管理员模式,以获取Thread_Query_Information访问权限。但是失败了:(

void QueryStartAddr()
{
    ULONG ReturnLength;
    PVOID buff;
    PSYSTEM_PROCESS_INFO spi;

    HMODULE ntdll = GetModuleHandleW(L"ntdll.dll");
    NTQUERYSYSTEMINFORMATION pNTQSI;
    pNTQSI = (NTQUERYSYSTEMINFORMATION)
        GetProcAddress(ntdll, "NtQuerySystemInformation");

    //get buffer size to allocate
    pNTQSI(SystemProcessInformation, NULL, NULL, &ReturnLength);
    buff = VirtualAlloc(NULL, ReturnLength, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
    printf("szBuffer: %d \n", ReturnLength);

    spi = (PSYSTEM_PROCESS_INFO)buff;
    pNTQSI(SystemProcessInformation, spi, ReturnLength, NULL);

    while (spi->NextEntryOffset)
    {

        int status = pNTQSI(SystemProcessInformation, spi, ReturnLength, NULL);
        printf("PID: %08x, %d \n", spi->UniqueProcessId, spi->UniqueProcessId);
        if (spi->UniqueProcessId == (HANDLE)pid)
        {
            PSYSTEM_THREAD_INFORMATION threads = spi->Threads;
            for (int i = 0; i < spi->NumberOfThreads; i++)
            {
                PSYSTEM_THREAD_INFORMATION thread = &threads[i];
                int startaddr = (int)thread->StartAddress;
                printf("PID: %d, TID: %d, Startaddr : %08x \n", spi->UniqueProcessId, thread->ClientId.UniqueThread, thread->StartAddress);

            }

        }

        spi = (PSYSTEM_PROCESS_INFO)((LPBYTE)spi + spi->NextEntryOffset);
    }

Result Comparison

0 个答案:

没有答案