Keylogger are very basic form of malware but still effective and a threat actor. If you are creative enough then you can make it more sophisticated and useful. Here I am sharing a basic Keylogger-making process for offensive security practice. (Learning purpose only).
In the wild, there are a lot of Python libraries. PyWinHook is one of the most effective ones. PyWinHooks enables us to easily capture all keyboard events. It actually takes advantage of the native Windows function SetWindwosHookEx. PyWinHook will take care of all Windows keyboard capturing functions which are low-level programming for attackers like us. I am not going to explain each line here but definitely I will explain each function's job in this code.
Lets open vs code and create your "keylogger.py"
First import all necessary libraries.
Necessary Python Libraries For KeyLogger
ctypes is a Python library used for interacting with dynamic libraries (DLLs) in a platform-independent way.
io.StringIO provides an in-memory stream for string-based I/O operations.
os provides operating system-related functionalities.
pythoncom is a module for interacting with COM (Component Object Model) objects in Python.
pyWinhook is a library for setting up and managing low-level Windows keyboard and mouse hooks.
sys provides access to some variables used or maintained by the Python interpreter.
time provides various time-related functions.
win32clipboard is a module for accessing the Windows clipboard
Then we will declare a variable. Which will contain an integer which is the time in seconds the keylogger will run.
Variable TIMEOUT
This keylogger will run for 100 seconds.
Then we will create a class for the keylogger.
Class KeyLogger
KeyLogger is a class that represents the keylogger.
__init__ is the constructor method that initializes the current_window attribute to None. This attribute will store the name of the currently focused window.
The function to get the current process
get_current_process retrieves information about the currently focused window and its associated process.
It uses various Windows API functions from windll.user32 and windll.kernel32 to achieve this.
It gets the handle of the foreground window using GetForegroundWindow().
It retrieves the process ID and executable name using GetWindowThreadProcessId() and GetModuleBaseNameA() respectively.
The window title is retrieved using GetWindowTextA().
The current_window attribute is updated with the decoded window title.
The gathered information is printed on the console.
The OpenProcess() and CloseHandle() functions are used to work with the process handle.
Thanks for detailed note it helps a lots
Does this get detected ?