O'Reilly logo

Effective Python Penetration Testing by Rejah Rehim

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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' ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required