-
Notifications
You must be signed in to change notification settings - Fork 107
/
tools.cpp
586 lines (477 loc) · 14.5 KB
/
tools.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#include "stdafx.h"
PUCHAR ntdll = nullptr;
PUCHAR win32u = nullptr;
namespace masterhide
{
namespace tools
{
bool GetProcessName( HANDLE PID, PUNICODE_STRING ProcessImageName )
{
KAPC_STATE apc{ };
bool bReturn = false;
if ( !ProcessImageName )
return false;
PEPROCESS Process = nullptr;
auto status = PsLookupProcessByProcessId( PID, &Process );
if ( !NT_SUCCESS( status ) )
return false;
KeStackAttachProcess( Process, &apc );
//
// Credits: iPower
//
wchar_t lpModuleName[ MAX_PATH ];
status = ZwQueryVirtualMemory( NtCurrentProcess(), PsGetProcessSectionBaseAddress( Process ), ( MEMORY_INFORMATION_CLASS )2, lpModuleName, sizeof( lpModuleName ), NULL );
if ( NT_SUCCESS( status ) )
{
PUNICODE_STRING pModuleName = ( PUNICODE_STRING )lpModuleName;
if ( pModuleName->Length > 0 )
{
AllocateUnicodeString( ProcessImageName, pModuleName->MaximumLength );
RtlCopyUnicodeString( ProcessImageName, pModuleName );
bReturn = true;
}
}
KeUnstackDetachProcess( &apc );
ObDereferenceObject( Process );
return bReturn;
}
bool GetProcessNameByPEPROCESS( PEPROCESS Process, PUNICODE_STRING ProcessImageName )
{
KAPC_STATE apc{ };
bool bReturn = false;
bool bAttached = false;
if ( !ProcessImageName )
return false;
if ( Process != PsGetCurrentProcess() )
{
KeStackAttachProcess( Process, &apc );
bAttached = true;
}
wchar_t lpModuleName[ MAX_PATH ];
auto status = ZwQueryVirtualMemory( NtCurrentProcess(), PsGetProcessSectionBaseAddress( Process ), ( MEMORY_INFORMATION_CLASS )2, lpModuleName, sizeof( lpModuleName ), NULL );
if ( NT_SUCCESS( status ) )
{
PUNICODE_STRING pModuleName = ( PUNICODE_STRING )lpModuleName;
if ( pModuleName->Length > 0 )
{
AllocateUnicodeString( ProcessImageName, pModuleName->MaximumLength );
RtlCopyUnicodeString( ProcessImageName, pModuleName );
bReturn = true;
}
}
if ( bAttached )
KeUnstackDetachProcess( &apc );
return bReturn;
}
PEPROCESS FindPEPROCESSById( PWCH wsName )
{
if ( !wsName )
return nullptr;
for ( unsigned i = 4; i < 0xFFFF; i += 0x4 )
{
PEPROCESS Process = nullptr;
if ( !NT_SUCCESS( PsLookupProcessByProcessId( HANDLE( i ), &Process ) ) )
continue;
UNICODE_STRING wsProcName{ };
if ( !GetProcessNameByPEPROCESS( Process, &wsProcName ) )
{
ObDereferenceObject( Process );
continue;
}
if ( wsProcName.Buffer && wcsstr( wsProcName.Buffer, wsName ) )
return Process;
ObDereferenceObject( Process );
}
return nullptr;
}
bool DumpMZ( PUCHAR pImageBase )
{
__try
{
if ( !pImageBase )
{
DBGPRINT( "[ DumpMZ ] Invalid image base!\n" );
return false;
}
ProbeForRead( pImageBase, sizeof( pImageBase ), __alignof( pImageBase ) );
PIMAGE_DOS_HEADER dos = PIMAGE_DOS_HEADER( pImageBase );
if ( dos->e_magic != IMAGE_DOS_SIGNATURE )
{
DBGPRINT( "[ DumpMZ ] Invalid DOS signature!\n" );
return false;
}
PIMAGE_NT_HEADERS32 nt32 = PIMAGE_NT_HEADERS32( pImageBase + dos->e_lfanew );
if ( nt32->Signature != IMAGE_NT_SIGNATURE )
{
DBGPRINT( "[ DumpMZ ] Invalid NT signature!\n" );
return false;
}
ULONG uImageSize = NULL;
if ( nt32->FileHeader.Machine == IMAGE_FILE_MACHINE_I386 )
{
uImageSize = nt32->OptionalHeader.SizeOfImage;
}
else
{
PIMAGE_NT_HEADERS64 nt64 = PIMAGE_NT_HEADERS64( pImageBase + dos->e_lfanew );
uImageSize = nt64->OptionalHeader.SizeOfImage;
}
if ( KeGetCurrentIrql() != PASSIVE_LEVEL )
{
DBGPRINT( "[ DumpMZ ] Curerent IRQL too high for IO operations!\n" );
return false;
}
DBGPRINT( "[ DumpMZ ] ImageBase: 0x%p\n", pImageBase );
DBGPRINT( "[ DumpMZ ] ImageSize: 0x%X\n", uImageSize );
wchar_t wsFilePath[ MAX_PATH ]{ };
RtlStringCbPrintfW( wsFilePath, sizeof( wsFilePath ), L"\\SystemRoot\\Dumped_%p.dll", pImageBase );
DBGPRINT( "[ DumpMZ ] Save Location: %ws\n", wsFilePath );
UNICODE_STRING wsFinalPath{ };
RtlInitUnicodeString( &wsFinalPath, wsFilePath );
OBJECT_ATTRIBUTES oa{ };
InitializeObjectAttributes( &oa, &wsFinalPath, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL );
IO_STATUS_BLOCK io{ };
HANDLE hFile{ };
auto res = ZwCreateFile( &hFile, GENERIC_WRITE, &oa, &io, NULL,
FILE_ATTRIBUTE_NORMAL,
0,
FILE_OVERWRITE_IF,
FILE_SYNCHRONOUS_IO_NONALERT,
NULL, 0 );
if ( !NT_SUCCESS( res ) )
{
DBGPRINT( "[ DumpMZ ] ZwCreateFile failed 0x%X\n", res );
return false;
}
res = ZwWriteFile( hFile, NULL, NULL, NULL, &io, pImageBase, uImageSize, NULL, NULL );
if ( !NT_SUCCESS( res ) )
{
ZwClose( hFile );
DBGPRINT( "[ DumpMZ ] ZwWriteFile failed 0x%X\n", res );
return false;
}
DBGPRINT( "[ DumpMZ ] Dump success!\n" );
ZwClose( hFile );
return false;
}
__except ( EXCEPTION_EXECUTE_HANDLER )
{
return false;
}
}
PIMAGE_SECTION_HEADER GetSectionHeader( const ULONG64 image_base, const char* section_name )
{
if ( !image_base || !section_name )
return nullptr;
const auto pimage_dos_header = reinterpret_cast< PIMAGE_DOS_HEADER >( image_base );
const auto pimage_nt_headers = reinterpret_cast< PIMAGE_NT_HEADERS64 >( image_base + pimage_dos_header->e_lfanew );
auto psection = IMAGE_FIRST_SECTION( pimage_nt_headers );
PIMAGE_SECTION_HEADER psection_hdr = nullptr;
const auto NumberOfSections = pimage_nt_headers->FileHeader.NumberOfSections;
for ( auto i = 0; i < NumberOfSections; ++i )
{
if ( strstr( ( char* )psection->Name, section_name ) )
{
psection_hdr = psection;
break;
}
++psection;
}
return psection_hdr;
}
bool bDataCompare( const char* pdata, const char* bmask, const char* szmask )
{
for ( ; *szmask; ++szmask, ++pdata, ++bmask )
{
if ( *szmask == 'x' && *pdata != *bmask )
return false;
}
return !*szmask;
}
ULONG64 InternalFindPattern( const ULONG64 base, const ULONG size, const char* bmask, const char* szmask )
{
for ( auto i = 0ul; i < size; ++i )
if ( bDataCompare( PCHAR( base + i ), bmask, szmask ) )
return base + i;
return 0;
}
ULONG64 FindPatternKM( const char* szModuleName, const char* szsection, const char* bmask, const char* szmask )
{
if ( !szModuleName || !szsection || !bmask || !szmask )
return 0;
const auto module_base = ULONG64( GetModuleBase( szModuleName ) );
if ( !module_base )
return 0;
const auto psection = GetSectionHeader( module_base, szsection );
return psection ? InternalFindPattern( module_base + psection->VirtualAddress, psection->Misc.VirtualSize, bmask, szmask ) : 0;
}
PVOID GetImageTextSection( const ULONG64 uImageBase, ULONG* ulSectionSize )
{
if ( !uImageBase )
return nullptr;
const auto pText = GetSectionHeader( uImageBase, ".text" );
if ( !pText )
return nullptr;
if ( ulSectionSize )
*ulSectionSize = pText->Misc.VirtualSize;
return PVOID( uImageBase + pText->VirtualAddress );
}
PVOID GetNtKernelBase()
{
return GetModuleBase( "\\SystemRoot\\System32\\ntoskrnl.exe" );
}
PVOID GetModuleBase( const char* szModule )
{
PSYSTEM_MODULE_INFORMATION pSystemInfoBuffer = nullptr;
ULONG ulBytes = 0;
PVOID pImageBase = nullptr;
__try
{
auto status = ZwQuerySystemInformation( SystemModuleInformation, 0, ulBytes, &ulBytes );
if ( !ulBytes )
{
DBGPRINT( "[ GetModuleBase ] ZwQuerySystemInformation failed 0x%X\n", status );
return nullptr;
}
pSystemInfoBuffer = PSYSTEM_MODULE_INFORMATION( ExAllocatePoolWithTag( PagedPool, ulBytes, TAG ) );
if ( !pSystemInfoBuffer )
{
DBGPRINT( "[ GetModuleBase ] ExAllocatePoolWithTag failed!\n" );
return nullptr;
}
status = ZwQuerySystemInformation( SystemModuleInformation, pSystemInfoBuffer, ulBytes, &ulBytes );
if ( !NT_SUCCESS( status ) )
{
DBGPRINT( "[ GetModuleBase ] ZwQuerySystemInformation[1] failed 0x%X\n", status );
ExFreePoolWithTag( pSystemInfoBuffer, TAG );
return nullptr;
}
for ( unsigned i = 0; i < pSystemInfoBuffer->ModulesCount; ++i )
{
auto Buff = &pSystemInfoBuffer->Modules[ i ];
if ( !_stricmp( Buff->ImageName, szModule ) )
{
pImageBase = Buff->Base;
break;
}
}
}
__finally
{
if ( pSystemInfoBuffer )
ExFreePoolWithTag( pSystemInfoBuffer, TAG );
}
return pImageBase;
}
NTSTATUS LoadFile( PUNICODE_STRING FileName, PUCHAR* pImageBase )
{
if ( !FileName )
return STATUS_INVALID_PARAMETER;
OBJECT_ATTRIBUTES oa{ };
InitializeObjectAttributes( &oa, FileName, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL );
if ( KeGetCurrentIrql() != PASSIVE_LEVEL )
{
DBGPRINT( "[ LoadFile ] IRQL too high for IO operations!\n" );
return STATUS_UNSUCCESSFUL;
}
HANDLE FileHandle = NULL;
IO_STATUS_BLOCK IoStatusBlock{ };
auto res = ZwCreateFile( &FileHandle,
GENERIC_READ,
&oa,
&IoStatusBlock, NULL,
FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ,
FILE_OPEN,
FILE_SYNCHRONOUS_IO_NONALERT,
NULL, 0 );
if ( !NT_SUCCESS( res ) )
{
DBGPRINT( "[ LoadFile ] ZwCreateFile failed 0x%X\n", res );
return STATUS_UNSUCCESSFUL;
}
FILE_STANDARD_INFORMATION StandardInformation{ };
res = ZwQueryInformationFile( FileHandle, &IoStatusBlock, &StandardInformation, sizeof( FILE_STANDARD_INFORMATION ), FileStandardInformation );
if ( !NT_SUCCESS( res ) )
{
DBGPRINT( "[ LoadFile ] ZwQueryInformationFile failed 0x%X\n", res );
ZwClose( FileHandle );
return STATUS_UNSUCCESSFUL;
}
auto FileSize = StandardInformation.EndOfFile.LowPart;
auto FileBuffer = PUCHAR( ExAllocatePoolWithTag( NonPagedPool, FileSize, TAG ) );
if ( !FileBuffer )
{
DBGPRINT( "[ LoadFile ] ExAllocatePoolWithTag failed\n" );
ZwClose( FileHandle );
return STATUS_SUCCESS;
}
LARGE_INTEGER li{ };
res = ZwReadFile( FileHandle,
NULL, NULL, NULL,
&IoStatusBlock,
FileBuffer,
FileSize,
&li, NULL );
if ( !NT_SUCCESS( res ) )
{
DBGPRINT( "[ LoadFile ] ZwReadFile failed 0x%X\n", res );
ExFreePoolWithTag( FileBuffer, TAG );
ZwClose( FileHandle );
return STATUS_SUCCESS;
}
auto dos = PIMAGE_DOS_HEADER( FileBuffer );
if ( dos->e_magic != IMAGE_DOS_SIGNATURE )
{
DBGPRINT( "[ LoadFile ] Invalid DOS signature!\n" );
ExFreePoolWithTag( FileBuffer, TAG );
ZwClose( FileHandle );
return STATUS_SUCCESS;
}
auto nt = PIMAGE_NT_HEADERS64( FileBuffer + dos->e_lfanew );
if ( nt->Signature != IMAGE_NT_SIGNATURE )
{
DBGPRINT( "[ LoadFile ] Invalid NT signature!\n" );
ExFreePoolWithTag( FileBuffer, TAG );
ZwClose( FileHandle );
return STATUS_SUCCESS;
}
auto Image = PUCHAR( ExAllocatePoolWithTag( NonPagedPool, nt->OptionalHeader.SizeOfImage, TAG ) );
if ( !Image )
{
DBGPRINT( "[ LoadFile ] ExAllocatePoolWithTag[1] failed!\n" );
ExFreePoolWithTag( FileBuffer, TAG );
ZwClose( FileHandle );
return STATUS_SUCCESS;
}
memcpy( Image, FileBuffer, nt->OptionalHeader.SizeOfHeaders );
auto pISH = IMAGE_FIRST_SECTION( nt );
for ( unsigned i = 0; i < nt->FileHeader.NumberOfSections; i++ )
memcpy(
Image + pISH[ i ].VirtualAddress,
FileBuffer + pISH[ i ].PointerToRawData,
pISH[ i ].SizeOfRawData );
if ( pImageBase )
*pImageBase = Image;
ExFreePoolWithTag( FileBuffer, TAG );
ZwClose( FileHandle );
return STATUS_SUCCESS;
}
PVOID GetFunctionAddress( PVOID Module, LPCSTR FunctionName )
{
PIMAGE_DOS_HEADER pIDH;
PIMAGE_NT_HEADERS pINH;
PIMAGE_EXPORT_DIRECTORY pIED;
PULONG Address, Name;
PUSHORT Ordinal;
ULONG i;
pIDH = ( PIMAGE_DOS_HEADER )Module;
pINH = ( PIMAGE_NT_HEADERS )( ( PUCHAR )Module + pIDH->e_lfanew );
pIED = ( PIMAGE_EXPORT_DIRECTORY )( ( PUCHAR )Module + pINH->OptionalHeader.DataDirectory[ IMAGE_DIRECTORY_ENTRY_EXPORT ].VirtualAddress );
Address = ( PULONG )( ( PUCHAR )Module + pIED->AddressOfFunctions );
Name = ( PULONG )( ( PUCHAR )Module + pIED->AddressOfNames );
Ordinal = ( PUSHORT )( ( PUCHAR )Module + pIED->AddressOfNameOrdinals );
for ( i = 0; i < pIED->AddressOfFunctions; i++ )
{
if ( !strcmp( FunctionName, ( char* )Module + Name[ i ] ) )
{
return ( PVOID )( ( PUCHAR )Module + Address[ Ordinal[ i ] ] );
}
}
return NULL;
}
ULONG GetNtSyscall( LPCSTR FunctionName )
{
if ( !ntdll )
{
UNICODE_STRING FileName = RTL_CONSTANT_STRING( L"\\SystemRoot\\System32\\ntdll.dll" );
auto res = LoadFile( &FileName, &ntdll );
if ( !NT_SUCCESS( res ) )
DBGPRINT( "[ GetNtSyscall ] Failed to load ntdll.dll 0x%X\n", res )
}
if ( ntdll )
{
auto Fn = PUCHAR( GetFunctionAddress( ntdll, FunctionName ) );
if ( Fn )
{
for ( int i = 0; i < 24; ++i )
{
if ( Fn[ i ] == 0xC2 || Fn[ i ] == 0xC3 )
break;
if ( Fn[ i ] == 0xB8 )
return *( PULONG )( Fn + i + 1 );
}
}
}
return 0;
}
ULONG GetWin32Syscall( LPCSTR FunctionName )
{
if ( !win32u )
{
UNICODE_STRING FileName = RTL_CONSTANT_STRING( L"\\SystemRoot\\System32\\win32u.dll" );
auto res = LoadFile( &FileName, &win32u );
if ( !NT_SUCCESS( res ) )
DBGPRINT( "[ GetWin32Syscall ] Failed to load win32u.dll 0x%X\n", res )
}
if ( win32u )
{
auto Fn = PUCHAR( GetFunctionAddress( win32u, FunctionName ) );
if ( Fn )
{
for ( int i = 0; i < 24; ++i )
{
if ( Fn[ i ] == 0xC2 || Fn[ i ] == 0xC3 )
break;
if ( Fn[ i ] == 0xB8 )
return *( PULONG )( Fn + i + 1 );
}
}
}
return 0;
}
void UnloadImages()
{
if ( ntdll )
ExFreePoolWithTag( ntdll, TAG );
if ( win32u )
ExFreePoolWithTag( win32u, TAG );
}
};
};
namespace masterhide
{
namespace utils
{
KIRQL WPOFF()
{
KIRQL Irql = KeRaiseIrqlToDpcLevel();
UINT_PTR cr0 = __readcr0();
cr0 &= ~0x10000;
__writecr0( cr0 );
_disable();
return Irql;
}
void WPON( KIRQL Irql )
{
UINT_PTR cr0 = __readcr0();
cr0 |= 0x10000;
_enable();
__writecr0( cr0 );
KeLowerIrql( Irql );
}
const PUCHAR FindCodeCave( PUCHAR Code, ULONG ulCodeSize, size_t CaveLength )
{
for ( unsigned i = 0, j = 0; i < ulCodeSize; i++ )
{
if ( Code[ i ] == 0x90 || Code[ i ] == 0xCC )
j++;
else
j = 0;
if ( j == CaveLength )
return PUCHAR( ( ULONG_PTR )Code + i - CaveLength + 1 );
}
return nullptr;
}
}
};