在C中捕获GNU/Linux中的击键

15 c linux keylogger

如果我在应用程序中工作并且我从键盘按下键,我怎样才能在用户区中的GNU/LINUX下的C中捕获该键(或字符串),包括源应用程序的名称,而不是X11 :)

谢谢.

aem*_*mus 20

好吧,没有X11这个问题就更难了.
对于击键部分,你可以使用类似于这个的代码,但你必须作为参数传递你正在阅读的设备(键盘,通常是/ dev/input/event0)

#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    int fd;
    if(argc < 2) {
        printf("usage: %s <device>\n", argv[0]);
        return 1;
    }
    fd = open(argv[1], O_RDONLY);
    struct input_event ev;

    while (1)
    {
    read(fd, &ev, sizeof(struct input_event));

    if(ev.type == 1)
        printf("key %i state %i\n", ev.code, ev.value);

    }
}
Run Code Online (Sandbox Code Playgroud)

积分不给我,这段代码取自Ventriloctrl黑客来获取击键. http://public.callutheran.edu/~abarker/ventriloctrl-0.4.tar.gz

希望我有所帮助.

  • 请注意,这样的程序必须以root身份运行,因为/ dev/input/event*文件是拥有的并且限制为root. (9认同)

Jon*_*ler 1

一种可能性:找到并查看“ sudosh ”的源代码,即“sudo shell”(或其替代品之一,因为它已经有一段时间没有被修改了;Google 是你的朋友)。

它与伪终端混淆并通过将信息记录到文件来跟踪所有输入和输出。

这对你来说是否足够精确可能还有待商榷。它会记录所有应用程序的所有击键。我也不确定它如何与 X11 一起工作 - 如果它与 X11 一起工作。