Windows 内核驱动程序 : Does the "HANDLE UniqueThread" in "CLIENT_ID CreatingThreadId" is the same during the process loading?

标签 windows winapi driver windows-kernel

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

在我理解了代码之后,我想到了如何修改它(我的问题由此而来)。

code ,作者使用了PsLookupThreadByThreadId接收指向目标进程的 ETHREAD 结构的引用指针。

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

但要得到 SYSTEM_THREAD_INFORMATION对于UniqueThread handle ,他用过ZwQuerySystemInformation

我想在加载 ntdll 后立即加载我的 dll,所以我想使用 PsSetCreateProcessNotifyRoutineEx并保存 UniqueThread来自 PS_CREATE_NOTIFY_INFO当我的目标进程调用回调时,我得到了。

在加载 ntdll 之后,我会知道这要感谢 PsSetLoadImageNotifyRoutineEx我可以使用他的 APC 注入(inject)逻辑注入(inject)我的 dll。

我的目标是将我的 dll 注入(inject) PloadImageNotifyRoutine回调,但不要使用 ZwQuerySystemInformation正如他为获得 UniqueThread 所做的那样, 但将其保存在 PcreateProcessNotifyRoutineEx 中回调。

因此,我的问题是:我可以信任 UniqueThread 吗?我从 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 不是针对新创建的进程/线程,而是针对创建者。如果你想在 PloadImageNotifyRoutine 回调中注入(inject)自己的 dll - PcreateProcessNotifyRoutineEx 对你没用。

图像映射到目标进程时调用的 PloadImageNotifyRoutine - 内部 ZwMapViewOfSection .您需要检查 ProcessId(PcreateProcessNotifyRoutineEx 的第二个参数 - 加载图像的进程的进程 ID)是否等于 PsGetCurrentProcessId() .这意味着图像已加载到当前进程,您可以使用 KeGetCurrentThread() - 你根本不需要PsLookupThreadByThreadId

I want to load my dll right after ntdll is loaded

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

我可以建议您在加载 kernel32.dll 时注入(inject)您的 dll。在这里你需要检查这是作为 dll 加载,而不仅仅是图像映射 - 检查线程 teb 中的 ArbitraryUserPointer - 它是否指向 L"*\\kernel32.dll" : smss.exe 在创建 \\KnownDlls 期间映射 kernel32.dll (在本例中为 ArbitraryUserPointer == 0) , wow64 处理多个时间映射 kernel32.dll(32 位和 64 位),L"WOW64_IMAGE_SECTION"L"NOT_AN_IMAGE" 名称在 任意用户指针

关于Windows 内核驱动程序 : Does the "HANDLE UniqueThread" in "CLIENT_ID CreatingThreadId" is the same during the process loading?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50304383/

相关文章:

c++ - 在 Windows 中将文件所有者设置为不存在的用户/SID

c++ - 结合 GetMessage 和 PeekMessage

c++ - 使用 C++ Hook QT 应用程序以捕获应用程序的文本名称

windows - 有谁知道从哪里可以获得旧的 WDM 版本的 toastr 驱动程序示例?

c - Minifilter 导致磁盘管理和系统恢复滞后

c - 如何运行KMDF驱动程序?

c++ - 如何以及在注册表中写入什么以在 Windows 启动时自动重启程序

c# - 通过 C# 运行时无法识别 BCDEDIT

c++ - Qt5、透明窗口和滚轮事件

c - 设备树在Linux(树莓派)启动时绑定(bind)所需的设备驱动程序