forked from changeofpace/MouHidInputHook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouhid.cpp
507 lines (426 loc) · 13.3 KB
/
mouhid.cpp
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
/*++
Copyright (c) 2019 changeofpace. All rights reserved.
Use of this source code is governed by the MIT license. See the 'LICENSE' file
for more information.
--*/
#include "mouhid.h"
#include <kbdmou.h>
#include "io_util.h"
#include "log.h"
#include "nt.h"
#include "pe.h"
//=============================================================================
// Constants
//=============================================================================
#define MODULE_TITLE "MouHid Context"
//
// We choose a search limit by adding a small delta to the actual connect data
// offset. This delta accounts for the field offset changing in future
// versions of the MouHid driver.
//
// The actual offset can be found in the IOCTL_INTERNAL_MOUSE_CONNECT case of
// the MouHid IRP_MJ_INTERNAL_DEVICE_CONTROL handler, mouhid!MouHid_IOCTL:
//
// Platform: Windows 7 x64
// File: mouhid.sys
// Version: 6.1.7600.16385 (win7_rtm.090713-1255)
// Ioctl Code: 0xF0203 (IOCTL_INTERNAL_MOUSE_CONNECT)
//
// Annotated Assembly:
//
// mov rax, [rsi+_IO_STACK_LOCATION.Parameters.DeviceIoControl.Type3InputBuffer]
// movdqu xmm0, xmmword ptr [rax+CONNECT_DATA.ClassDeviceObject]
// movdqu xmmword ptr [r12+MOUHID_DEVICE_EXTENSION.ConnectData_B8.ClassDeviceObject], xmm0
//
// Unannotated Assembly:
//
// mov rax, [rsi+20h]
// movdqu xmm0, xmmword ptr [rax]
// movdqu xmmword ptr [r12+0B8h], xmm0
//
#define DEVICE_EXTENSION_SEARCH_SIZE 0x100
//=============================================================================
// Private Types
//=============================================================================
typedef struct _MOUHID_CONTEXT
{
//
// The field offset, in bytes, of the CONNECT_DATA object in the device
// extension of a MouHid device object.
//
SIZE_T ConnectDataFieldOffset;
} MOUHID_CONTEXT, *PMOUHID_CONTEXT;
//=============================================================================
// Module Globals
//=============================================================================
EXTERN_C static MOUHID_CONTEXT g_MhdContext = {};
//=============================================================================
// Private Prototypes
//=============================================================================
_IRQL_requires_(PASSIVE_LEVEL)
_IRQL_requires_same_
_Check_return_
EXTERN_C
static
NTSTATUS
MhdpResolveConnectDataFieldOffsetForDevice(
_In_ PDEVICE_OBJECT pDeviceObject,
_Out_ PSIZE_T pcbConnectDataFieldOffset
);
_IRQL_requires_(PASSIVE_LEVEL)
_IRQL_requires_same_
_Check_return_
EXTERN_C
static
NTSTATUS
MhdpResolveConnectDataFieldOffset(
_Out_ PSIZE_T pcbConnectDataFieldOffset
);
//=============================================================================
// Meta Interface
//=============================================================================
_Use_decl_annotations_
EXTERN_C
NTSTATUS
MhdDriverEntry()
/*++
Routine Description:
Initializes the MouHid Context module.
Required Modules:
None
--*/
{
SIZE_T cbConnectDataFieldOffset = 0;
NTSTATUS ntstatus = STATUS_SUCCESS;
DBG_PRINT("Loading %s.", MODULE_TITLE);
ntstatus = MhdpResolveConnectDataFieldOffset(&cbConnectDataFieldOffset);
if (!NT_SUCCESS(ntstatus))
{
ERR_PRINT("MhdpResolveConnectDataFieldOffset failed: 0x%X",
ntstatus);
goto exit;
}
//
// Initialize the global context.
//
g_MhdContext.ConnectDataFieldOffset = cbConnectDataFieldOffset;
DBG_PRINT("%s loaded:", MODULE_TITLE);
DBG_PRINT(" ConnectDataFieldOffset: 0x%IX",
g_MhdContext.ConnectDataFieldOffset);
exit:
return ntstatus;
}
//=============================================================================
// Public Interface
//=============================================================================
_Use_decl_annotations_
EXTERN_C
SIZE_T
MhdGetConnectDataFieldOffset()
{
return g_MhdContext.ConnectDataFieldOffset;
}
//=============================================================================
// Private Interface
//=============================================================================
_Use_decl_annotations_
EXTERN_C
static
NTSTATUS
MhdpResolveConnectDataFieldOffsetForDevice(
PDEVICE_OBJECT pDeviceObject,
PSIZE_T pcbConnectDataFieldOffset
)
/*++
Routine Description:
Dynamically resolves the field offset of the CONNECT_DATA object in the
device extension of the specified MouHid device object.
Parameters:
pDeviceObject - Referenced pointer to a MouHid device object.
pcbConnectDataFieldOffset - Returns the field offset of the CONNECT_DATA
for the specified MouHid device object.
Remarks:
This routine utilizes knowledge of the MouClass communication protocol as a
heuristic for resolving the CONNECT_DATA field offset.
Heuristic:
1. Obtain a list of all the MouHid device objects.
2. For each MouHid device object:
2.a Get the device object attached to the MouHid device object.
2.b Get the address range of every executable image section in the
driver of the attached device object.
2.c Search the device extension of the MouHid device object for a
valid CONNECT_DATA object by interpreting each pointer-aligned
address as a CONNECT_DATA candidate. A candidate is valid if it
meets the following criteria:
i. The 'ClassDeviceObject' field matches the attached
device object.
ii. The 'ClassService' field points to an address contained
in one of the executable image sections from (2.b).
NOTE This heuristic may be applicable to other types of mouse device
stacks, e.g., PS/2.
--*/
{
PDEVICE_OBJECT pAttachedDevice = NULL;
PVOID pDriverStart = NULL;
ULONG_PTR ImageBase = 0;
PIMAGE_SECTION_HEADER* ppExecutableSections = NULL;
ULONG nExecutableSections = 0;
PVOID pDeviceExtension = NULL;
SIZE_T Span = 0;
ULONG_PTR SearchEnd = 0;
PCONNECT_DATA pConnectData = NULL;
ULONG i = 0;
ULONG_PTR SectionBase = 0;
BOOLEAN fClassServiceValidated = FALSE;
SIZE_T cbConnectDataFieldOffset = 0;
NTSTATUS ntstatus = STATUS_SUCCESS;
//
// Zero out parameters.
//
*pcbConnectDataFieldOffset = 0;
DBG_PRINT(
"Resolving MouHid connect data field offset for device."
" (DeviceObject = %p)",
pDeviceObject);
//
// Get a referenced pointer to the device object attached to the current
// MouHid device object.
//
pAttachedDevice = IouGetUpperDeviceObject(pDeviceObject);
if (!pAttachedDevice)
{
ERR_PRINT("Unexpected AttachedDevice. (DeviceObject = %p)",
pDeviceObject);
ntstatus = STATUS_UNSUCCESSFUL;
goto exit;
}
//
// Get the executable image sections for the driver of the attached device.
//
pDriverStart = pAttachedDevice->DriverObject->DriverStart;
if (!pDriverStart)
{
ERR_PRINT("Unexpected DriverStart. (DriverObject = %p)",
pAttachedDevice->DriverObject);
ntstatus = STATUS_UNSUCCESSFUL;
goto exit;
}
if (!RtlPcToFileHeader(pDriverStart, (PVOID*)&ImageBase))
{
ERR_PRINT("RtlPcToFileHeader failed. (PcValue = %p)", pDriverStart);
ntstatus = STATUS_UNSUCCESSFUL;
goto exit;
}
ntstatus = PeGetExecutableSections(
ImageBase,
&ppExecutableSections,
&nExecutableSections);
if (!NT_SUCCESS(ntstatus))
{
ERR_PRINT("PeGetExecutableSections failed: 0x%X", ntstatus);
goto exit;
}
pDeviceExtension = pDeviceObject->DeviceExtension;
//
// Clamp the search range.
//
Span = ADDRESS_AND_SIZE_TO_SPAN_PAGES(
pDeviceExtension,
DEVICE_EXTENSION_SEARCH_SIZE);
if (1 == Span)
{
SearchEnd =
(ULONG_PTR)pDeviceExtension + DEVICE_EXTENSION_SEARCH_SIZE;
}
else
{
SearchEnd = (ULONG_PTR)(
PAGE_ALIGN((ULONG_PTR)pDeviceExtension + PAGE_SIZE));
}
//
// Search the device extension for a valid connect data object.
//
for (pConnectData = (PCONNECT_DATA)pDeviceExtension;
(ULONG_PTR)pConnectData + sizeof(*pConnectData) <= SearchEnd;
pConnectData =
OFFSET_POINTER(pConnectData, sizeof(ULONG_PTR), CONNECT_DATA))
{
//
// Filter invalid ClassDeviceObject values.
//
if (pConnectData->ClassDeviceObject != pAttachedDevice)
{
continue;
}
//
// Filter ClassService values which do not point to an address inside
// an executable image section in the driver of the attached device
// object.
//
for (i = 0; i < nExecutableSections; ++i)
{
SectionBase = ImageBase + ppExecutableSections[i]->VirtualAddress;
if ((ULONG_PTR)pConnectData->ClassService >= SectionBase &&
(ULONG_PTR)pConnectData->ClassService <
SectionBase + ppExecutableSections[i]->Misc.VirtualSize)
{
fClassServiceValidated = TRUE;
break;
}
}
//
if (!fClassServiceValidated)
{
continue;
}
//
// If we found multiple matches then our heuristic is flawed.
//
if (cbConnectDataFieldOffset)
{
ERR_PRINT("Found multiple field offset candidates.");
ntstatus = STATUS_INTERNAL_ERROR;
goto exit;
}
cbConnectDataFieldOffset =
POINTER_OFFSET(pConnectData, pDeviceExtension);
}
//
if (!cbConnectDataFieldOffset)
{
ERR_PRINT(
"Failed to resolve connect data field offset for device."
" (DeviceObject = %p)",
pDeviceObject);
ntstatus = STATUS_UNSUCCESSFUL;
goto exit;
}
//
// Set out parameters.
//
*pcbConnectDataFieldOffset = cbConnectDataFieldOffset;
exit:
if (ppExecutableSections)
{
ExFreePool(ppExecutableSections);
}
if (pAttachedDevice)
{
ObDereferenceObject(pAttachedDevice);
}
return ntstatus;
}
_Use_decl_annotations_
EXTERN_C
static
NTSTATUS
MhdpResolveConnectDataFieldOffset(
PSIZE_T pcbConnectDataFieldOffset
)
{
UNICODE_STRING usMouHidDriverObject = {};
PDRIVER_OBJECT pMouHidDriverObject = NULL;
BOOLEAN fHasDriverObjectReference = FALSE;
PDEVICE_OBJECT* ppMouHidDeviceObjectList = NULL;
ULONG nMouHidDeviceObjectList = 0;
ULONG i = 0;
SIZE_T cbFieldOffset = 0;
SIZE_T cbFieldOffsetCandidate = 0;
NTSTATUS ntstatus = STATUS_SUCCESS;
//
// Zero out parameters.
//
*pcbConnectDataFieldOffset = 0;
DBG_PRINT("Resolving MouHid connect data field offset.");
//
// Open the MouHid driver object.
//
usMouHidDriverObject = RTL_CONSTANT_STRING(MOUHID_DRIVER_OBJECT_PATH_U);
ntstatus = ObReferenceObjectByName(
&usMouHidDriverObject,
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
NULL,
0,
*IoDriverObjectType,
KernelMode,
NULL,
(PVOID*)&pMouHidDriverObject);
if (!NT_SUCCESS(ntstatus))
{
ERR_PRINT("ObReferenceObjectByName failed: 0x%X", ntstatus);
goto exit;
}
//
fHasDriverObjectReference = TRUE;
//
// Get a snapshot of all the MouHid device objects.
//
ntstatus = IouEnumerateDeviceObjectList(
pMouHidDriverObject,
&ppMouHidDeviceObjectList,
&nMouHidDeviceObjectList);
if (!NT_SUCCESS(ntstatus))
{
ERR_PRINT("IouEnumerateDeviceObjectList failed: 0x%X", ntstatus);
goto exit;
}
#if defined(DBG)
IouPrintDeviceObjectList(
MOUHID_DRIVER_OBJECT_PATH_U,
ppMouHidDeviceObjectList,
nMouHidDeviceObjectList);
#endif
//
// Apply the connect data heuristic to every MouHid device object to verify
// that each device object yields the same offset.
//
for (i = 0; i < nMouHidDeviceObjectList; ++i)
{
ntstatus = MhdpResolveConnectDataFieldOffsetForDevice(
ppMouHidDeviceObjectList[i],
&cbFieldOffset);
if (!NT_SUCCESS(ntstatus))
{
ERR_PRINT(
"MhdpResolveConnectDataFieldOffsetForDevice failed: 0x%X",
ntstatus);
goto exit;
}
if (cbFieldOffsetCandidate)
{
if (cbFieldOffsetCandidate != cbFieldOffset)
{
ERR_PRINT("Found multiple field offset candidates.");
ntstatus = STATUS_INTERNAL_ERROR;
goto exit;
}
}
else
{
cbFieldOffsetCandidate = cbFieldOffset;
}
}
//
if (!cbFieldOffsetCandidate)
{
ERR_PRINT("Failed to resolve connect data field offset.");
ntstatus = STATUS_UNSUCCESSFUL;
goto exit;
}
//
// Set out parameters.
//
*pcbConnectDataFieldOffset = cbFieldOffsetCandidate;
exit:
if (ppMouHidDeviceObjectList)
{
IouFreeDeviceObjectList(
ppMouHidDeviceObjectList,
nMouHidDeviceObjectList);
}
if (fHasDriverObjectReference)
{
ObDereferenceObject(pMouHidDriverObject);
}
return ntstatus;
}