forked from vRare/AutoSpitta-x64
-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyboardhook.c
332 lines (252 loc) · 8.48 KB
/
keyboardhook.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
#include "shared.h"
#include "keyboardhook.h"
#include <stdio.h>
#define Tag 'KYBD'
#define KBDCLASS_0 0x1
#define KBDCLASS_1 0x2
#define KBDCLASS_2 0x4
#define ALLOC_SIZE 0x1000
KeyboardServiceDpc KeyboardDpcRoutine=NULL;
kbdinput KeyboardInputRoutine=NULL;
PKEYBOARD_INPUT_DATA kbdIrp=NULL;
KEYBOARD_INPUT_DATA kdata;
ULONG g_kbdclsnum;
LONG g_StartCount = 0;
ULONG g_nop = 0;
static PDEVICE_OBJECT g_KbdDeviceObject = NULL;
static PDEVICE_OBJECT g_IOKbdDeviceObject = NULL;
static PVOID g_node=0;
PDEVICE_OBJECT g_TopOfStack;
IRPMJREAD g_OldReadFunction = NULL;
IRPMJREAD g_OldInternalDeviceFunction = NULL;
ULONGLONG *g_keyboard_rootine = NULL;
char KEY_DATA[256];
ULONG kbdId=0;
static int keyboard_inited = 0;
NTSTATUS Keyboard_Create(IN PDRIVER_OBJECT driverObject)
{
NTSTATUS status = STATUS_SUCCESS;
UNICODE_STRING uniKbdDeviceName;
PDEVICE_OBJECT devicePtr;
KeyboardAddDevice KeyboardAddDevicePtr;
ULONGLONG node=0;
int i;
memset((void*)&kdata,0,sizeof(kdata));
for(i=0; i<256; i++) KEY_DATA[i]=0;
RtlInitUnicodeString(&uniKbdDeviceName,L"\\Device\\IOKeyboard");
status = IoCreateDevice(driverObject,0,&uniKbdDeviceName,FILE_DEVICE_UNKNOWN,FILE_DEVICE_SECURE_OPEN,FALSE,&g_IOKbdDeviceObject);
if(!NT_SUCCESS(status)) {
return status;
}
g_IOKbdDeviceObject->Flags|=DO_BUFFERED_IO;
g_IOKbdDeviceObject->Flags&=~DO_DEVICE_INITIALIZING;
status = Keyboard_Hook(driverObject);
if(!NT_SUCCESS(status)) {
return status;
}
FindDevNodeRecurse(g_KbdDeviceObject,&node);
if(g_IOKbdDeviceObject->DeviceObjectExtension) {
g_node = g_IOKbdDeviceObject->DeviceObjectExtension->DeviceNode;
g_IOKbdDeviceObject->DeviceObjectExtension->DeviceNode=(PVOID)node;
}
if(g_KbdDeviceObject->DriverObject) {
if(g_KbdDeviceObject->DriverObject->DriverExtension) {
KeyboardAddDevicePtr=(KeyboardAddDevice)g_KbdDeviceObject->DriverObject->DriverExtension->AddDevice;
if(KeyboardAddDevicePtr) {
KeyboardAddDevicePtr(g_KbdDeviceObject->DriverObject,g_IOKbdDeviceObject);
}
}
}
DbgPrintEx( DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL,"Keyboard_Init ----> Keyboard_Init status[%x].\n", status);
keyboard_inited = 1;
return status;
}
NTSTATUS Keyboard_Close(IN PDRIVER_OBJECT driverObject)
{
NTSTATUS status = STATUS_SUCCESS;
if( keyboard_inited == 0 ) return status;
Keyboard_UnHook(driverObject);
if(g_IOKbdDeviceObject) {
if(g_IOKbdDeviceObject->DeviceObjectExtension) g_IOKbdDeviceObject->DeviceObjectExtension->DeviceNode = g_node;
IoDeleteDevice(g_IOKbdDeviceObject);
g_IOKbdDeviceObject = NULL;
}
keyboard_inited = 0;
return status;
}
NTSTATUS Keyboard_Hook(IN PDRIVER_OBJECT driverObject)
{
ULONG i;
int found = 0;
NTSTATUS status = STATUS_SUCCESS;
PIO_STACK_LOCATION stack;
UNICODE_STRING uniKbdDeviceName;
PFILE_OBJECT KbdFileObject;
PDEVICE_OBJECT KbdDeviceObject;
ULONG counter;
HANDLE hDir;
OBJECT_ATTRIBUTES oa;
UNICODE_STRING uniOa;
PVOID pBuffer;
PVOID pContext;
ULONG RetLen;
PDIRECTORY_BASIC_INFORMATION pDirBasicInfo;
UNICODE_STRING uniKbdDrv;
char* KbdclsNum;
char arKbdCls[0x10];
WCHAR tmpNameBuffer[512];
DbgPrint("Running Keyboard_Create.");
RtlInitUnicodeString(&uniOa, L"\\Device");
InitializeObjectAttributes(&oa, &uniOa, OBJ_CASE_INSENSITIVE, NULL, NULL);
status = ZwOpenDirectoryObject( &hDir, DIRECTORY_ALL_ACCESS, &oa );
if(!NT_SUCCESS(status)) return status;
pBuffer = ExAllocatePoolWithTag(PagedPool, ALLOC_SIZE, Tag);
pContext = ExAllocatePoolWithTag(PagedPool, ALLOC_SIZE, Tag);
memset(pBuffer, 0, ALLOC_SIZE);
memset(pContext, 0, ALLOC_SIZE);
memset(arKbdCls, 0, 0x10);
counter = 0;
g_kbdclsnum = 0;
while(TRUE) {
status = ZwQueryDirectoryObject( hDir, pBuffer, ALLOC_SIZE, TRUE, FALSE, pContext, &RetLen );
if(!NT_SUCCESS(status)) break;
pDirBasicInfo = (PDIRECTORY_BASIC_INFORMATION)pBuffer;
pDirBasicInfo->ObjectName.Length -= 2;
RtlInitUnicodeString(&uniKbdDrv, L"KeyboardClass");
if(RtlCompareUnicodeString( &pDirBasicInfo->ObjectName, &uniKbdDrv, FALSE ) == 0)
{
kbdId = (ULONG)(*(char *)(pDirBasicInfo->ObjectName.Buffer+pDirBasicInfo->ObjectName.Length));
kbdId -= 0x30;
pDirBasicInfo->ObjectName.Length += 2;
RtlInitUnicodeString(&uniKbdDeviceName, pDirBasicInfo->ObjectName.Buffer);
found = 1;
break;
}
pDirBasicInfo->ObjectName.Length += 2;
}
ExFreePool(pBuffer);
ExFreePool(pContext);
ZwClose(hDir);
if( found == 0 ) return status;
DbgPrintEx( DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL,"Keyboard_Hook ----> kbdId[%d].\n", kbdId);
swprintf(tmpNameBuffer, L"\\Device\\%s", uniKbdDeviceName.Buffer);
RtlInitUnicodeString(&uniKbdDeviceName, tmpNameBuffer);
status = My_IoGetDeviceObjectPointer( &uniKbdDeviceName, FILE_ALL_ACCESS, &KbdFileObject, &KbdDeviceObject );
if(NT_SUCCESS(status))
{
g_KbdDeviceObject = KbdDeviceObject;
ObDereferenceObject(KbdFileObject);
g_OldReadFunction = g_KbdDeviceObject->DriverObject->MajorFunction[IRP_MJ_READ];
g_KbdDeviceObject->DriverObject->MajorFunction[IRP_MJ_READ] = Keyboard_HookProc;
g_OldInternalDeviceFunction = g_KbdDeviceObject->DriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL];
g_KbdDeviceObject->DriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = Keyboard_IO_InternalIoctl;
DbgPrintEx( DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL,"KeyBoard_Create ----> KeyBoard_HookProc success.\n");
}
return STATUS_SUCCESS;
}
NTSTATUS Keyboard_UnHook(IN PDRIVER_OBJECT driverObject)
{
ULONG i;
NTSTATUS status=STATUS_SUCCESS;
PIO_STACK_LOCATION stack;
UNICODE_STRING uniKbdDeviceName;
PFILE_OBJECT KbdFileObject;
PDEVICE_OBJECT KbdDeviceObject;
ULONG counter;
HANDLE hDir;
OBJECT_ATTRIBUTES oa;
UNICODE_STRING uniOa;
PVOID pBuffer;
PVOID pContext;
ULONG RetLen;
PDIRECTORY_BASIC_INFORMATION pDirBasicInfo;
UNICODE_STRING uniKbdDrv;
char* KbdclsNum;
char arKbdCls[0x10];
DbgPrint("Running Keyboard_Close.");
if( keyboard_inited == 0 ) return status;
if(g_keyboard_rootine) *g_keyboard_rootine = (ULONGLONG)KeyboardInputRoutine;
if(g_OldReadFunction)
{
g_KbdDeviceObject->DriverObject->MajorFunction[IRP_MJ_READ] = g_OldReadFunction;
g_OldReadFunction = NULL;
}
if(g_OldInternalDeviceFunction)
{
g_KbdDeviceObject->DriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = g_OldInternalDeviceFunction;
g_OldInternalDeviceFunction = NULL;
}
return STATUS_SUCCESS;
}
NTSTATUS KeyboardApc(void *a1, void *a2, void *a3, void *a4, void *a5)
{
unsigned char max=(unsigned char)kbdIrp->MakeCode;
if(!kbdIrp->Flags)
{
KEY_DATA[(max)-1]=1;
}
else if(kbdIrp->Flags&KEY_BREAK)
{
KEY_DATA[(max)-1]=0;
}
//DbgPrint("KeyboardApc: max=%x", max);
return KeyboardInputRoutine(a1,a2,a3,a4,a5);
}
NTSTATUS Keyboard_HookProc( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp )
{
PIO_STACK_LOCATION ios;
PCONNECT_DATA cd;
NTSTATUS status;
ULONGLONG *routine;
// DbgPrint("KeyBoard_HookProc: success");
ios=IoGetCurrentIrpStackLocation(Irp);
if(ios->Parameters.DeviceIoControl.IoControlCode==KBDCLASS_CONNECT_REQUEST && KeyboardDpcRoutine == NULL)
{
cd=ios->Parameters.DeviceIoControl.Type3InputBuffer;
KeyboardDpcRoutine=(KeyboardServiceDpc)cd->ClassService;
DbgPrintEx( DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL,"Keyboard_IO_InternalIoctl ----> KeyboardDpcRoutine[%x]\n", KeyboardDpcRoutine);
}
routine=(ULONGLONG*)Irp;
routine+=0xb;
if(!KeyboardInputRoutine)
{
KeyboardInputRoutine=(kbdinput)*routine;
}
*routine=(ULONGLONG)KeyboardApc;
g_keyboard_rootine = routine;
kbdIrp=(PKEYBOARD_INPUT_DATA)Irp->UserBuffer;
status = g_OldReadFunction(DeviceObject, Irp);
return status;
}
NTSTATUS Keyboard_IO_InternalIoctl(PDEVICE_OBJECT device, PIRP irp)
{
PIO_STACK_LOCATION ios;
PCONNECT_DATA cd;
NTSTATUS status;
ios=IoGetCurrentIrpStackLocation(irp);
if(ios->Parameters.DeviceIoControl.IoControlCode==KBDCLASS_CONNECT_REQUEST)
{
cd=ios->Parameters.DeviceIoControl.Type3InputBuffer;
KeyboardDpcRoutine=(KeyboardServiceDpc)cd->ClassService;
DbgPrintEx( DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL,"Keyboard_IO_InternalIoctl ----> KeyboardDpcRoutine[%x]\n", KeyboardDpcRoutine);
}
//if(g_OldInternalDeviceFunction) status = g_OldInternalDeviceFunction(device, irp);
return STATUS_SUCCESS;
}
int GetKeyState(unsigned char scan)
{
if(KEY_DATA[scan-1]) return 1;
return 0;
}
void SynthesizeKeyboard(PKEYBOARD_INPUT_DATA a1)
{
KIRQL irql;
char *endptr;
ULONG fill=1;
endptr=(char*)a1;
endptr+=sizeof(KEYBOARD_INPUT_DATA);
a1->UnitId=(USHORT)kbdId;
KeRaiseIrql(DISPATCH_LEVEL,&irql);
if(KeyboardDpcRoutine) KeyboardDpcRoutine(g_KbdDeviceObject,a1,(PKEYBOARD_INPUT_DATA)endptr,&fill);
KeLowerIrql(irql);
}