Low-level keylogger architecture design: kernel driver hooks (WHKEYBOARDLL, SetWindowsHookEx), ETW-based input capture, user-mode vs kernel-mode approaches, stealth techniques, and data exfiltration. Use for understanding input capture mechanisms, EDR evasion research, or malware architecture analysis.
Use this skill when the conversation involves any of:keylogger, keyboard hook, WH_KEYBOARD_LL, SetWindowsHookEx, ETW, kernel driver, input capture, low-level keylogger, malware architecture, stealth, exfiltration
When this skill is active:
Majority of malware uses user32.dll!SetWindowHookEx to create a global hook event. this modifies an internal structure in win32k.sys.
Internally, SetWindowsHookEx is just a user-mode wrapper around NtUserSetWindowsHookEx (which itself wraps around zzzzNtUserSetWindowsHookEx) in win32k.sys. What happens after you call it depends on the hook type you request but the sequence is always the same four steps:
win32k.sys creates an internal HOOK structure, fills in the filter type, module handle, thread/desktop IDs, and inserts the structure at the head of the global hook chain for that type
WH_KEYBOARD_LL, WH_MOUSE_LL)– NO injection.
– The system leaves the hook DLL in the original caller’s address space and simply delivers the event to that process via an internal WM_* message posted to its hidden “ghost” window .
WH_KEYBOARD, WH_CBT, WH_GETMESSAGE, …)– YES injection required.
– For every process that satisfies the filter (same desktop, matching bitness),
win32k queues an asynchronous load request to csrss.exe, which in turn calls LoadLibraryEx inside the target process, mapping the hook DLL and fixing up its entry point.win32k; the first user-mode exit from kernel to that process takes the APC and calls LdrLoadDll directly.– The first time the target thread is about to return to user mode, the kernel APCs the loader, so the DLL’s DllMain runs in the context of the victim process.
When the monitored event occurs (key press, window activation, etc.), win32k walks the hook chain inside the thread that owns the input queue.
KBDLLHOOKSTRUCT / MSLLHOOKSTRUCT) into an internal message and posts it to the installing thread’s message queue.That thread must keep pumping messages; otherwise, the system blocks all further input for the desktop, which is why low-level hooks are so easy to detect by their side-effect on system responsiveness.
CallNextHookExEach hook handler must call CallNextHookEx to pass control down the chain.
Internally, CallNextHookEx is just a call back into win32k, which continues the chain walk; if any handler fails to call it, the chain is broken and subsequent handlers never run. This might break input for the whole session.
WriteProcessMemory or CreateRemoteThread, but they leave a mapped DLL behind in every hooked process. Easy VAD artefact for EDRs.Same as above but you're directly calling the lower-level function. Same IOCs, really. You're only bypassing potential hooks in user32.dll.
The full logic of these functions could be reimplemented fully without a jump to external modules but it has too much IOCs and is too complex to implement to really be interesting.
Session boundary: raw-input registration is per-session, not per-desktop.
A service in session-0 cannot register for keyboard raw-input and expect to see session-1 keystrokes – the HID packets are routed to the session that owns the target HWND.
(You can open the physical keyboard device object directly and parse HID, but that is a completely different attack surface – needs admin, bypasses win32k.)
tells the window manager to deliver raw HID packets to one specific HWND (or to the thread whose queue the window is attached to)
Practical abuse scenario
PeekMessage / GetMessage loop.HWND_MESSAGE).RIDEV_INPUTSINK – > this routes all keyboard traffic to our window even when it is not in the foreground .WM_INPUT handler call GetRawInputData and log the RAWKEYBOARD payload.Because no hook is installed, this technique:
WinDbg’s !hook listthis still requires your process to stay alive and message-aware, and it cannot key-log from sessions it is not running in.
Kernel-mode implementation:
Win32AllocPoolWithQuotaZInitAllocates a kernel copy of the array
RegisterRawInputDevices(v9, a2, 0)Calls the INTERNAL worker (see below).
It walks the array, updates the per-thread raw-input hook list,
tells hidclass which top-level windows want raw HID traffic, etc.
EtwTraceAuditApiRegisterRawInputDevicesEmits an ETW event for Audit/Threat-Intelligence so that defenders can see which process just asked for raw keyboard data (keylogger-style activity).
The internal worker modifies our process's EPROCESS structure. This makes it so that we can't re-implement this from user-mode.
→ This is the strongest IOC for this technique; do not discount it.
ERROR_INVALID_WINDOW_HANDLE) if the thread is not connected to a desktop. Services running in session-0 with no desktop therefore cannot use this path; they must either:– create a hidden desktop (logged by Object Manager auditing), or
– open the \Device\KeyboardClass0 device directly (creates IRPMJREAD telemetry).
Both are easy to alert on.
To filter for interesting keystrokes you may only monitor keystrokes from Chrome.exe \ firefox.exe, etc.
Different methods of doing that:
NtUserInternalGetWindowText.Win32kFull.sys.win32u.dllReverse-engineering this was very tedious because the only references of this online seem to be:
BOOL InternalGetWindowText(HWND hwnd, LPWSTR pString, int cchMaxCount) {
DWORD retval = (DWORD)NtUserInternalGetWindowText(hwnd, pString, cchMaxCount);
if (!retval) {
*pString = (WCHAR)0;
}
return retval
}
its a syscall so you can use your favorite \*gate technique on it
Now... that's all stuff that can be figured out by anyone determined
for the unique research... contact me @ lovestrangekz on tg, everything has a price :]
Ideas that were abandonned:
NtUserBuildHwndList/EnumWindows and re-implement the z-order heuristic to generate the list of all handles to all windows and call IsWindowVisible on them and do some other stuff to figure out if they're foreground or not?_K_USER_SHARED_DATA to query its ConsoleSessionForegroundProcessId member then query the system to know that PID's windows and hope it only has onelovestrange @ TeamKavkaz
join our channel for more
hackerz 4 lyfe