Implementing a KeyLogger in Python

Keylogger Python

Hello everyone! In today’s post, we’ll be doing something exciting – building a KeyLogger in Python!

Have you ever wanted to monitor your keyboard and look at the your typing history and analyze how you type? Well, the starting step is to build a keyboard monitoring tool – or a KeyLogger!

While you may very well realize that this can be used in malicious ways, we assume that you’re the one in control of your own machine!

Let’s get started!


Installing Necessary Modules

The first step is to make sure that you have the right tools with you! Along with Python 3, you must also install the pynput module, to read input from your keyboard. Let’s use the pip install command.

pip install pynput

Although we could control the keyboard too, we’re going to simply monitor and log what is typed on it!

This module simply uses a backend engine, depending on your Operating System to monitor your keyboard. For example, if you’re using Linux, you might have an xorg server which you’d use as the backend.

This module interacts with the backend engine, to fetch input from the keyboard.

The pipeline is shown in the below diagram:

Pipeline Keylogger
Pipeline Keylogger

As a result, this module will work across different Operating Systems, since it does all the work of taking care of the backend calls!

We’ll design the following KeyLogger in Python:

  • We create a main loop which simply waits for a key to be pressed.
  • As soon as the listener detects a key-press, we’ll print it on the console.

Let’s start writing the code now.

Implement the Keylogger in Python

We’ll write a keylogger in Python, which uses the pynput.keyboard class.

Let’s make the necessary imports first

import pynput.keyboard as Keyboard

Now, we’ll to listen to a keyboard, we’ll monitor two kinds of events:

  • Key Presses – Whenever a key is pressed
  • Key Releases – Whenever a key is released

Now, pynput already makes our life very easy. We simply need to define two functions that handle the logic when a key is pressed and released.

We simply need to define these functions, and call pass them as arguments to our keyboard Listener, using pynput.

The format for creating the listener is as follows:

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

It’s just two lines of code! Here, there are two callback functions called on_press() and on_release() that will get called accordingly.

The second line simply waits for the listener thread to finish executing, using the Threading.join() method.

Let’s now define these two functions too.

def on_press(key):
	# Callback function whenever a key is pressed
	try:
		print(f'Key {key.char} pressed!')
	except AttributeError:
		print(f'Special Key {key} pressed!')

def on_release(key):
	print(f'Key {key} released')
	if key == Keyboard.Key.esc:
		# Stop the listener
		return False

Here, we first print whatever key is pressed/released using key.char.

If a special key is pressed, we must print key instead, as key.char is not a valid ASCII value.

Similarly, we do the same for on_release(key), until the <Esc> key is pressed.

We simply return False, and this will automatically stop the listener and finish our program!

Here is the complete program until now:

import pynput.keyboard as Keyboard

def on_press(key):
	# Callback function whenever a key is pressed
	try:
		print(f'Key {key.char} pressed!')
	except AttributeError:
		print(f'Special Key {key} pressed!')

def on_release(key):
	print(f'Key {key} released')
	if key == Keyboard.Key.esc:
		# Stop the listener
		return False

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

Sample Output

Key q pressed!
Key 'q' released
Key w pressed!
Key 'w' released
Special Key Key.shift pressed!
Key A pressed!
Key 'A' released
Key Key.shift released
Key a pressed!
Key 'a' released
Special Key Key.shift pressed!
Key A pressed!
Key 'A' released
Key Key.shift released
Special Key Key.shift pressed!
Key @ pressed!
Key '@' released
Key Key.shift released
Special Key Key.shift pressed!
Key $ pressed!
Key '$' released
Key Key.shift released
Special Key Key.shift pressed!
Key ) pressed!
Key ')' released
Key Key.shift released
Special Key Key.shift pressed!
Key > pressed!
Key '>' released
Key Key.shift released
Key . pressed!
Key '.' released
Special Key Key.esc pressed!
Key Key.esc released

As you can see, this is able to successfully capture and print the keyboard output, even with special keys such as <Shift>!


Conclusion

Hopefully, you were able to get your keylogger working easily now! You could build upon this and implement more functionality to your keylogger application too. Until next time!


References