-
Notifications
You must be signed in to change notification settings - Fork 106
/
shadow_ssdt.cpp
402 lines (330 loc) · 12.2 KB
/
shadow_ssdt.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
#include "stdafx.h"
PSYSTEM_SERVICE_TABLE g_KeServiceDescriptorTableShadow = NULL;
HANDLE hCsrssPID = HANDLE( -1 );
ULONGLONG GetKeServiceDescriptorTableShadow64()
{
PUCHAR StartSearchAddress = ( PUCHAR )__readmsr( 0xC0000082 );
PUCHAR EndSearchAddress = StartSearchAddress + 0x500;
PUCHAR i = NULL;
UCHAR b1 = 0, b2 = 0, b3 = 0;
ULONG templong = 0;
ULONGLONG addr = 0;
for ( i = StartSearchAddress; i < EndSearchAddress; i++ )
{
if ( MmIsAddressValid( i ) && MmIsAddressValid( i + 1 ) && MmIsAddressValid( i + 2 ) )
{
b1 = *i;
b2 = *( i + 1 );
b3 = *( i + 2 );
if ( b1 == 0x4c && b2 == 0x8d && b3 == 0x1d )
{
memcpy( &templong, i + 3, 4 );
addr = ( ULONGLONG )templong + ( ULONGLONG )i + 7;
return addr;
}
}
}
return 0;
}
ULONGLONG GetSSSDTFuncCurAddr64( ULONG64 Index )
{
ULONGLONG W32pServiceTable = 0, qwTemp = 0;
LONG dwTemp = 0;
W32pServiceTable = ( ULONGLONG )( g_KeServiceDescriptorTableShadow->ServiceTableBase );
qwTemp = W32pServiceTable + 4 * ( Index - 0x1000 );
dwTemp = *( PLONG )qwTemp;
dwTemp = dwTemp >> 4;
qwTemp = W32pServiceTable + ( LONG64 )dwTemp;
return qwTemp;
}
bool HookSSSDT( PUCHAR pCode, ULONG ulCodeSize, PVOID pNewFunction, PVOID* pOldFunction, ULONG SyscallNum )
{
if ( !pNewFunction || !pOldFunction || SyscallNum <= 0 )
return false;
ULONGLONG W32pServiceTable = 0, qwTemp = 0;
LONG dwTemp = 0;
KIRQL irql;
//
// Log the Syscall number that we're hooking
//
DBGPRINT( "[ HookSSSDT ] Syscall: 0x%X\n", SyscallNum );
//
// Log the Original function address
//
*pOldFunction = PVOID( GetSSSDTFuncCurAddr64( SyscallNum ) );
DBGPRINT( "[ HookSSSDT ] Original: 0x%p\n", *pOldFunction );
*( PULONG64 )( jmp_trampoline + 3 ) = ULONG64( pNewFunction );
//
// Find a suitable code cave inside the module .text section that we can use to trampoline to our hook
//
auto pCodeCave = utils::FindCodeCave( pCode, ulCodeSize, sizeof( jmp_trampoline ) );
if ( !pCodeCave )
{
DBGPRINT( "[ HookSSSDT ] Failed to find a suitable code cave.\n" );
return false;
}
DBGPRINT( "[ HookSSSDT ] Code Cave: 0x%p\n", pCodeCave );
//
// Change page protection
//
auto Mdl = IoAllocateMdl( pCodeCave, sizeof( jmp_trampoline ), 0, 0, NULL );
if ( Mdl == NULL )
{
DBGPRINT( "[ HookSSSDT ] IoAllocateMdl failed!\n" );
return false;
}
MmProbeAndLockPages( Mdl, KernelMode, IoWriteAccess );
auto Mapping = MmMapLockedPagesSpecifyCache( Mdl, KernelMode, MmCached, NULL, FALSE, NormalPagePriority );
if ( Mapping == NULL )
{
MmUnlockPages( Mdl );
IoFreeMdl( Mdl );
DBGPRINT( "[ HookSSSDT ] MmMapLockedPagesSpecifyCache failed!\n" );
return false;
}
//
// Modify SSSDT table
//
irql = utils::WPOFF();
RtlCopyMemory( Mapping, jmp_trampoline, sizeof( jmp_trampoline ) );
W32pServiceTable = ( ULONGLONG )( g_KeServiceDescriptorTableShadow->ServiceTableBase );
qwTemp = W32pServiceTable + 4 * ( SyscallNum - 0x1000 );
dwTemp = ( LONG )( ( ULONG64 )pCodeCave - W32pServiceTable );
dwTemp = dwTemp << 4;
*( PLONG )qwTemp = dwTemp;
utils::WPON( irql );
//
// Restore protection
//
MmUnmapLockedPages( Mapping, Mdl );
MmUnlockPages( Mdl );
IoFreeMdl( Mdl );
return true;
}
bool UnhookSSSDT( PVOID pFunction, ULONG SyscallNum )
{
if ( !pFunction || SyscallNum <= 0 )
return false;
ULONGLONG W32pServiceTable = 0, qwTemp = 0;
LONG dwTemp = 0;
KIRQL irql;
irql = utils::WPOFF();
W32pServiceTable = ( ULONGLONG )( g_KeServiceDescriptorTableShadow->ServiceTableBase );
qwTemp = W32pServiceTable + 4 * ( SyscallNum - 0x1000 );
dwTemp = ( LONG )( ( ULONG64 )pFunction - W32pServiceTable );
dwTemp = dwTemp << 4;
*( PLONG )qwTemp = dwTemp;
utils::WPON( irql );
return true;
}
PSYSTEM_HANDLE_INFORMATION_EX GetSystemHandleInformation()
{
PSYSTEM_HANDLE_INFORMATION_EX pSHInfo = NULL;
NTSTATUS Status = STATUS_NO_MEMORY;
ULONG SMInfoLen = 0x1000;
do
{
pSHInfo = ( PSYSTEM_HANDLE_INFORMATION_EX )ExAllocatePoolWithTag( PagedPool, SMInfoLen, TAG );
if ( !pSHInfo )
break;
Status = ZwQuerySystemInformation( SystemHandleInformation, pSHInfo, SMInfoLen, &SMInfoLen );
if ( !NT_SUCCESS( Status ) )
{
ExFreePoolWithTag( pSHInfo, TAG );
pSHInfo = NULL;
}
} while ( Status == STATUS_INFO_LENGTH_MISMATCH );
return pSHInfo;
}
HANDLE GetCsrssPid()
{
HANDLE CsrId = ( HANDLE )0;
PSYSTEM_HANDLE_INFORMATION_EX pHandles = GetSystemHandleInformation();
if ( pHandles )
{
unsigned i;
for ( i = 0; i < pHandles->NumberOfHandles && !CsrId; i++ )
{
OBJECT_ATTRIBUTES obj; CLIENT_ID cid;
HANDLE Process, hObject;
InitializeObjectAttributes( &obj, NULL, OBJ_KERNEL_HANDLE, NULL, NULL );
cid.UniqueProcess = ( HANDLE )pHandles->Information[ i ].ProcessId;
cid.UniqueThread = 0;
auto res = ZwOpenProcess( &Process, PROCESS_DUP_HANDLE, &obj, &cid );
if ( NT_SUCCESS( res ) )
{
res = ZwDuplicateObject( Process, ( PHANDLE )( pHandles->Information[ i ].Handle ), NtCurrentProcess(), &hObject, 0, FALSE, DUPLICATE_SAME_ACCESS );
if ( NT_SUCCESS( res ) )
{
UCHAR Buff[ 0x200 ];
POBJECT_NAME_INFORMATION ObjName = ( POBJECT_NAME_INFORMATION )&Buff;
res = ZwQueryObject( hObject, ObjectTypeInformation, ObjName, sizeof( Buff ), NULL );
if ( NT_SUCCESS( res ) )
{
if ( ObjName->Name.Buffer && ( !wcsncmp( L"Port", ObjName->Name.Buffer, 4 ) || !wcsncmp( L"ALPC Port", ObjName->Name.Buffer, 9 ) ) )
{
res = ZwQueryObject( hObject, ( OBJECT_INFORMATION_CLASS )1, ObjName, sizeof( Buff ), NULL );
if ( NT_SUCCESS( res ) )
{
if ( ObjName->Name.Buffer && !wcsncmp( L"\\Windows\\ApiPort", ObjName->Name.Buffer, 20 ) )
CsrId = ( HANDLE )pHandles->Information[ i ].ProcessId;
}
}
}
else
DBGPRINT( "[ GetCsr ] ZwQueryObject failed 0x%X\n", res );
ZwClose( hObject );
}
else if ( res != STATUS_NOT_SUPPORTED )
DBGPRINT( "[ GetCsr ] ZwDuplicateObject failed 0x%X\n", res );
ZwClose( Process );
}
else
DBGPRINT( "[ GetCsr ] NtOpenProcess failed 0x%X\n", res );
}
ExFreePoolWithTag( pHandles, TAG );
}
return CsrId;
}
void sssdt::Init()
{
#ifndef USE_KASPERSKY
g_KeServiceDescriptorTableShadow = PSYSTEM_SERVICE_TABLE( GetKeServiceDescriptorTableShadow64() + sizeof( SYSTEM_SERVICE_TABLE ) );
DBGPRINT( "KeServiceDescriptorTableShadow: 0x%p\n", g_KeServiceDescriptorTableShadow );
if ( !g_KeServiceDescriptorTableShadow )
return;
auto W32pServiceTable = PULONG( g_KeServiceDescriptorTableShadow->ServiceTableBase );
DBGPRINT( "KeServiceDescriptorTableShadow->ServiceTableBase: 0x%p\n", W32pServiceTable );
if ( !W32pServiceTable )
return;
DBGPRINT( "KeServiceDescriptorTableShadow->NumberOfServices: %lld\n", g_KeServiceDescriptorTableShadow->NumberOfServices );
auto Csrss = GetCsrssPid();
PEPROCESS Process = nullptr;
auto res = PsLookupProcessByProcessId( Csrss, &Process );
if ( !NT_SUCCESS( res ) )
{
DBGPRINT( "[ ShadowSSDT ] PsLookupProcessByProcessId failed 0x%X\n", res );
return;
}
//
// Save csrss.exe PID for later
//
hCsrssPID = Csrss;
KAPC_STATE apc{ };
KeStackAttachProcess( Process, &apc );
auto win32k = ULONG64( tools::GetModuleBase( "\\SystemRoot\\System32\\win32k.sys" ) );
DBGPRINT( "win32k: 0x%llx\n", win32k );
if ( !win32k )
return;
ULONG ulCodeSize = 0;
auto pCode = PUCHAR( tools::GetImageTextSection( win32k, &ulCodeSize ) );
if ( pCode )
{
DBGPRINT( "win32k.sys .text section 0x%p\n", pCode );
if ( HookSSSDT( pCode, ulCodeSize, &hkNtUserQueryWindow, reinterpret_cast< PVOID* >( &oNtUserQueryWindow ), SYSCALL_NTUSERQUERYWND ) )
{
DBGPRINT( "NtUserQueryWindow hooked successfully!\n" );
}
else
DBGPRINT( "Failed to hook NtUserQueryWindow!\n" );
if ( HookSSSDT( pCode, ulCodeSize, &hkNtUserFindWindowEx, reinterpret_cast< PVOID* >( &oNtUserFindWindowEx ), SYSCALL_NTUSERFINDWNDEX ) )
{
DBGPRINT( "NtUserFindWindowEx hooked successfully!\n" );
}
else
DBGPRINT( "Failed to hook NtUserFindWindowEx!\n" );
if ( HookSSSDT( pCode, ulCodeSize, &hkNtUserWindowFromPoint, reinterpret_cast< PVOID* >( &oNtUserWindowFromPoint ), SYSCALL_NTUSERWNDFROMPOINT ) )
{
DBGPRINT( "NtUserWindowFromPoint hooked successfully!\n" );
}
else
DBGPRINT( "Failed to hook NtUserWindowFromPoint!\n" );
if ( HookSSSDT( pCode, ulCodeSize, &hkNtUserBuildHwndList, reinterpret_cast< PVOID* >( &oNtUserBuildHwndList ), SYSCALL_NTUSERBUILDWNDLIST ) )
{
DBGPRINT( "NtUserBuildHwndList hooked successfully!\n" );
}
else
DBGPRINT( "Failed to hook NtUserBuildHwndList!\n" );
if ( HookSSSDT( pCode, ulCodeSize, &hkNtUserGetForegroundWindow, reinterpret_cast< PVOID* >( &oNtUserGetForegroundWindow ), SYSCALL_NTGETFOREGROUNDWND ) )
{
DBGPRINT( "NtUserGetForegroundWindow hooked successfully!\n" );
}
else
DBGPRINT( "Failed to hook NtUserGetForegroundWindow!\n" );
}
KeUnstackDetachProcess( &apc );
ObDereferenceObject( Process );
#else
if ( kaspersky::hook_shadow_ssdt_routine( SYSCALL_NTUSERQUERYWND, hkNtUserQueryWindow, reinterpret_cast< PVOID* >( &oNtUserQueryWindow ) ) )
{
DBGPRINT( "NtUserQueryWindow ( 0x%X ) hooked successfully!\n", SYSCALL_NTUSERQUERYWND );
}
else
DBGPRINT( "Failed to hook NtUserQueryWindow!\n" );
if ( kaspersky::hook_shadow_ssdt_routine( SYSCALL_NTUSERFINDWNDEX, hkNtUserFindWindowEx, reinterpret_cast< PVOID* >( &oNtUserFindWindowEx ) ) )
{
DBGPRINT( "NtUserFindWindowEx ( 0x%X ) hooked successfully!\n", SYSCALL_NTUSERFINDWNDEX );
}
else
DBGPRINT( "Failed to hook NtUserFindWindowEx!\n" );
if ( kaspersky::hook_shadow_ssdt_routine( SYSCALL_NTUSERWNDFROMPOINT, hkNtUserWindowFromPoint, reinterpret_cast< PVOID* >( &oNtUserWindowFromPoint ) ) )
{
DBGPRINT( "NtUserWindowFromPoint ( 0x%X ) hooked successfully!\n", SYSCALL_NTUSERWNDFROMPOINT );
}
else
DBGPRINT( "Failed to hook NtUserWindowFromPoint!\n" );
if ( kaspersky::hook_shadow_ssdt_routine( SYSCALL_NTUSERBUILDWNDLIST, hkNtUserBuildHwndList, reinterpret_cast< PVOID* >( &oNtUserBuildHwndList ) ) )
{
DBGPRINT( "NtUserBuildHwndList ( 0x%X ) hooked successfully!\n", SYSCALL_NTUSERBUILDWNDLIST );
}
else
DBGPRINT( "Failed to hook NtUserBuildHwndList!\n" );
if ( kaspersky::hook_shadow_ssdt_routine( SYSCALL_NTGETFOREGROUNDWND, hkNtUserGetForegroundWindow, reinterpret_cast< PVOID* >( &oNtUserGetForegroundWindow ) ) )
{
DBGPRINT( "NtUserGetForegroundWindow ( 0x%X ) hooked successfully!\n", SYSCALL_NTGETFOREGROUNDWND );
}
else
DBGPRINT( "Failed to hook NtUserGetForegroundWindow!\n" );
#endif
}
void sssdt::Destroy()
{
#ifndef USE_KASPERSKY
if ( !g_KeServiceDescriptorTableShadow )
return;
PEPROCESS Process = nullptr;
auto res = PsLookupProcessByProcessId( hCsrssPID, &Process );
if ( !NT_SUCCESS( res ) )
{
DBGPRINT( "[ DestroyShadowSSDT ] PsLookupProcessByProcessId failed 0x%X\n", res );
return;
}
KAPC_STATE apc{ };
KeStackAttachProcess( Process, &apc );
if ( !UnhookSSSDT( oNtUserFindWindowEx, SYSCALL_NTUSERFINDWNDEX ) )
DBGPRINT( "Failed to unhook NtUserFindWindowEx!\n" );
if ( !UnhookSSSDT( oNtUserWindowFromPoint, SYSCALL_NTUSERWNDFROMPOINT ) )
DBGPRINT( "Failed to unhook NtUserWindowFromPoint!\n" );
if ( !UnhookSSSDT( oNtUserBuildHwndList, SYSCALL_NTUSERBUILDWNDLIST ) )
DBGPRINT( "Failed to unhook NtUserBuildHwndList!\n" );
if ( !UnhookSSSDT( oNtUserGetForegroundWindow, SYSCALL_NTGETFOREGROUNDWND ) )
DBGPRINT( "Failed to unhook NtUserGetForegroundWindow!\n" );
if ( !UnhookSSSDT( oNtUserQueryWindow, SYSCALL_NTUSERQUERYWND ) )
DBGPRINT( "Failed to unhook NtUserQueryWindow!\n" );
KeUnstackDetachProcess( &apc );
ObDereferenceObject( Process );
#else
if ( !kaspersky::is_klhk_loaded() )
return;
if ( !kaspersky::unhook_shadow_ssdt_routine( SYSCALL_NTUSERBUILDWNDLIST, oNtUserBuildHwndList ) )
DBGPRINT( "Failed to unhook NtUserBuildHwndList" );
if ( !kaspersky::unhook_shadow_ssdt_routine( SYSCALL_NTUSERWNDFROMPOINT, oNtUserWindowFromPoint ) )
DBGPRINT( "Failed to unhook NtUserWindowFromPoint" );
if ( !kaspersky::unhook_shadow_ssdt_routine( SYSCALL_NTUSERFINDWNDEX, oNtUserFindWindowEx ) )
DBGPRINT( "Failed to unhook NtUserFindWindowEx" );
if ( !kaspersky::unhook_shadow_ssdt_routine( SYSCALL_NTGETFOREGROUNDWND, oNtUserGetForegroundWindow ) )
DBGPRINT( "Failed to unhook NtUserGetForegroundWindow" );
if ( !kaspersky::unhook_shadow_ssdt_routine( SYSCALL_NTUSERQUERYWND, oNtUserQueryWindow ) )
DBGPRINT( "Failed to unhook NtUserQueryWindow" );
#endif
}