-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathmain.cpp
More file actions
1787 lines (1473 loc) · 42.2 KB
/
main.cpp
File metadata and controls
1787 lines (1473 loc) · 42.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
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 <ntifs.h>
#include <ntddmou.h>
#include <kbdmou.h>
#include <intrin.h>
#include "img.h"
//
// hvci doesn't support LIDT instruction, unlucky
//
#define HVCI 1
#pragma warning (disable: 4201)
#pragma warning (disable: 4996)
#include "ia32.hpp"
extern "C"
{
int _fltused;
}
//
// features:
// - exception hook
// - mouse hook
// - syscall hook (https://github.com/everdox/InfinityHook)
// - SwapContext hook
// tested Win11 22H3 + Win10 22H2 HVCI/Core Isolation enabled
//
#define POOLTAG (DWORD)'ACEC'
typedef ULONG_PTR QWORD;
typedef unsigned __int32 DWORD;
typedef unsigned __int16 WORD;
typedef unsigned __int8 BYTE;
typedef union _virt_addr_t
{
QWORD value;
struct
{
QWORD offset : 12;
QWORD pt_index : 9;
QWORD pd_index : 9;
QWORD pdpt_index : 9;
QWORD pml4_index : 9;
QWORD reserved : 16;
};
} virt_addr_t, * pvirt_addr_t;
#define LOG(...) DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[driver.sys] " __VA_ARGS__)
typedef struct
{
QWORD base, size;
} MODULE_INFO;
MODULE_INFO get_module_info(PWCH module_name);
PWCH get_module_name(QWORD address);
void NtSleep(DWORD milliseconds)
{
QWORD ms = milliseconds;
ms = (ms * 1000) * 10;
ms = ms * -1;
#ifdef _KERNEL_MODE
KeDelayExecutionThread(KernelMode, 0, (PLARGE_INTEGER)&ms);
#else
NtDelayExecution(0, (PLARGE_INTEGER)&ms);
#endif
}
namespace globals
{
MODULE_INFO ntoskrnl;
MODULE_INFO vmusbmouse;
DWORD exit = 0;
}
namespace hooks
{
BOOLEAN install(void);
void uninstall(void);
namespace efi
{
QWORD(*oGetVariable)(PCWCH VariableName, GUID* VendorGuid, DWORD* Attributes, QWORD* DataSize, VOID* Data);
QWORD(*oSetVariable)(PCWCH VariableName, GUID* VendorGuid, DWORD Attributes, QWORD DataSize, VOID* Data);
QWORD NtSetSystemEnvironmentValueEx;
QWORD NtQuerySystemEnvironmentValueEx;
NTSTATUS NTAPI NtQuerySystemEnvironmentValueExHook(
PUNICODE_STRING VariableName,
LPGUID VendorGuid,
PVOID Value,
PULONG ValueLength,
PULONG Attributes
);
NTSTATUS NTAPI NtSetSystemEnvironmentValueExHook(
PUNICODE_STRING VariableName,
LPGUID VendorGuid,
PVOID Value,
ULONG ValueLength,
ULONG Attributes
);
}
namespace syscall
{
QWORD SystemCallEntryPage;
void __fastcall SyscallStub(unsigned int SyscallIndex, void** SyscallFunction);
QWORD(*oKeQueryPerformanceCounter)(QWORD rcx);
QWORD KeQueryPerformanceCounterHook(QWORD rcx);
}
namespace input
{
PDRIVER_OBJECT mouclass;
PDRIVER_OBJECT mouhid;
QWORD apc_offset;
PDEVICE_OBJECT get_mouse_by_unit(WORD unit_id);
NTSTATUS mouse_apc(void* a1, void* a2, void* a3, void* a4, void* a5);
NTSTATUS (*rimInputApc)(void* a1, void* a2, void* a3, void* a4, void* a5);
NTSTATUS (*oMouseClassRead)(PDEVICE_OBJECT device, PIRP irp);
NTSTATUS MouseClassReadHook(PDEVICE_OBJECT device, PIRP irp);
}
namespace exception
{
QWORD KdpDebugRoutineSelect;
QWORD PoHiderInProgress;
void (*oKdTrap)(void);
void KdTrapHook();
}
namespace swap_ctx
{
UCHAR(*oHalClearLastBranchRecordStack)(void);
UCHAR HalClearLastBranchRecordStackHook();
}
}
enum class TRACE_OPERATION
{
TRACE_START,
TRACE_SYSCALL,
TRACE_END
};
NTSTATUS ApplyTraceSettings(_In_ TRACE_OPERATION Operation);
QWORD FindPattern(QWORD base, unsigned char* pattern, unsigned char* mask);
PDRIVER_OBJECT get_driver_object(PCWSTR driver_name);
extern "C" VOID DriverUnload(
_In_ struct _DRIVER_OBJECT* DriverObject
)
{
UNREFERENCED_PARAMETER(DriverObject);
globals::exit = 1;
while (globals::exit != 2)
{
NtSleep(100);
LOG("Please move mouse in order to unload the driver\n");
}
hooks::uninstall();
NtSleep(200);
LOG("Shutdown\n");
}
typedef struct _USB_DEVICE_DESCRIPTOR {
UCHAR bLength;
UCHAR bDescriptorType;
USHORT bcdUSB;
UCHAR bDeviceClass;
UCHAR bDeviceSubClass;
UCHAR bDeviceProtocol;
UCHAR bMaxPacketSize0;
USHORT idVendor;
USHORT idProduct;
USHORT bcdDevice;
UCHAR iManufacturer;
UCHAR iProduct;
UCHAR iSerialNumber;
UCHAR bNumConfigurations;
} USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESCRIPTOR;
typedef struct _USBD_INTERFACE_INFORMATION {
USHORT Length;
UCHAR InterfaceNumber;
UCHAR AlternateSetting;
UCHAR Class;
UCHAR SubClass;
UCHAR Protocol;
UCHAR Reserved;
PVOID InterfaceHandle;
ULONG NumberOfPipes;
PVOID Pipes[1];
} USBD_INTERFACE_INFORMATION, *PUSBD_INTERFACE_INFORMATION;
extern "C" NTSTATUS DriverEntry(
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
)
{
UNREFERENCED_PARAMETER(RegistryPath);
UNREFERENCED_PARAMETER(DriverObject);
globals::ntoskrnl = get_module_info(0);
globals::vmusbmouse = get_module_info(L"vmusbmouse.sys");
if (!hooks::install())
{
LOG("failed to install hooks\n");
return STATUS_ENTRYPOINT_NOT_FOUND;
}
LOG("Running\n");
DriverObject->DriverUnload = DriverUnload;
return STATUS_SUCCESS;
}
inline PVOID get_virtual_address(QWORD physical_address)
{
return MmGetVirtualForPhysical( *(PHYSICAL_ADDRESS*)&physical_address ) ;
}
inline QWORD get_physical_address(PVOID virtual_address)
{
return MmGetPhysicalAddress( virtual_address ).QuadPart;
}
namespace pagetable
{
PVOID duplicate(QWORD cr3, PVOID *pml4, PVOID *pdpt, PVOID *pde);
void free(PVOID clone_cr3_virt);
}
//
// interrupt work done by Erik3000
// https://www.unknowncheats.me/forum/anti-cheat-bypass/658736-universal-ac-bypass.html
//
namespace interrupts
{
PVOID create_table(void);
void hook(PVOID idt_table, int index, PVOID handler);
void enable_table(PVOID idt_table);
void disable_table(void);
PVOID get_original_address(int index);
PVOID get_current_address(int index);
void unhook(PVOID idt_table, int index);
void free(PVOID clone_interrupt_table);
}
#define GET_BIT(data, bit) ((data >> bit) & 1)
extern "C"
{
PVOID nmi_handler_original, pagefault_handler_original;
DWORD idt_nmi_count = 0, idt_pf_count=0;
}
extern "C" void asm_nmi_handler(void);
extern "C" void asm_pagefault_handler(void);
extern "C" void __fastcall nmi_handler()
{
nmi_handler_original = interrupts::get_original_address(exception_vector::nmi);
QWORD current_thread = __readgsqword(0x188);
QWORD current_process = *(QWORD*)(current_thread + 0xB8);
QWORD process_cr3 = *(QWORD*)(current_process + 0x28);
if (__readcr3() != process_cr3)
{
__writecr3((QWORD)process_cr3);
QWORD cr4 = __readcr4();
__writecr4(cr4 ^ 0x80);
__writecr4(cr4);
}
interrupts::disable_table();
idt_nmi_count++;
}
extern "C" QWORD __fastcall pagefault_handler(QWORD error_code)
{
pagefault_handler_original = interrupts::get_original_address(exception_vector::page_fault);
//
// 1: continue execution normally
// 0: let windows page fault handle it
//
QWORD pagefault_handled = 0;
QWORD current_thread = __readgsqword(0x188);
QWORD current_process = *(QWORD*)(current_thread + 0xB8);
QWORD process_cr3 = *(QWORD*)(current_process + 0x28);
if (!GET_BIT(error_code, 4)) // nx???
{
goto E0;
}
QWORD fault_address = __readcr2();
QWORD MmPteBase = *(QWORD*)((QWORD)MmGetVirtualForPhysical + 0x20 + 0x02);
QWORD pte_address = MmPteBase + ((fault_address >> 9) & 0x7FFFFFFFF8);
QWORD pde_address = MmPteBase + ((pte_address >> 9) & 0x7FFFFFFFF8);
// QWORD pdpt_address = MmPteBase + ((pde_address >> 9) & 0x7FFFFFFFF8);
// QWORD pml4_address = MmPteBase + ((pdpt_address >> 9) & 0x7FFFFFFFF8);
((pde_64*)pde_address)->execute_disable = 0;
((pde_64*)pde_address)->accessed = 1;
//
// our PF handler did it
//
pagefault_handled = 1;
idt_pf_count++;
E0:
if (__readcr3() != process_cr3)
{
__writecr3((QWORD)process_cr3);
QWORD cr4 = __readcr4();
__writecr4(cr4 ^ 0x80);
__writecr4(cr4);
}
interrupts::disable_table();
return pagefault_handled;
}
#ifdef HVCI
NTSTATUS NTAPI hooks::efi::NtQuerySystemEnvironmentValueExHook(
PUNICODE_STRING VariableName,
LPGUID VendorGuid,
PVOID Value,
PULONG ValueLength,
PULONG Attributes
)
{
if (VariableName == 0 || VendorGuid == 0)
{
return STATUS_INVALID_PARAMETER_1;
}
QWORD status = oGetVariable(VariableName->Buffer, VendorGuid, (DWORD*)Attributes, (QWORD*)ValueLength, Value);
return status == 0 ? STATUS_SUCCESS : STATUS_INVALID_PARAMETER;
}
#else
NTSTATUS NTAPI hooks::efi::NtQuerySystemEnvironmentValueExHook(
PUNICODE_STRING VariableName,
LPGUID VendorGuid,
PVOID Value,
PULONG ValueLength,
PULONG Attributes
)
{
if (VariableName == 0 || VendorGuid == 0)
{
return STATUS_INVALID_PARAMETER_1;
}
pml4e_64* pml4;
pdpte_64* pdpt;
pde_64* pde;
QWORD process_cr3 = __readcr3();
PVOID clone_cr3_virt = pagetable::duplicate(process_cr3, (PVOID*)&pml4, (PVOID*)&pdpt, (PVOID*)&pde);
QWORD clone_cr3_phys = get_physical_address(clone_cr3_virt);
virt_addr_t ntoskrnl = *(virt_addr_t*)&globals::ntoskrnl.base;
DWORD nmi_count = idt_nmi_count;
//
// trap ntoskrnl.exe range
//
for (int i = 0; i < 512; i++)
{
virt_addr_t addr{};
addr.pml4_index = ntoskrnl.pml4_index;
addr.pdpt_index = ntoskrnl.pdpt_index;
addr.pd_index = i;
addr.reserved = 0xFFFF;
if (addr.value >= globals::ntoskrnl.base &&
addr.value <= globals::ntoskrnl.base + globals::ntoskrnl.size)
{
pde[i].execute_disable = 1;
}
}
//
// hijack interrupts
//
PVOID idt = interrupts::create_table();
interrupts::hook(idt, exception_vector::nmi, asm_nmi_handler);
interrupts::hook(idt, exception_vector::page_fault, asm_pagefault_handler);
interrupts::enable_table(idt);
//
// swap to cached table
//
__writecr3((QWORD)clone_cr3_phys);
uint64_t cr4 = __readcr4();
__writecr4(cr4 ^ 0x80);
__writecr4(cr4);
//
// call efi GetVariable
//
QWORD status = oGetVariable(VariableName->Buffer, VendorGuid, (DWORD*)Attributes, (QWORD*)ValueLength, Value);
//
// swap to original table
//
if (__readcr3() != process_cr3)
{
__writecr3((QWORD)process_cr3);
cr4 = __readcr4();
__writecr4(cr4 ^ 0x80);
__writecr4(cr4);
}
//
// unhook interrupts
//
interrupts::disable_table();
interrupts::free(idt);
//
// check accesses
//
if (nmi_count == idt_nmi_count)
{
for (int i = 0; i < 512; i++)
{
if (!pde[i].accessed)
{
continue;
}
virt_addr_t addr{};
addr.pml4_index = ntoskrnl.pml4_index;
addr.pdpt_index = ntoskrnl.pdpt_index;
addr.pd_index = i;
addr.reserved = 0xFFFF;
if (addr.value >= globals::ntoskrnl.base &&
addr.value <= globals::ntoskrnl.base + globals::ntoskrnl.size)
{
LOG("[GetVariableHook detected] ntoskrnl.exe 0x%llx\n", addr.value);
}
}
}
//
// free memory
//
pagetable::free(clone_cr3_virt);
return status == 0 ? STATUS_SUCCESS : STATUS_INVALID_PARAMETER;
}
#endif
#ifdef HVCI
NTSTATUS NTAPI hooks::efi::NtSetSystemEnvironmentValueExHook(
PUNICODE_STRING VariableName,
LPGUID VendorGuid,
PVOID Value,
ULONG ValueLength,
ULONG Attributes
)
{
if (VariableName == 0 || VendorGuid == 0)
{
return STATUS_INVALID_PARAMETER_1;
}
QWORD status = oSetVariable(VariableName->Buffer, VendorGuid, Attributes, ValueLength, Value);
return status == 0 ? STATUS_SUCCESS : STATUS_INVALID_PARAMETER;
}
#else
NTSTATUS NTAPI hooks::efi::NtSetSystemEnvironmentValueExHook(
PUNICODE_STRING VariableName,
LPGUID VendorGuid,
PVOID Value,
ULONG ValueLength,
ULONG Attributes
)
{
if (VariableName == 0 || VendorGuid == 0)
{
return STATUS_INVALID_PARAMETER_1;
}
pml4e_64* pml4;
pdpte_64* pdpt;
pde_64* pde;
QWORD process_cr3 = __readcr3();
PVOID clone_cr3_virt = pagetable::duplicate(process_cr3, (PVOID*)&pml4, (PVOID*)&pdpt, (PVOID*)&pde);
QWORD clone_cr3_phys = get_physical_address(clone_cr3_virt);
virt_addr_t ntoskrnl = *(virt_addr_t*)&globals::ntoskrnl.base;
DWORD nmi_count = idt_nmi_count;
//
// trap ntoskrnl.exe range
//
for (int i = 0; i < 512; i++)
{
virt_addr_t addr{};
addr.pml4_index = ntoskrnl.pml4_index;
addr.pdpt_index = ntoskrnl.pdpt_index;
addr.pd_index = i;
addr.reserved = 0xFFFF;
if (addr.value >= globals::ntoskrnl.base &&
addr.value <= globals::ntoskrnl.base + globals::ntoskrnl.size)
{
pde[i].execute_disable = 1;
}
}
//
// hijack interrupts
//
PVOID idt = interrupts::create_table();
interrupts::hook(idt, exception_vector::nmi, asm_nmi_handler);
interrupts::hook(idt, exception_vector::page_fault, asm_pagefault_handler);
interrupts::enable_table(idt);
//
// swap to cached table
//
__writecr3((QWORD)clone_cr3_phys);
uint64_t cr4 = __readcr4();
__writecr4(cr4 ^ 0x80);
__writecr4(cr4);
//
// call efi SetVariable
//
QWORD status = oSetVariable(VariableName->Buffer, VendorGuid, Attributes, ValueLength, Value);
//
// swap to original table
//
if (__readcr3() != process_cr3)
{
__writecr3((QWORD)process_cr3);
cr4 = __readcr4();
__writecr4(cr4 ^ 0x80);
__writecr4(cr4);
}
//
// unhook interrupts
//
interrupts::disable_table();
interrupts::free(idt);
//
// check accesses
//
if (nmi_count == idt_nmi_count)
{
for (int i = 0; i < 512; i++)
{
if (!pde[i].accessed)
{
continue;
}
virt_addr_t addr{};
addr.pml4_index = ntoskrnl.pml4_index;
addr.pdpt_index = ntoskrnl.pdpt_index;
addr.pd_index = i;
addr.reserved = 0xFFFF;
if (addr.value >= globals::ntoskrnl.base &&
addr.value <= globals::ntoskrnl.base + globals::ntoskrnl.size)
{
LOG("[SetVariableHook detected] ntoskrnl.exe 0x%llx\n", addr.value);
}
}
}
//
// free memory
//
pagetable::free(clone_cr3_virt);
return status == 0 ? STATUS_SUCCESS : STATUS_INVALID_PARAMETER;
}
#endif
void __fastcall hooks::syscall::SyscallStub(unsigned int SyscallIndex, void** SyscallFunction)
{
UNREFERENCED_PARAMETER(SyscallIndex);
UNREFERENCED_PARAMETER(SyscallFunction);
if (*SyscallFunction == (void*)efi::NtQuerySystemEnvironmentValueEx)
{
*SyscallFunction = efi::NtQuerySystemEnvironmentValueExHook;
}
else if (*SyscallFunction == (void*)efi::NtSetSystemEnvironmentValueEx)
{
*SyscallFunction = efi::NtSetSystemEnvironmentValueExHook;
}
}
QWORD hooks::syscall::KeQueryPerformanceCounterHook(QWORD rcx)
{
if (ExGetPreviousMode() == KernelMode)
{
return oKeQueryPerformanceCounter(rcx);
}
QWORD current_thread = (QWORD)PsGetCurrentThread();
DWORD syscall_index = *(DWORD*)(current_thread + 0x80);
if (KeGetCurrentIrql() != PASSIVE_LEVEL)
{
return oKeQueryPerformanceCounter(rcx);
}
PVOID* StackMax = (PVOID*)__readgsqword(0x1A8);
PVOID* StackFrame = (PVOID*)_AddressOfReturnAddress();
for (PVOID* StackCurrent = StackMax;
StackCurrent > StackFrame;
--StackCurrent)
{
PULONG AsUlong = (PULONG)StackCurrent;
if (*AsUlong != ((ULONG)0x501802))
{
continue;
}
//
// If the first magic is set, check for the second magic.
//
--StackCurrent;
PUSHORT AsShort = (PUSHORT)StackCurrent;
if (*AsShort != ((USHORT)0xF33))
{
continue;
}
//
// Now we reverse the direction of the stack walk.
//
int index = 0;
for (;
StackCurrent < StackMax;
++StackCurrent)
{
PULONGLONG AsUlonglong = (PULONGLONG)StackCurrent;
index++;
if (index > 8)
{
break;
}
if (*AsUlonglong < syscall::SystemCallEntryPage ||
*AsUlonglong > syscall::SystemCallEntryPage + 0x1000
)
{
continue;
}
void** syscall = &StackCurrent[9];
SyscallStub(syscall_index, syscall);
break;
}
break;
}
return oKeQueryPerformanceCounter(rcx);
}
PDEVICE_OBJECT hooks::input::get_mouse_by_unit(WORD unit_id)
{
PDEVICE_OBJECT hid_device_object = mouhid->DeviceObject;
while (hid_device_object)
{
QWORD ext = (QWORD)hid_device_object->DeviceExtension;
if (*(WORD*)(ext + 36) == unit_id)
{
return hid_device_object;
}
hid_device_object = hid_device_object->NextDevice;
}
return 0;
}
typedef struct {
QWORD device_object;
QWORD timestamp;
} DEVICE_INFO;
QWORD SDL_GetTicksNS(void);
double ns_to_herz(double ns) { return 1.0 / (ns / 1e9); }
//
// https:://github.com/everdox/hidinput
//
NTSTATUS hooks::input::mouse_apc(void* a1, void* a2, void* a3, void* a4, void* a5)
{
//
// tested win11 23h2 / win10 22h2
//
if (!apc_offset)
{
QWORD tmp = (QWORD)a2 + 0x10;
while (*(QWORD*)(tmp) != 0x200000000) tmp ++;
tmp += 0x08;
apc_offset = tmp - (QWORD)a1;
}
PMOUSE_INPUT_DATA input = (PMOUSE_INPUT_DATA)((QWORD)a1 + apc_offset);
PDEVICE_OBJECT mouse = get_mouse_by_unit(input->UnitId);
if (!mouse)
{
LOG("invalid UnitID, timestamp: %lld\n", SDL_GetTicksNS());
input->ButtonFlags = 0;
input->LastX = 0;
input->LastY = 0;
return rimInputApc(a1, a2, a3, a4, a5);
}
//
// check hidusb
//
PDEVICE_OBJECT hidusb = mouse->DeviceObjectExtension->AttachedTo;
QWORD ext = *(QWORD *)((QWORD)hidusb->DeviceExtension + 16LL);
PUSB_DEVICE_DESCRIPTOR desc = (PUSB_DEVICE_DESCRIPTOR) * (QWORD*)(ext + 0x08);
PUSBD_INTERFACE_INFORMATION intf = (PUSBD_INTERFACE_INFORMATION) * (QWORD*)(ext + 0x10);
//
// Class:
// 3: hid
//
// Subclass:
// 0: none
// 1: Boot
//
// Protocol:
// 0: none
// 1: keyboard
// 2: mouse
//
//
// Real mouse should be:
// HID, Boot, Mouse (3,1,2)
//
// KMBOX:
// HID, NONE, NONE (3,0,0)
//
// https://github.com/torvalds/linux/blob/master/drivers/hid/usbhid/usbmouse.c#L217
//
//
if (intf->Class == 3 && intf->SubClass == 0 && intf->Protocol == 0)
{
LOG("potential kmbox (%lx:%lx) [%d:%d:%d]\n", desc->idVendor, desc->idProduct, intf->Class, intf->SubClass, intf->Protocol);
}
QWORD hid_extension = (QWORD)mouse->DeviceExtension;
//
// check if there is invalid packets in queue
//
for (int i = sizeof(MOUSE_INPUT_DATA); i--;)
{
if (((unsigned char*)hid_extension + 0x160)[i] != ((unsigned char*)input)[i])
{
LOG("invalid mouse packet detected, timestamp: %lld\n", SDL_GetTicksNS());
input->ButtonFlags = 0;
input->LastX = 0;
input->LastY = 0;
return rimInputApc(a1, a2, a3, a4, a5);
}
}
//
// did someone inject empty :D
//
// steelseries mouse driver can cause false positive
//
/*
BOOLEAN empty = 1;
for (int i = 4; i < sizeof(MOUSE_INPUT_DATA); i++)
{
if (((BYTE*)input)[i] != 0)
{
empty = 0;
break;
}
}
if (empty)
{
DbgPrintEx(77, 0, "empty mouse packet detected\n");
}
*/
//
// do not ever write code like this. should be good enough for demo
//
// ---------------------------------------------------------------------------------
static DEVICE_INFO dev{};
if (dev.device_object == 0 || dev.device_object != (QWORD)mouse)
{
dev.device_object = (QWORD)mouse;
dev.timestamp = SDL_GetTicksNS();
return rimInputApc(a1, a2, a3, a4, a5);
}
QWORD timestamp = SDL_GetTicksNS();
if (timestamp - dev.timestamp < 117647) // if latency is less than 117647 ns (8500 Hz)
{
//
// https://www.unitjuggler.com/convert-frequency-from-Hz-to-ns(p).html?val=8500
//
LOG("Device: 0x%llx, timestamp: %lld, hz: [%lld]\n",
(QWORD)mouse, timestamp, (QWORD)ns_to_herz( (double)(timestamp - dev.timestamp) ));
}
dev.timestamp = timestamp;
//
// ---------------------------------------------------------------------------------
//
return rimInputApc(a1, a2, a3, a4, a5);
}
//
// https:://github.com/everdox/hidinput
//
NTSTATUS hooks::input::MouseClassReadHook(PDEVICE_OBJECT device, PIRP irp)
{
QWORD* routine;
routine = (QWORD*)irp;
routine += 0xb;
if (rimInputApc == 0)
{
*(QWORD*)&rimInputApc = *routine;
}
if (globals::exit == 0)
{
*routine = (ULONGLONG)mouse_apc;
}
else
{
// safe to close signal
globals::exit = 2;
}
return hooks::input::oMouseClassRead(device, irp);
}
extern "C" __declspec(dllimport) PCSTR PsGetProcessImageFileName(QWORD process);
void __fastcall hooks::exception::KdTrapHook(void)
{
//
// disable upcoming DPC call
//
*(BYTE*)(exception::PoHiderInProgress) = 1;
QWORD KeBugCheckExPtr = (QWORD)KeBugCheckEx;
KeBugCheckExPtr = KeBugCheckExPtr + 0x23;
KeBugCheckExPtr = KeBugCheckExPtr + 0x03;
DWORD ctx_offset = *(DWORD*)KeBugCheckExPtr;
struct _CONTEXT* current_context = *(struct _CONTEXT**)(__readgsqword(0x20) + ctx_offset);
//
// check if exception was caught
//
if (current_context == 0 || current_context->Rip == 0)
{
return oKdTrap();
}
//
// skipping usermode exceptions
//
/*
if (current_context->Rip <= 0x7FFFFFFFFFFF)
{
return oKdTrap();
}
*/
/*
QWORD thread = __readgsqword(0x188);
QWORD process = *(QWORD*)(thread + 0xB8);
QWORD thread_process = *(QWORD*)(thread + 0x98 + 0x20);
//
// exception was caught
//
LOG("[%s:%s][%llX][%llX] exception was caught\n",
PsGetProcessImageFileName(process),
PsGetProcessImageFileName(thread_process),
__readcr3(),
current_context->Rip
);*/
return oKdTrap();
}
BOOLEAN is_unlinked_thread(QWORD process, QWORD thread)
{
PLIST_ENTRY list_head = (PLIST_ENTRY)((QWORD)process + 0x30);
PLIST_ENTRY list_entry = list_head;
QWORD entry = (QWORD)((char*)list_entry - 0x2f8);
if (entry == thread)
{
return 0;
}
while ((list_entry = list_entry->Flink) != 0 && list_entry != list_head)
{
entry = (QWORD)((char*)list_entry - 0x2f8);
if (entry == thread)
{
return 0;
}
}
return 1;
}
BOOLEAN unlink_thread_detection(QWORD thread)
{
if (thread == 0)
return 0;
QWORD host_process = *(QWORD*)(thread + 0x220);
QWORD lookup_thread;
if (NT_SUCCESS(PsLookupThreadByThreadId(
(HANDLE)PsGetThreadId((PETHREAD)thread),
(PETHREAD*)&lookup_thread
)))
{
ObfDereferenceObject((PVOID)lookup_thread);
if (lookup_thread == thread)
{
return 0;
}
}