forked from Vicshann/Common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWow64Ext.hpp
More file actions
623 lines (582 loc) · 26.8 KB
/
Wow64Ext.hpp
File metadata and controls
623 lines (582 loc) · 26.8 KB
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
#pragma once
/**
*
* WOW64Ext Library
*
* Copyright (c) 2014 ReWolf
* http://blog.rewolf.pl/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
class NWOW64E
{
//------------------------------------------------------------------------------------------------------------
#define EMIT(a) __asm __emit (a)
#define X64_Start_with_CS(_cs) \
{ \
EMIT(0x6A) EMIT(_cs) /* push _cs */ \
EMIT(0xE8) EMIT(0) EMIT(0) EMIT(0) EMIT(0) /* call $+5 */ \
EMIT(0x83) EMIT(4) EMIT(0x24) EMIT(5) /* add dword [esp], 5 */ \
EMIT(0xCB) /* retf */ \
}
#define X64_End_with_CS(_cs) \
{ \
EMIT(0xE8) EMIT(0) EMIT(0) EMIT(0) EMIT(0) /* call $+5 */ \
EMIT(0xC7) EMIT(0x44) EMIT(0x24) EMIT(4) EMIT(_cs) EMIT(0) EMIT(0) EMIT(0) /* mov dword [rsp + 4], _cs */ \
EMIT(0x83) EMIT(4) EMIT(0x24) EMIT(0xD) /* add dword [rsp], 0xD */ \
EMIT(0xCB) /* retf */ \
}
#define X64_Start() X64_Start_with_CS(0x33)
#define X64_End() X64_End_with_CS(0x23)
#define _RAX 0
#define _RCX 1
#define _RDX 2
#define _RBX 3
#define _RSP 4
#define _RBP 5
#define _RSI 6
#define _RDI 7
#define _R8 8
#define _R9 9
#define _R10 10
#define _R11 11
#define _R12 12
#define _R13 13
#define _R14 14
#define _R15 15
#define X64_Push(r) EMIT(0x48 | ((r) >> 3)) EMIT(0x50 | ((r) & 7))
#define X64_Pop(r) EMIT(0x48 | ((r) >> 3)) EMIT(0x58 | ((r) & 7))
#define REX_W EMIT(0x48) __asm
//to fool M$ inline asm compiler I'm using 2 DWORDs instead of DWORD64
//use of DWORD64 will generate wrong 'pop word ptr[]' and it will break stack
union reg64
{
DWORD64 v;
DWORD dw[2];
};
//------------------------------------------------------------------------------------------------------------
public:
#include "NtDefs.hpp"
//------------------------------------------------------------------------------------------------------------
#pragma warning(push)
#pragma warning(disable : 4409)
static DWORD64 __cdecl X64Call(DWORD64 func, int argC, ...)
{
va_list args;
va_start(args, argC);
reg64 _rcx = { (argC > 0) ? argC--, va_arg(args, DWORD64) : 0 };
reg64 _rdx = { (argC > 0) ? argC--, va_arg(args, DWORD64) : 0 };
reg64 _r8 = { (argC > 0) ? argC--, va_arg(args, DWORD64) : 0 };
reg64 _r9 = { (argC > 0) ? argC--, va_arg(args, DWORD64) : 0 };
reg64 _rax = { 0 };
reg64 restArgs = { (DWORD64)&va_arg(args, DWORD64) };
// conversion to QWORD for easier use in inline assembly
reg64 _argC = { (DWORD64)argC };
DWORD back_esp = 0;
WORD back_fs = 0;
#ifndef _AMD64_
__asm
{
;// reset FS segment, to properly handle RFG
mov back_fs, fs
mov eax, 0x2B
mov fs, ax
;// keep original esp in back_esp variable
mov back_esp, esp
;// align esp to 0x10, without aligned stack some syscalls may return errors !
;// (actually, for syscalls it is sufficient to align to 8, but SSE opcodes
;// requires 0x10 alignment), it will be further adjusted according to the
;// number of arguments above 4
and esp, 0xFFFFFFF0
X64_Start();
;// below code is compiled as x86 inline asm, but it is executed as x64 code
;// that's why it need sometimes REX_W() macro, right column contains detailed
;// transcription how it will be interpreted by CPU
;// fill first four arguments
REX_W mov ecx, _rcx.dw[0] ;// mov rcx, qword ptr [_rcx]
REX_W mov edx, _rdx.dw[0] ;// mov rdx, qword ptr [_rdx]
push _r8.v ;// push qword ptr [_r8]
X64_Pop(_R8); ;// pop r8
push _r9.v ;// push qword ptr [_r9]
X64_Pop(_R9); ;// pop r9
;//
REX_W mov eax, _argC.dw[0] ;// mov rax, qword ptr [_argC]
;//
;// final stack adjustment, according to the ;//
;// number of arguments above 4 ;//
test al, 1 ;// test al, 1
jnz _no_adjust ;// jnz _no_adjust
sub esp, 8 ;// sub rsp, 8
_no_adjust: ;//
;//
push edi ;// push rdi
REX_W mov edi, restArgs.dw[0] ;// mov rdi, qword ptr [restArgs]
;//
;// put rest of arguments on the stack ;//
REX_W test eax, eax ;// test rax, rax
jz _ls_e ;// je _ls_e
REX_W lea edi, dword ptr [edi + 8*eax - 8] ;// lea rdi, [rdi + rax*8 - 8]
;//
_ls: ;//
REX_W test eax, eax ;// test rax, rax
jz _ls_e ;// je _ls_e
push dword ptr [edi] ;// push qword ptr [rdi]
REX_W sub edi, 8 ;// sub rdi, 8
REX_W sub eax, 1 ;// sub rax, 1
jmp _ls ;// jmp _ls
_ls_e: ;//
;//
;// create stack space for spilling registers ;//
REX_W sub esp, 0x20 ;// sub rsp, 20h
;//
call func ;// call qword ptr [func]
;//
;// cleanup stack ;//
REX_W mov ecx, _argC.dw[0] ;// mov rcx, qword ptr [_argC]
REX_W lea esp, dword ptr [esp + 8*ecx + 0x20] ;// lea rsp, [rsp + rcx*8 + 20h]
;//
pop edi ;// pop rdi
;//
// set return value ;//
REX_W mov _rax.dw[0], eax ;// mov qword ptr [_rax], rax
X64_End();
mov ax, ds
mov ss, ax
mov esp, back_esp
;// restore FS segment
mov ax, back_fs
mov fs, ax
}
#endif
return _rax.v;
}
#pragma warning(pop)
//------------------------------------------------------------------------------------------------------------
static void _stdcall getMem64(void* dstMem, DWORD64 srcMem, size_t sz)
{
if (!dstMem || !srcMem || !sz)return;
reg64 _src = { srcMem };
#ifndef _AMD64_
__asm
{
X64_Start();
;// below code is compiled as x86 inline asm, but it is executed as x64 code
;// that's why it need sometimes REX_W() macro, right column contains detailed
;// transcription how it will be interpreted by CPU
push edi ;// push rdi
push esi ;// push rsi
;//
mov edi, dstMem ;// mov edi, dword ptr [dstMem] ; high part of RDI is zeroed
REX_W mov esi, _src.dw[0] ;// mov rsi, qword ptr [_src]
mov ecx, sz ;// mov ecx, dword ptr [sz] ; high part of RCX is zeroed
;//
mov eax, ecx ;// mov eax, ecx
and eax, 3 ;// and eax, 3
shr ecx, 2 ;// shr ecx, 2
;//
rep movsd ;// rep movs dword ptr [rdi], dword ptr [rsi]
;//
test eax, eax ;// test eax, eax
je _move_0 ;// je _move_0
cmp eax, 1 ;// cmp eax, 1
je _move_1 ;// je _move_1
;//
movsw ;// movs word ptr [rdi], word ptr [rsi]
cmp eax, 2 ;// cmp eax, 2
je _move_0 ;// je _move_0
;//
_move_1: ;//
movsb ;// movs byte ptr [rdi], byte ptr [rsi]
;//
_move_0: ;//
pop esi ;// pop rsi
pop edi ;// pop rdi
X64_End();
}
#endif
}
//------------------------------------------------------------------------------------------------------------
static void _stdcall setMem64(DWORD64 dstMem, void* srcMem, size_t sz)
{
if (!dstMem || !srcMem || !sz)return;
reg64 _dst = { dstMem };
#ifndef _AMD64_
__asm
{
X64_Start();
;// below code is compiled as x86 inline asm, but it is executed as x64 code
;// that's why it need sometimes REX_W() macro, right column contains detailed
;// transcription how it will be interpreted by CPU
push edi ;// push rdi
push esi ;// push rsi
;//
mov esi, srcMem ;// mov esi, dword ptr [srcMem] ; high part of RSI is zeroed
REX_W mov edi, _dst.dw[0] ;// mov rdi, qword ptr [_dst]
mov ecx, sz ;// mov ecx, dword ptr [sz] ; high part of RCX is zeroed
;//
mov eax, ecx ;// mov eax, ecx
and eax, 3 ;// and eax, 3
shr ecx, 2 ;// shr ecx, 2
;//
rep movsd ;// rep movs dword ptr [rdi], dword ptr [rsi]
;//
test eax, eax ;// test eax, eax
je _move_0 ;// je _move_0
cmp eax, 1 ;// cmp eax, 1
je _move_1 ;// je _move_1
;//
movsw ;// movs word ptr [rdi], word ptr [rsi]
cmp eax, 2 ;// cmp eax, 2
je _move_0 ;// je _move_0
;//
_move_1: ;//
movsb ;// movs byte ptr [rdi], byte ptr [rsi]
;//
_move_0: ;//
pop esi ;// pop rsi
pop edi ;// pop rdi
X64_End();
}
#endif
}
//------------------------------------------------------------------------------------------------------------
static bool _stdcall cmpMem64(void* dstMem, DWORD64 srcMem, size_t sz)
{
if (!dstMem || !srcMem || !sz)return false;
bool result = false;
reg64 _src = { srcMem };
#ifndef _AMD64_
__asm
{
X64_Start();
;// below code is compiled as x86 inline asm, but it is executed as x64 code
;// that's why it need sometimes REX_W() macro, right column contains detailed
;// transcription how it will be interpreted by CPU
push edi ;// push rdi
push esi ;// push rsi
;//
mov edi, dstMem ;// mov edi, dword ptr [dstMem] ; high part of RDI is zeroed
REX_W mov esi, _src.dw[0] ;// mov rsi, qword ptr [_src]
mov ecx, sz ;// mov ecx, dword ptr [sz] ; high part of RCX is zeroed
;//
mov eax, ecx ;// mov eax, ecx
and eax, 3 ;// and eax, 3
shr ecx, 2 ;// shr ecx, 2
;//
repe cmpsd ;// repe cmps dword ptr [rsi], dword ptr [rdi]
jnz _ret_false ;// jnz _ret_false
;//
test eax, eax ;// test eax, eax
je _move_0 ;// je _move_0
cmp eax, 1 ;// cmp eax, 1
je _move_1 ;// je _move_1
;//
cmpsw ;// cmps word ptr [rsi], word ptr [rdi]
jnz _ret_false ;// jnz _ret_false
cmp eax, 2 ;// cmp eax, 2
je _move_0 ;// je _move_0
;//
_move_1: ;//
cmpsb ;// cmps byte ptr [rsi], byte ptr [rdi]
jnz _ret_false ;// jnz _ret_false
;//
_move_0: ;//
mov result, 1 ;// mov byte ptr [result], 1
;//
_ret_false: ;//
pop esi ;// pop rsi
pop edi ;// pop rdi
X64_End();
}
#endif
return result;
}
//------------------------------------------------------------------------------------------------------------
static DWORD64 _fastcall getTEB64(void) // TODO: NTDLL::NtCurrentTeb (Optional)
{
reg64 reg;
reg.v = 0;
#ifndef _AMD64_
X64_Start();
// R12 register should always contain pointer to TEB64 in WoW64 processes
X64_Push(_R12);
// below pop will pop QWORD from stack, as we're in x64 mode now
__asm pop reg.dw[0]
X64_End();
#endif
return reg.v;
}
//------------------------------------------------------------------------------------------------------------
static int _fastcall _wcsicmp(const wchar_t *string1, const wchar_t *string2)
{
wchar_t c1;
wchar_t c2;
int i = 0;
do
{
c1 = string1[i];
if (c1 >= 'A' && c1 <= 'Z')c1 += 0x20;
c2 = string2[i];
if (c2 >= 'A' && c2 <= 'Z')c2 += 0x20;
i++;
}
while (c1 && c1 == c2);
return c1 - c2;
}
//------------------------------------------------------------------------------------------------------------
static DWORD64 _stdcall GetModuleHandle64(wchar_t* lpModuleName, DWORD* DllSize=NULL) // TODO: ntdll::LdrGetDllHandleEx
{
TEB64 teb64;
PEB64 peb64;
PEB_LDR_DATA64 ldr;
LDR_DATA_TABLE_ENTRY64 head;
DWORD64 LastEntry = 0; // peb64.Ldr + offsetof(PEB_LDR_DATA64, InLoadOrderModuleList);
getMem64(&teb64, getTEB64(), sizeof(TEB64));
getMem64(&peb64, teb64.ProcessEnvironmentBlock, sizeof(PEB64));
getMem64(&ldr, peb64.Ldr, sizeof(PEB_LDR_DATA64));
head.InLoadOrderLinks.Flink = ldr.InLoadOrderModuleList.Flink;
wchar_t tempBuf[1028]; // ~2K // No full module path expected
do
{
getMem64(&head, head.InLoadOrderLinks.Flink, sizeof(LDR_DATA_TABLE_ENTRY64));
getMem64(tempBuf, head.BaseDllName.Buffer, head.BaseDllName.MaximumLength);
if(0 == _wcsicmp(lpModuleName, tempBuf))
{
if(DllSize)*DllSize = head.SizeOfImage;
return head.DllBase;
}
}
while(head.InLoadOrderLinks.Flink != LastEntry);
return 0;
}
//------------------------------------------------------------------------------------------------------------
static DWORD64 _stdcall getNTDLL64(void)
{
static DWORD64 ntdll64 = 0;
if(0 != ntdll64)return ntdll64;
ntdll64 = GetModuleHandle64(L"ntdll.dll");
return ntdll64;
}
//------------------------------------------------------------------------------------------------------------
static DWORD64 _stdcall GetProcAddressSimpleX64(DWORD64 modBase, char* ProcName) // TODO: NTDLL::LdrGetProcedureAddress
{
if(!modBase || !ProcName || !*ProcName)return 0;
IMAGE_DOS_HEADER idh;
IMAGE_NT_HEADERS64 inh;
IMAGE_EXPORT_DIRECTORY ied;
char PNameBuf[128]; // Enough for ntdll.dll
DWORD NameTable[512]; // NOTE: Disable chkstk
getMem64(&idh, modBase, sizeof(idh));
getMem64(&inh, modBase + idh.e_lfanew, sizeof(inh));
IMAGE_DATA_DIRECTORY& idd = inh.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
if(!idd.VirtualAddress)return 0;
getMem64(&ied, modBase + idd.VirtualAddress, sizeof(ied));
LOGMSG("ModBase=%016llX, AddressOfNames=%08X",modBase, ied.AddressOfNames);
int PNameLen = 0;
while(ProcName[PNameLen])PNameLen++;
DWORD MaxNamesInBuf = sizeof(NameTable) / sizeof(DWORD);
for(UINT i=0,ctr=MaxNamesInBuf; i < ied.NumberOfFunctions; i++, ctr++) // lazy search, there is no need to use binsearch for just one function
{
if(ctr >= MaxNamesInBuf)
{
UINT RecsLeft = ied.NumberOfNames - i;
if(RecsLeft > MaxNamesInBuf)RecsLeft = MaxNamesInBuf;
getMem64(&NameTable, modBase + ied.AddressOfNames + (i * sizeof(DWORD)), sizeof(DWORD)*RecsLeft);
ctr = 0;
}
// LOGMSG("NameTable %p: %08X",&NameTable, NameTable[ctr]);
getMem64(PNameBuf, modBase + NameTable[ctr], PNameLen + 1); // Including 0
int NLen = 0;
while((PNameBuf[NLen] == ProcName[NLen]) && PNameBuf[NLen])NLen++;
if(NLen != PNameLen)continue; // Not match
WORD OrdTblVal;
DWORD RvaTblVal;
getMem64(&OrdTblVal, modBase + ied.AddressOfNameOrdinals + (sizeof(WORD) * i), sizeof(WORD));
getMem64(&RvaTblVal, modBase + ied.AddressOfFunctions + (sizeof(DWORD) * OrdTblVal), sizeof(DWORD));
LOGMSG("Found: %016llX",(modBase + RvaTblVal));
return modBase + RvaTblVal;
}
return 0;
}
//------------------------------------------------------------------------------------------------------------
/*static VOID __cdecl SetLastErrorFromX64Call(DWORD64 status)
{
typedef ULONG (WINAPI *RtlNtStatusToDosError_t)(NTSTATUS Status);
typedef ULONG (WINAPI *RtlSetLastWin32Error_t)(NTSTATUS Status);
static RtlNtStatusToDosError_t RtlNtStatusToDosError = nullptr;
static RtlSetLastWin32Error_t RtlSetLastWin32Error = nullptr;
if ((nullptr == RtlNtStatusToDosError) || (nullptr == RtlSetLastWin32Error))
{
HMODULE ntdll = GetModuleHandleW(L"ntdll.dll");
RtlNtStatusToDosError = (RtlNtStatusToDosError_t)GetProcAddress(ntdll, "RtlNtStatusToDosError");
RtlSetLastWin32Error = (RtlSetLastWin32Error_t)GetProcAddress(ntdll, "RtlSetLastWin32Error");
}
if ((nullptr != RtlNtStatusToDosError) && (nullptr != RtlSetLastWin32Error))
{
RtlSetLastWin32Error(RtlNtStatusToDosError((DWORD)status));
}
} */
//------------------------------------------------------------------------------------------------------------
static DWORD64 _stdcall GetProcAddress64(DWORD64 hModule, char* funcName)
{
static DWORD64 xproc = 0;
if(!xproc)
{
xproc = GetProcAddressSimpleX64(getNTDLL64(), "LdrGetProcedureAddress");
if(!xproc)return 0;
}
_UNICODE_STRING_T<DWORD64> fName = { 0 };
fName.Buffer = (DWORD64)funcName;
fName.Length = (WORD)strlen(funcName);
fName.MaximumLength = fName.Length + 1;
DWORD64 funcRet = 0;
X64Call(xproc, 4, (DWORD64)hModule, (DWORD64)&fName, (DWORD64)0, (DWORD64)&funcRet);
return funcRet;
}
//------------------------------------------------------------------------------------------------------------
static NTSTATUS _stdcall QuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass, PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength)
{
static DWORD64 xproc = 0;
if(!xproc)
{
xproc = GetProcAddress64(getNTDLL64(), "NtQuerySystemInformation");
if(!xproc)return -1;
}
return X64Call(xproc, 4, (DWORD64)SystemInformationClass, (DWORD64)SystemInformation, (DWORD64)SystemInformationLength, (DWORD64)ReturnLength);
}
//------------------------------------------------------------------------------------------------------------
static NTSTATUS _stdcall QueryInformationThread(HANDLE ThreadHandle, THREADINFOCLASS ThreadInformationClass, PVOID ThreadInformation, ULONG ThreadInformationLength, PULONG ReturnLength)
{
static DWORD64 xproc = 0;
if(!xproc)
{
xproc = GetProcAddress64(getNTDLL64(), "NtQueryInformationThread");
if(!xproc)return -1;
}
return X64Call(xproc, 5, (DWORD64)ThreadHandle, (DWORD64)ThreadInformationClass, (DWORD64)ThreadInformation, (DWORD64)ThreadInformationLength, (DWORD64)ReturnLength);
}
//------------------------------------------------------------------------------------------------------------
static NTSTATUS _stdcall SetInformationThread(HANDLE ThreadHandle, THREADINFOCLASS ThreadInformationClass, PVOID ThreadInformation, ULONG ThreadInformationLength)
{
static DWORD64 xproc = 0;
if(!xproc)
{
xproc = GetProcAddress64(getNTDLL64(), "NtSetInformationThread");
if(!xproc)return -1;
}
return X64Call(xproc, 4, (DWORD64)ThreadHandle, (DWORD64)ThreadInformationClass, (DWORD64)ThreadInformation, (DWORD64)ThreadInformationLength);
}
//------------------------------------------------------------------------------------------------------------
// Can`t read anything if range includes a ME_RESERVE segment. (.NET modules have this)
static NTSTATUS _stdcall ReadVirtualMemory(HANDLE ProcessHandle, DWORD64 BaseAddress, PVOID Buffer, SIZE_T BufferSize, DWORD64* NumberOfBytesRead)
{
static DWORD64 xproc = 0;
if(!xproc)
{
xproc = GetProcAddress64(getNTDLL64(), "NtReadVirtualMemory");
if(!xproc)return -1;
}
return X64Call(xproc, 5, (DWORD64)ProcessHandle, (DWORD64)BaseAddress, (DWORD64)Buffer, (DWORD64)BufferSize, (DWORD64)NumberOfBytesRead);
}
//------------------------------------------------------------------------------------------------------------
static NTSTATUS _stdcall WriteVirtualMemory(HANDLE ProcessHandle, DWORD64 BaseAddress, PVOID Buffer, SIZE_T BufferSize, DWORD64* NumberOfBytesWritten)
{
static DWORD64 xproc = 0;
if(!xproc)
{
xproc = GetProcAddress64(getNTDLL64(), "NtWriteVirtualMemory");
if(!xproc)return -1;
}
return X64Call(xproc, 5, (DWORD64)ProcessHandle, (DWORD64)BaseAddress, (DWORD64)Buffer, (DWORD64)BufferSize, (DWORD64)NumberOfBytesWritten);
}
//------------------------------------------------------------------------------------------------------------
static NTSTATUS _stdcall GetContextThread(HANDLE ThreadHandle, _CONTEXT64* ThreadContext)
{
static DWORD64 xproc = 0;
if(!xproc)
{
xproc = GetProcAddress64(getNTDLL64(), "NtGetContextThread");
if(!xproc)return -1;
}
return X64Call(xproc, 2, (DWORD64)ThreadHandle, (DWORD64)ThreadContext);
}
//------------------------------------------------------------------------------------------------------------
static NTSTATUS _stdcall SetContextThread(HANDLE ThreadHandle, _CONTEXT64* ThreadContext)
{
static DWORD64 xproc = 0;
if(!xproc)
{
xproc = GetProcAddress64(getNTDLL64(), "NtSetContextThread");
if(!xproc)return -1;
}
return X64Call(xproc, 2, (DWORD64)ThreadHandle, (DWORD64)ThreadContext);
}
//------------------------------------------------------------------------------------------------------------
static NTSTATUS _stdcall UnmapViewOfSection(HANDLE ProcessHandle, DWORD64 BaseAddress)
{
static DWORD64 xproc = 0;
if(!xproc)
{
xproc = GetProcAddress64(getNTDLL64(), "NtUnmapViewOfSection");
if(!xproc)return -1;
}
return X64Call(xproc, 2, (DWORD64)ProcessHandle, (DWORD64)BaseAddress);
}
//------------------------------------------------------------------------------------------------------------
static NTSTATUS _stdcall MapViewOfSection(HANDLE SectionHandle, HANDLE ProcessHandle, DWORD64* BaseAddress, DWORD64 ZeroBits, DWORD64 CommitSize, PLARGE_INTEGER SectionOffset, DWORD64* ViewSize, SECTION_INHERIT InheritDisposition, ULONG AllocationType, ULONG Win32Protect)
{
static DWORD64 xproc = 0;
if(!xproc)
{
xproc = GetProcAddress64(getNTDLL64(), "NtMapViewOfSection");
if(!xproc)return -1;
}
return X64Call(xproc, 10, (DWORD64)SectionHandle, (DWORD64)ProcessHandle, (DWORD64)BaseAddress, (DWORD64)ZeroBits, (DWORD64)CommitSize, (DWORD64)SectionOffset, (DWORD64)ViewSize, (DWORD64)InheritDisposition, (DWORD64)AllocationType, (DWORD64)Win32Protect);
}
//------------------------------------------------------------------------------------------------------------
static NTSTATUS _stdcall ProtectVirtualMemory(HANDLE ProcessHandle, DWORD64* BaseAddress, DWORD64* RegionSize, ULONG NewProtect, PULONG OldProtect)
{
static DWORD64 xproc = 0;
if(!xproc)
{
xproc = GetProcAddress64(getNTDLL64(), "NtProtectVirtualMemory");
if(!xproc)return -1;
}
return X64Call(xproc, 5, (DWORD64)ProcessHandle, (DWORD64)BaseAddress, (DWORD64)RegionSize, (DWORD64)NewProtect, (DWORD64)OldProtect);
}
//------------------------------------------------------------------------------------------------------------
static NTSTATUS _stdcall QueryVirtualMemory(HANDLE ProcessHandle, DWORD64 BaseAddress, MEMORY_INFORMATION_CLASS MemoryInformationClass, PVOID MemoryInformation, SIZE_T MemoryInformationLength, DWORD64* ReturnLength)
{
static DWORD64 xproc = 0;
if(!xproc)
{
xproc = GetProcAddress64(getNTDLL64(), "NtQueryVirtualMemory");
if(!xproc)return -1;
}
return X64Call(xproc, 6, (DWORD64)ProcessHandle, (DWORD64)BaseAddress, (DWORD64)MemoryInformationClass, (DWORD64)MemoryInformation, (DWORD64)MemoryInformationLength, (DWORD64)ReturnLength);
}
//------------------------------------------------------------------------------------------------------------
static NTSTATUS _stdcall memcpy(DWORD64 Dst, DWORD64 Src, DWORD64 Size)
{
static DWORD64 xproc = 0;
if(!xproc)
{
xproc = GetProcAddress64(getNTDLL64(), "memcpy");
if(!xproc)return -1;
}
return X64Call(xproc, 3, (DWORD64)Dst, (DWORD64)Src, (DWORD64)Size);
}
//------------------------------------------------------------------------------------------------------------
};