【问题标题】:Cannot capture keystrokes in C [Ubuntu 16.04LTS]无法在 C 中捕获击键 [Ubuntu 16.04LTS]
【发布时间】:2016-09-18 16:02:56
【问题描述】:

一段时间以来,我一直在尝试在 Ubuntu 16.04LTS 上制作键盘记录器,这就是我目前所拥有的:

#include <stdio.h>
#include <fcntl.h>
#include <linux/input.h>
#include <stdbool.h>

int main()
{
        char devname[] = "/dev/input/event0";
        int device = open(devname, O_RDONLY);
        struct input_event ev; 
        bool logging = true;

        while(logging)
        {
                if (read(device,&ev, sizeof(ev)) >= 0){ 
                        printf("Key: %i State: %i Type: %i\n",ev.code,ev.value,ev.type);
                }
        }

}

但是,当我编译并运行它(gcc)时,它没有输出任何东西! 我已经尝试了 /dev/input/by-id 中列出的所有设备,但似乎没有任何效果。

当我使用 GCC 编译代码时,我收到警告:

keylogger.c: In function ‘main’:
keylogger.c:15:7: warning: implicit declaration of function ‘read’ [-Wimplicit-function-declaration]
   if (read(device,&ev, sizeof(ev)) >= 0){       
       ^

我不知道这是否与程序的功能有关。

感谢任何帮助!谢谢!

【问题讨论】:

  • read 的原型在 unistd.h .. 每当您收到这种类型的隐式函数声明警告时,请执行 man **function_name**.. man 有多个页面.. 并且 read 在页面中2 ...所以你做man 2 read
  • @ParnabSanyal 谢谢,但不幸的是它仍然无法正常工作。检查一下,Ubuntu中的默认键盘是/dev/input/event0,对吗?
  • 我知道...我还没有给你解决方案。我还没有。我已经告诉你修复隐式函数警告的方法。
  • 我明白了,我很抱歉。
  • 没关系。做一件事。在读取函数调用之后.. 执行printf("%s\n",strerror(errno));。看看它说了什么。做两个导入 errno.hstring.h

标签: c linux ubuntu ubuntu-16.04 keylogger


【解决方案1】:

我想通了,没有超级用户权限很简单。我使用sudo 执行了文件,现在一切正常。

【讨论】:

  • 您是否尝试以sudo 运行可执行文件。而不是更改文件权限。
  • 这很可悲 :-( 。无论如何,很高兴你已经弄清楚了。
  • 哎呀,没关系!我使用 sudo 编译它,但没有用 sudo 执行它,我的错。
  • 好吧,试试看会发生什么。
猜你喜欢
  • 2011-05-12
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 2017-12-07
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多