-
Notifications
You must be signed in to change notification settings - Fork 3
/
hookproc.c
1799 lines (1336 loc) · 61.7 KB
/
hookproc.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) 2004 Security Architects Corporation. All rights reserved.
*
* Module Name:
*
* hookproc.c
*
* Abstract:
*
* This module implements various service operation (system call) hooking routines.
*
* Since not all the Zw* services are exported, we use a hack to find the required information.
* ntdll.dll contains stubs for calling all the system services. All stubs start with
* "mov eax, function_index" instruction where function_index is an index into a global
* system call table. By parsing ntdll.dll and extracting all the function indeces
* we can map all the Zw* names to their appropriate system call table indeces.
*
* Author:
*
* Eugene Tsyrklevich 16-Feb-2004
*
* Revision History:
*
* None.
*/
#include <NTDDK.h>
#include "hookproc.h"
#include "sysinfo.h"
#include "ntproto.h"
#include "learn.h"
#include "file.h"
#include "registry.h"
#include "section.h"
#include "sysinfo.h"
#include "semaphore.h"
#include "dirobj.h"
#include "symlink.h"
#include "mutant.h"
#include "event.h"
#include "port.h"
#include "job.h"
#include "token.h"
#include "timer.h"
#include "driverobj.h"
#include "process.h"
#include "procname.h"
#include "time.h"
#include "atom.h"
#include "vdm.h"
#include "debug.h"
#include "i386.h"
#include "misc.h"
#include "log.h"
#ifdef ALLOC_PRAGMA
#pragma alloc_text (INIT, InitSyscallsHooks)
#pragma alloc_text (INIT, InstallSyscallsHooks)
#pragma alloc_text (PAGE, RemoveSyscallsHooks)
#endif
#if DBG
int HookedRoutineRunning = 0;
#endif
PCHAR NTDLL_Base;
int ZwCallsNumber = 0;
/*
* FindFunctionOffset()
*
* Description:
* Finds a Zw* system call offset in a system service table.
*
* Implementation of all the Zw* system services (such as ZwOpenFile or ZwCreateProcess())
* start with a "mov eax, function_offset" instruction. Function offset is extracted from
* the first instruction.
*
* Parameters:
* Function - Pointer to the function code.
*
* Returns:
* Integer function offset (-1 in case of a failure).
*/
/* "MOV EAX,IMM32" opcode */
#define OPCODE_MOV 0xB8
/* macro shortcut for bailing out of FindFunctionOffset in case of an error */
#define ABORT_FindFunctionOffset(msg) { \
LOG(LOG_SS_HOOKPROC, LOG_PRIORITY_DEBUG, ("Error occured in FindFunctionOffset():")); \
LOG(LOG_SS_HOOKPROC, LOG_PRIORITY_DEBUG, msg); \
return -1; \
}
int
FindFunctionOffset(PULONG_PTR Function)
{
PUCHAR Instruction;
ULONG num, ServiceIDNumber, ServiceTableIndex;
/*
* Make sure that the service code starts with a MOV EAX,IMM32 instruction:
*
* lkd> u ZwCreateFile
* nt!ZwCreateFile:
* 804f86f4 b825000000 mov eax,0x25
*/
Instruction = (PUCHAR) Function;
if (*Instruction != OPCODE_MOV)
ABORT_FindFunctionOffset(("Invalid opcode %x\n", *Instruction));
/*
* Extract the Service Descriptor Table index (4 bytes following the mov opcode)
*
* The index format is as follows:
*
* Leading 18 bits are all zeroes
* Following 2 bits are system service table index (3 bits on Win64)
* Following 12 bits are service number
*/
num = * (PULONG) ++Instruction;
/* only SERVICE_TABLE_INDEX_BITS LSB bits should be set */
ServiceTableIndex = num >> SERVICE_ID_NUMBER_BITS;
if (ServiceTableIndex >= NUMBER_SERVICE_TABLES)
ABORT_FindFunctionOffset(("Invalid SSDT index: %x (%x)\n", ServiceTableIndex, num));
/* XXX temporary? There exist 4 (8 on IA64) service tables. All the Zw* system services are in table 0 */
if (ServiceTableIndex != 0)
ABORT_FindFunctionOffset(("Invalid SSDT index2: %x (%x)\n", ServiceTableIndex, num));
/* Verify Service ID Number is in range */
ServiceIDNumber = num & SERVICE_ID_NUMBER_MASK;
//XXX shouldn't we be using the shadow table instead??
//shadow table Zw* base address is the same in addition to GUI table
if (ServiceIDNumber > KeServiceDescriptorTable[ServiceTableIndex].NumberOfServices)
ABORT_FindFunctionOffset(("Invalid service id number %d (max is %d)\n", ServiceIDNumber, KeServiceDescriptorTable[ServiceTableIndex].NumberOfServices));
return ServiceIDNumber;
}
#if 0
/*
* HookSystemService()
*
* Description:
* Replaces an existing sytem service pointer (n a global system service table) with another function pointer.
*
* Parameters:
* OldService - Pointer to the service code to mediate.
* NewService - Pointer to the new function code.
*
* Returns:
* Current OldService indexed system service function pointer.
*/
/* macro shortcut for bailing out of HookSystemService in case of an error */
#define ABORT_HookSystemService(msg) { \
LOG(LOG_SS_HOOKPROC, LOG_PRIORITY_DEBUG, ("Error occured in HookSystemService():")); \
LOG(LOG_SS_HOOKPROC, LOG_PRIORITY_DEBUG, (msg)); \
return NULL; \
}
PVOID
HookSystemService(PVOID OldService, PVOID NewService)
{
PULONG_PTR ssdt;
PULONG_PTR retptr = NULL;
ULONG ServiceIDNumber;
if (OldService == NULL || NewService == NULL)
ABORT_HookSystemService(("NULL detected. OldService=%x NewService=%x", OldService, NewService));
ServiceIDNumber = FindFunctionOffset(OldService);
if (ServiceIDNumber == -1)
ABORT_HookSystemService(("FindFunctionOffset(%x) failed", OldService));
ssdt = KeServiceDescriptorTable[0].ServiceTableBase;
retptr = (PULONG_PTR) ssdt[ServiceIDNumber];
if (retptr == NULL)
ABORT_HookSystemService(("ssdt[index] = NULL\n"));
if (((ULONG) retptr & SystemAddressStart) == 0)
ABORT_HookSystemService(("invalid code instruction specified\n"));
retptr = ExchangeReadOnlyMemoryPointer((PVOID *) &ssdt[ServiceIDNumber], NewService);
return retptr;
}
#endif
/*
* HookSystemServiceByIndex()
*
* Description:
* Replaces an existing sytem service (n a global system service table) with another function pointer.
*
* Parameters:
* ServiceIDNumber - Index of a system service to mediate.
* NewService - Pointer to the new function code.
*
* Returns:
* Current ServiceIDNumber indexed system service function pointer.
*/
/* macro shortcut for bailing out of HookSystemServiceByIndex in case of an error */
#define ABORT_HookSystemServiceByIndex(msg) { \
LOG(LOG_SS_HOOKPROC, LOG_PRIORITY_DEBUG, ("Error occured in HookSystemServiceByIndex():")); \
LOG(LOG_SS_HOOKPROC, LOG_PRIORITY_DEBUG, msg); \
return NULL; \
}
PVOID
HookSystemServiceByIndex(ULONG ServiceIDNumber, PVOID NewService)
{
PULONG_PTR ssdt;
PULONG_PTR retptr = NULL;
ULONG ServiceTableIndex = 0;
ssdt = KeServiceDescriptorTable[ServiceTableIndex].ServiceTableBase;
/* Verify Service ID Number is in range */
//XXX shouldn't we be using the shadow table instead??
if (ServiceIDNumber > KeServiceDescriptorTable[ServiceTableIndex].NumberOfServices)
ABORT_HookSystemServiceByIndex(("Invalid service id number %d (max is %d)\n", ServiceIDNumber, KeServiceDescriptorTable[ServiceTableIndex].NumberOfServices));
retptr = (PULONG_PTR) ssdt[ServiceIDNumber];
if (retptr == NULL)
ABORT_HookSystemServiceByIndex(("ssdt[index] = NULL\n"));
if (((ULONG) retptr & SystemAddressStart) == 0)
ABORT_HookSystemServiceByIndex(("invalid code instruction specified\n"));
retptr = ExchangeReadOnlyMemoryPointer((PVOID *) &ssdt[ServiceIDNumber], NewService);
return retptr;
}
#if 0
/*
* FindSystemServiceByIndex()
*
* Description:XXX
* Replaces an existing sytem service (n a global system service table) with another function pointer.
*
* Parameters:
* ServiceIDNumber - Index of a system service to mediate.
* NewService - Pointer to the new function code.
*
* Returns:
* Current ServiceIDNumber indexed system service function pointer.
*/
/* macro shortcut for bailing out of HookSystemServiceByIndex in case of an error */
#define ABORT_FindSystemServiceByIndex(msg) { LOG(LOG_SS_HOOKPROC, LOG_PRIORITY_DEBUG, ("Error occured in FindSystemServiceByIndex():")); LOG(LOG_SS_HOOKPROC, LOG_PRIORITY_DEBUG, msg); return NULL; }
PULONG
FindSystemServiceByIndex(ULONG ServiceIDNumber)
{
PULONG_PTR ssdt;
PULONG_PTR retptr = NULL;
ULONG ServiceTableIndex = 0;
ssdt = KeServiceDescriptorTable[ServiceTableIndex].ServiceTableBase;
/* Verify Service ID Number is in range */
//XXX shouldn't we be using the shadow table instead??
if (ServiceIDNumber > KeServiceDescriptorTable[ServiceTableIndex].NumberOfServices)
ABORT_FindSystemServiceByIndex(("Invalid service id number %d (max is %d)\n", ServiceIDNumber, KeServiceDescriptorTable[ServiceTableIndex].NumberOfServices));
retptr = (PULONG_PTR) ssdt[ServiceIDNumber];
if (retptr == NULL)
ABORT_FindSystemServiceByIndex(("ssdt[index] = NULL\n"));
if (((ULONG) retptr & SystemAddressStart) == 0)
ABORT_FindSystemServiceByIndex(("invalid code instruction specified\n"));
return (PULONG) ssdt[ServiceIDNumber];
}
#endif
/*
* HookSystemServiceByName()
*
* Description:
* Replaces an existing sytem service (n a global system service table) with another function pointer.
*
* Parameters:
* ServiceName - Name of a Zw* system service to mediate.
* HookFunction - Pointer to the mediator function code.
*
* Returns:
* TRUE to indicate success, FALSE if failed.
*/
BOOLEAN
HookSystemServiceByName(PCHAR ServiceName, PULONG_PTR HookFunction)
{
int i;
for (i = 0; i < ZwCallsNumber; i++)
{
if (strlen(ServiceName) == ZwCalls[i].ZwNameLength && _stricmp(ServiceName, ZwCalls[i].ZwName + 2) == 0)
{
// LOG(LOG_SS_HOOKPROC, LOG_PRIORITY_DEBUG, ("HookSystemServiceByName: Matched rule: %x\n", ZwCalls[i].ServiceIDNumber));
// hijack the syscall if not hijacked already
if (ZwCalls[i].Hijacked == FALSE && ZwCalls[i].ServiceIDNumber != -1)
{
if ((ZwCalls[i].OriginalFunction = HookSystemServiceByIndex(ZwCalls[i].ServiceIDNumber,
HookFunction ? HookFunction : ZwCalls[i].HookFunction)) != NULL)
{
ZwCalls[i].Hijacked = TRUE;
}
}
return TRUE;
}
}
return FALSE;
}
#if 0
/*
* FindSystemServiceIndex()
*
* Description:
* Find a system service index for a specified service name.
*
* Parameters:
* ServiceName - Name of a Zw* system service to find.
*
* Returns:
* System service index if found, -1 otherwise.
*/
ULONG
FindSystemServiceIndex(PCHAR ServiceName)
{
int i;
for (i = 0; i < ZwCallsNumber; i++)
{
if (strlen(ServiceName) == ZwCalls[i].ZwNameLength && _stricmp(ServiceName, ZwCalls[i].ZwName + 2) == 0)
{
// LOG(LOG_SS_HOOKPROC, LOG_PRIORITY_DEBUG, ("FindSystemServiceByName: Matched rule: %x\n", ZwCalls[i].ServiceIDNumber));
return ZwCalls[i].ServiceIDNumber;
}
}
return -1;
}
#endif
/*
* FindSystemServiceNumber()
*
* Description:
* Find a system service number for a specified service name.
*
* Parameters:
* ServiceName - Name of a Zw* system service to find.
*
* Returns:
* System service number if found, -1 otherwise.
*/
ULONG
FindSystemServiceNumber(PCHAR ServiceName)
{
int i;
for (i = 0; i < ZwCallsNumber; i++)
{
if (strlen(ServiceName) == ZwCalls[i].ZwNameLength && _stricmp(ServiceName, ZwCalls[i].ZwName + 2) == 0)
{
// LOG(LOG_SS_HOOKPROC, LOG_PRIORITY_DEBUG, ("FindSystemServiceByName: Matched rule: %x\n", ZwCalls[i].ServiceIDNumber));
return i;
}
}
return -1;
}
/*
* Find_NTDLL_Base()
*
* Description:
* Returns the base address of mapped ntdll.dll in a "System" process context.
*
* NOTE: Based on "Windows NT/2000 Native API Reference" implementation.
*
* Parameters:
* None.
*
* Returns:
* ntdll.dll base address (NULL if not found).
*/
PVOID
Find_NTDLL_Base()
{
ULONG size, i;
PVOID ntdll = NULL;
PULONG SystemInfo;
PSYSTEM_MODULE_INFORMATION pSMI;
NTSTATUS status;
/*
* The data returned to the SystemInformation buffer is a ULONG count of the number of
* modules followed immediately by an array of SYSTEM_MODULE_INFORMATION.
*
* The system modules are the Portable Executable (PE) format files loaded into the
* kernel address space (ntoskrnl.exe, hal.dll, device drivers, and so on) and ntdll.dll.
*/
/* first, find out the total amount of module information to be returned */
status = ZwQuerySystemInformation(SystemModuleInformation, &size, 0, &size);
if (size == 0 || size > 1024*1024)
{
LOG(LOG_SS_HOOKPROC, LOG_PRIORITY_DEBUG, ("Find_NTDLL_Base: ZwQuerySystemInformation failed. status=%x size=%d\n", status, size));
return NULL;
}
/* second, allocate the required amount of memory */
SystemInfo = ExAllocatePoolWithTag(PagedPool, size + 4, _POOL_TAG);
if (SystemInfo == NULL)
{
LOG(LOG_SS_HOOKPROC, LOG_PRIORITY_DEBUG, ("Find_NTDLL_Base: out of memory (requested %d bytes)\n", size + 4));
return NULL;
}
/* third, request the module information */
ZwQuerySystemInformation(SystemModuleInformation, SystemInfo, size, &i);
if (size != ((*SystemInfo * sizeof(SYSTEM_MODULE_INFORMATION)) + 4))
{
LOG(LOG_SS_HOOKPROC, LOG_PRIORITY_DEBUG, ("Find_NTDLL_Base: inconsistent size (%d != %d * %d + 4)\n", size, *SystemInfo, sizeof(SYSTEM_MODULE_INFORMATION)));
return NULL;
}
pSMI = (PSYSTEM_MODULE_INFORMATION) (SystemInfo + 1);
for (i = 0; i < *SystemInfo; i++)
{
// LOG(LOG_SS_HOOKPROC, LOG_PRIORITY_DEBUG, ("comparing '%s' (base 0x%x)\n", pSMI[i].ImageName + pSMI[i].ModuleNameOffset, pSMI[i].Base));
if (_stricmp(pSMI[i].ImageName + pSMI[i].ModuleNameOffset, "ntdll.dll") == 0)
{
LOG(LOG_SS_HOOKPROC, LOG_PRIORITY_VERBOSE, ("Find_NTDLL_Base: ntdll.dll base address is %x\n", pSMI[i].Base));
ntdll = pSMI[i].Base;
break;
}
}
ExFreePoolWithTag(SystemInfo, _POOL_TAG);
return ntdll;
}
#if 0
PVOID
Find_Kernel32_Base2()
{
PVOID Kernel32 = NULL;
NTSTATUS status;
OBJECT_ATTRIBUTES ObjectAttributes;
IO_STATUS_BLOCK isb;
HANDLE FileHandle;
UNICODE_STRING usName;
PVOID BaseAddress = NULL;
SIZE_T Size;
CHAR buffer[256];
// RtlInitUnicodeString(&usName, L"\\SystemRoot\\System32\\kernel32.dll");
RtlInitUnicodeString(&usName, L"\\??\\c:\\windows\\system32\\kernel32.dll");
InitializeObjectAttributes(&ObjectAttributes, &usName, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
status = ZwCreateFile(&FileHandle, GENERIC_READ, &ObjectAttributes, &isb, NULL, 0, 0, 0, 0, NULL, 0);
if (!NT_SUCCESS(status))
{
LOG(LOG_SS_MISC, LOG_PRIORITY_DEBUG, ("Find_Kernel32_Base: Failed to open file %S (%x)\n", usName.Buffer, status));
return FALSE;
}
status = ZwReadFile(FileHandle, NULL, NULL, NULL, &isb, (PVOID) buffer, sizeof(buffer) - 1, 0, NULL);
if (! NT_SUCCESS(status))
{
if (status != STATUS_END_OF_FILE)
{
LOG(LOG_SS_POLICY, LOG_PRIORITY_DEBUG, ("LoadSecurityPolicy: ZwReadFile failed rc=%x\n", status));
}
}
ZwClose(FileHandle);
return Kernel32;
}
PVOID
Find_Kernel32_Base()
{
PVOID Kernel32 = NULL;
NTSTATUS status;
OBJECT_ATTRIBUTES ObjectAttributes;
HANDLE SectionHandle;
UNICODE_STRING usName;
PVOID BaseAddress = NULL;
SIZE_T Size;
NTSTATUS q;
RtlInitUnicodeString(&usName, L"\\KnownDlls\\kernel32.dll");
InitializeObjectAttributes(&ObjectAttributes, &usName, OBJ_KERNEL_HANDLE, NULL, NULL);
if (NT_SUCCESS( ZwOpenSection(&SectionHandle, SECTION_MAP_READ, &ObjectAttributes) ))
{
q = ZwMapViewOfSection(SectionHandle, NtCurrentProcess(), &BaseAddress, 0, 0, NULL, &Size, ViewShare, MEM_RESERVE, PAGE_READWRITE);
if (NT_SUCCESS(q))
{
KdPrint(("XXX mapped kernel32.dll at BaseAddress %x\n", BaseAddress));
if (!NT_SUCCESS( ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress) ))
{
KdPrint(("ZwUnmapViewOfSection failed"));
}
ZwClose(SectionHandle);
}
else
{
KdPrint(("ZwMapViewOfSection failed with status %x", q));
}
}
else
{
KdPrint(("ZwOpenSection failed"));
}
return Kernel32;
}
#endif
/*
* FindFunctionBase()
*
* Description:
* Finds the address where a function is mapped at.
*
* NOTE: Based on "Windows NT/2000 Native API Reference" implementation.
*
* Parameters:
* Base - Mapped address of a DLL exporting the function.
* Name - Function name.
*
* Returns:
* Address where a function is mapped at (NULL if not found).
*/
/* macro shortcut for bailing out of FindFunctionBase in case of an error */
#define ABORT_FindFunctionBase(msg) { \
LOG(LOG_SS_HOOKPROC, LOG_PRIORITY_DEBUG, ("Error occured in FindFunctionBase():")); \
LOG(LOG_SS_HOOKPROC, LOG_PRIORITY_DEBUG, msg); \
return NULL; \
}
PVOID
FindFunctionBase(PCHAR ImageBase, PCSTR Name)
{
PIMAGE_DOS_HEADER DosHeader;
PIMAGE_NT_HEADERS PeHeader;
PIMAGE_DATA_DIRECTORY ImageExportDirectoryEntry;
ULONG ExportDirectorySize, ExportDirectoryOffset, i;
PIMAGE_EXPORT_DIRECTORY ExportDirectory;
PULONG ExportAddressTable;
PSHORT ExportOrdinalTable;
PULONG ExportNameTable;
if ( (DosHeader = (PIMAGE_DOS_HEADER) ImageBase) == NULL)
ABORT_FindFunctionBase(("Base pointer is NULL (name = %s)\n", Name));
if (DosHeader->e_magic != IMAGE_DOS_SIGNATURE)
ABORT_FindFunctionBase(("DOS Signature not found! (%x %x)\n", DosHeader, DosHeader->e_magic));
if (DosHeader->e_lfanew > 1024*1024)
ABORT_FindFunctionBase(("Invalid e_lfanew value %x\n", DosHeader->e_lfanew));
if ( (PeHeader = (PIMAGE_NT_HEADERS) (ImageBase + DosHeader->e_lfanew)) == NULL)
ABORT_FindFunctionBase(("NT header pointer is NULL (name = %s)\n", Name));
if (PeHeader->Signature != IMAGE_PE_SIGNATURE)
ABORT_FindFunctionBase(("PE Signature not found! (%x %x)\n", PeHeader, PeHeader->Signature));
if ( (ImageExportDirectoryEntry = PeHeader->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_EXPORT) == NULL)
ABORT_FindFunctionBase(("Export directory pointer is NULL (name = %s)\n", Name));
ExportDirectorySize = ImageExportDirectoryEntry->Size;
ExportDirectoryOffset = ImageExportDirectoryEntry->VirtualAddress;
if ( (ExportDirectory = (PIMAGE_EXPORT_DIRECTORY) (ImageBase + ExportDirectoryOffset)) == NULL)
ABORT_FindFunctionBase(("Exports pointer is NULL (name = %s)\n", Name));
ExportAddressTable = (PULONG) (ImageBase + ExportDirectory->AddressOfFunctions);
ExportOrdinalTable = (PSHORT) (ImageBase + ExportDirectory->AddressOfNameOrdinals);
ExportNameTable = (PULONG) (ImageBase + ExportDirectory->AddressOfNames);
//XXX "AcceptConnectPort\0" \0 necessary to make sure ZwCreateProcess does not match ZwCreateProcessEx ?
for (i = 0; i < ExportDirectory->NumberOfNames; i++)
{
ULONG ord = ExportOrdinalTable[i];
if (ExportAddressTable[ord] < ExportDirectoryOffset ||
ExportAddressTable[ord] >= ExportDirectoryOffset + ExportDirectorySize)
{
//XXX windows loader uses binary search?
if (strcmp(ImageBase + ExportNameTable[i], Name) == 0)
{
return ImageBase + ExportAddressTable[ord];
}
}
}
return NULL;
}
/*
* FindZwFunctionIndex()
*
* Description:
* Finds a system service table index of a Zw* system service.
*
* NOTE: Based on "Windows NT/2000 Native API Reference" implementation.
*
* Parameters:
* Name - Zw* service name.
*
* Returns:
* Zw* service index in a system service table (-1 if not found).
*/
int
FindZwFunctionIndex(PCSTR Name)
{
PULONG_PTR FunctionBase;
if (NTDLL_Base == NULL)
if ( (NTDLL_Base = Find_NTDLL_Base()) == NULL)
return -1;
if ( (FunctionBase = FindFunctionBase(NTDLL_Base, Name)) == NULL)
{
// LOG(LOG_SS_HOOKPROC, LOG_PRIORITY_DEBUG, ("FindZwFunctionIndex: FindZwFunctionBase(%s) returned NULL", Name));
return -1;
}
return FindFunctionOffset(FunctionBase);
}
/*
* ZwCalls structure describes all known system services.
* Part of the structure (i.e. system call table offset and system call address)
* is filled in at runtime.
*
* Automatically generated with
* dumpbin /exports c:\windows2003\system32\ntdll.dll|grep Zw|
* perl -wnle "print qq{\t{ \"$1\", NULL, (PULONG_PTR) SystemCallHandler$i, NULL, FALSE },} if /(Zw.*)/; ++$i" > win2k3_syscalls
*
* perl -wne "if (/\"Zw(.*?)\"/) { $q=length $1; s/\"Zw(.*?)\"/\"Zw$1\", $q/} print" q.txt > q2.txt
*/
struct _ZwCalls /* {
PCHAR ZwName; // System call name
USHORT ZwNameLength; // System call name length
USHORT ServiceIDNumber; // System call index (filled in at runtime)
PULONG_PTR HookFunction; // Address of the hijacking function (function that will be called instead of the original system call)
PULONG_PTR OriginalFunction; // PlaceHolder for the address of the original syscall address
BOOLEAN Hijacked; // Flag indicating whether we already hijacked this system call
// or whether this is a special system service that needs to be hijacked initially
} */ ZwCalls[] =
{
{ "ZwAcceptConnectPort", 17, -1, (PULONG_PTR) SystemCallHandler0, NULL, FALSE },
{ "ZwAccessCheck", 11, -1, (PULONG_PTR) SystemCallHandler1, NULL, FALSE },
{ "ZwAccessCheckAndAuditAlarm", 24, -1, (PULONG_PTR) SystemCallHandler2, NULL, FALSE },
{ "ZwAccessCheckByType", 17, -1, (PULONG_PTR) SystemCallHandler3, NULL, FALSE },
{ "ZwAccessCheckByTypeAndAuditAlarm", 30, -1, (PULONG_PTR) SystemCallHandler4, NULL, FALSE },
{ "ZwAccessCheckByTypeResultList", 27, -1, (PULONG_PTR) SystemCallHandler5, NULL, FALSE },
{ "ZwAccessCheckByTypeResultListAndAuditAlarm", 40, -1, (PULONG_PTR) SystemCallHandler6, NULL, FALSE },
{ "ZwAccessCheckByTypeResultListAndAuditAlarmByHandle", 48, -1, (PULONG_PTR) SystemCallHandler7, NULL, FALSE },
#if HOOK_ATOM
{ "ZwAddAtom", 7, -1, (PULONG_PTR) HookedNtAddAtom, NULL, TRUE },
#else
{ "ZwAddAtom", 7, -1, (PULONG_PTR) SystemCallHandler8, NULL, FALSE },
#endif
//XXX
{ "ZwAddBootEntry", 12, -1, (PULONG_PTR) SystemCallHandler9, NULL, FALSE },
{ "ZwAddDriverEntry", 14, -1, (PULONG_PTR) SystemCallHandler10, NULL, FALSE },
#if HOOK_TOKEN_ZZZ
{ "ZwAdjustGroupsToken", 17, -1, (PULONG_PTR) HookedNtAdjustGroupsToken, NULL, TRUE },
#else
{ "ZwAdjustGroupsToken", 17, -1, (PULONG_PTR) SystemCallHandler11, NULL, FALSE },
#endif
#if HOOK_TOKEN
{ "ZwAdjustPrivilegesToken", 21, -1, (PULONG_PTR) HookedNtAdjustPrivilegesToken, NULL, TRUE },
#else
{ "ZwAdjustPrivilegesToken", 21, -1, (PULONG_PTR) SystemCallHandler12, NULL, FALSE },
#endif
{ "ZwAlertResumeThread", 17, -1, (PULONG_PTR) SystemCallHandler13, NULL, FALSE },
{ "ZwAlertThread", 11, -1, (PULONG_PTR) SystemCallHandler14, NULL, FALSE },
{ "ZwAllocateLocallyUniqueId", 23, -1, (PULONG_PTR) SystemCallHandler15, NULL, FALSE },
{ "ZwAllocateUserPhysicalPages", 25, -1, (PULONG_PTR) SystemCallHandler16, NULL, FALSE },
{ "ZwAllocateUuids", 13, -1, (PULONG_PTR) SystemCallHandler17, NULL, FALSE },
{ "ZwAllocateVirtualMemory", 21, -1, (PULONG_PTR) SystemCallHandler18, NULL, FALSE },
{ "ZwApphelpCacheControl", 19, -1, (PULONG_PTR) SystemCallHandler19, NULL, FALSE },
{ "ZwAreMappedFilesTheSame", 21, -1, (PULONG_PTR) SystemCallHandler20, NULL, FALSE },
{ "ZwAssignProcessToJobObject", 24, -1, (PULONG_PTR) SystemCallHandler21, NULL, FALSE },
{ "ZwCallbackReturn", 14, -1, (PULONG_PTR) SystemCallHandler22, NULL, FALSE },
{ "ZwCancelDeviceWakeupRequest", 25, -1, (PULONG_PTR) SystemCallHandler23, NULL, FALSE },
{ "ZwCancelIoFile", 12, -1, (PULONG_PTR) SystemCallHandler24, NULL, FALSE },
{ "ZwCancelTimer", 11, -1, (PULONG_PTR) SystemCallHandler25, NULL, FALSE },
{ "ZwClearEvent", 10, -1, (PULONG_PTR) SystemCallHandler26, NULL, FALSE },
/* don't mediate for performance reasons, requires a valid handle anyway */
{ "ZwClose", 5, -1, NULL, NULL, FALSE },
{ "ZwCloseObjectAuditAlarm", 21, -1, (PULONG_PTR) SystemCallHandler28, NULL, FALSE },
{ "ZwCompactKeys", 11, -1, (PULONG_PTR) SystemCallHandler29, NULL, FALSE },
{ "ZwCompareTokens", 13, -1, (PULONG_PTR) SystemCallHandler30, NULL, FALSE },
{ "ZwCompleteConnectPort", 19, -1, (PULONG_PTR) SystemCallHandler31, NULL, FALSE },
{ "ZwCompressKey", 11, -1, (PULONG_PTR) SystemCallHandler32, NULL, FALSE },
#if HOOK_PORT
{ "ZwConnectPort", 11, -1, (PULONG_PTR) HookedNtConnectPort, NULL, TRUE },
#else
{ "ZwConnectPort", 11, -1, (PULONG_PTR) SystemCallHandler33, NULL, FALSE },
#endif
{ "ZwContinue", 8, -1, (PULONG_PTR) SystemCallHandler34, NULL, FALSE },
#if HOOK_DEBUG_ZZZ
{ "ZwCreateDebugObject", 17, -1, (PULONG_PTR) HookedNtCreateDebugObject, NULL, TRUE },
#else
{ "ZwCreateDebugObject", 17, -1, (PULONG_PTR) SystemCallHandler35, NULL, FALSE },
#endif
#if HOOK_DIROBJ
{ "ZwCreateDirectoryObject", 21, -1, (PULONG_PTR) HookedNtCreateDirectoryObject, NULL, TRUE },
#else
{ "ZwCreateDirectoryObject", 21, -1, (PULONG_PTR) SystemCallHandler36, NULL, FALSE },
#endif
#if HOOK_EVENT
{ "ZwCreateEvent", 11, -1, (PULONG_PTR) HookedNtCreateEvent, NULL, TRUE },
{ "ZwCreateEventPair", 15, -1, (PULONG_PTR) HookedNtCreateEventPair, NULL, TRUE },
#else
{ "ZwCreateEvent", 11, -1, (PULONG_PTR) SystemCallHandler37, NULL, FALSE },
{ "ZwCreateEventPair", 15, -1, (PULONG_PTR) SystemCallHandler38, NULL, FALSE },
#endif
#if HOOK_FILE
{ "ZwCreateFile", 10, -1, (PULONG_PTR) HookedNtCreateFile, NULL, TRUE },
#else
{ "ZwCreateFile", 10, -1, (PULONG_PTR) SystemCallHandler39, NULL, FALSE },
#endif
{ "ZwCreateIoCompletion", 18, -1, (PULONG_PTR) SystemCallHandler40, NULL, FALSE },
#if HOOK_JOB
{ "ZwCreateJobObject", 15, -1, (PULONG_PTR) HookedNtCreateJobObject, NULL, TRUE },
#else
{ "ZwCreateJobObject", 15, -1, (PULONG_PTR) SystemCallHandler41, NULL, FALSE },
#endif
//XXX ???
{ "ZwCreateJobSet", 12, -1, (PULONG_PTR) SystemCallHandler42, NULL, FALSE },
#if HOOK_REGISTRY
{ "ZwCreateKey", 9, -1, (PULONG_PTR) HookedNtCreateKey, NULL, TRUE },
#else
{ "ZwCreateKey", 9, -1, (PULONG_PTR) SystemCallHandler43, NULL, FALSE },
#endif
{ "ZwCreateKeyedEvent", 16, -1, (PULONG_PTR) SystemCallHandler44, NULL, FALSE },
#if HOOK_FILE
{ "ZwCreateMailslotFile", 18, -1, (PULONG_PTR) HookedNtCreateMailslotFile, NULL, TRUE },
#else
{ "ZwCreateMailslotFile", 18, -1, (PULONG_PTR) SystemCallHandler45, NULL, FALSE },
#endif
#if HOOK_MUTANT
{ "ZwCreateMutant", 12, -1, (PULONG_PTR) HookedNtCreateMutant, NULL, TRUE },
#else
{ "ZwCreateMutant", 12, -1, (PULONG_PTR) SystemCallHandler46, NULL, FALSE },
#endif
#if HOOK_FILE
{ "ZwCreateNamedPipeFile", 19, -1, (PULONG_PTR) HookedNtCreateNamedPipeFile, NULL, TRUE },
#else
{ "ZwCreateNamedPipeFile", 19, -1, (PULONG_PTR) SystemCallHandler47, NULL, FALSE },
#endif
{ "ZwCreatePagingFile", 16, -1, (PULONG_PTR) SystemCallHandler48, NULL, FALSE },
#if HOOK_PORT
{ "ZwCreatePort", 10, -1, (PULONG_PTR) HookedNtCreatePort, NULL, TRUE },
#else
{ "ZwCreatePort", 10, -1, (PULONG_PTR) SystemCallHandler49, NULL, FALSE },
#endif
#if HOOK_PROCESS
{ "ZwCreateProcess", 13, -1, (PULONG_PTR) HookedNtCreateProcess, NULL, TRUE },
{ "ZwCreateProcessEx", 15, -1, (PULONG_PTR) HookedNtCreateProcessEx, NULL, TRUE },
#else
{ "ZwCreateProcess", 13, -1, (PULONG_PTR) SystemCallHandler50, NULL, FALSE },
{ "ZwCreateProcessEx", 15, -1, (PULONG_PTR) SystemCallHandler51, NULL, FALSE },
#endif
{ "ZwCreateProfile", 13, -1, (PULONG_PTR) SystemCallHandler52, NULL, FALSE },
#if HOOK_SECTION
{ "ZwCreateSection", 13, -1, (PULONG_PTR) HookedNtCreateSection, NULL, TRUE },
#else
{ "ZwCreateSection", 13, -1, (PULONG_PTR) SystemCallHandler53, NULL, FALSE },
#endif
#if HOOK_SEMAPHORE
{ "ZwCreateSemaphore", 15, -1, (PULONG_PTR) HookedNtCreateSemaphore, NULL, TRUE },
#else
{ "ZwCreateSemaphore", 15, -1, (PULONG_PTR) SystemCallHandler54, NULL, FALSE },
#endif
#if HOOK_SYMLINK
{ "ZwCreateSymbolicLinkObject", 24, -1, (PULONG_PTR) HookedNtCreateSymbolicLinkObject, NULL, TRUE },
#else
{ "ZwCreateSymbolicLinkObject", 24, -1, (PULONG_PTR) SystemCallHandler55, NULL, FALSE },
#endif
#if HOOK_PROCESS
{ "ZwCreateThread", 12, -1, (PULONG_PTR) HookedNtCreateThread, NULL, TRUE },
#else
{ "ZwCreateThread", 12, -1, (PULONG_PTR) SystemCallHandler56, NULL, FALSE },
#endif