ULONG KernelVirtualProtect(HANDLE pid, PVOID addr, SIZE_T size, ULONG protect)
{
ULONG OldProtect
=
-
32768
;
/
/
初始化之前的保护
PEPROCESS process;
NTSTATUS status;
HANDLE hProcess
=
NULL;
KAPC_STATE apc_state;
CLIENT_ID stClientId
=
{
0
};
OBJECT_ATTRIBUTES objectAttributs
=
{
0
};
if
((ULONG_PTR)addr >
=
0x70000000
&& (ULONG_PTR)addr <
0x80000000
) addr
=
(PVOID)
0x70000000
;
status
=
PsLookupProcessByProcessId(pid, &process);
if
(!NT_SUCCESS(status)) {
return
-
32766
; }
KeStackAttachProcess(process, &apc_state);
stClientId.UniqueProcess
=
pid;
stClientId.UniqueThread
=
0
;
InitializeObjectAttributes(&objectAttributs,
0
,
0
,
0
,
0
);
status
=
ZwOpenProcess(&hProcess, PROCESS_ALL_ACCESS, &objectAttributs, &stClientId);
if
(!NT_SUCCESS(status)){
return
-
32767
;}
if
(hProcess !
=
NULL)
{
status
=
pfn_NtProtectVirtualMemory(hProcess, (PVOID
*
)&addr, &size, PAGE_EXECUTE_READWRITE, &OldProtect);
if
(!NT_SUCCESS(status))
{
DPRINT((
"[X64Svc] pfn_NtProtectVirtualMemory status = %08X\n"
, status));
return
status;
}
}
KeUnstackDetachProcess(&apc_state);
ObDereferenceObject(process);
ZwClose(hProcess);
DPRINT((
"[X64Svc] pfn_NtProtectVirtualMemory status = %08X\n"
, status));
return
OldProtect;
}