-
Notifications
You must be signed in to change notification settings - Fork 2
/
arxan.cpp
1342 lines (1092 loc) · 43.5 KB
/
arxan.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
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
#include <asmjit/core/operand.h>
#include <asmjit/x86/x86operand.h>
#include <cstring>
#define PHNT_VERSION PHNT_WIN10_22H2
#include <phnt_windows.h>
#include <phnt.h>
#include <ntexapi.h>
#include <ntpsapi.h>
#include <minidumpapiset.h>
#include <TlHelp32.h>
#include <mmeapi.h>
#include <filesystem>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <intrin.h>
#include <string>
#include <string_view>
#include <iostream>
#include <filesystem>
#include <asmjit/core/jitruntime.h>
#include <asmjit/x86/x86assembler.h>
#include "libs/patterns/Hooking.Patterns.h"
#include "libs/minhook/include/MinHook.h"
#include "restorentdll.h"
#include "utils.h"
#include "systemhooks.h"
#include "arxan.h"
#include "paths.h"
#include "syscalls.h"
std::vector<intactChecksumHook> intactchecksumHooks;
std::vector<intactBigChecksumHook> intactBigchecksumHooks;
std::vector<splitChecksumHook> splitchecksumHooks;
LPVOID ntdllAsmStubLocation;
inlineAsmStub* inlineStubs = nullptr;
size_t stubCounter = 0;
int FixChecksum(uint64_t rbpOffset, uint64_t ptrOffset, uint64_t* ptrStack, uint32_t jmpInstructionDistance, uint32_t calculatedChecksumFromArg)
{
// get size of image from codcw
uint64_t baseAddressStart = (uint64_t)GetModuleHandle(nullptr);
IMAGE_DOS_HEADER* pDOSHeader = (IMAGE_DOS_HEADER*)GetModuleHandle(nullptr);
IMAGE_NT_HEADERS* pNTHeaders =(IMAGE_NT_HEADERS*)((BYTE*)pDOSHeader + pDOSHeader->e_lfanew);
auto sizeOfImage = pNTHeaders->OptionalHeader.SizeOfImage;
uint64_t baseAddressEnd = baseAddressStart + sizeOfImage;
// check if our checksum hooks got overwritten
// we could probably ifdef this now but it's a good indicator to know if our checksum hooks still exist
{
for (int i=0; i < intactchecksumHooks.size(); i++)
{
DWORD old_protect{};
if (memcmp(intactchecksumHooks[i].functionAddress, intactchecksumHooks[i].buffer, sizeof(uint8_t) * 7))
{
uint64_t idaAddress = (uint64_t)intactchecksumHooks[i].functionAddress - baseAddressStart + StartOfBinary;
printf("%llx %llx got changed\n", idaAddress, (uint64_t)intactchecksumHooks[i].functionAddress);
fprintf(logFile, "%llx got changed\n", idaAddress);
fflush(logFile);
}
VirtualProtect(intactchecksumHooks[i].functionAddress, sizeof(uint8_t) * 7, PAGE_EXECUTE_READWRITE, &old_protect);
memcpy(intactchecksumHooks[i].functionAddress, intactchecksumHooks[i].buffer, sizeof(uint8_t) * 7);
VirtualProtect(intactchecksumHooks[i].functionAddress, sizeof(uint8_t) * 7, old_protect, &old_protect);
FlushInstructionCache(GetCurrentProcess(), intactchecksumHooks[i].functionAddress, sizeof(uint8_t) * 7);
}
for (int i=0; i < intactBigchecksumHooks.size(); i++)
{
DWORD old_protect{};
if (memcmp(intactBigchecksumHooks[i].functionAddress, intactBigchecksumHooks[i].buffer, sizeof(uint8_t) * 7))
{
uint64_t idaAddress = (uint64_t)intactBigchecksumHooks[i].functionAddress - baseAddressStart + StartOfBinary;
printf("%llx %llx got changed\n", idaAddress, (uint64_t)intactBigchecksumHooks[i].functionAddress);
fprintf(logFile, "%llx got changed\n", idaAddress);
fflush(logFile);
}
VirtualProtect(intactBigchecksumHooks[i].functionAddress, sizeof(uint8_t) * 10, PAGE_EXECUTE_READWRITE, &old_protect);
memcpy(intactBigchecksumHooks[i].functionAddress, intactBigchecksumHooks[i].buffer, sizeof(uint8_t) * 10);
VirtualProtect(intactBigchecksumHooks[i].functionAddress, sizeof(uint8_t) * 10, old_protect, &old_protect);
FlushInstructionCache(GetCurrentProcess(), intactBigchecksumHooks[i].functionAddress, sizeof(uint8_t) * 10);
}
for (int i=0; i < splitchecksumHooks.size(); i++)
{
DWORD old_protect{};
if (memcmp(splitchecksumHooks[i].functionAddress, splitchecksumHooks[i].buffer, sizeof(uint8_t) * 7))
{
uint64_t idaAddress = (uint64_t)splitchecksumHooks[i].functionAddress - baseAddressStart + StartOfBinary;
printf("%llx %llx got changed\n", idaAddress, (uint64_t)splitchecksumHooks[i].functionAddress);
fprintf(logFile, "%llx got changed\n", idaAddress);
fflush(logFile);
}
VirtualProtect(splitchecksumHooks[i].functionAddress, sizeof(uint8_t) * 8, PAGE_EXECUTE_READWRITE, &old_protect);
memcpy(splitchecksumHooks[i].functionAddress, splitchecksumHooks[i].buffer, sizeof(uint8_t) * 8);
VirtualProtect(splitchecksumHooks[i].functionAddress, sizeof(uint8_t) * 8, old_protect, &old_protect);
FlushInstructionCache(GetCurrentProcess(), splitchecksumHooks[i].functionAddress, sizeof(uint8_t) * 8);
}
}
static int fixChecksumCalls = 0;
fixChecksumCalls++;
uint32_t calculatedChecksum = calculatedChecksumFromArg;
uint32_t reversedChecksum = reverse_bytes(calculatedChecksumFromArg);
uint32_t* calculatedChecksumPtr = (uint32_t*)((char*)ptrStack+0x120); // 0x120 is a good starting point to decrement downwards to find the calculated checksum on the stack
uint32_t* calculatedReversedChecksumPtr = (uint32_t*)((char*)ptrStack+0x120); // 0x120 is a good starting point to decrement downwards to find the calculated checksum on the stack
bool doubleTextChecksum = false;
uint64_t* previousResultPtr = nullptr;
if (ptrOffset == 0 && rbpOffset < 0x90)
{
uint64_t* textPtr = (uint64_t*)((char*)ptrStack+rbpOffset + (rbpOffset % 0x8)); // make sure rbpOffset is aligned by 8 bytes
int pointerCounter = 0;
for (int i=0; i < 20; i++)
{
uint64_t derefPtr = *(uint64_t*)textPtr;
if (derefPtr >= baseAddressStart && derefPtr <= baseAddressEnd)
{
uint64_t derefResult = **(uint64_t**)textPtr;
pointerCounter++;
// store the ptr above 0xffffffffffffffff and then use it in our originalchecksum check
if (derefResult == 0xffffffffffffffff)
{
if (pointerCounter > 2)
{
doubleTextChecksum = true;
// because textptr will be pointing at 0xffffffffffffffff, increment it once
// so we are pointing to the correct checksum location
// TODO: remove this, doesnt do anything, confirm with checksum 0x79d397c8
// since we use previousResultPtr which doesnt rely on this
textPtr++;
}
break;
}
previousResultPtr = textPtr;
}
textPtr--;
}
}
else
{ // for debugging stack traces on bigger rbp offset checksums
uint64_t* textPtr = (uint64_t*)((char*)ptrStack+rbpOffset + (rbpOffset % 0x8)); // make sure rbpOffset is aligned by 8 bytes
for (int i=0; i < 30; i++)
{
uint64_t derefPtr = *(uint64_t*)textPtr;
if (derefPtr >= baseAddressStart && derefPtr <= baseAddressEnd)
uint64_t derefResult = **(uint64_t**)textPtr;
textPtr--;
}
}
// find calculatedChecksumPtr, we will overwrite this later with the original checksum
for (int i=0; i < 80; i++)
{
uint32_t derefPtr = *(uint32_t*)calculatedChecksumPtr;
if (derefPtr == calculatedChecksum)
break;
calculatedChecksumPtr--;
}
// find calculatedReversedChecksumPtr, we will overwrite this later with the original checksum
for (int i=0; i < 80; i++)
{
uint32_t derefPtr = *(uint32_t*)calculatedReversedChecksumPtr;
if (derefPtr == reversedChecksum)
break;
calculatedReversedChecksumPtr--;
}
uint64_t* textPtr = (uint64_t*)((char*)ptrStack+rbpOffset + (rbpOffset % 0x8)); // add remainder to align ptr
uint32_t originalChecksum = NULL;
uint32_t* originalChecksumPtr = nullptr;
// searching for a .text pointer that points to the original checksum, upwards from the rbp
for (int i=0; i < 10; i++)
{
uint64_t derefPtr = *(uint64_t*)textPtr;
if (derefPtr >= baseAddressStart && derefPtr <= baseAddressEnd)
{
if (ptrOffset == 0 && rbpOffset < 0x90)
{
if (doubleTextChecksum)
originalChecksum = **(uint32_t**)previousResultPtr;
else
originalChecksum = *(uint32_t*)derefPtr;
}
else
{
originalChecksum = *(uint32_t*)((char*)derefPtr+ptrOffset*4); // if ptrOffset is used the original checksum is in a different spot
originalChecksumPtr = (uint32_t*)((char*)derefPtr+ptrOffset*4);
}
break;
}
textPtr--;
}
*calculatedChecksumPtr = (uint32_t)originalChecksum;
*calculatedReversedChecksumPtr = reverse_bytes((uint32_t)originalChecksum);
// for big intact we need to keep overwriting 4 more times
// seems to still run even if we comment this out wtf?
uint32_t* tmpOriginalChecksumPtr = originalChecksumPtr;
uint32_t* tmpCalculatedChecksumPtr = calculatedChecksumPtr;
uint32_t* tmpReversedChecksumPtr = calculatedReversedChecksumPtr;
if (originalChecksumPtr != nullptr)
{
for (int i=0; i <= ptrOffset; i++)
{
*tmpCalculatedChecksumPtr = *(uint32_t*)tmpOriginalChecksumPtr;
*tmpReversedChecksumPtr = reverse_bytes(*(uint32_t*)tmpOriginalChecksumPtr);
tmpOriginalChecksumPtr--;
tmpCalculatedChecksumPtr--;
tmpReversedChecksumPtr--;
}
}
return originalChecksum;
}
NTSTATUS ntdllSyscallSetInformation(HANDLE handle, THREADINFOCLASS ThreadInformationClass, PVOID ThreadInformation, ULONG ThreadInformationLength)
{
DWORD flags;
if (ThreadInformation == 0 && ThreadInformationLength == 0 && (GetHandleInformation(handle, &flags) != 0))
{
return 0;
}
else
return 0xc0000008;
}
NTSTATUS ntdllSyscallQueryInformation(HANDLE handle, THREADINFOCLASS ThreadInformationClass, PVOID ThreadInformation, ULONG ThreadInformationLength)
{
if (weAreDebugging)
return 0x1337;
DWORD flags;
if (ThreadInformationLength == sizeof(BOOLEAN) && (GetHandleInformation(handle, &flags) != 0))
{
char* info = (char*)ThreadInformation;
*info = 1;
return 0;
}
else
return 0x80000002;
}
NTSTATUS ntdllSyscallCreateThreadEx(PHANDLE ThreadHandle, NTSTATUS syscallResult)
{
NTSTATUS setThreadResult = SetThreadContextOrig(*(PHANDLE*)ThreadHandle, &context);
return syscallResult;
}
NTSTATUS NTAPI ntdllSyscallNtClose(HANDLE Handle)
{
OBJECT_HANDLE_FLAG_INFORMATION flags;
NTSTATUS Status;
Status = NtQueryObject(Handle, ObjectHandleFlagInformation, &flags, sizeof(OBJECT_HANDLE_FLAG_INFORMATION), nullptr);
if (NT_SUCCESS(Status))
{
if (flags.ProtectFromClose)
{
printf("not closable\n");
return STATUS_HANDLE_NOT_CLOSABLE;
}
// do syscall so we dont recursively call ntclose hook
return 0x1337;
}
printf("invalid handle: %llx\n", Handle);
return STATUS_INVALID_HANDLE;
}
void ntdllQueryInformationProcess(PROCESSINFOCLASS ProcessInformationClass, PVOID ProcessInformation)
{
switch(ProcessInformationClass)
{
// TODO: we aren't setting ntstatus to 0xC0000353 if debugobjecthandle is true
case ProcessDebugObjectHandle:
case ProcessDebugPort:
memset(ProcessInformation, 0x0, sizeof(uint8_t) * 8);
break;
case ProcessImageFileName:
case ProcessImageFileNameWin32:
if (ProcessInformation != nullptr)
remove_evil_keywords_from_string(*static_cast<UNICODE_STRING*>(ProcessInformation));
break;
case ProcessDebugFlags:
memset(ProcessInformation, 1, sizeof(uint64_t));
break;
default:
break;
}
}
void ntdllQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass, PVOID SystemInformation)
{
switch(SystemInformationClass)
{
// TODO: we aren't setting ntstatus to 0xC0000353 if debugobjecthandle is true
case SystemProcessInformation:
case SystemSessionProcessInformation:
case SystemExtendedProcessInformation:
case SystemFullProcessInformation:
uint8_t* addr;
addr = (uint8_t*)(SystemInformation);
SYSTEM_PROCESS_INFORMATION* previousInfo;
while (true)
{
SYSTEM_PROCESS_INFORMATION* info;
info = (SYSTEM_PROCESS_INFORMATION*)addr;
if (info->ImageName.Buffer != nullptr)
if (remove_evil_keywords_from_string(info->ImageName))
previousInfo->NextEntryOffset += info->NextEntryOffset;
previousInfo = (SYSTEM_PROCESS_INFORMATION*)addr;
if (!info->NextEntryOffset)
return;
addr = addr + info->NextEntryOffset;
}
break;
case SystemHandleInformation:
case SystemExtendedHandleInformation:
printf("\nchecked for handle information\n");
break;
case SystemObjectInformation:
printf("checked for object information\n");
break;
default:
break;
}
}
void NtdllAsmStub()
{
hook::pattern syscallLocations = hook::module_pattern(GetModuleHandle("ntdll.dll"), "4C 8B D1 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0F 05");
size_t syscallCount = syscallLocations.size();
printf("ntdll syscallCount %d\n", syscallCount);
// allocate asm stub rwx page
const size_t allocationSize = sizeof(uint8_t) * 128;
ntdllAsmStubLocation = allocate_somewhere_near(GetModuleHandle("ntdll.dll"), allocationSize);
memset(ntdllAsmStubLocation, 0x90, allocationSize);
printf("ntdll stub location %llx\n", ntdllAsmStubLocation);
// assembly stub
static asmjit::JitRuntime runtime;
asmjit::CodeHolder code;
code.init(runtime.environment());
using namespace asmjit::x86;
Assembler a(&code);
asmjit::Label L1 = a.newLabel();
asmjit::Label SetInformation = a.newLabel();
asmjit::Label QueryInformation = a.newLabel();
asmjit::Label CreateThreadEx = a.newLabel();
asmjit::Label RegularSyscall = a.newLabel();
asmjit::Label NtClose = a.newLabel();
asmjit::Label NtClose_L1 = a.newLabel();
asmjit::Label NtQueryObject = a.newLabel();
asmjit::Label NtQueryObject_L1 = a.newLabel();
asmjit::Label QueryInformationProcess = a.newLabel();
asmjit::Label QueryInformationProcess_L1 = a.newLabel();
asmjit::Label QuerySystemInformation = a.newLabel();
asmjit::Label QuerySystemInformation_L1 = a.newLabel();
asmjit::Label QuerySystemInformation_L2 = a.newLabel();
asmjit::Label DEBUG = a.newLabel();
a.test(byte_ptr(0x7FFE0308), 1);
a.jnz(L1);
a.cmp(rax, SetInformationSysCall);
a.je(SetInformation);
a.cmp(rax, QueryInformationSysCall);
a.je(QueryInformation);
a.cmp(rax, CreateThreadExSysCall);
a.je(CreateThreadEx);
a.cmp(rax, NtQueryObjectSysCall);
a.je(NtQueryObject);
a.cmp(rax, NtCloseSysCall);
a.je(NtClose);
a.cmp(rax, QueryInformationProcessSysCall);
a.je(QueryInformationProcess);
a.cmp(rax, QuerySystemInformationSysCall);
a.je(QuerySystemInformation);
a.bind(RegularSyscall);
a.syscall();
a.ret();
a.bind(L1);
a.int_(0x2E);
a.ret();
#if 1
a.bind(DEBUG);
a.int3();
a.jmp(DEBUG);
#endif
// rdi has the address location?
a.bind(QueryInformationProcess);
a.cmp(rdx, ProcessImageFileNameWin32);
a.je(QueryInformationProcess_L1);
a.cmp(rdx, ProcessImageFileName);
a.je(QueryInformationProcess_L1);
a.cmp(rdx, ProcessDebugPort);
a.je(QueryInformationProcess_L1);
a.cmp(rdx, ProcessDebugFlags);
a.je(QueryInformationProcess_L1);
a.cmp(rdx, ProcessDebugObjectHandle);
a.je(QueryInformationProcess_L1);
// else do regular syscall
a.jmp(RegularSyscall);
a.bind(QueryInformationProcess_L1);
a.movq(xmm14, rdx); // ProcessInformationClass
a.movq(xmm15, r8); // ProcessInformation
a.syscall();
#if 0
a.bind(DEBUG);
a.int3();
a.jmp(DEBUG);
#endif
pushad64();
a.movq(rcx, xmm14);
a.movq(rdx, xmm15);
a.sub(rsp, 0x20);
a.call(ntdllQueryInformationProcess);
a.add(rsp, 0x20);
popad64();
a.push(rax);
a.mov(rax, 0);
a.movq(xmm14, rax);
a.movq(xmm15, rax);
a.pop(rax);
a.ret();
a.bind(QuerySystemInformation);
a.cmp(rcx, SystemProcessInformation);
a.je(QuerySystemInformation_L1);
a.cmp(rcx, SystemSessionProcessInformation);
a.je(QuerySystemInformation_L1);
a.cmp(rcx, SystemExtendedProcessInformation);
a.je(QuerySystemInformation_L1);
a.cmp(rcx, SystemFullProcessInformation);
a.je(QuerySystemInformation_L1);
a.cmp(rcx, SystemHandleInformation);
a.je(QuerySystemInformation_L1);
a.cmp(rcx, SystemExtendedHandleInformation);
a.je(QuerySystemInformation_L1);
a.cmp(rcx, SystemObjectInformation);
a.je(QuerySystemInformation_L1);
// else do regular syscall
a.jmp(RegularSyscall);
a.bind(QuerySystemInformation_L1);
a.movq(xmm14, rcx); // ProcessInformationClass
a.movq(xmm15, rdx); // ProcessInformation
a.syscall();
// check if ntstatus is success
a.cmp(rax, 0x0);
a.je(QuerySystemInformation_L2);
a.ret();
a.bind(QuerySystemInformation_L2);
pushad64();
a.movq(rcx, xmm14);
a.movq(rdx, xmm15);
#if 0
a.bind(DEBUG);
a.int3();
a.jmp(DEBUG);
#endif
a.sub(rsp, 0x20);
a.call(ntdllQuerySystemInformation);
a.add(rsp, 0x20);
popad64();
a.push(rax);
a.mov(rax, 0);
a.movq(xmm14, rax);
a.movq(xmm15, rax);
a.pop(rax);
a.ret();
a.bind(SetInformation);
// do regular syscall if hidefromdebugger is not set
a.cmp(rdx, ThreadHideFromDebugger);
a.jne(RegularSyscall);
a.sub(rsp, 0x20);
a.call(ntdllSyscallSetInformation);
a.add(rsp, 0x20);
a.ret();
a.bind(QueryInformation);
// do regular syscall if hidefromdebugger is not set
a.cmp(rdx, ThreadHideFromDebugger);
a.jne(RegularSyscall);
a.sub(rsp, 0x20);
a.call(ntdllSyscallQueryInformation);
a.add(rsp, 0x20);
// if we call the function ourselves
a.cmp(rax, 0x1337);
a.je(RegularSyscall);
a.ret();
a.bind(CreateThreadEx);
// remove hide from debugger flag
a.movzx(eax, dword_ptr(rsp, 0x38));
a.mov(r15, rax);
a.mov(rax, THREAD_CREATE_FLAGS_HIDE_FROM_DEBUGGER);
a.not_(rax);
a.and_(r15, rax);
a.mov(rax, r15);
a.mov(dword_ptr(rsp, 0x38), eax);
// create thread with syscall
a.mov(rax, CreateThreadExSysCall);
a.mov(r15, rcx);
a.syscall();
a.mov(rdx, rax);
a.mov(rcx, r15);
// set hwbp's on newly created thread
a.sub(rsp, 0x20);
a.call(ntdllSyscallCreateThreadEx);
a.add(rsp, 0x20);
a.ret();
a.bind(NtQueryObject);
a.cmp(rdx, ObjectBasicInformation);
a.je(NtQueryObject_L1);
a.cmp(rdx, ObjectNameInformation);
a.je(NtQueryObject_L1);
a.cmp(rdx, ObjectTypeInformation);
a.je(NtQueryObject_L1);
a.cmp(rdx, ObjectTypesInformation);
a.je(NtQueryObject_L1);
a.syscall();
a.ret();
a.bind(NtQueryObject_L1);
a.mov(rax, 0xC0000005);
a.ret();
a.bind(NtClose);
// a.jmp(DEBUG);
/*
pushad64();
a.sub(rsp, 0x28);
a.call(ntdllSyscallNtClose);
a.add(rsp, 0x28);
popad64();
*/
a.cmp(rcx, 0xfffffffffffffffc);
a.je(NtClose_L1);
a.cmp(rcx, 0x12345);
a.je(NtClose_L1);
a.jmp(RegularSyscall);
a.ret();
a.bind(NtClose_L1);
a.mov(rax, 0);
a.ret();
void* asmjitResult = nullptr;
runtime.add(&asmjitResult, &code);
// copy over the content to the stub
uint8_t* tempBuffer = (uint8_t*)malloc(sizeof(uint8_t) * code.codeSize());
memcpy(tempBuffer, asmjitResult, code.codeSize());
memcpy(ntdllAsmStubLocation, tempBuffer, sizeof(uint8_t) * code.codeSize());
DWORD old_protect{};
VirtualProtect(ntdllAsmStubLocation, allocationSize, PAGE_EXECUTE, &old_protect);
//char* functionAddress = (char*)syscallLocations.get(0).get<void*>(0) + 18;
//const int callInstructionBytes = 0x100000;
//const int callInstructionLength = sizeof(uint8_t) * callInstructionBytes;
//DWORD old_protect{};
//BOOL result = VirtualProtect(functionAddress, callInstructionLength, PAGE_EXECUTE_READWRITE, &old_protect);
// inline hook every function using syscall
//for (int i=0; i < syscallCount; i++)
for (int i=0; i < syscallCount-5; i++)
{
// 18 since the signature puts us at the start of the function
char* functionAddress = (char*)syscallLocations.get(i).get<void*>(0) + 18;
uint64_t jmpDistance = (uint64_t)ntdllAsmStubLocation - (uint64_t)functionAddress - 5;
const int callInstructionBytes = 6;
const int callInstructionLength = sizeof(uint8_t) * callInstructionBytes;
// E8 cd CALL rel32 Call near, relative, displacement relative to next instruction
uint8_t* jmpInstructionBuffer = (uint8_t*)malloc(sizeof(uint8_t) * callInstructionBytes);
if (jmpInstructionBuffer != nullptr)
{
jmpInstructionBuffer[0] = 0xE9;
jmpInstructionBuffer[1] = (jmpDistance >> (0 * 8));
jmpInstructionBuffer[2] = (jmpDistance >> (1 * 8));
jmpInstructionBuffer[3] = (jmpDistance >> (2 * 8));
jmpInstructionBuffer[4] = (jmpDistance >> (3 * 8));
jmpInstructionBuffer[5] = 0x90;
}
DWORD old_protect{};
int c = VirtualProtect(functionAddress, callInstructionLength, PAGE_EXECUTE_READWRITE, &old_protect);
if (c != 0)
memcpy(functionAddress, jmpInstructionBuffer, callInstructionLength);
else
printf("couldnt change page protection at %llx\n", functionAddress);
//int d = VirtualProtect(functionAddress, callInstructionLength, old_protect, &old_protect);
FlushInstructionCache(GetCurrentProcess(), functionAddress, callInstructionLength);
}
//printf("done\n");
}
void CreateInlineAsmStub()
{
hook::pattern locationsIntact = hook::module_pattern(GetModuleHandle(nullptr), "89 04 8A 83 45 ? FF");
hook::pattern locationsIntactBig = hook::module_pattern(GetModuleHandle(nullptr), "89 04 8A 83 85");
hook::pattern locationsSplit = hook::module_pattern(GetModuleHandle(nullptr), "89 04 8A E9");
uint64_t baseAddr = reinterpret_cast<uint64_t>(GetModuleHandle(nullptr));
size_t intactCount = locationsIntact.size();
size_t intactBigCount = locationsIntactBig.size();
size_t splitCount = locationsSplit.size();
size_t totalCount = intactCount + intactBigCount + splitCount;
const size_t allocationSize = sizeof(uint8_t) * 128;
inlineStubs = (inlineAsmStub*)malloc(sizeof(inlineAsmStub) * totalCount);
for (int i=0; i < intactCount; i++)
{
inlineStubs[stubCounter].functionAddress = locationsIntact.get(i).get<void*>(0);
inlineStubs[stubCounter].type = intactSmall;
inlineStubs[stubCounter].bufferSize = 7;
stubCounter++;
}
for (int i=0; i < intactBigCount; i++)
{
inlineStubs[stubCounter].functionAddress = locationsIntactBig.get(i).get<void*>(0);
inlineStubs[stubCounter].type = intactBig;
inlineStubs[stubCounter].bufferSize = 10;
stubCounter++;
}
for (int i=0; i < splitCount; i++)
{
inlineStubs[stubCounter].functionAddress = locationsSplit.get(i).get<void*>(0);
inlineStubs[stubCounter].type = split;
inlineStubs[stubCounter].bufferSize = 8;
stubCounter++;
}
LPVOID asmBigStubLocation = allocate_somewhere_near(GetModuleHandle(nullptr), allocationSize * 0x80);
memset(asmBigStubLocation, 0x90, allocationSize * 0x80);
// avoid stub generation collision
char* previousStubOffset = nullptr;
// for jmp distance calculation
char* currentStubOffset = nullptr;
// TODO: once we are done with that merge all the checksum fix stub generators into one function
// make that also use one big allocated memory page
// TODO: fix the asm stub that requires a movzx, registers maybe are getting owned?
for (int i=0; i < stubCounter; i++)
{
// we don't know the previous offset yet
if (currentStubOffset == nullptr)
currentStubOffset = (char*)asmBigStubLocation;
if (previousStubOffset != nullptr)
currentStubOffset = previousStubOffset;
void* functionAddress = inlineStubs[i].functionAddress;
uint64_t jmpDistance = (uint64_t)currentStubOffset - (uint64_t)functionAddress - 5; // 5 bytes from relative call instruction
// backup instructions that will get destroyed
const int length = sizeof(uint8_t) * 8;
uint8_t instructionBuffer[8] = {};
memcpy(instructionBuffer, functionAddress, length);
uint32_t instructionBufferJmpDistance = 0;
if (instructionBuffer[3] == 0xE9)
memcpy(&instructionBufferJmpDistance, (char*)functionAddress+0x4, 4); // 0x4 so we skip 0xE9
uint64_t rbpOffset = 0x0;
bool jumpDistanceNegative = instructionBufferJmpDistance >> 31; // get sign bit from jump distance
int32_t jumpDistance = instructionBufferJmpDistance;
if (inlineStubs[i].type == split)
{
// TODO: receive the rbpOffset by going through the jmp instruction
// on big rbp offsets we could do the same hack we did on big intact where we do rbpOffset+0x100 if its below 0x60
char* rbpOffsetPtr = nullptr;
// TODO: just use jumpDistance once we got a working test case
if (jumpDistanceNegative)
rbpOffsetPtr = (char*)((uint64_t)functionAddress+jumpDistance+0x8);
else
rbpOffsetPtr = (char*)((uint64_t)functionAddress+instructionBufferJmpDistance+0x8);
rbpOffsetPtr++;
// depending on the rbp offset from add dword ptr we need one more byte for the rbpOffset
if (*(unsigned char*)rbpOffsetPtr == 0x45) // add dword ptr [rbp+68],-01
{
rbpOffsetPtr++;
rbpOffset = *(char*)rbpOffsetPtr;
}
else if (*(unsigned char*)rbpOffsetPtr == 0x85) // add dword ptr [rbp+1CC],-01
{
rbpOffsetPtr++;
rbpOffset = *(short*)rbpOffsetPtr;
}
}
// create assembly stub content
// TODO: we could create three different asmjit build sections for each type
// so we don't have if statements inbetween instructions for the cost of LOC but it would be more readible
static asmjit::JitRuntime runtime;
asmjit::CodeHolder code;
code.init(runtime.environment());
asmjit::x86::Assembler a(&code);
if (inlineStubs[i].type != split)
rbpOffset = instructionBuffer[5];
a.sub(asmjit::x86::rsp, 0x32);
pushad64();
a.mov(asmjit::x86::qword_ptr(asmjit::x86::rsp, 0x20), asmjit::x86::rax);
a.mov(asmjit::x86::rdx, asmjit::x86::rcx); // offset within text section pointer (ecx*4)
// we dont use rbpoffset since we only get 1 byte from the 2 byte offset (rbpOffset)
// 0x130 is a good starting ptr to decrement downwards so we can find the original checksum
if (inlineStubs[i].type == intactBig)
a.mov(asmjit::x86::rcx, 0x120);
else
a.mov(asmjit::x86::rcx, rbpOffset);
a.mov(asmjit::x86::r8, asmjit::x86::rbp);
if (inlineStubs[i].type == split)
{
if (jumpDistanceNegative)
a.mov(asmjit::x86::r9, jumpDistance);
else
a.mov(asmjit::x86::r9, instructionBufferJmpDistance);
}
else
a.mov(asmjit::x86::r9, instructionBufferJmpDistance); // incase we mess up a split checksum
a.mov(asmjit::x86::rax, (uint64_t)(void*)FixChecksum);
a.call(asmjit::x86::rax);
a.add(asmjit::x86::rsp, 0x8*4); // so that r12-r15 registers dont get corrupt
popad64WithoutRAX();
a.add(asmjit::x86::rsp, 0x32);
a.mov(ptr(asmjit::x86::rdx, asmjit::x86::rcx, 2), asmjit::x86::eax); // mov [rdx+rcx*4], eax
if (instructionBufferJmpDistance == 0)
{
if (inlineStubs[i].type == intactBig)
rbpOffset += 0x100;
a.add(dword_ptr(asmjit::x86::rbp, rbpOffset), -1); // add dword ptr [rbp+rbpOffset], 0FFFFFFFFh
}
else
{
// jmp loc_7FF641C707A5
// push the desired address on to the stack and then perform a 64 bit RET
a.add(asmjit::x86::rsp, 0x8); // pop return address off the stack cause we will jump
uint64_t addressToJump = (uint64_t)functionAddress + instructionBufferJmpDistance;
if (inlineStubs[i].type == split)
{
// TODO: just use jumpDistance once we got a working test case
if (jumpDistanceNegative)
addressToJump = (uint64_t)functionAddress + jumpDistance + 0x8; // 0x8 call instruction + offset + 2 nops
else
addressToJump = (uint64_t)functionAddress + instructionBufferJmpDistance + 0x8; // 0x8 call instruction + offset + 2 nops
}
a.mov(asmjit::x86::r11, addressToJump); // r11 is being used but should be fine based on documentation
if (inlineStubs[i].type == split)
a.add(asmjit::x86::rsp, 0x8); // since we dont pop off rax we need to sub 0x8 the rsp
a.push(asmjit::x86::r11);
}
if (inlineStubs[i].type != split)
a.add(asmjit::x86::rsp, 0x8); // since we dont pop off rax we need to sub 0x8 the rsp
a.ret();
void* asmjitResult = nullptr;
runtime.add(&asmjitResult, &code);
// copy over the content to the stub
uint8_t* tempBuffer = (uint8_t*)malloc(sizeof(uint8_t) * code.codeSize());
memcpy(tempBuffer, asmjitResult, code.codeSize());
memcpy(currentStubOffset, tempBuffer, sizeof(uint8_t) * code.codeSize());
size_t callInstructionBytes = inlineStubs[i].bufferSize;
size_t callInstructionLength = sizeof(uint8_t) * callInstructionBytes;
DWORD old_protect{};
VirtualProtect(functionAddress, callInstructionLength, PAGE_EXECUTE_READWRITE, &old_protect);
memset(functionAddress, 0, callInstructionLength);
VirtualProtect(functionAddress, callInstructionLength, old_protect, &old_protect);
FlushInstructionCache(GetCurrentProcess(), functionAddress, callInstructionLength);
// E8 cd CALL rel32 Call near, relative, displacement relative to next instruction
uint8_t* jmpInstructionBuffer = (uint8_t*)malloc(sizeof(uint8_t) * callInstructionBytes);
jmpInstructionBuffer[0] = 0xE8;
jmpInstructionBuffer[1] = (jmpDistance >> (0 * 8));
jmpInstructionBuffer[2] = (jmpDistance >> (1 * 8));
jmpInstructionBuffer[3] = (jmpDistance >> (2 * 8));
jmpInstructionBuffer[4] = (jmpDistance >> (3 * 8));
jmpInstructionBuffer[5] = 0x90;
jmpInstructionBuffer[6] = 0x90;
if (inlineStubs[i].type == intactBig)
{
jmpInstructionBuffer[7] = 0x90;
jmpInstructionBuffer[8] = 0x90;
jmpInstructionBuffer[9] = 0x90;
}
if (inlineStubs[i].type == split)
jmpInstructionBuffer[7] = 0x90;
VirtualProtect(functionAddress, callInstructionLength, PAGE_EXECUTE_READWRITE, &old_protect);
memcpy(functionAddress, jmpInstructionBuffer, callInstructionLength);
VirtualProtect(functionAddress, callInstructionLength, old_protect, &old_protect);
FlushInstructionCache(GetCurrentProcess(), functionAddress, callInstructionLength);
// store location & bytes to check if arxan is removing our hooks
if (inlineStubs[i].type == intactSmall)
{
intactChecksumHook intactChecksum = {};
intactChecksum.functionAddress = (uint64_t*)functionAddress;
memcpy(intactChecksum.buffer, jmpInstructionBuffer, sizeof(uint8_t) * inlineStubs[i].bufferSize);
inlineStubs[i].buffer = intactChecksum.buffer;
intactchecksumHooks.push_back(intactChecksum);
}
if (inlineStubs[i].type == intactBig)
{
intactBigChecksumHook intactBigChecksum = {};
intactBigChecksum.functionAddress = (uint64_t*)functionAddress;
memcpy(intactBigChecksum.buffer, jmpInstructionBuffer, sizeof(uint8_t) * inlineStubs[i].bufferSize);
inlineStubs[i].buffer = intactBigChecksum.buffer;
intactBigchecksumHooks.push_back(intactBigChecksum);
}
if (inlineStubs[i].type == split)
{
splitChecksumHook splitChecksum = {};
splitChecksum.functionAddress = (uint64_t*)functionAddress;
memcpy(splitChecksum.buffer, jmpInstructionBuffer, sizeof(uint8_t) * inlineStubs[i].bufferSize);
inlineStubs[i].buffer = splitChecksum.buffer;
splitchecksumHooks.push_back(splitChecksum);
}
previousStubOffset = currentStubOffset + sizeof(uint8_t) * code.codeSize() + 0x8;
}
printf("p %d\n", intactCount);
printf("p %d\n", intactBigCount);
printf("p %d\n", splitCount);
}
bool arxanHealingChecksum(uint64_t rbp)
{
// check if rbpAddressLocationPtr is within the range of 8 bytes up & down from every checksum that we placed.
uint64_t rbpAddressLocationPtr = *(uint64_t*)(rbp+0x10);
for (int i=0; i < stubCounter; i++)
{
// 0x8
// TODO: if 0x7 is too big then "mov [rdx], al" will make the game crash probably because its trying to overwrite areas next to our hooks that have to get modified.
// we could do two seperate functions since "mov [rdx], eax" would be a 32 byte offset (?) and "mov [rdx], al" would be 4 byte offset (?)
if (rbpAddressLocationPtr+0x7 >= (uint64_t)inlineStubs[i].functionAddress &&
rbpAddressLocationPtr-0x7 <= (uint64_t)inlineStubs[i].functionAddress)
{
return true;
}
}
return false;
}
void CreateChecksumHealingStub()
{
void* baseModule = GetModuleHandle(nullptr);
checksumHealingLocation healingLocations[] {
{hook::module_pattern(baseModule, "89 02 8B 45 20"), 5},
{hook::module_pattern(baseModule, "88 02 83 45 20 FF"), 6},
{hook::module_pattern(baseModule, "89 02 E9"), 7},
{hook::module_pattern(baseModule, "88 02 E9"), 7},
};
const size_t allocationSize = sizeof(uint8_t) * 0x100 * 1000;
LPVOID healingStubLocation = allocate_somewhere_near(GetModuleHandle(nullptr), allocationSize);
memset(healingStubLocation, 0x90, allocationSize);
// avoid stub generation collision
char* previousStubOffset = nullptr;
// for jmp distance calculation
char* currentStubOffset = nullptr;
size_t amountOfPatterns = sizeof(healingLocations) / sizeof(checksumHealingLocation);
for (int type=0; type < amountOfPatterns; type++)
{
size_t locations = healingLocations[type].checksumPattern.size();
for (int i=0; i < locations; i++)