-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathHelperFunc.c
2157 lines (1817 loc) · 57 KB
/
HelperFunc.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
/*++
Copyright (c) Bombs
Module Name:
HelperFunc.c
Author :Bombs
Time :2014-4-29 16:44:25
Abstract:
This file contains some useful functions used by other modules of the driver
--*/
#include <ntifs.h>
#include "HelperFunc.h"
#include "DetourFunc.h"
#include <ntimage.h>
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, ProcCreateNotify)
#pragma alloc_text(PAGE, SSDTHookInit)
#pragma alloc_text(PAGE, HookSSDT)
#pragma alloc_text(PAGE, UnhookSSDT)
#pragma alloc_text(PAGE, GetFunctionIndex)
#pragma alloc_text(PAGE, GetOrigFunctionAddr)
#endif
//
// global variable for hook
//
DWORD g_dwUserPostMessageIndex;
DWORD g_dwOldUserPostMessageAddr;
PF_UserPostMessage pfOrigUserPostMessage;
PF_UserPostMessage pfMyUserPostMessage;
DWORD g_dwSetInformationThreadIndex;
DWORD g_dwOldSetInformationThreadAddr;
PF_SetInformationThread pfOrigSetInformationThread;
PF_SetInformationThread pfMySetInformationThread;
DWORD g_dwUserBuildHwndListWin8Index;
DWORD g_dwOldUserBuildHwndListWin8Addr;
PF_UserBuildHwndListWin8 pfOrigUserBuildHwndListWin8;
PF_UserBuildHwndListWin8 pfMyUserBuildHwndListWin8;
DWORD g_dwCloseIndex;
DWORD g_dwOldCloseAddr;
PF_Close pfOrigClose;
PF_Close pfMyClose;
DWORD g_dwUserSetParentIndex;
DWORD g_dwOldUserSetParentAddr;
PF_UserSetParent pfOrigUserSetParent;
PF_UserSetParent pfMyUserSetParent;
DWORD g_dwUserFindWindowExIndex;
DWORD g_dwOldUserFindWindowExAddr;
PF_UserFindWindowEx pfOrigUserFindWindowEx;
PF_UserFindWindowEx pfMyUserFindWindowEx;
DWORD g_dwDuplicateObjectIndex;
DWORD g_dwOldDuplicateObjectAddr;
PF_DuplicateObject pfOrigDuplicateObject;
PF_DuplicateObject pfMyDuplicateObject;
DWORD g_dwCreateUserProcessIndex;
DWORD g_dwOldCreateUserProcessAddr;
PF_CreateUserProcess pfOrigCreateUserProcess;
PF_CreateUserProcess pfMyCreateUserProcess;
DWORD g_dwQueryObjectIndex;
DWORD g_dwOldQueryObjectAddr;
PF_QueryObject pfOrigQueryObject;
PF_QueryObject pfMyQueryObject;
DWORD g_dwQuerySystemInformationIndex;
DWORD g_dwOldQuerySystemInformationAddr;
PF_QuerySystemInformation pfOrigQuerySystemInformation;
PF_QuerySystemInformation pfMyQuerySystemInformation;
DWORD g_dwYieldExecutionIndex;
DWORD g_dwOldYieldExecutionAddr;
PF_YieldExecution pfOrigYieldExecution;
PF_YieldExecution pfMyYieldExecution;
DWORD g_dwCreateProcessIndex;
DWORD g_dwOldCreateProcessAddr;
PF_CreateProcess pfOrigCreateProcess;
PF_CreateProcess pfMyCreateProcess;
DWORD g_dwOpenProcessIndex;
DWORD g_dwOldOpenProcessAddr;
PF_OpenProcess pfOrigOpenProcess;
PF_OpenProcess pfMyOpenProcess;
DWORD g_dwUserQueryWindowIndex;
DWORD g_dwOldUserQueryWindowAddr;
PF_UserQueryWindow pfOrigUserQueryWindow;
PF_UserQueryWindow pfMyUserQueryWindow;
DWORD g_dwOpenThreadIndex;
DWORD g_dwOldOpenThreadAddr;
PF_OpenThread pfOrigOpenThread;
PF_OpenThread pfMyOpenThread;
DWORD g_dwUserGetForegroundWindowIndex;
DWORD g_dwOldUserGetForegroundWindowAddr;
PF_UserGetForegroundWindow pfOrigUserGetForegroundWindow;
PF_UserGetForegroundWindow pfMyUserGetForegroundWindow;
DWORD g_dwSetContextThreadIndex;
DWORD g_dwOldSetContextThreadAddr;
PF_SetContextThread pfOrigSetContextThread;
PF_SetContextThread pfMySetContextThread;
DWORD g_dwCreateProcessExIndex;
DWORD g_dwOldCreateProcessExAddr;
PF_CreateProcessEx pfOrigCreateProcessEx;
PF_CreateProcessEx pfMyCreateProcessEx;
DWORD g_dwUserBuildHwndListIndex;
DWORD g_dwOldUserBuildHwndListAddr;
PF_UserBuildHwndList pfOrigUserBuildHwndList;
PF_UserBuildHwndList pfMyUserBuildHwndList;
DWORD g_dwQueryInformationProcessIndex;
DWORD g_dwOldQueryInformationProcessAddr;
PF_QueryInformationProcess pfOrigQueryInformationProcess;
PF_QueryInformationProcess pfMyQueryInformationProcess;
// not used
DWORD g_dwObjectTableOffsetInEProcess = 0;
DWORD g_dwExitTimeOffsetInEProcess = 0;
DWORD g_dwSmssProcId = 0;
PVOID g_Win32kBase = NULL;
ULONG g_ulWin32kSize = 0;
PVOID g_NtoskrnlBase = NULL;
ULONG g_ulNtoskrnlSize = 0;
PVOID g_pWin32kTable = NULL;
PVOID g_pNtoskrnlTable = NULL;
LARGE_INTEGER g_liDbgCtrlRegValue = {0};
PLARGE_INTEGER g_pliDbgCtrlReg = &g_liDbgCtrlRegValue;
ULONG g_ulOldTrap01 = 0;
char g_OldDpl = 0;
PVOID GetSystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass)
/*++
Routine Description:
Get system information by ZwQuerySystemInformaion
Return a pointer to corresponding structure
Note: you must free the pool pointed by the returned pointer
--*/
{
PVOID pRet = NULL;
ULONG ulRetLen = 0;
ULONG ulSysInfoLen = 4096;
NTSTATUS status;
do
{
ulSysInfoLen *= 2;
if(pRet != NULL)
{
ExFreePoolWithTag(pRet, TAG_STRONG_OD);
}
pRet = ExAllocatePoolWithTag(PagedPool, ulSysInfoLen, TAG_STRONG_OD);
if(pRet == NULL)
{
break;
}
RtlZeroMemory(pRet, ulSysInfoLen);
status = ZwQuerySystemInformation(SystemInformationClass,
pRet,
ulSysInfoLen,
&ulRetLen);
} while (status == STATUS_INFO_LENGTH_MISMATCH);
if(status != STATUS_SUCCESS)
{
if(pRet != NULL)
{
ExFreePoolWithTag(pRet, TAG_STRONG_OD);
}
pRet = NULL;
}
return pRet;
}
HANDLE GetCsrssProcId()
/*++
Routine Description:
Get the proc id of csrss.exe
first try to get the id by "\\Windows\\ApiPort",
if failed, get the id by GetProcIdByName;
--*/
{
PSYSTEM_HANDLE_INFORMATION pSysHandleInfo;
HANDLE hProcId = 0;
ULONG i = 0;
OBJECT_ATTRIBUTES ObjAttr;
CLIENT_ID ClientId;
HANDLE hProcess, hObject;
UCHAR Buffer[0x100];
POBJECT_NAME_INFORMATION pObjNameInfo = (POBJECT_NAME_INFORMATION)Buffer;
pSysHandleInfo = (PSYSTEM_HANDLE_INFORMATION)GetSystemInformation(SystemHandleInformation);
if(pSysHandleInfo != NULL)
{
for(i = 0; i < pSysHandleInfo->NumberOfHandles; i++)
{
if(pSysHandleInfo->Handles[i].ObjectTypeIndex == 21) // [Warning:Original code in ida ObjectTypeIndex == 18]
{
InitializeObjectAttributes(&ObjAttr, NULL, OBJ_KERNEL_HANDLE,
NULL, NULL);
ClientId.UniqueProcess = (HANDLE)pSysHandleInfo->Handles[i].ProcessId;
ClientId.UniqueThread = 0;
if(NT_SUCCESS(ZwOpenProcess(&hProcess, PROCESS_DUP_HANDLE,
&ObjAttr, &ClientId)))
{
if(NT_SUCCESS(ZwDuplicateObject(hProcess, (HANDLE)pSysHandleInfo->Handles[i].HandleValue,
NtCurrentProcess(), &hObject, 0, 0, DUPLICATE_SAME_ACCESS)))
{
if(NT_SUCCESS(ZwQueryObject(hObject, 1/*ObjectNameInformation*/, pObjNameInfo, 0x100, NULL)))
{
if(pObjNameInfo->Name.Buffer != 0 &&
0 == wcsncmp(L"\\Windows\\ApiPort", pObjNameInfo->Name.Buffer, 20))
{
hProcId = (HANDLE)pSysHandleInfo->Handles[i].ProcessId;
KdPrint(("GetCsrssProcId: get csrss.exe proc id by LPC Port, proc id:%d\n",
hProcId));
}
}
ZwClose(hObject);
}
ZwClose(hProcess);
}
}
}
ExFreePool(pSysHandleInfo);
if(0 == hProcId)
{
hProcId = GetProcIdByName(L"csrss.exe");
}
}
return hProcId;
}
HANDLE GetProcIdByName(wchar_t * szProcName)
/*++
Routine Description:
Get Process id by GetSystemInformation(SystemProcessesAndThreadsInformation)
--*/
{
HANDLE hProcId = NULL;
UNICODE_STRING unProcName;
PSYSTEM_PROCANDTHREAD_INFORMATION pSysProcInfo = NULL;
PSYSTEM_PROCANDTHREAD_INFORMATION pTmp = NULL;
RtlInitUnicodeString(&unProcName, szProcName);
pSysProcInfo = (PSYSTEM_PROCANDTHREAD_INFORMATION)GetSystemInformation(SystemProcessesAndThreadsInformation);
if(pSysProcInfo != NULL)
{
pTmp = pSysProcInfo;
while(1) // [Warning: some differences with ida code]
{
if(0 == RtlCompareUnicodeString(&pTmp->ProcessName, &unProcName, TRUE))
{
hProcId = (HANDLE)pTmp->ProcessId;
break;
}
if(pTmp->NextEntryDelta == 0)
{
break;
}
pTmp = (PSYSTEM_PROCANDTHREAD_INFORMATION)((char *)pTmp + pTmp->NextEntryDelta);
}
ExFreePoolWithTag(pSysProcInfo, TAG_STRONG_OD);
}
return hProcId;
}
PVOID GetServiceDescriptorTableShadowAddr()
/*++
Routine Description:
The first service_table_descriptor of KeServiceDescriptorTable
is the same with that of KeServiceDescriptorTableShadow, and the
unducumented routine KeAddSystemServiceTalbe references both of them.
so we can search the memory of KeAddSystemServiceTable to get the
correct addr of KeServiceDescriptorTableShadow.
note: use an exception handler when searching memory
--*/
{
int i = 0;
ULONG Addr = 0;
PVOID pRet = NULL;
while(i < 0x1000)
{
__try
{
Addr = *(PULONG)((PUCHAR)KeAddSystemServiceTable + i);
if(MmIsAddressValid((PVOID)Addr) &&
Addr != (ULONG)KeServiceDescriptorTable &&
0 == memcmp((PVOID)Addr, KeServiceDescriptorTable, 16))
{
pRet = (PVOID)Addr;
break;
}
}__except(EXCEPTION_EXECUTE_HANDLER)
{
pRet = NULL;
}
i++;
}
return pRet;
}
VOID ProcCreateNotify(
IN HANDLE ParentId,
IN HANDLE ProcessId,
IN BOOLEAN Create
)
/*++
Routine Description:
Process create notify callback, del proc from proc list
--*/
{
NTSTATUS status;
PEPROCESS pEProcess;
if(!Create)
{
status = PsLookupProcessByProcessId(ProcessId, &pEProcess);
if(NT_SUCCESS(status))
{
if(IsProcessInList(pEProcess, 0, SOD_WHITE_PROCESS))
{
DelProcInfoFromList(pEProcess, 0, SOD_WHITE_PROCESS);
if(0 == GetProcCount(SOD_WHITE_PROCESS))
{
UnhookSSDT();
}
}
if(IsProcessInList(pEProcess, 0, SOD_BLACK_PROCESS))
{
DelProcInfoFromList(pEProcess, 0, SOD_BLACK_PROCESS);
if(0 == GetProcCount(SOD_BLACK_PROCESS))
{
UnhookINT1();
}
}
ObDereferenceObject(pEProcess);
}
}
}
ULONG g_ServiceTableOffsetInKThread = 0;
VOID SSDTHookInit()
/*++
Routine Description:
init global variables, function index of ssdt table, original function address, etc.
--*/
{
NTSTATUS status;
PKTHREAD pThread = NULL;
int i = 0;
UNICODE_STRING unModuleName;
PEPROCESS pEprocessOfCsrss;
KAPC_STATE ApcState;
if(KeServiceDescriptorTable != NULL)
{
pThread = KeGetCurrentThread();
for(i = 0; i < 336; i++)
{
if(*(PULONG)((PUCHAR)pThread + i) == (ULONG)KeServiceDescriptorTable)
{
g_ServiceTableOffsetInKThread = i;
break;
}
}
}
RtlInitUnicodeString(&unModuleName, L"\\SystemRoot\\system32\\ntdll.dll");
g_dwSetContextThreadIndex = GetFunctionIndex("NtSetContextThread", &unModuleName);
pfMySetContextThread = MySetContextThread;
pfOrigSetContextThread = GetOrigFunctionAddr(g_dwSetContextThreadIndex, 0);
KdPrint(("SSDTHookInit:g_dwSetContextThreadIndex[%d], pfOrigSetContextThread[%08X]\n",
g_dwSetContextThreadIndex, pfOrigSetContextThread));
g_dwQuerySystemInformationIndex = GetFunctionIndex("NtQuerySystemInformation", &unModuleName);
pfMyQuerySystemInformation = MyQuerySystemInformation;
pfOrigQuerySystemInformation = GetOrigFunctionAddr(g_dwQuerySystemInformationIndex, 0);
KdPrint(("SSDTHookInit:g_dwQuerySystemInformationIndex[%d], pfOrigQuerySystemInformation[%08X]\n",
g_dwQuerySystemInformationIndex, pfOrigQuerySystemInformation));
g_dwQueryObjectIndex = GetFunctionIndex("NtQueryObject", &unModuleName);
pfMyQueryObject = MyQueryObject;
pfOrigQueryObject = GetOrigFunctionAddr(g_dwQueryObjectIndex, 0);
KdPrint(("SSDTHookInit:g_dwQueryObjectIndex[%d], pfOrigQueryObject[%08X]\n",
g_dwQueryObjectIndex, pfOrigQueryObject));
g_dwYieldExecutionIndex = GetFunctionIndex("NtYieldExecution", &unModuleName);
pfMyYieldExecution = MyYieldExecution;
pfOrigYieldExecution = GetOrigFunctionAddr(g_dwYieldExecutionIndex, 0);
KdPrint(("SSDTHookInit:g_dwYieldExecutionIndex[%d], pfOrigYieldExecution[%08X]\n",
g_dwYieldExecutionIndex, pfOrigYieldExecution));
g_dwOpenProcessIndex = GetFunctionIndex("NtOpenProcess", &unModuleName);
pfMyOpenProcess = MyOpenProcess;
pfOrigOpenProcess = GetOrigFunctionAddr(g_dwOpenProcessIndex, 0);
KdPrint(("SSDTHookInit:g_dwOpenProcessIndex[%d], pfOrigOpenProcess[%08X]\n",
g_dwOpenProcessIndex, pfOrigOpenProcess));
g_dwOpenThreadIndex = GetFunctionIndex("NtOpenThread", &unModuleName);
pfMyOpenThread = MyOpenThread;
pfOrigOpenThread = GetOrigFunctionAddr(g_dwOpenThreadIndex, 0);
KdPrint(("SSDTHookInit:g_dwOpenThreadIndex[%d], pfOrigOpenThread[%08X]\n",
g_dwOpenThreadIndex, pfOrigOpenThread));
g_dwSetInformationThreadIndex = GetFunctionIndex("NtSetInformationThread", &unModuleName);
pfMySetInformationThread = MySetInformationThread;
pfOrigSetInformationThread = GetOrigFunctionAddr(g_dwSetInformationThreadIndex, 0);
KdPrint(("SSDTHookInit:g_dwSetInformationThreadIndex[%d], pfOrigSetInformationThread[%08X]\n",
g_dwSetInformationThreadIndex, pfOrigSetInformationThread));
g_dwQueryInformationProcessIndex = GetFunctionIndex("NtQueryInformationProcess", &unModuleName);
pfMyQueryInformationProcess = MyQueryInformationProcess;
pfOrigQueryInformationProcess = GetOrigFunctionAddr(g_dwQueryInformationProcessIndex, 0);
KdPrint(("SSDTHookInit:g_dwQueryInformationProcessIndex[%d], pfOrigQueryInformationProcess[%08X]\n",
g_dwQueryInformationProcessIndex, pfOrigQueryInformationProcess));
g_dwCloseIndex = GetFunctionIndex("NtClose", &unModuleName);
pfMyClose = MyClose;
pfOrigClose = GetOrigFunctionAddr(g_dwCloseIndex, 0);
KdPrint(("SSDTHookInit:g_dwCloseIndex[%d], pfOrigClose[%08X]\n",
g_dwCloseIndex, pfOrigClose));
g_dwDuplicateObjectIndex = GetFunctionIndex("NtDuplicateObject", &unModuleName);
pfMyDuplicateObject = MyDuplicateObject;
pfOrigDuplicateObject = GetOrigFunctionAddr(g_dwDuplicateObjectIndex, 0);
KdPrint(("SSDTHookInit:g_dwDuplicateObjectIndex[%d], pfOrigDuplicateObject[%08X]\n",
g_dwDuplicateObjectIndex, pfOrigDuplicateObject));
if(g_ulMajorVer >= 6)
{
g_dwCreateUserProcessIndex = GetFunctionIndex("NtCreateUserProcess", &unModuleName);
pfMyCreateUserProcess = MyCreateUserProcess;
pfOrigCreateUserProcess = GetOrigFunctionAddr(g_dwCreateUserProcessIndex, 0);
}
else
{
g_dwCreateProcessIndex = GetFunctionIndex("NtCreateProcess", &unModuleName);
pfMyCreateProcess = MyCreateProcess;
pfOrigCreateUserProcess = GetOrigFunctionAddr(g_dwCreateProcessIndex, 0);
g_dwCreateProcessExIndex = GetFunctionIndex("NtCreateProcessEx", &unModuleName);
pfMyCreateProcessEx = MyCreateProcessEx;
pfOrigCreateProcessEx = GetOrigFunctionAddr(g_dwCreateProcessExIndex, 0);
}
pfMyUserPostMessage = MyUserPostMessage;
g_dwOldUserPostMessageAddr = 0;
g_dwUserPostMessageIndex = -1;
pfMyUserQueryWindow = MyUserQueryWindow;
g_dwOldUserQueryWindowAddr = 0;
g_dwUserQueryWindowIndex = -1;
pfMyUserBuildHwndList = MyUserBuildHwndList;
g_dwOldUserBuildHwndListAddr = 0;
g_dwUserBuildHwndListIndex = -1;
pfMyUserBuildHwndListWin8 = MyUserBuildHwndListWin8;
g_dwOldUserBuildHwndListWin8Addr = 0;
g_dwUserBuildHwndListWin8Index = -1;
pfMyUserFindWindowEx = MyUserFindWindowEx;
g_dwOldUserFindWindowExAddr = 0;
g_dwUserFindWindowExIndex = -1;
pfMyUserGetForegroundWindow = MyUserGetForegroundWindow;
g_dwOldUserGetForegroundWindowAddr = 0;
g_dwUserGetForegroundWindowIndex = -1;
pfMyUserSetParent = MyUserSetParent;
g_dwOldUserSetParentAddr = 0;
g_dwUserSetParentIndex = -1;
if(g_ulMajorVer == 5 && g_ulMinorVer == 0)
{
// win2000
g_dwUserQueryWindowIndex = 466;
g_dwUserBuildHwndListIndex = 302;
g_dwUserFindWindowExIndex = 368;
g_dwUserGetForegroundWindowIndex = 393;
g_dwUserSetParentIndex = 510;
g_dwUserPostMessageIndex = 459;
g_dwObjectTableOffsetInEProcess = 296;
g_dwExitTimeOffsetInEProcess = 144;
g_dwSmssProcId = (DWORD)GetProcIdByName(L"smss.exe");
}
else if(g_ulMajorVer == 5 && g_ulMinorVer == 1)
{
// winxp
g_dwUserQueryWindowIndex = 483;
g_dwUserBuildHwndListIndex = 312;
g_dwUserFindWindowExIndex = 378;
g_dwUserGetForegroundWindowIndex = 404;
g_dwUserSetParentIndex = 529;
g_dwUserPostMessageIndex = 475;
g_dwObjectTableOffsetInEProcess = 196;
g_dwExitTimeOffsetInEProcess = 120;
}
else if(g_ulMajorVer == 5 && g_ulMinorVer == 2)
{
// win2003
g_dwUserQueryWindowIndex = 481;
g_dwUserBuildHwndListIndex = 311;
g_dwUserFindWindowExIndex = 377;
g_dwUserGetForegroundWindowIndex = 403;
g_dwUserSetParentIndex = 526;
g_dwUserPostMessageIndex = 474;
g_dwObjectTableOffsetInEProcess = 212;
g_dwExitTimeOffsetInEProcess = 136;
}
else if(g_ulMajorVer == 6 && g_ulMinorVer == 0)
{
// winvista
g_dwUserQueryWindowIndex = 504;
g_dwUserBuildHwndListIndex = 322;
g_dwUserFindWindowExIndex = 391;
g_dwUserGetForegroundWindowIndex = 418;
g_dwUserSetParentIndex = 550;
g_dwUserPostMessageIndex = 497;
g_dwObjectTableOffsetInEProcess = 0;
g_dwExitTimeOffsetInEProcess = 0;
}
else if(g_ulMajorVer == 6 && g_ulMinorVer == 1 && g_ulBuildNum >= 0x1DB0)
{
// win7
g_dwUserQueryWindowIndex = 515;
g_dwUserBuildHwndListIndex = 323;
g_dwUserFindWindowExIndex = 396;
g_dwUserGetForegroundWindowIndex = 423;
g_dwUserSetParentIndex = 560;
g_dwUserPostMessageIndex = 508;
g_dwObjectTableOffsetInEProcess = 244;
g_dwExitTimeOffsetInEProcess = 168;
}
else if(g_ulMajorVer == 6 && g_ulMinorVer == 2 && g_ulBuildNum >= 0x23F0)
{
// win8
g_dwUserQueryWindowIndex = 482;
g_dwUserBuildHwndListIndex = -1;
g_dwUserBuildHwndListWin8Index = 360;
g_dwUserFindWindowExIndex = 459;
g_dwUserGetForegroundWindowIndex = 429;
g_dwUserSetParentIndex = 590;
g_dwUserPostMessageIndex = 490;
g_dwObjectTableOffsetInEProcess = 0;
g_dwExitTimeOffsetInEProcess = 696;
}
else
{
// unknown os ver
g_dwUserQueryWindowIndex = -1;
g_dwUserBuildHwndListIndex = -1;
g_dwUserBuildHwndListWin8Index = -1;
g_dwUserFindWindowExIndex = -1;
g_dwUserGetForegroundWindowIndex = -1;
g_dwUserSetParentIndex = -1;
g_dwUserPostMessageIndex = -1;
g_dwObjectTableOffsetInEProcess = 0;
g_dwExitTimeOffsetInEProcess = 0;
}
if(g_dwCsrssProcId != 0)
{
status = PsLookupProcessByProcessId((HANDLE)g_dwCsrssProcId, &pEprocessOfCsrss);
if(NT_SUCCESS(status))
{
KeStackAttachProcess(pEprocessOfCsrss, &ApcState);
pfOrigUserQueryWindow = GetOrigFunctionAddr(g_dwUserQueryWindowIndex, 1);
KdPrint(("SSDTHookInit:g_dwUserQueryWindowIndex[%d], pfOrigUserQueryWindow[%08X]\n",
g_dwUserQueryWindowIndex, pfOrigUserQueryWindow));
pfOrigUserBuildHwndList = GetOrigFunctionAddr(g_dwUserBuildHwndListIndex, 1);
KdPrint(("SSDTHookInit:g_dwUserBuildHwndListIndex[%d], pfOrigUserBuildHwndList[%08X]\n",
g_dwUserBuildHwndListIndex, pfOrigUserBuildHwndList));
pfOrigUserBuildHwndListWin8 = GetOrigFunctionAddr(g_dwUserBuildHwndListWin8Index, 1);
KdPrint(("SSDTHookInit:g_dwUserBuildHwndListWin8Index[%d], pfOrigUserBuildHwndListWin8[%08X]\n",
g_dwUserBuildHwndListWin8Index, pfOrigUserBuildHwndListWin8));
pfOrigUserFindWindowEx = GetOrigFunctionAddr(g_dwUserFindWindowExIndex, 1);
KdPrint(("SSDTHookInit:g_dwUserFindWindowExIndex[%d], pfOrigUserFindWindowEx[%08X]\n",
g_dwUserFindWindowExIndex, pfOrigUserFindWindowEx));
pfOrigUserGetForegroundWindow = GetOrigFunctionAddr(g_dwUserGetForegroundWindowIndex, 1);
KdPrint(("SSDTHookInit:g_dwUserGetForegroundWindowIndex[%d], pfOrigUserGetForegroundWindow[%08X]\n",
g_dwUserGetForegroundWindowIndex, pfOrigUserGetForegroundWindow));
pfOrigUserSetParent = GetOrigFunctionAddr(g_dwUserSetParentIndex, 1);
KdPrint(("SSDTHookInit:g_dwUserSetParentIndex[%d], pfOrigUserSetParent[%08X]\n",
g_dwUserSetParentIndex, pfOrigUserSetParent));
pfOrigUserPostMessage = GetOrigFunctionAddr(g_dwUserPostMessageIndex, 1);
KdPrint(("SSDTHookInit:g_dwUserPostMessageIndex[%d], pfOrigUserPostMessage[%08X]\n",
g_dwUserPostMessageIndex, pfOrigUserPostMessage));
KeUnstackDetachProcess(&ApcState);
ObDereferenceObject(pEprocessOfCsrss);
}
}
}
BOOLEAN IsProcessInList(PEPROCESS pEProcess,
ULONG hProcId,
DWORD dwType)
/*++
Routine Description:
Check if a process is in the proc list by its EPROCESS or ProcId,
dwType == 1(Black process) dwType == 2(White process)
--*/
{
BOOLEAN bRet = FALSE;
KIRQL OldIrql;
int i = 0, j = 0;
PEPROCESS pEProcessTmp;
if( (NULL != pEProcess || 0 != hProcId) &&
0 != dwType)
{
if(NULL != pEProcess)
{
KeAcquireSpinLock(&g_SpinLock, &OldIrql);
for(i = 0; i < 100; i++)
{
if(g_ProcList[i].pEProcess == pEProcess &&
g_ProcList[i].dwType == dwType)
{
bRet = TRUE;
break;
}
}
KeReleaseSpinLock(&g_SpinLock, OldIrql);
}
else if(NT_SUCCESS(PsLookupProcessByProcessId((HANDLE)hProcId, &pEProcessTmp)))
{
KeAcquireSpinLock(&g_SpinLock, &OldIrql);
for(j = 0; j < 100; j++)
{
if((ULONG)g_ProcList[j].hProcId == hProcId)
{
bRet = TRUE;
break;
}
}
KeReleaseSpinLock(&g_SpinLock, OldIrql);
ObDereferenceObject(pEProcessTmp);
}
}
return bRet;
}
PLIST_NODE GetProcInfoFromList(PEPROCESS pEProcess, ULONG hProcId)
/*++
Routine Description:
Get proc info from the proc list by its EPROCESS or ProcId
--*/
{
KIRQL OldIrql;
PVOID pRet = NULL;
int i = 0, j = 0;
if(NULL != pEProcess || 0 != hProcId)
{
KeAcquireSpinLock(&g_SpinLock, &OldIrql);
if(NULL != pEProcess)
{
for(i = 0; i < 100; i++)
{
if(g_ProcList[i].pEProcess == pEProcess)
{
pRet = &g_ProcList[i];
break;
}
}
}
else if(0 != hProcId)
{
for(j = 0; j < 100; j++)
{
if((ULONG)g_ProcList[j].hProcId == hProcId)
{
pRet = &g_ProcList[j];
break;
}
}
}
KeReleaseSpinLock(&g_SpinLock, OldIrql);
}
return pRet;
}
PMDL AllocateAndBuildMdl(PVOID pBuffer, ULONG ulLen)
/*++
Routine Description:
Allocate and build a mdl for pBuffer.
--*/
{
PMDL pRet = NULL;
pRet = IoAllocateMdl(pBuffer, ulLen, FALSE, FALSE, NULL);
if(pRet != NULL)
{
MmBuildMdlForNonPagedPool(pRet);
}
return pRet;
}
PVOID MapSharedMemory(PMDL pMdl, PEPROCESS pEProcess)
/*++
Routine Description:
Map the physical pages that are described by the pMdl to
a user mode address
--*/
{
PVOID pRet = NULL;
if(KeGetCurrentIrql() <= APC_LEVEL)
{
__try
{
pRet = MmMapLockedPagesSpecifyCache(pMdl, UserMode, MmNonCached, NULL, 0, NormalPagePriority);
}__except(EXCEPTION_EXECUTE_HANDLER)
{
pRet = NULL;
}
}
return pRet;
}
BOOLEAN CreateSharedMemory(LIST_NODE * pNode)
/*++
Routine Description:
Create shared memory and map it to a user mode address
--*/
{
BOOLEAN bRet = FALSE;
KAPC_STATE ApcState;
if(pNode->dwType == 1)
{
KeStackAttachProcess(pNode->pEProcess, &ApcState);
pNode->pPool = ExAllocatePoolWithTag(NonPagedPool, 0x1000, TAG_STRONG_OD);
if(pNode->pPool != NULL)
{
pNode->pMdl = AllocateAndBuildMdl(pNode->pPool, 0x1000);
if(pNode->pMdl != NULL)
{
pNode->pUserAddrOfSharedMem = MapSharedMemory(pNode->pMdl, pNode->pEProcess);
if(pNode->pUserAddrOfSharedMem != NULL)
{
bRet = TRUE;
}
else
{
IoFreeMdl(pNode->pMdl);
pNode->pMdl = NULL;
ExFreePoolWithTag(pNode->pPool, TAG_STRONG_OD);
pNode->pPool = NULL;
}
}
else
{
ExFreePoolWithTag(pNode->pPool, TAG_STRONG_OD);
pNode->pPool = NULL;
}
}
KeUnstackDetachProcess(&ApcState);
}
return bRet;
}
BOOLEAN UnmapSharedMemory(PMDL pMdl, PVOID pMapAddr)
/*++
Routine Description:
release a mapping set up by a preceding call to MmMapXXX
--*/
{
BOOLEAN bRet = FALSE;
if(KeGetCurrentIrql() < DISPATCH_LEVEL)
{
__try
{
MmUnmapLockedPages(pMapAddr, pMdl);
bRet = TRUE;
}__except(EXCEPTION_EXECUTE_HANDLER)
{
bRet = FALSE;
}
}
return bRet;
}
BOOLEAN ClearSharedMemory(LIST_NODE *pNode)
/*++
Routine Description:
Clear shared memory when process exit
--*/
{
BOOLEAN bRet = FALSE;
KAPC_STATE ApcState;
if(pNode->dwType == 1)
{
KeStackAttachProcess(pNode->pEProcess, &ApcState);
__try
{
UnmapSharedMemory(pNode->pMdl, pNode->pUserAddrOfSharedMem);
IoFreeMdl(pNode->pMdl);
ExFreePoolWithTag(pNode->pPool, TAG_STRONG_OD);
bRet = TRUE;
}__except(EXCEPTION_EXECUTE_HANDLER)
{
bRet = FALSE;
}
KeUnstackDetachProcess(&ApcState);
}
return bRet;
}
ULONG AddProcInfoToList(PEPROCESS pEProcess, ULONG hProcId, DWORD dwType)
/*++
Routine Description:
Add proc info to the proc list
--*/
{
ULONG ulRet = 0;
KIRQL OldIrql;
int i = 0;
PEPROCESS pEProcessTmp = NULL;
if(pEProcess != NULL || hProcId != 0)
{
if(0 != dwType)
{
KeAcquireSpinLock(&g_SpinLock, &OldIrql);
for(i = 0; i < 100; i++)
{
if(g_ProcList[i].pEProcess != pEProcess ||
(ULONG)g_ProcList[i].hProcId != hProcId)
{
if(g_ProcList[i].pEProcess == NULL &&
g_ProcList[i].hProcId == NULL)
{
g_ProcList[i].dwType = dwType;
g_ProcList[i].hProcId = (HANDLE)hProcId;
if(pEProcess != NULL)
{
g_ProcList[i].pEProcess = pEProcess;
}
KdPrint(("AddProcInfoToList: add proc to list, EProcess[%08X], ProcId[%d], dwType[%d]\n",
pEProcess, hProcId, dwType));
break;
}
}
}
KeReleaseSpinLock(&g_SpinLock, OldIrql);
if(pEProcess == NULL &&
NT_SUCCESS(PsLookupProcessByProcessId((HANDLE)hProcId, &pEProcessTmp)))
{
g_ProcList[i].pEProcess = pEProcessTmp;
ObDereferenceObject(pEProcessTmp);
}
if( dwType != 1 || CreateSharedMemory(&g_ProcList[i]))
{
ulRet = 1;
}
else
{
ulRet = -1; // black process && create shared memory failed!
}
}
}
return ulRet;
}
BOOLEAN DelProcInfoFromList(PEPROCESS pEProcess, ULONG hProcId, DWORD dwType)
/*++
Routine Description:
del proc info from the proc list
--*/
{
BOOLEAN bRet = FALSE;
LIST_NODE Node;
KIRQL OldIrql;
int i = 0, j = 0;
if(pEProcess != NULL || hProcId != 0)
{
if(0 != dwType)
{
KeAcquireSpinLock(&g_SpinLock, &OldIrql);
for(i = 0; i < 100; i++)
{
if(pEProcess != NULL &&
g_ProcList[i].pEProcess == pEProcess &&
g_ProcList[i].dwType == dwType)
{