Windows内核驱动:"CLIENT_ID CreatingThreadId"中的"HANDLE UniqueThread"在加载过程中是否相同?

Windows Kernel Driver: Does the "HANDLE UniqueThread" in "CLIENT_ID CreatingThreadId" is the same during the process loading?

我正在尝试编写一个 APC dll 注入驱动程序,我找到了 this 示例并想根据我的需要对其进行修改。

理解代码后,我想到了修改它的方式(我的问题由此而来)。

code, the writer used PsLookupThreadByThreadId中接收指向目标进程的ETHREAD结构的引用指针。

PsLookupThreadByThreadId(pSpi->Threads[0].ClientId.UniqueThread,&Thread)

但要获得 SYSTEM_THREAD_INFORMATION for the UniqueThread handle, he used ZwQuerySystemInformation

我想在加载 ntdll 后立即加载我的 dll,所以我想使用 PsSetCreateProcessNotifyRoutineEx and save the UniqueThread from the PS_CREATE_NOTIFY_INFO 我在为我的目标进程调用回调时得到的。

并且在加载 ntdll 之后,感谢 PsSetLoadImageNotifyRoutineEx 我会知道这一点,我可以使用他的 APC 注入逻辑注入我的 dll。

我的目标是在 PloadImageNotifyRoutine callback, but don't use ZwQuerySystemInformation as he does to get the UniqueThread, but save it in the PcreateProcessNotifyRoutineEx 回调中注入我的 dll。

所以,我的问题是:我可以相信 UniqueThread I get from PS_CREATE_NOTIFY_INFO 在所有进程加载期间都是相同的吗?

I want to use PsSetCreateProcessNotifyRoutineEx and save the UniqueThread from the PS_CREATE_NOTIFY_INFO I got when the callback is called for the process I'm targeting.

关于 CreatingThreadId 来自 PS_CREATE_NOTIFY_INFO

The process ID and thread ID of the process and thread that created the new process

此 ID 不适用于新创建的 process/thread,但适用于创建者。如果你想在 PloadImageNotifyRoutine 回调中注入自己的 dll - PcreateProcessNotifyRoutineEx 对你没用。

当图像映射到目标进程时调用的 PloadImageNotifyRoutine - 在 ZwMapViewOfSection . you need check that ProcessId (second parameter of PcreateProcessNotifyRoutineEx - The process ID of the process where image is loaded) is equal to PsGetCurrentProcessId(). this mean that image loaded to the current process and you can use KeGetCurrentThread() 内部 - 你 根本不需要 PsLookupThreadByThreadId

I want to load my dll right after ntdll is loaded

目前正在处理尚未初始化的任何用户模式结构。因为它是由 ntdll 初始化的。结果 - 如果你注入你的 apc 并在此时强制执行它 - 你会遇到进程崩溃。仅此而已

我可以建议您在加载 kernel32.dll 时注入您的 dll。在这里你需要检查这是作为 dll 加载,而不仅仅是图像映射 - 检查线程 teb 中的 ArbitraryUserPointer - 它是否指向 L"*\kernel32.dll"smss.exe map kernel32.dll during create \KnownDlls (ArbitraryUserPointer == 0 in this case), wow64 process several time map kernel32.dll(32 位和 64 位)在 ArbitraryUserPointer

中具有 L"WOW64_IMAGE_SECTION"L"NOT_AN_IMAGE" 名称