diff --git a/Anti-Cheat_Driver/process_func.cpp b/Anti-Cheat_Driver/process_func.cpp index 70add97..2ba59d0 100644 --- a/Anti-Cheat_Driver/process_func.cpp +++ b/Anti-Cheat_Driver/process_func.cpp @@ -1,7 +1,14 @@ #include "process_func.h" +#include "kernel_function.h" +#include "process_handle.h" +#include "utils.h" +#include "kernel_api.h" + namespace process_function { ULONG orin_protect_val; + HANDLE g_thread_handle; + KEVENT g_stop_event; auto eprocess_get_protect_offset() -> UINT32 { @@ -12,6 +19,131 @@ namespace process_function return offset; } + auto ps_get_next_process_thread(PEPROCESS Process, PETHREAD Thread)->PETHREAD + { + typedef PETHREAD(NTAPI* fnPsGetNextProcessThread)(IN PEPROCESS Process, IN PETHREAD Thread); + static fnPsGetNextProcessThread _PsGetNextProcessThread = NULL; + if (!_PsGetNextProcessThread) + { + auto ps_resume_process = reinterpret_cast(kernel_api::imported.ps_resume_process); + if (!ps_resume_process) return 0; + + unsigned int break_by_count = 0; + unsigned int cur_index_count = 0; + unsigned int cur_ref_count = 0; + + auto os = utils::GetVersion(); + if (os.dwBuildNumber == 7600 || os.dwBuildNumber == 7601) + { + break_by_count = 3; + while (cur_index_count < break_by_count) + { + if (ps_resume_process[cur_ref_count++] == 0xe8) + { + cur_index_count++; + } + } + _PsGetNextProcessThread = (fnPsGetNextProcessThread)((*(int*)&ps_resume_process[cur_ref_count]) + &ps_resume_process[cur_ref_count] + 4); + } + else if (os.dwBuildNumber >= 14393 && os.dwBuildNumber <= 19045) + { + break_by_count = 2; + + while (cur_index_count < break_by_count) + { + if (ps_resume_process[cur_ref_count++] == 0xe8) + { + cur_index_count++; + } + } + + _PsGetNextProcessThread = (fnPsGetNextProcessThread)((*(int*)&ps_resume_process[cur_ref_count]) + &ps_resume_process[cur_ref_count] + 4); + } + else if (os.dwBuildNumber >= 22000) + { + auto ntoskrnl = kernel_function::GetKernelModule("ntoskrnl.exe"); + + auto address = utils::FindSectionsCode(ntoskrnl, + "\xF7\x00\x74\x00\x00\x20\x00\x75\x00\xBA\x01\x00\x00\x00\x48\x8B\x00\xE8", + "x?xxxxxx?xxxxxxx?x", ".text"); + + if (!address) return 0; + + + while (break_by_count < 1) + { + if (*reinterpret_cast(address) == 0xe8) + { + ++break_by_count; + break; + } + + --reinterpret_cast(address); + } + + _PsGetNextProcessThread = (fnPsGetNextProcessThread)((*(int*)&ps_resume_process[cur_ref_count] + 1) + &ps_resume_process[cur_ref_count] + 5); + } + } + + if (!_PsGetNextProcessThread) return 0; + + return _PsGetNextProcessThread(Process, Thread); + } + + + auto ps_get_next_process(PEPROCESS process)->PEPROCESS + { + typedef PEPROCESS(NTAPI* fnPsGetNextProcess)(IN PEPROCESS Process); + static fnPsGetNextProcess _PsGetNextProcess = NULL; + + if (!_PsGetNextProcess) + { + auto os = utils::GetVersion(); + if (os.dwBuildNumber == 7601 || os.dwBuildNumber == 7600) + { + + auto kmoudle = kernel_function::GetKernelModule("ntoskrnl.exe"); + if (!kmoudle) return 0; + + auto address = utils::FindSectionsCode(kmoudle, + "\xE8\x00\x00\x00\x00\x48\x8B\xF8\x48\x89\x44\x24\x48\x48\x3B\xC3\xB9\x01\x00\x00\x00", + "x????xxxxxxxxxxxxxxxx", "PAGE"); + + if (!address) return 0; + + _PsGetNextProcess = (fnPsGetNextProcess)((*(int*)(address + 1)) + address + 5); + } + else + { + + auto kmoudle = kernel_function::GetKernelModule("ntoskrnl.exe"); + if (!kmoudle) return 0; + + auto address = utils::FindSectionsCode(kmoudle, "\x81\x00\x22\x00\x00\xC0\x75\x00\x48\x8B\x00\x24\x00\x48\x8B", "x?xxxxx?xx?x?xx", "PAGE"); + if (!address) return 0; + + + unsigned int break_by_count = 0; + while (break_by_count < 1) + { + if (*reinterpret_cast(address) == 0xe8) + { + ++break_by_count; + break; + } + + ++reinterpret_cast(address); + } + + _PsGetNextProcess = (fnPsGetNextProcess)((*(int*)(address + 1)) + address + 5); + } + } + if (!_PsGetNextProcess) return 0; + + + return _PsGetNextProcess(process); + } + auto eprocess_protect_process_on(UINT32 Pid) -> NTSTATUS { @@ -51,6 +183,85 @@ namespace process_function return status; } + auto monitor_process_handle(LPVOID)->VOID + { + while (true) + { + NTSTATUS status{ 0 }; + for (ULONG i = 4; i < 0x401000; i += 4) + { + PEPROCESS process{ 0 }; + + status = PsLookupProcessByProcessId(ULongToHandle((ULONG)i), &process); + if (!NT_SUCCESS(status)) + continue; + + if (PsGetProcessExitStatus(process) != STATUS_PENDING) { + ObDereferenceObject(process); + continue; + } + + KAPC_STATE apc_state{ 0 }; + KeStackAttachProcess(process, &apc_state); + + process_handle::scan_and_protect_handles(process); + + KeUnstackDetachProcess(&apc_state); + + ObDereferenceObject(process); + } + + + LARGE_INTEGER interval{ 0 }; + interval.QuadPart = -20 * 1000 * 1000; + if (KeWaitForSingleObject(&g_stop_event, + Executive, + KernelMode, + FALSE, + &interval) != STATUS_TIMEOUT) + { + PsTerminateSystemThread(STATUS_SUCCESS); + } + } + } + + auto start_process_handle_monitor()->NTSTATUS + { + KeInitializeEvent(&g_stop_event, NotificationEvent, FALSE); + return PsCreateSystemThread(&g_thread_handle, THREAD_ALL_ACCESS, 0, 0, 0, monitor_process_handle, 0); + } + auto stop_process_handle_monitor() -> NTSTATUS + { + + KeSetEvent(&g_stop_event, IO_NO_INCREMENT, FALSE); + if (g_thread_handle) + { + PETHREAD thread_object = nullptr; + NTSTATUS reference_status = ObReferenceObjectByHandle( + g_thread_handle, + 0, + *PsThreadType, + KernelMode, + reinterpret_cast(&thread_object), + nullptr); + if (NT_SUCCESS(reference_status) && thread_object) + { + KeWaitForSingleObject( + thread_object, + Executive, + KernelMode, + FALSE, + nullptr); + ObDereferenceObject(thread_object); + } + ObCloseHandle(g_thread_handle, KernelMode); + g_thread_handle = nullptr; + } + + return NTSTATUS(); + } + + diff --git a/Anti-Cheat_Driver/process_func.h b/Anti-Cheat_Driver/process_func.h index 445a9f4..7fbc7cc 100644 --- a/Anti-Cheat_Driver/process_func.h +++ b/Anti-Cheat_Driver/process_func.h @@ -6,11 +6,21 @@ namespace process_function { + + auto ps_get_next_process(PEPROCESS process)->PEPROCESS; + + auto ps_get_next_process_thread(PEPROCESS Process, PETHREAD Thread)->PETHREAD; + auto eprocess_protect_process_on(UINT32 Pid)->NTSTATUS; auto eprocess_protect_process_off(UINT32 Pid)->NTSTATUS; auto kernel_kill_process(UINT32 Pid)->NTSTATUS; + + auto start_process_handle_monitor()->NTSTATUS; + + auto stop_process_handle_monitor()->NTSTATUS; + } #endif // !PROCESS_FUNC