This project utilizes Python to make a keylogger, a common tool used by many hackers to track keystrokes of a user. There are many ways to go about this, especially in Python but mine came out in only 10 lines as you see above. Very simple!

I started up my Kali VM box and opened my terminal. Before getting started, I needed to install pynput which is a library that allows you to monitor input devices like a mouse and keyboard. Perfect for keylogging.

Next, I used vim in the command line to create and edit a file for future use. This was named ‘Keylogger.py’ and is where I entered my keylogger script.

I will now go over a line-by-line explanation for better understanding. To finish, I will show what I entered when I ran the script and show the final output to confirm if this was a success.

Lines 1-2

These first two lines are for importing required libraries. ‘Pynput’ reads keystrokes as the user types and ‘logging’ will log the keystrokes into a file. The ‘Key’ class represents keys on the keyboard and the ‘Listener’ class is used to listen and handle events related to inputs.

Line 4

Here, I created a basic configuration for the logging system where I specify where the keystrokes will be recorded. I chose ‘keylog.txt’ for my location. The level is setting the log level to ‘DEBUG’, meaning any severity level with that or above will be recorded. The following specifies the format in which the keystrokes will be recorded. It will be seen as:

Year-Month-Day Hour-Minute-Second(ms) – Key

Line 6-7

These lines start with a function called ‘on_press’ in which it 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

This line creates a ‘Listener’ object with a ‘on_press’ function as a callback. The with statement ensures that resources are properly released when the program exits. Line 10 starts the listener and will only stop when it’s told to. This runs in the background, capturing the key pressed and invoking the ‘on_press’ function to log them.

Running Keylogger.py script

I started running the ‘Keylogger.py’ file I created in vim using python3 in the VM. While running, I entered a few lines of words like “Hello World” and “My favorite food is Taco’s”. I then stopped running the script because that needed to be done manually as mentioned earlier.

Keylog.txt output

My last step was to open the keylog.txt file I created in the script where everything logs. As you can see, everything I entered was captured from letters to other keys. The format I entered in the logging configuration looks correct as well.

In conclusion, it was great to create a tool used in the cybersecurity world and gain confidence and experience with Python. Although it’s only ten lines, I had my challenges and looked into many sources for confirmation. Trial and error is the best learning experience!

Thanks for reading along!

Leave a Reply

Your email address will not be published. Required fields are marked *