如何在c++中检测win32进程的创建/终止
我知道,要接收有关 Win32 进程创建或终止的通知,我们可以使用 API PsSetCreateProcessNotifyRoutine()
实现 NT 内核模式驱动程序,该驱动程序提供注册由操作系统调用的系统范围回调函数的能力每次新进程启动、退出或终止时。
是否可以在不创建 NT 内核模式驱动程序的情况下,仅使用 c++ 使用 Win32 API 函数?当然不使用无限循环查询活动进程列表的基本解决方案。
是否有任何库或 win32 API 提供相同的功能(系统范围回调、异步事件)?
I know that to receive notifications about Win32 process creation or termination we might implement a NT kernel-mode driver using the APIs PsSetCreateProcessNotifyRoutine()
that offers the ability to register system-wide callback function which is called by OS each time when a new process starts, exits or is terminated.
Is this possible without creating a NT kernel-mode driver, only using Win32 API functions using c++? Not using the basic solution of a infinite cycle querying the list of active process of course.
Is there any library or win32 API that provides the same functionality (system wide callback, asynchronous events)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
WMI 很棒,它也适用于进程名称。尽管如果您需要跟踪进程终止,更轻量级和更简单的方法如下:
一旦进程终止,此代码将调用
WaitOrTimerCallback
。WMI is great and it works with process names too. Although if you need to track process termination the more lightweight and easier way is the following:
This code will call
WaitOrTimerCallback
once the process terminated.我唯一能想到的是 WMI,不确定它是否提供进程创建回调,但它可能值得研究。
The only thing I could think of is WMI, not sure if it provides a process creation callback, but it might be worth looking into.
Anders 是正确的,WMI 对此非常有效。因为我的项目需要这个,所以我可以共享用于检测(任意)进程终止的代码(给定其 ID):
ProcessTerminationNotification.h:
ProcessTerminationNotification.cpp:
请注意,初始化 COM 和 COM 进程安全性(CoInitializeEx 和 CoInitializeSecurity)的代码是此处省略,因为它应该在应用程序初始化中完成。
将其与全局函数一起使用或使用 boost::bind 连接到任意方法,后者的示例:
Anders is correct, WMI works nicely for this. Since I needed this for a project I can share the code for detecting (arbitrary) process termination (given its ID):
ProcessTerminationNotification.h:
ProcessTerminationNotification.cpp:
Note that the code to initialize COM and COM process security (CoInitializeEx and CoInitializeSecurity) is omitted here since it should be done in the application initialization.
Use it with global functions or use boost::bind to connect to an arbitrary method, example of the latter:
正如前面的评论已经暗示的那样,使用 WMI 监视进程事件有一个缺点,因为 WMI 不同步提供事件,即短暂的延迟。
《Windows Internals Part 1》一书提到了一种名为“Event Tracing for Windows (ETW)”的机制,它是操作系统事件的低级机制。
以下博客文章展示了如何在 .Net 中使用 ETW 来监视进程:
http://blogs.msdn.com/b/vancem/archive/2013/03/09/using-traceevent-to-mine-information-in-os-registered-etw-providers.aspx
As already hinted by a previous comment, there's a drawback with using WMI to monitor process events as WMI is not providing events synchronously, .i.e. with a short delay.
The book "Windows Internals Part 1" is referring to a mechanism called "Event Tracing for Windows (ETW)" which is a low-level mechanism for operating system events.
The following blog post shows how ETW can be used within .Net to monitor processes:
http://blogs.msdn.com/b/vancem/archive/2013/03/09/using-traceevent-to-mine-information-in-os-registered-etw-providers.aspx
您可以使用 SetWindowsHookEx 监视所有窗口创建过程与 CBTProc,但是更多这需要 WMI、Windows 驱动程序或一点点“黑魔法'
You can monitor all Window creating processes using SetWindowsHookEx with a CBTProc, however anything more than that requires either WMI, a windows driver or a little bit of 'Black Magic'
您可以通过挂钩 CreateProcessInternalW 函数来监视进程创建。通过挂钩此函数,您甚至可以将 DLL 注入到新进程中。
You can monitor process creation by hooking
CreateProcessInternalW
function. By hooking this function, you can even inject DLLs into the new process.如果设计不当,WMI 查询可能会消耗大量 CPU 性能。如果使用 Win32_Process 类中的内部事件来跟踪进程创建事件,则会影响性能 严重。另一种方法是利用安全审核日志。您可以使用本地安全策略启用进程跟踪,或者在多台计算机的情况下使用 GPO。进程跟踪开始后,您可以使用自定义 XML 查询订阅安全事件日志,以监控您感兴趣的某些进程。进程创建事件ID为4688。
`
`
WMI queries can cost heavy CPU performance if not designed properly. If an intrinsic event from Win32_Process class is used to track process creation event, this impacts performance heavily. An alternate approach is to leverage Security Audit logs. You can enable Process Tracking using Local Security Policy or using a GPO in case of multiple machines. Once the process tracking starts, you can subscribe to security event logs with a custom XML query to monitor certain processes of your interest. The process creation event ID is 4688.
`
`
除了WMI之外,或者如果您需要阻止进程或线程启动,或者当您需要同步通知时,您可以使用内核模式驱动程序方法。例如,我们的 CallbackProcess 产品正是这样做的。
Besides WMI, or if you need to prevent the process or thread from being started, or when you need synchronous notifications, you can use a kernel-mode driver approach. Our CallbackProcess product, for example, does exactly this.
API 挂钩应该是完成此类任务的正确方法。您可以挂钩 createProcess(A/W/asUserA/W.... 等) 和 NtTerminateProcess
API-hooking should be the right way to fullfill something like that. You can hook createProcess(A/W/asUserA/W.... etc) and NtTerminateProcess