Keyloggers are a type of monitoring software used to record keystrokes made by the user with their keyboard.
They are often used for monitoring the network usage as well as troubleshoot the technical problems. On the other hand, a lot of malicious software uses keyloggers to attempt to get usernames and passwords for different websites.
To continue following this tutorial we will need the following Python library: pynput.
If you don’t have it installed, please open “Command Prompt” (on Windows) and install it using the following code:
pip install pynput
Press and release keyboard keys using Python
The first thing we will discuss is how to control the keyboard using Python and specifically how to press keys on the keyboard.
There are two types of keys that we should consider:
- Regular keys — which includes letters, numbers, and signs.
- Special keys — which include space, shift, ctrl, and so on.
To begin controlling the keyboard, we need to create an instance of Controller() class which will have the .press() and .release() methods. This class send the keyboard events to our system.
You can think of these methods exactly as they are described, and it is how we type, we press and release each key on the keyboard.
Here is a simple example:
Note that this code will type “a” wherever your mouse cursor is located. It is also designed to press and release one key at a time.
Of course you can press and release multiple keys:
Now you will see that the output is “ab”.
Now, how do we handle special keys? What if I want to press “a b” (a, space, b)?
A complete list of all special keys is available here. Special keys are called using the Key class from the pynput module.
Here is an example:
And the output is “a b”.
This option works for special keys that we press and release, such as space, enter, and so on. But how about the keys that we keep pressed while typing? Such as shift? And our goal is to press “Ab” (capital a, b).
There needs to be some convenient way of using it. And there is! There is a very useful .pressed() method of the Controller() class that we can use:
And the output is “Ab”.
You can practice with different combinations of keys, and depending on what you need the code can differ a little, but this is a general overview of how the press and release logic works for controlling the keyboard.
Create a sample log file
We would like our keylogger to record the keys that we press and store them in a simple text file.
Let’s first create this sample file and then integrate it into the key logging process:
Running the above code will create a log.txt file which will have This is our log file written in it. In our case, we would like Python to record the keys that we press and add them to this file.
Create a simple keylogger using Python
We already know how to create the log.txt file with some sample text. Now, what we want to do is have Python write the keys we press into this file (rather than having the sample text there).
So let’s first think conceptually what we want to happen? Well what we need is some list that will keep appending keys that we pressed on keyboard right? And then, once a key is pressed it will write this list to a file.
Let’s see how we can do it:
Okay, so conceptually the above function works, but we still need to define our write_keys_to_file() function.
Keep in mind, that so far each key pressed comes in the “key” format, and in order for us to write it to a file, we should have it as a String. In addition, we will need to remove ‘quotation’ marks from each key, since each key is a string and we want to join them together.
Okay, now on each key press Python will create a log.txt file with the list of keys pressed since the time the script started running up to the last key pressed.
If we leave the code as is, it will keep running all the time. What we want to do is to define some stop key or a combination of keys that will stop the key logger. Let’s say, our stop key is “Esc”. How would we do it?
We already know that if we press “Esc”, it will be added to log.txt, so what we can do is define an operation that will take place once we release the “Esc” key after pressing it:
As the last step we will need to assemble everything together and get the keylogger running.
To run our keylogger we will need some sort of listening instance that will record keyboard events. To do this we will use the Listener() class from the pynput module.
This class has a few parameters, but we will need only two of them:
- on_press — action to call when a key is pressed.
- on_release — action to call when a key is released.
As you can follow from our steps, we can use our defined on_each_key_press() and on_each_key_release() functions as parameters for the Listener() class so we can join each key press to each other:
Now we have all the components and let’s look at the complete code how to create a simple keylogger using Python.
Full Keylogger Code
In this article we covered how you can create a simple keylogger using Python and pynput library.
Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Python Programming articles.
Originally published at https://pyshark.com on April 3, 2021.