-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayer.c
273 lines (235 loc) · 5.63 KB
/
layer.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
/*
* WehnTrust
*
* Copyright (c) 2004, Wehnus.
*/
#include <ntddk.h>
/*#include "precomp.h"*/
#include "Layer.h"
#include "disasm.h"
extern ULONG **KeServiceDescriptorTable;
#define ALLOC_TAG 'wENU'
BOOL DirectJmp;
//
// Installs a call layer at the supplied address and redirects it to the hook
// address
//
NTSTATUS InstallCallLayer(
IN ULONG_PTR FunctionToHookAddress,
IN ULONG_PTR HookFunctionAddress,
OUT PCALL_LAYER *Layer)
{
PCALL_LAYER Result = NULL;
NTSTATUS Status = STATUS_INSUFFICIENT_RESOURCES;
PUCHAR JmpSlotBuffer = NULL;
ULONG JmpSlotBufferSize = 0;
ULONG PreambleSize;
UCHAR Overwrite[6];
KIRQL OldIrql;
//
// Determine the preamble size of the function we're hooking.
//
PreambleSize = DisassembleUntil((PUCHAR)FunctionToHookAddress, 6);
JmpSlotBufferSize = PreambleSize + 6;
do
{
//
// Allocate storage in non-paged pool for the call layer context.
//
if (!(Result = (PCALL_LAYER)ExAllocatePoolWithTag(
NonPagedPool,
sizeof(CALL_LAYER),
ALLOC_TAG)))
{
KdPrint(("InstallCallLayer(): Failed to allocate call layer context."));
break;
}
RtlZeroMemory(
Result,
sizeof(CALL_LAYER));
//
// Allocate storage in the non-paged pool for the jump slot buffer that is
// to be associated with the function to be hooked.
//
if (!(JmpSlotBuffer = (PUCHAR)ExAllocatePoolWithTag(
NonPagedPool,
JmpSlotBufferSize,
ALLOC_TAG)))
{
KdPrint(("InstallCallLayer(): Failed to allocate jmp slot buffer (%lu bytes).",
JmpSlotBufferSize));
break;
}
//
// Copy the function's preamble to the start of the jump slot
//
RtlCopyMemory(
JmpSlotBuffer,
(PVOID)FunctionToHookAddress,
PreambleSize);
//
// Store the address of the instruction immediately after the first n
// bytes of the rpeamble in RealFunctionPostPreamble.
//
Result->RealFunctionPostPreamble = FunctionToHookAddress + PreambleSize;
Result->FunctionToHookAddress = FunctionToHookAddress;
Result->HookFunctionAddress = HookFunctionAddress;
Result->Target = (ULONG_PTR)Result->RealFunction;
Result->RealFunction = JmpSlotBuffer;
//
// Initialize the relative jump to after the function's preamble.
//
if(!DirectJmp)
{
JmpSlotBuffer[PreambleSize] = 0xff;
JmpSlotBuffer[PreambleSize + 1] = 0x25;
*(PULONG_PTR)(JmpSlotBuffer + PreambleSize + 2) = (ULONG_PTR)&Result->RealFunctionPostPreamble;
}
else
{
JmpSlotBuffer[PreambleSize] = 0xe9;
*(PULONG_PTR)(JmpSlotBuffer + PreambleSize + 1) = (ULONG_PTR)&Result->RealFunctionPostPreamble;
}
//
// Preserve the first n bytes of the function's preamble since we're about
// to overwrite them.
//
RtlCopyMemory(
Result->SavePreamble,
(PVOID)FunctionToHookAddress,
6);
//
// Set up the indirect jump to overwrite the preamble with.
//
Overwrite[0] = 0xff;
Overwrite[1] = 0x25;
*(PULONG_PTR)(Overwrite + 2) = (ULONG_PTR)(&Result->HookFunctionAddress);
//
// Probe & lock the pages
//
if (!(Result->Mdl = MmCreateMdl(
NULL,
(PVOID)FunctionToHookAddress,
6)))
{
KdPrint(("MmCreateMdl failed."));
break;
}
MmBuildMdlForNonPagedPool(Result->Mdl);
__try
{
if (!(Result->LockedAddress = MmMapLockedPages(
Result->Mdl,
KernelMode)))
{
IoFreeMdl(Result->Mdl);
KdPrint(("MmMapLockedPages failed."));
break;
}
} __except(EXCEPTION_EXECUTE_HANDLER)
{
Status = GetExceptionCode();
break;
}
OldIrql = KeRaiseIrqlToDpcLevel();
//
// Copy over the preamble
//
RtlCopyMemory(
FunctionToHookAddress,
Overwrite,
6);
KeLowerIrql(OldIrql);
KdPrint(("InstallCallLayer(): Call Layer installed at %.8x (Real at %.8x).",
Result,
Result->RealFunction));
*Layer = Result;
Status = STATUS_SUCCESS;
} while (0);
//
// Cleanup on failure
//
if (!NT_SUCCESS(Status))
{
if (JmpSlotBuffer)
ExFreePool(JmpSlotBuffer);
if (Result)
{
if (Result->Mdl)
IoFreeMdl(Result->Mdl);
ExFreePool(Result);
}
}
return Status;
}
//
// Uninstalls a previously installed call layer
//
NTSTATUS UninstallCallLayer(
IN PCALL_LAYER Layer)
{
NTSTATUS Status = STATUS_UNSUCCESSFUL;
do
{
if (!Layer)
break;
//
// Restore the first 6 bytes of the preamble
//
RtlCopyMemory(
(PVOID)Layer->LockedAddress,
Layer->SavePreamble,
6);
//
// Unmap the locked pages and free the Mdl
//
MmUnmapLockedPages(
Layer->LockedAddress,
Layer->Mdl);
IoFreeMdl(
Layer->Mdl);
KdPrint(("Successfully removed call Layer at %.8x.",
Layer));
//
// Deallocate the Buffers
//
ExFreePool(
Layer->RealFunction);
ExFreePool(
Layer);
Status = STATUS_SUCCESS;
} while (0);
return Status;
}
//
// Returns the pointer to the kernel mode system call handler
//
NTSTATUS GetSystemCallRoutine(
IN PVOID SystemCallToHook OPTIONAL,
IN ULONG SystemCallIndex OPTIONAL,
OUT PULONG_PTR SystemCallAddress)
{
NTSTATUS Status;
PULONG SystemCallTable;
//
// If a system call virtual address was provided, use it in place of the
// index
//
if (SystemCallToHook)
SystemCallIndex = *(PULONG)((PCHAR)SystemCallToHook+1);
//
// Make sure we aren't supplied with an index that exceeds the maximum
//
if (SystemCallIndex < (ULONG)KeServiceDescriptorTable[2])
{
SystemCallTable = KeServiceDescriptorTable[0];
*SystemCallAddress = SystemCallTable[SystemCallIndex];
Status = STATUS_SUCCESS;
}
else
{
*SystemCallAddress = 0;
Status = STATUS_NOT_FOUND;
}
return Status;
}