How to monitor keylogger with Python under Linux
Ideas
In the previous article, we said how to use Python to get the name of the device that corresponds to an event in the/dev/input directory. Then we can know the name of the device, you can know by name which event corresponds to the keyboard (possibly more than one keyboard). Then the corresponding event under/dev/input/is the event that is responsible for processing the data entered by the keyboard.
The Evdev input event driver provides a default event handling method for the input subsystem. It receives most of the events from the underlying driver and processes them using the appropriate logic. The Evdev input event driver receives event information from the underlying and reflects it to the Sys file system, and the user program can achieve the ability to handle events by manipulating the Sys file system.
This is done using a library called Evdev, whose principle is to read the buffer array in the/DEV/EVENTX device with the C function Evdev_read (), which contains the Input_event type data, and the following kernel-escalated events will exist here.
Write the following procedure:
#!/usr/bin/env python#coding: Utf-8 fromEvdevImportInputDevice fromSelectImportSelect def detectinputkey():dev = InputDevice ('/dev/input/event4 ') while True: select ([Dev], [], []) forEventinchDev.read ():Print "code:%s value:%s"% (Event.code, Event.value)if__name__ = =' __main__ ': Detectinputkey ()
The role of select is to wait for Dev to change before running the subsequent code, Dev.read () returns the buffer array, which contains input_event type data
Use root permissions to execute the program, by observing. Its code is the input key value, its value is corresponding to its state, press a key, its value is 1, released when value is 0.
Then modify the code a little bit to know the input values and status.
Code
def detectinputkey():dev = InputDevice ('/dev/input/event4 ') while True: select ([Dev], [], []) forEventinchDev.read ():if(Event.value = =1 orEvent.value = =0) andEvent.code! =0:Print "Key:%s Status:%s"% (Event.code,"pressed" ifEvent.valueElse "Release")
This enables the program to read the keyboard input key and its state.
Effect
To execute the program, press ZXC in turn:
If you want to monitor multiple keyboard inputs at the same time, open multiple threads to monitor it.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Use Python to capture keyboard input under Linux