简体   繁体   English

windows 服务的全球键盘挂钩

[英]Global Keyboard Hook from windows service

Is it possible to write a global Keyboard Hook from windows(xp and 7) service?是否可以从 Windows(xp 和 7)服务编写全局键盘挂钩? ( using SetWindowsHookEx didn't work from a system service ) (使用 SetWindowsHookEx 在系统服务中不起作用)

The documentation for SetWindowsHookEx says: SetWindowsHookEx的文档说:

or with all threads in the same desktop as the calling thread.或与调用线程在同一桌面上的所有线程。

So you need to be associated with the same desktop (and there will be multiple desktops even without considering terminal services: the normal desktop, the secure desktop (used for UAC and login) and the screen saver).所以你需要关联同一个桌面(即使不考虑终端服务也会有多个桌面:普通桌面、安全桌面(用于UAC和登录)和屏幕保护程序)。

Since services are not associated with an interactive session (and, from Windows V6, cannot be) you'll need a process within the user's session to do the hooking, and communicate with the backend service (eg. via a named pipe).由于服务不与交互式 session 相关联(并且,从 Windows V6 开始,不能),因此您需要在用户的 session 中使用一个进程来与命名为管道的后端服务进行通信(例如)。 And don't forget to test with no-one logged in, and multiple users logged in.并且不要忘记在没有人登录和多个用户登录的情况下进行测试。

To summarize what Richard said (and to add my own commentary): You cannot directly set a global keyboard hook from a Windows service starting with Windows Vista.总结理查德所说的(并添加我自己的评论):您不能直接从 Windows 服务设置全局键盘挂钩,从 Windows Vista 开始。 However, it still would be a logical choice to use a Windows service as you will have all the necessary privileges to set global hooks.但是,使用 Windows 服务仍然是一个合乎逻辑的选择,因为您将拥有设置全局挂钩所需的所有权限。 You can duplicate your privileges to child processes that you create in each logged-on user's session to enable your child processes to set global hooks within their own desktop/session even if the user does not hold adequate privileges.您可以将您的权限复制到您在每个登录用户的 session 中创建的子进程,以使您的子进程能够在他们自己的桌面/会话中设置全局挂钩,即使用户没有足够的权限。

Personally, (assuming you're writing a keylogger) instead of aggregating all of the logged keystrokes to one process and writing them to disk, it might make sense to have each individual process write separate files according to their username.就个人而言,(假设您正在编写键盘记录器)而不是将所有记录的击键汇总到一个进程并将它们写入磁盘,让每个单独的进程根据其用户名编写单独的文件可能是有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM