β οΈ Disclaimer:
DLL injection is a technique used to run code within the address space of another process by forcing it to load a dynamic-link library (DLL) - a shared module used natively in Windows systems to control essential resources and functionalities. (Microsoft Documentation).
This method allows the injected code to manipulate the target process's behavior, which can be used for both legitimate and malicious purposes.
The injected DLL can perform various actions, from altering the process's behavior to stealing sensitive information. It's a technique used in debugging, game modding, malware analysis, but also in data theft, bypassing protections, or functionality hijacking.
- System-wide Key Capture: Low-level keyboard hook captures all keystrokes across the entire system
- DLL Injection: Injects keylogger into target processes for stealth operation
- Character Conversion: Converts virtual key codes to readable characters
- Special Key Support: Handles Shift, Caps Lock, punctuation, and function keys
- Real-time Logging: Immediate keystroke logging to file
- Process Persistence: Runs as long as the host process is active
- x64 Architecture: Compiled for 64-bit Windows systems
KeyHook/
βββ keyboardhook.cpp # Main keylogger DLL with keyboard hook
βββ injector.cpp # DLL injector utility
βββ build.bat # Automated build script
βββ built/ # compiled files (generated)
β βββ keylogger.dll # Main keylogger DLL (generated)
β βββ injector.exe # DLL injection tool (generated)
βββ obj/ # object files (generated)
β βββ keyboardhook.obj # Compiled from keyboardhook.cpp (generated)
β βββ injector.obj # Compiled from injector.cpp (generated)
βββ log.txt # Keystroke log file (generated)
βββ README.md # This documentation
- Windows 10/11 (64-bit)
- Visual Studio 2019/2022 or Visual Studio Build Tools
- Administrator privileges (recommended for injection)
- Visual Studio Community 2022 (Free)
- Visual Studio Build Tools (Minimal)
- Clone/Download the project to your local machine
- Open any Command Prompt or PowerShell (regular window, not elevated)
- Navigate to the project directory:
cd C:\path\to\KeyHook
- Run the build script:
.\build.bat
The build script will:
- Auto-detect Visual Studio installation
- Set up the x64 compilation environment
- Compile all components
- Place executables in the
built/directory
If you prefer manual compilation:
- Open "x64 Native Tools Command Prompt for VS"
- Navigate to project directory
- Compile individual components:
cl /LD /EHsc /Fekeylogger.dll keyboardhook.cpp user32.lib kernel32.lib cl /EHsc /Feinjector.exe injector.cpp user32.lib kernel32.lib
Get the Process ID (PID) of your target process. For system-wide monitoring, use explorer.exe:
Get-Process explorer | Select-Object Id,ProcessNameOutput example:
Id ProcessName
-- -----------
12345 explorer
Inject the keylogger DLL into the target process:
.\built\injector.exe 12345 "C:\full\path\to\KeyHook\built\keylogger.dll"Expected output:
DLL injected successfully.
The keylogger will create log.txt and capture all keystrokes:
# View recent keystrokes
Get-Content log.txt -Tail 20
# Monitor in real-time
Get-Content log.txt -Wait -Tail 5To stop the keylogger, terminate the host process:
# For explorer.exe (will restart automatically)
taskkill /f /im explorer.exe
Start-Process explorer.exe
# For other processes
taskkill /f /im target_process.exeThe main component implementing:
- Low-Level Keyboard Hook (
WH_KEYBOARD_LL) - Virtual Key to Character Conversion
- File Logging with Absolute Paths
- Shift/Caps Lock State Detection
Key Features:
- Captures letters (a-z, A-Z) with proper case
- Numbers (0-9) and symbols with Shift
- Special keys ([ENTER], [BACKSPACE], [F1-F12], etc.)
- Punctuation and symbols
Utility for injecting DLLs into target processes:
- Process Memory Allocation
- Remote Thread Creation
- LoadLibrary Injection Technique
- Error Handling and Cleanup
- Target: x64 Windows processes
- Hook Type: Low-level keyboard hook (WH_KEYBOARD_LL)
- Injection Method: Classic DLL injection via CreateRemoteThread
- Persistence: Runs within host process context
// Example: Virtual Key Code β Character
VK_A (0x41) β 'a' or 'A' (depending on Shift/Caps)
VK_1 (0x31) β '1' or '!' (depending on Shift)
VK_SPACE (0x20) β ' '
VK_RETURN (0x0D) β '\n'- Location: Project directory (
log.txt) - Format: Plain text, real-time appending
- Path: Absolute path to handle working directory changes
-
Find the Target Process
Use Windows APIs likeOpenProcess()to get a handle to the process you want to inject into. -
Allocate Memory
Allocate memory inside the target process usingVirtualAllocEx(). -
Write DLL Path
Write the path to the DLL into that memory usingWriteProcessMemory(). -
Create Remote Thread
UseCreateRemoteThread()to runLoadLibraryA()inside the target process, which loads the DLL.
The injected DLL now runs as if it's part of the target application, sharing its memory and privileges.
[Injector Process - injector.exe]
β
βββββββββββ΄βββββββββββββ
β Open Target Process β β OpenProcess
βββββββββββ¬βββββββββββββ
β
βββββββββββΌβββββββββββββ
β Allocate Memory β β VirtualAllocEx
βββββββββββ¬βββββββββββββ
β
βββββββββββΌβββββββββββββ
β Write DLL Path β β WriteProcessMemory
βββββββββββ¬βββββββββββββ
β
βββββββββββΌβββββββββββββ
β Get LoadLibraryA β β GetProcAddress
βββββββββββ¬βββββββββββββ
β
βββββββββββΌβββββββββββββ
β Start Remote Thread β β CreateRemoteThread
βββββββββββ¬βββββββββββββ
β
βββββββββββΌβββββββββββββ
β DLL Loaded into β
β Target Process β
βββββββββββ¬βββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββ
β DllMain(DLL_PROCESS_ATTACH): β β Automatically called
β β
β - DisableThreadLibraryCalls() β
β - CreateThread(...) β β Background thread for hook
βββββββββββ¬ββββββββββββββββββββββ
β
βββββββββββΌββββββββββββββββββββββββββ
β HookThread(): β
β - SetWindowsHookEx(WH_KEYBOARD_LL)β β Installs low-level keyboard hook
β - Loop: GetMessage/Dispatch β β Keeps hook alive (event loop)
βββββββββββββββββββββββββββββββββββββ
Once injected into a process's memory space, your DLL has full access to its memory, windows, UI, keyboard events, and more. Here's what it could potentially do:
- Hook into the process's text buffer to log user input.
- Automatically modify text typed into applications in real time.
- Intercept system calls like
WriteFile,ReadFile,SendMessage, etc. - Modify or redirect the process's behavior at runtime.
- Scan process memory for sensitive content (e.g., typed text, clipboard).
- Exfiltrate in-memory data or inject custom content.
- Create or hijack threads within the target process.
- Use the process as a stealth container to run hidden operations.
- Launch additional processes or payloads from within the target.
- Communicate with external systems using the process's identity to evade detection.
- Runs within legitimate system processes
- No standalone process footprint
- Minimal API calls
- File logging can be customized/encrypted
- Reflective DLL Loading: Load from memory without touching disk
- API Unhooking: Bypass EDR hooks before injection
- Process Hollowing: Replace legitimate process memory
- Signed DLLs: Use legitimate signed libraries as carriers
| Technique | Detection Method |
|---|---|
| DLL Injection | Monitor CreateRemoteThread, VirtualAllocEx calls |
| Keyboard Hooks | Detect SetWindowsHookEx in unusual processes |
| Suspicious Files | Monitor writes to log.txt, unusual DLL loads |
| Process Behavior | Notepad/Explorer with unexpected network/file activity |
| Process Module Enumeration | scan process's memory to detect modules that are in memory but without a corresponding file path on disk |
| Hook Chain Inspection | look for unexpected modifications on critical Windows functions, like on kernel32.dll (API Unhooking) |
| File System Monitoring | Monitor system-wide file activity for suspicious patterns |
| Behavioral Analysis | Use baselines and machine learning to detect "normal" activity anomalies (Process Hollowing or privilege escalation) |
Problem: 'cl' is not recognized
Solution: Run build.bat from any command prompt - it auto-detects Visual Studio
Problem: cannot open file 'built\keylogger.dll'
Solution: DLL is loaded in a process. Restart target process first.
Problem: No log file created Solution: Check absolute path in code matches your system
Problem: DLL injection failed Solution:
- Run as Administrator
- Check target process architecture (x64)
- Verify process PID is correct
Problem: Only virtual key codes logged Solution: Rebuild with updated character conversion code
Problem: Wrong characters logged Solution: Check keyboard layout and Shift state detection
This project demonstrates:
- Windows API Programming: Advanced API usage
- Process Injection: Modern injection techniques
- Hook Programming: System-wide hooks
- Character Encoding: Virtual key conversion
- System Programming: Low-level Windows internals
- Microsoft Windows API Documentation
- SetWindowsHookEx Function
- Virtual Key Codes
- DLL Injection Techniques
This project is for educational purposes only. Use responsibly and in accordance with local laws and regulations.
Created for educational and security research purposes. Always obtain proper authorization before use.