-
Notifications
You must be signed in to change notification settings - Fork 22
/
AntiAntiDebugALL.c
2079 lines (1573 loc) · 51.7 KB
/
AntiAntiDebugALL.c
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 "ntddk.h"
#include "dbgtool.h"
#include "DRRWE.h"
#include "KernelStruct.h "
#include "intrin.h"
#include <ntimage.h>
#include "VMProtectDDK.h"
#include "./Hooks/PageHook.h"
typedef struct _ReadWriteR3MemData{
HANDLE ProcessHandle;
PVOID BaseAddress;
PVOID Buffer;
ULONG BufferLength;
PULONG ReturnLength;
BOOLEAN IsReadOrWrite;
NTSTATUS Status;
}ReadWriteR3MemData, *PReadWriteR3MemData;
extern PDEVICE_OBJECT pDevObj;
static KSPIN_LOCK KrwLock = NULL;
KEVENT KwaitForReadOrWrite;
ULONG64 fc_DbgkGetAdrress(PUNICODE_STRING64 funcstr);
extern BOOLEAN VtMode;
//////////////
/////////////
NTKERNELAPI char* PsGetProcessImageFileName(PEPROCESS Process);
typedef ULONG(__fastcall *pfNtOpenProcess)(
__out PHANDLE ProcessHandle,
__in ACCESS_MASK AccessMask,
__in PVOID ObjectAttributes,
__in PCLIENT_ID ClientId);
typedef signed __int64(__fastcall *pfNtReadVirtualMemory)(PHANDLE ProcessHandle, unsigned __int64 AccessMode, __int64 a3, __int64 a4, unsigned __int64 a5);
typedef signed __int64(__fastcall *pfNtWriteVirtualMemory)(PHANDLE ProcessHandle, __int64 a2, unsigned __int64 AccessMode, __int64 a4, unsigned __int64 a5);
pfNtWriteVirtualMemory PwriteMem = NULL;
pfNtReadVirtualMemory PreadMem = NULL;
extern pfNtOpenProcess XNtOpenProcess;
extern pfNtWriteVirtualMemory NtWriteVirtualMemory;
extern pfNtReadVirtualMemory NtReadVirtualMemory;
#define WOW64_MAXIMUM_SUPPORTED_EXTENSION 512
#define WOW64_SIZE_OF_80387_REGISTERS 80
typedef LONG DWORD;
typedef INT16 WORD;
typedef char BYTE;
extern p_save_handlentry PmainList;
static KSPIN_LOCK local_lock;
ULONG64 KiRestoreDebugRegisterState;
extern ULONG64 KiSaveDebugRegisterState;
ULONG64 KiAttachProcess;
typedef NTSTATUS (__fastcall* pfKiAttachProcess)(
IN PKTHREAD Thread,
IN PKPROCESS Process,
IN PKLOCK_QUEUE_HANDLE ApcLock,
IN PRKAPC_STATE SavedApcState);
typedef struct _WOW64_FLOATING_SAVE_AREA {
DWORD ControlWord;
DWORD StatusWord;
DWORD TagWord;
DWORD ErrorOffset;
DWORD ErrorSelector;
DWORD DataOffset;
DWORD DataSelector;
char RegisterArea[WOW64_SIZE_OF_80387_REGISTERS];
DWORD Cr0NpxState;
} WOW64_FLOATING_SAVE_AREA;
typedef WOW64_FLOATING_SAVE_AREA *PWOW64_FLOATING_SAVE_AREA;
typedef struct _WOW64_CONTEXT {
//
// The flags values within this flag control the contents of
// a CONTEXT record.
//
// If the context record is used as an input parameter, then
// for each portion of the context record controlled by a flag
// whose value is set, it is assumed that that portion of the
// context record contains valid context. If the context record
// is being used to modify a threads context, then only that
// portion of the threads context will be modified.
//
// If the context record is used as an IN OUT parameter to capture
// the context of a thread, then only those portions of the thread's
// context corresponding to set flags will be returned.
//
// The context record is never used as an OUT only parameter.
//
DWORD ContextFlags;
//
// This section is specified/returned if CONTEXT_DEBUG_REGISTERS is
// set in ContextFlags. Note that CONTEXT_DEBUG_REGISTERS is NOT
// included in CONTEXT_FULL.
//
DWORD Dr0;
DWORD Dr1;
DWORD Dr2;
DWORD Dr3;
DWORD Dr6;
DWORD Dr7;
//
// This section is specified/returned if the
// ContextFlags word contians the flag CONTEXT_FLOATING_POINT.
//
WOW64_FLOATING_SAVE_AREA FloatSave;
//
// This section is specified/returned if the
// ContextFlags word contians the flag CONTEXT_SEGMENTS.
//
DWORD SegGs;
DWORD SegFs;
DWORD SegEs;
DWORD SegDs;
//
// This section is specified/returned if the
// ContextFlags word contians the flag CONTEXT_INTEGER.
//
DWORD Edi;
DWORD Esi;
DWORD Ebx;
DWORD Edx;
DWORD Ecx;
DWORD Eax;
//
// This section is specified/returned if the
// ContextFlags word contians the flag CONTEXT_CONTROL.
//
DWORD Ebp;
DWORD Eip;
DWORD SegCs; // MUST BE SANITIZED
DWORD EFlags; // MUST BE SANITIZED
DWORD Esp;
DWORD SegSs;
//
// This section is specified/returned if the ContextFlags word
// contains the flag CONTEXT_EXTENDED_REGISTERS.
// The format and contexts are processor specific
//
char ExtendedRegisters[WOW64_MAXIMUM_SUPPORTED_EXTENSION];
} WOW64_CONTEXT;
typedef WOW64_CONTEXT *PWOW64_CONTEXT;
/*
typedef struct DECLSPEC_ALIGN(16) _M128A{
ULONGLONG Low;
LONGLONG High;
} M128A, *PM128A;
*/
//
// Format of data for 32-bit fxsave/fxrstor instructions.
//
typedef struct _XMM_SAVE_AREA321 {
WORD ControlWord;
WORD StatusWord;
BYTE TagWord;
BYTE Reserved1;
WORD ErrorOpcode;
DWORD ErrorOffset;
WORD ErrorSelector;
WORD Reserved2;
DWORD DataOffset;
WORD DataSelector;
WORD Reserved3;
DWORD MxCsr;
DWORD MxCsr_Mask;
M128A FloatRegisters[8];
M128A XmmRegisters[16];
BYTE Reserved4[96];
} XMM_SAVE_AREA321, *PXMM_SAVE_AREA321;
#define LEGACY_SAVE_AREA_LENGTH sizeof(XMM_SAVE_AREA32)
typedef struct DECLSPEC_ALIGN(16) _myCONTEXT{
DWORD64 P1Home;
DWORD64 P2Home;
DWORD64 P3Home;
DWORD64 P4Home;
DWORD64 P5Home;
DWORD64 P6Home;
DWORD ContextFlags;
DWORD MxCsr;
WORD SegCs;
WORD SegDs;
WORD SegEs;
WORD SegFs;
WORD SegGs;
WORD SegSs;
DWORD EFlags;
DWORD64 Dr0;
DWORD64 Dr1;
DWORD64 Dr2;
DWORD64 Dr3;
DWORD64 Dr6;
DWORD64 Dr7;
DWORD64 Rax;
DWORD64 Rcx;
DWORD64 Rdx;
DWORD64 Rbx;
DWORD64 Rsp;
DWORD64 Rbp;
DWORD64 Rsi;
DWORD64 Rdi;
DWORD64 R8;
DWORD64 R9;
DWORD64 R10;
DWORD64 R11;
DWORD64 R12;
DWORD64 R13;
DWORD64 R14;
DWORD64 R15;
DWORD64 Rip;
union {
XMM_SAVE_AREA321 FltSave;
struct {
M128A Header[2];
M128A Legacy[8];
M128A Xmm0;
M128A Xmm1;
M128A Xmm2;
M128A Xmm3;
M128A Xmm4;
M128A Xmm5;
M128A Xmm6;
M128A Xmm7;
M128A Xmm8;
M128A Xmm9;
M128A Xmm10;
M128A Xmm11;
M128A Xmm12;
M128A Xmm13;
M128A Xmm14;
M128A Xmm15;
};
};
M128A VectorRegister[26];
DWORD64 VectorControl;
//
// Special debug control registers.
//
DWORD64 DebugControl;
DWORD64 LastBranchToRip;
DWORD64 LastBranchFromRip;
DWORD64 LastExceptionToRip;
DWORD64 LastExceptionFromRip;
} myCONTEXT, *PmyCONTEXT;
void GetKernelModuleBase(char* lpModuleName, ULONG64 *ByRefBase, ULONG *ByRefSize);
typedef struct _HOOK_64BIT{
PVOID orgcode;
PVOID orgcodenumber;
PVOID FreeSpacebyte;
}HOOK_64BIT, *PHOOK_64BIT;
extern p_save_handlentry PmainList;
ULONG RtlWalkFrameChain(OUT PVOID *Callers, IN ULONG Count, IN ULONG Flags);
PVOID HookKernelApi(IN PVOID ApiAddress, IN PVOID Proxy_ApiAddress, OUT PVOID *Original_ApiAddress, OUT ULONG *PatchSize);
VOID UnhookKernelApi(IN PVOID ApiAddress, IN PVOID OriCode, IN ULONG PatchSize);
NTKERNELAPI PEPROCESS IoThreadToProcess(IN PETHREAD Thread);
PVOID HookKernelApi4_6bit(IN ULONG64 ApiAddress, IN PVOID Proxy_ApiAddress, PHOOK_64BIT structb);
VOID UnHookKernelApi4_6bit(IN ULONG64 ApiAddress, PHOOK_64BIT structb);
ULONG64 ExCompareExchangeCallBack;
ULONG64 ObpCallPreOperationCallbacks;
ULONG64 ExGetCallBackBlockRoutine;
ULONG64 NtQueryInformationThread;
typedef __int64(__fastcall *pfObpCallPreOperationCallbacks)(__int64 a1, __int64 a2, __int64 a3);
typedef __int64(__fastcall *pfExGetCallBackBlockRoutine)(__int64 a1);
typedef BOOLEAN(__fastcall *pfExCompareExchangeCallBack)(ULONG64 a1,ULONG64 a2,ULONG64 a3);
typedef NTSTATUS(__fastcall *pfNtQueryInformationThread)(IN HANDLE ThreadHandle,
IN THREADINFOCLASS ThreadInformationClass,
OUT PVOID ThreadInformation,
IN ULONG ThreadInformationLength,
OUT PULONG ReturnLength OPTIONAL
);
NTSTATUS PsLookupProcessByProcessId(_In_ HANDLE ProcessId, _Out_ PEPROCESS *Process);
NTKERNELAPI NTSTATUS PsLookupThreadByThreadId(_In_ HANDLE ThreadId, _Outptr_ PETHREAD *Thread);
pfNtQueryInformationThread orgNtQueryInformationThread;
typedef (__fastcall *pfnNtReadVirtualMemory)(
IN HANDLE ProcessHandle,
IN PVOID BaseAddress,
OUT PVOID Buffer,
IN ULONG BufferLength,
OUT PULONG ReturnLength OPTIONAL
);
typedef (__fastcall *pfnNtWriteVirtualMemory)(
IN HANDLE ProcessHandle,
IN PVOID BaseAddress,
OUT PVOID Buffer,
IN ULONG BufferLength,
OUT PULONG ReturnLength OPTIONAL
);
typedef NTSTATUS(__fastcall*pfNtSetInformationThread)(HANDLE threadHandle, THREADINFOCLASS threadInformationClass, PVOID threadInformation, ULONG threadInformationLength);
ULONG pslp_patch_size9 = 0; //ObpCallPreOperationCallbacks被修改了N字节
PUCHAR pslp_head_n_byte9 = NULL; //ObpCallPreOperationCallbacks的前N字节数组
PVOID ori_pslp9 = NULL; //ObpCallPreOperationCallbacks的原函数
ULONG pslp_patch_size10 = 0; //ExGetCallBackBlockRoutine被修改了N字节
PUCHAR pslp_head_n_byte10 = NULL; //ExGetCallBackBlockRoutine的前N字节数组
PVOID ori_pslp10 = NULL; //ExGetCallBackBlockRoutine的原函数
ULONG pslp_patch_size19 = 0; //NtQueryInformationThread被修改了N字节
PUCHAR pslp_head_n_byte19 = NULL; //NtQueryInformationThread的前N字节数组
PVOID ori_pslp19 = NULL; //NtQueryInformationThread的原函数
ULONG pslp_patch_size20 = 0; //ExCompareExchangeCallBack被修改了N字节
PUCHAR pslp_head_n_byte20 = NULL; //ExCompareExchangeCallBack的前N字节数组
PVOID ori_pslp20 = NULL; //ExCompareExchangeCallBack的原函数
ULONG pslp_patch_size21 = 0; //proxyPsLookupThreadByThreadId被修改了N字节
PUCHAR pslp_head_n_byte21 = NULL; //proxyPsLookupThreadByThreadId的前N字节数组
PVOID ori_pslp21 = NULL; //proxyPsLookupThreadByThreadId的原函数
ULONG pslp_patch_size22 = 0; //proxyPsLookupProcessByProcessId被修改了N字节
PUCHAR pslp_head_n_byte22 = NULL; //proxyPsLookupProcessByProcessId的前N字节数组
PVOID ori_pslp22 = NULL; //proxyPsLookupProcessByProcessId的原函数
ULONG pslp_patch_size23 = 0; //KiRestoreDebugRegisterState被修改了N字节
PUCHAR pslp_head_n_byte23 = NULL; //KiRestoreDebugRegisterState的前N字节数组
PVOID ori_pslp23 = NULL; //KiRestoreDebugRegisterState的原函数
ULONG pslp_patch_size24= 0; //KiSaveDebugRegisterState被修改了N字节
PUCHAR pslp_head_n_byte24 = NULL; //KiSaveDebugRegisterState的前N字节数组
PVOID ori_pslp24 = NULL; //KiSaveDebugRegisterState的原函数
ULONG pslp_patch_size25 = 0; //RtlpCopyLegacyContextX86被修改了N字节
PUCHAR pslp_head_n_byte25 = NULL; //RtlpCopyLegacyContextX86的前N字节数组
PVOID ori_pslp25 = NULL; //RtlpCopyLegacyContextX86的原函数
ULONG pslp_patch_size26 = 0; //pfKiAttachProcess被修改了N字节
PUCHAR pslp_head_n_byte26 = NULL; //pfKiAttachProcess的前N字节数组
pfKiAttachProcess ori_pslp26 = NULL; //pfKiAttachProcess的原函数
ULONG pslp_patch_size27 = 0; //ReadProcessMemory被修改了N字节
PUCHAR pslp_head_n_byte27 = NULL; //ReadProcessMemory的前N字节数组
pfnNtReadVirtualMemory ori_pslp27 = NULL; //ReadProcessMemory的原函数
ULONG pslp_patch_size28 = 0; //WriteProcessMemory被修改了N字节
PUCHAR pslp_head_n_byte28 = NULL; //WriteProcessMemory的前N字节数组
pfnNtWriteVirtualMemory ori_pslp28 = NULL; //WriteProcessMemory的原函数
ULONG64 RtlpCopyLegacyContextX86 = NULL;
ULONG64 KrnlBase = 0;
ULONG KrnlSize = 0;
HOOK_64BIT orgcode;
typedef NTSTATUS(__fastcall *pfPsLookupProcessByProcessId)(_In_ HANDLE ProcessId, _Out_ PEPROCESS *Process);
typedef NTSTATUS(__fastcall *pfPsLookupThreadByThreadId)(_In_ HANDLE ThreadId, _Outptr_ PETHREAD *Thread);
extern __fastcall myKiSaveDebugRegisterState();
extern __fastcall myKiRestoreDebugRegisterState();
ULONG64 _Search64Process(char *szProcessName, ULONG64 callBackFUNC);
NTSTATUS
NTAPI
NtSetInformationThread(
_In_ HANDLE ThreadHandle,
_In_ THREADINFOCLASS ThreadInformationClass,
_In_reads_bytes_(ThreadInformationLength) PVOID ThreadInformation,
_In_ ULONG ThreadInformationLength
);
__int64 __fastcall proxyKiAttachProcess(
IN PKTHREAD Thread,
IN PKPROCESS Process,
IN PKLOCK_QUEUE_HANDLE ApcLock,
IN PRKAPC_STATE SavedApcState)
{
PEPROCESS g_Guiprocess = NULL;
char *CallProcessName = PsGetProcessImageFileName(PsGetCurrentProcess());
if (strstr(CallProcessName,"System")!=NULL || PsGetCurrentProcessId()==4)
{
return ori_pslp26(Thread, Process, ApcLock, SavedApcState);
}
p_save_handlentry Padd = NULL;
Padd = querylist(PmainList, NULL, Process);
if (Padd != NULL)
{
Padd = querylist(PmainList, PsGetCurrentProcessId(), PsGetCurrentProcess());
if (Padd!=NULL)
{
return ori_pslp26(Thread, Process, ApcLock, SavedApcState);
}
g_Guiprocess = _Search64Process("csrss", 0);
DbgPrint(" myProcessName:%s \n", PsGetProcessImageFileName(PsGetCurrentProcess()));
return ori_pslp26(Thread, g_Guiprocess, ApcLock, SavedApcState);;
}
return ori_pslp26(Thread, Process, ApcLock, SavedApcState);
}
__int64 __fastcall proxyObpCallPreOperationCallbacks(__int64 a1, __int64 a2, __int64 a3){
if (VtMode)
{
PPAGE_HOOK_ENTRY pEntry = PHGetHookEntry(ObpCallPreOperationCallbacks);
ori_pslp9 = pEntry->OriginalData;
}
p_save_handlentry Padd;
Padd = querylist(PmainList, PsGetCurrentProcessId(), PsGetCurrentProcess());
if (Padd == NULL){
return ((pfObpCallPreOperationCallbacks)ori_pslp9)(a1, a2, a3);//不是我们的进程操作进程对象就调用原来函数
}
else{
return 0;//放行
}
return ((pfObpCallPreOperationCallbacks)ori_pslp9)(a1, a2, a3);//不是我们的进程操作进程对象就调用原来函数return 0;//放行
}
VOID proxyListFunc(_In_ HANDLE ParentId, _In_ HANDLE ProcessId, _In_ BOOLEAN Create){
return ;
}
/*
NTSTATUS myNtSetInformationThread(HANDLE threadHandle, THREADINFOCLASS threadInformationClass, PVOID threadInformation, ULONG threadInformationLength){
PEPROCESS_S Process = NULL;
PETHREAD Thread = NULL;
p_save_handlentry Padd = NULL;
NTSTATUS Sstatus;
NTSTATUS status=STATUS_SUCCESS;
PPROCESS_List PlIST = NULL;;
PTHREAD_dr_List TList = NULL;
THREAD_dr_List t = {0};
PKTRAP_FRAME pframe = NULL;
PWOW64_CONTEXT contex = threadInformation;
WOW64_CONTEXT mycontex = { 0 };
Padd = querylist(PmainList, PsGetCurrentProcessId(), PsGetCurrentProcess());
if (Padd == NULL)
{
status = NtSetInformationThread(threadHandle, threadInformationClass, threadInformation, threadInformationLength);
return status;
}
Sstatus = ObReferenceObjectByHandle(threadHandle,
THREAD_ALL_ACCESS,
*PsThreadType,
KernelMode,
(PVOID*)&Thread,
NULL);
if (!NT_SUCCESS(Sstatus)){
return Sstatus;
}
ObDereferenceObject(Thread);
Process = IoThreadToProcess(Thread);
if (Process!=NULL)
{
Padd = querylist(PmainList, 0x10086, Process);
if (Padd != NULL)
{
status = NtSetInformationThread(threadHandle, threadInformationClass, threadInformation, threadInformationLength);
return status;
}
}
Padd = querylist(PmainList, PsGetCurrentProcessId(), PsGetCurrentProcess());
if (Padd != NULL)
{
if (threadInformationClass == ThreadWow64Context && threadInformationLength == 0x2CC && threadInformation != NULL && threadHandle != NULL)//ThreadWow64Context
{
Sstatus = ObReferenceObjectByHandle(threadHandle,
THREAD_ALL_ACCESS,
*PsThreadType,
KernelMode,
(PVOID*)&Thread,
NULL);
if (!NT_SUCCESS(Sstatus)){
return STATUS_SUCCESS;
}
BOOLEAN lock = FALSE;
lock = ExAcquireRundownProtection(&Thread->RundownProtect);
if (MmIsAddressValid(Thread) != TRUE || lock != TRUE)
{
return;
}
pframe = Thread->Tcb.TrapFrame;
if ((pframe != NULL) && MmIsAddressValid(&pframe->Dr0) && MmIsAddressValid(&pframe->Dr1) && MmIsAddressValid(&pframe->Dr2) && MmIsAddressValid(&pframe->Dr3) && MmIsAddressValid(&pframe->Dr6) && MmIsAddressValid(&pframe->Dr7))
{
if (contex->Dr7 != NULL)
{
*(UCHAR*)(Thread + 0x3) = 0x40;
}
mycontex.Dr0 = contex->Dr0;
mycontex.Dr1 = contex->Dr1;
mycontex.Dr2 = contex->Dr2;
mycontex.Dr3 = contex->Dr3;
mycontex.Dr6 = contex->Dr6;
mycontex.Dr7 = contex->Dr7;
mycontex.EFlags = contex->EFlags;
contex->Dr0 = ((PLARGE_INTEGER)(&pframe->Dr0))->LowPart;
contex->Dr1 = ((PLARGE_INTEGER)(&pframe->Dr1))->LowPart;
contex->Dr2 = ((PLARGE_INTEGER)(&pframe->Dr2))->LowPart;
contex->Dr3 = ((PLARGE_INTEGER)(&pframe->Dr3))->LowPart;
contex->Dr6 = ((PLARGE_INTEGER)(&pframe->Dr6))->LowPart;
// contex->Dr7 = ((PLARGE_INTEGER)(&pframe->Dr7))->LowPart;
// contex->EFlags = pframe->EFlags;
}
ExReleaseRundownProtection(&Thread->RundownProtect);
//DbgPrint("thread: %p", Thread);
//OD设置线程上下文的时候DR我们设置成线程自己的
status = NtSetInformationThread(threadHandle, threadInformationClass, threadInformation, threadInformationLength);
contex->Dr0=mycontex.Dr0;
contex->Dr1 = mycontex.Dr1;
contex->Dr2 = mycontex.Dr2;
contex->Dr3 = mycontex.Dr3;
contex->Dr6 = mycontex.Dr6;
contex->Dr7 = mycontex.Dr7;
DbgPrint("SET thread: %p dr0: %d dr1 :%d dr2 :%d dr3 :%d dr6:%d dr7:%d", Thread, contex->Dr0, contex->Dr1, contex->Dr2, contex->Dr3, contex->Dr6, contex->Dr7);
ObDereferenceObject(Thread);
}
}
if (threadInformationClass == ThreadWow64Context && threadInformationLength == 0x2CC && threadInformation != NULL && threadHandle != NULL)//ThreadWow64Context
{
Sstatus = ObReferenceObjectByHandle(threadHandle,
THREAD_ALL_ACCESS,
*PsThreadType,
KernelMode,
(PVOID*)&Thread,
NULL);
if (!NT_SUCCESS(Sstatus)){
return STATUS_SUCCESS;
}
ObDereferenceObject(Thread);
if (NT_SUCCESS(Sstatus)){
Process = IoThreadToProcess(Thread);
if (Process==NULL)
{
return STATUS_SUCCESS;
}
PlIST = Dr_FindProcessList(Process);
if (PlIST != NULL){
///////////
TList = Dr_FindThreadContextByThreadList(PlIST, Thread);
if (TList != NULL)
{
t.Dr0 = mycontex.Dr0;
t.Dr1 = mycontex.Dr1;
t.Dr2 = mycontex.Dr2;
t.Dr3 = mycontex.Dr3;
t.Dr6 = mycontex.Dr6;
t.Dr7 = mycontex.Dr7;
t.eflag = mycontex.EFlags;
t.Thread = Thread;
Dr_UpdataThreadContextByThreadList(PlIST, Thread, &t);
}
else
{
t.Dr0 = mycontex.Dr0;
t.Dr1 = mycontex.Dr1;
t.Dr2 = mycontex.Dr2;
t.Dr3 = mycontex.Dr3;
t.Dr6 = mycontex.Dr6;
t.Dr7 = mycontex.Dr7;
t.eflag = mycontex.EFlags;
t.Thread = Thread;
Dr_AddThreadStructToList(PlIST, &t);
}
}
else
{
PlIST = Dr_AddProcessToList(Process);
if (PlIST==NULL)
{
return STATUS_SUCCESS;
}
t.Dr0 = mycontex.Dr0;
t.Dr1 = mycontex.Dr1;
t.Dr2 = mycontex.Dr2;
t.Dr3 = mycontex.Dr3;
t.Dr6 = mycontex.Dr6;
t.Dr7 = mycontex.Dr7;
t.eflag = mycontex.EFlags;
t.Thread = Thread;
Dr_AddThreadStructToList(PlIST, &t);
////////////
}
}
}
return STATUS_SUCCESS;
}
*/
/*
NTSTATUS __fastcall myNtQueryInformationThread(IN HANDLE ThreadHandle,
IN THREADINFOCLASS ThreadInformationClass,
OUT PVOID ThreadInformation,
IN ULONG ThreadInformationLength,
OUT PULONG ReturnLength OPTIONAL
){
PEPROCESS_S Process = NULL;
PETHREAD Thread = NULL;
PPROCESS_List PlIST = NULL;;
PTHREAD_dr_List TList = NULL;
p_save_handlentry Padd = NULL;
NTSTATUS Sstatus=STATUS_SUCCESS;
PKTRAP_FRAME pframe = NULL;
PWOW64_CONTEXT contex = ThreadInformation;
NTSTATUS status = STATUS_SUCCESS;
//orgNtQueryInformationThread = ori_pslp19;
orgNtQueryInformationThread = NtQueryInformationThread;
Padd = querylist(PmainList, PsGetCurrentProcessId(), PsGetCurrentProcess());
if (Padd == NULL)
{
status = orgNtQueryInformationThread(ThreadHandle, ThreadInformationClass, ThreadInformation, ThreadInformationLength, ReturnLength);
return status;
}
Sstatus = ObReferenceObjectByHandle(ThreadHandle,
THREAD_ALL_ACCESS,
*PsThreadType,
KernelMode,
(PVOID*)&Thread,
NULL);
if (!NT_SUCCESS(Sstatus)){
return Sstatus;
}
ObDereferenceObject(Thread);
Process = IoThreadToProcess(Thread);
if (Process != NULL)
{
Padd = querylist(PmainList, 0x10086, Process);
if (Padd != NULL)
{
status = orgNtQueryInformationThread(ThreadHandle, ThreadInformationClass, ThreadInformation, ThreadInformationLength, ReturnLength);
return status;
}
}
Padd = querylist(PmainList, PsGetCurrentProcessId(), PsGetCurrentProcess());
if (Padd != NULL)
{
if (ThreadInformationClass == ThreadWow64Context && ThreadInformationLength == 0x2CC && ThreadInformation != NULL && ThreadHandle != NULL)//ThreadWow64Context
{
Sstatus = ObReferenceObjectByHandle(ThreadHandle,
THREAD_ALL_ACCESS,
*PsThreadType,
KernelMode,
(PVOID*)&Thread,
NULL);
if (!NT_SUCCESS(Sstatus)){
return STATUS_SUCCESS;
}
BOOLEAN lock = FALSE;
lock = ExAcquireRundownProtection(&Thread->RundownProtect);
if (MmIsAddressValid(Thread) != TRUE || lock != TRUE)
{
return;
}
pframe = Thread->Tcb.TrapFrame;
if ((pframe != NULL) && MmIsAddressValid(&pframe->Dr0) && MmIsAddressValid(&pframe->Dr1) && MmIsAddressValid(&pframe->Dr2) && MmIsAddressValid(&pframe->Dr3) && MmIsAddressValid(&pframe->Dr6) && MmIsAddressValid(&pframe->Dr7))
{
/ * contex->Dr0 = ((PLARGE_INTEGER)(&pframe->Dr0))->LowPart;
contex->Dr1 = ((PLARGE_INTEGER)(&pframe->Dr1))->LowPart;
contex->Dr2 = ((PLARGE_INTEGER)(&pframe->Dr2))->LowPart;
contex->Dr3 = ((PLARGE_INTEGER)(&pframe->Dr3))->LowPart;
contex->Dr6 = ((PLARGE_INTEGER)(&pframe->Dr6))->LowPart;
contex->Dr7 = ((PLARGE_INTEGER)(&pframe->Dr7))->LowPart;
contex->EFlags = pframe->EFlags;* /
}
ExReleaseRundownProtection(&Thread->RundownProtect);
//DbgPrint("thread: %p", Thread);
//OD设置线程上下文的时候DR我们设置成线程自己的
status = orgNtQueryInformationThread(ThreadHandle, ThreadInformationClass, ThreadInformation, ThreadInformationLength, ReturnLength);
ObDereferenceObject(Thread);
}
}
if (ThreadInformationClass == ThreadWow64Context && ThreadInformationLength == 0x2CC && ThreadInformation != NULL && ThreadHandle != NULL)//ThreadWow64Context
{
Sstatus = ObReferenceObjectByHandle(ThreadHandle,
THREAD_ALL_ACCESS,
*PsThreadType,
KernelMode,
(PVOID*)&Thread,
NULL);
if (!NT_SUCCESS(Sstatus)){
return status;
}
ObDereferenceObject(Thread);
if (NT_SUCCESS(Sstatus)){
Process = IoThreadToProcess(Thread);
if (Process != NULL){
PlIST = Dr_FindProcessList(Process);
if (PlIST != NULL)
{
TList = Dr_FindThreadContextByThreadList(PlIST, Thread);
if (TList != NULL)
{
contex->Dr0 = TList->Dr0;
contex->Dr1 = TList->Dr1;
contex->Dr2 = TList->Dr2;
contex->Dr3 = TList->Dr3;
contex->Dr6 = TList->Dr6;
contex->Dr7 = TList->Dr7;
contex->EFlags = TList->eflag;
DbgPrint(" QUERY thread: %p dr0: %d dr1 :%d dr2 :%d dr3 :%d dr6:%d dr7:%d", Thread, contex->Dr0, contex->Dr1, contex->Dr2, contex->Dr3, contex->Dr6, contex->Dr7);
}
}
}
}
return status;
}
return status;
//PspWow64GetContextThreadOnAmd64_0 ETHREAD/ MODE/ THREADINFORMATION
}*/
NTSTATUS __fastcall myNtQueryInformationThread(IN HANDLE ThreadHandle,
IN THREADINFOCLASS ThreadInformationClass,
OUT PVOID ThreadInformation,
IN ULONG ThreadInformationLength,
OUT PULONG ReturnLength OPTIONAL
){
//VMProtectBegin("myNtQueryInformationThread");
if (VtMode)
{
PPAGE_HOOK_ENTRY pEntry = PHGetHookEntry(NtQueryInformationThread);
ori_pslp19 = pEntry->OriginalData;
}
PEPROCESS_S Process=NULL;
PETHREAD Thread = NULL;
PPROCESS_List PlIST = NULL;;
PTHREAD_dr_List TList = NULL;
p_save_handlentry Padd = NULL;
NTSTATUS Sstatus;
PWOW64_CONTEXT contex = ThreadInformation;
NTSTATUS status;
orgNtQueryInformationThread = ori_pslp19;
//orgNtQueryInformationThread = NtQueryInformationThread;
Padd = querylist(PmainList, PsGetCurrentProcessId(), PsGetCurrentProcess());
status=orgNtQueryInformationThread(ThreadHandle, ThreadInformationClass, ThreadInformation, ThreadInformationLength, ReturnLength);
if (Padd!=NULL)
{
return status;
}
if (ThreadInformationClass == ThreadWow64Context && ThreadInformationLength == 0x2CC && ThreadInformation != NULL && ThreadHandle!=NULL)//ThreadWow64Context
{
Sstatus = ObReferenceObjectByHandle(ThreadHandle,
THREAD_ALL_ACCESS,
*PsThreadType,
KernelMode,
(PVOID*)&Thread,
NULL);
if (!NT_SUCCESS(Sstatus)){
return status;
}
ObDereferenceObject(Thread);
if (NT_SUCCESS(Sstatus)){
Process = IoThreadToProcess(Thread);
if (Process != NULL){
PlIST = Dr_FindProcessList(Process);
if (PlIST!=NULL)
{
TList = Dr_FindThreadContextByThreadList(PlIST, Thread);
if (TList!=NULL)
{
contex->Dr0 = TList->Dr0;
contex->Dr1 = TList->Dr1;
contex->Dr2 = TList->Dr2;
contex->Dr3 = TList->Dr3;
contex->Dr6 = TList->Dr6;
contex->Dr7 = TList->Dr7;
contex->EFlags = TList->eflag;
}
else
{
contex->Dr0 = NULL;
contex->Dr1 = NULL;
contex->Dr2 = NULL;
contex->Dr3 = NULL;
contex->Dr6 = NULL;
contex->Dr7 = NULL;
contex->EFlags &= ~0x100;
}
}
}
}
return status;
}
return status;
// VMProtectEnd();
//PspWow64GetContextThreadOnAmd64_0 ETHREAD/ MODE/ THREADINFORMATION
}
void *__fastcall myRtlpCopyLegacyContextX86(BOOLEAN islegacy, PWOW64_CONTEXT destcontex, ULONG nouse_falg, PWOW64_CONTEXT srccontext)
{
//VMProtectBegin("myRtlpCopyLegacyContextX86");
void *result = 0x10020;;
PETHREAD Thread = NULL;
PEPROCESS Process = NULL;
PPROCESS_List PlIST = NULL;
PTHREAD_dr_List TList = NULL;
Thread = PsGetCurrentThread();
if (islegacy)
{
destcontex->ContextFlags = srccontext->ContextFlags;
//dr
//////