Keyloggers with pyhook

To create a simple keylogger script to record keystroke activities on a computer and to store it in a text file, we could use the pyhook module. This will provide callback for global mouse and keyboard events in Windows systems.

Import the required modules. Here, we are importing the pyhook and pythoncom modules from ActivePython Package. The pythoncom module is used in this script to pump all messages for the current thread:

import pyHook, pythoncom, sys, logging 

Define the file where to save the logging data. (Windows filenames use a backslash as a separator. But, in Python, backslash is an escape character, so we have to put a double slash "\\" in path. Otherwise, we can use rawstring to define the file name.):

file_log='C:\\log.txt' ...

Get Effective Python Penetration Testing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.