From 76f00a931276cc2eb78d3821b17336d59fc2f884 Mon Sep 17 00:00:00 2001 From: maowenrui <3505719167@qq.com> Date: Thu, 8 Oct 2026 10:04:16 +0800 Subject: [PATCH] feat check apc stack --- Anti-Cheat_Driver/driver_main.cpp | 341 +++++++++++++++++++++++++++++- 1 file changed, 331 insertions(+), 10 deletions(-) diff --git a/Anti-Cheat_Driver/driver_main.cpp b/Anti-Cheat_Driver/driver_main.cpp index cf54a55..5d4886e 100644 --- a/Anti-Cheat_Driver/driver_main.cpp +++ b/Anti-Cheat_Driver/driver_main.cpp @@ -7,6 +7,308 @@ #include "inline_hooks.h" #include "mouse_keybord_hook.h" #include "ssdt.h" +#include "process_func.h" +#include "kernel_function.h" +#include "memory_protect.h" + +KEVENT g_stop_event; +HANDLE g_thread_handle; + +typedef struct _MONITOR_STATISTICS +{ + ULONG ProcessCount; + ULONG ThreadCount; + ULONG AttachCount; + LONGLONG ScanTimeUs; +} MONITOR_STATISTICS, * PMONITOR_STATISTICS; + +static PEPROCESS +ReadCurrentApcProcess( + _In_ PETHREAD Thread +) +{ + if (Thread == nullptr) { + return nullptr; + } + + __try + { + auto apcState = + reinterpret_cast( + reinterpret_cast(Thread) + + 0x98 + ); + + return apcState->Process; + } + __except (EXCEPTION_EXECUTE_HANDLER) + { + return nullptr; + } +} + +static BOOLEAN +MonitorStopping() +{ + return KeReadStateEvent(&g_stop_event) != 0; +} + +static VOID +ScanAllThreads( + _In_ PEPROCESS TargetProcess, + _Out_ PMONITOR_STATISTICS Statistics +) +{ + RtlZeroMemory( + Statistics, + sizeof(MONITOR_STATISTICS) + ); + + LARGE_INTEGER frequency; + LARGE_INTEGER begin; + LARGE_INTEGER end; + + begin = KeQueryPerformanceCounter(&frequency); + + PEPROCESS process = nullptr; + + for (;;) + { + if (MonitorStopping()) { + break; + } + + PEPROCESS nextProcess = + process_function::ps_get_next_process(process); + + // + // 按你的封装约定,不在这里释放 process + // + process = nextProcess; + + if (process == nullptr) { + break; + } + + Statistics->ProcessCount++; + + PETHREAD thread = nullptr; + + for (;;) + { + if (MonitorStopping()) { + break; + } + + PETHREAD nextThread = + process_function::ps_get_next_process_thread( + process, + thread + ); + + // + // 按你的封装约定,不在这里释放 thread + // + thread = nextThread; + + if (thread == nullptr) { + break; + } + + Statistics->ThreadCount++; + + PEPROCESS ownerProcess = + PsGetThreadProcess(thread); + + if (ownerProcess == nullptr) { + continue; + } + + // + // 目标进程自身线程不属于外部 attach + // + if (ownerProcess == TargetProcess) { + continue; + } + + PEPROCESS currentApcProcess = + ReadCurrentApcProcess(thread); + + if (currentApcProcess == TargetProcess) + { + Statistics->AttachCount++; + + // + // 只在命中时输出,避免每个线程都打印造成时序干扰 + // + DbgPrintEx( + DPFLTR_IHVDRIVER_ID, + DPFLTR_ERROR_LEVEL, + "[Attch] " + "恶意者=%p " + "被操作者=%p " + "ApcProcess=%p\n", + PsGetProcessId(ownerProcess), + PsGetProcessId(TargetProcess), + currentApcProcess + ); + } + } + } + + end = KeQueryPerformanceCounter(nullptr); + + if (frequency.QuadPart != 0) + { + Statistics->ScanTimeUs = + ((end.QuadPart - begin.QuadPart) * 1000000LL) / + frequency.QuadPart; + } +} + +static VOID +MonitorProcessAttach( + _In_ PVOID Context +) +{ + UNREFERENCED_PARAMETER(Context); + + DbgPrintEx( + DPFLTR_IHVDRIVER_ID, + DPFLTR_ERROR_LEVEL, + "[Monitor] thread started\n" + ); + + PEPROCESS targetProcess = nullptr; + + NTSTATUS status = + PsLookupProcessByProcessId( + ULongToHandle(1620), + &targetProcess + ); + + if (!NT_SUCCESS(status)) + { + DbgPrintEx( + DPFLTR_IHVDRIVER_ID, + DPFLTR_ERROR_LEVEL, + "[Monitor] PsLookupProcessByProcessId failed: 0x%08X\n", + status + ); + + PsTerminateSystemThread(status); + return; + } + + while (!MonitorStopping()) + { + MONITOR_STATISTICS statistics{}; + + ScanAllThreads( + targetProcess, + &statistics + ); + + + // + // 每 1000 轮输出一次,避免日志影响扫描 + // + //if ((scanNumber % 1000) == 0) + //{ + // DbgPrintEx( + // DPFLTR_IHVDRIVER_ID, + // DPFLTR_ERROR_LEVEL, + // "[Monitor] " + // "scan=%lu " + // "processes=%lu " + // "threads=%lu " + // "attach=%lu " + // "scanTime=%lld us\n", + // scanNumber, + // statistics.ProcessCount, + // statistics.ThreadCount, + // statistics.AttachCount, + // statistics.ScanTimeUs + // ); + //} + + // + // 不额外等待,下一轮立即开始。 + // 如果 CPU 占用过高,可以改成 KeStallExecutionProcessor(1)。 + // + + KeStallExecutionProcessor(1); + } + + ObDereferenceObject(targetProcess); + + DbgPrintEx( + DPFLTR_IHVDRIVER_ID, + DPFLTR_ERROR_LEVEL, + "[Monitor] thread stopped\n" + ); + + PsTerminateSystemThread(STATUS_SUCCESS); +} + +NTSTATUS StartMonitorThread() +{ + KeInitializeEvent( + &g_stop_event, + NotificationEvent, + FALSE + ); + + g_thread_handle = nullptr; + + NTSTATUS status = + PsCreateSystemThread( + &g_thread_handle, + THREAD_ALL_ACCESS, + nullptr, + nullptr, + nullptr, + MonitorProcessAttach, + nullptr + ); + + if (!NT_SUCCESS(status)) + { + g_thread_handle = nullptr; + + DbgPrintEx( + DPFLTR_IHVDRIVER_ID, + DPFLTR_ERROR_LEVEL, + "[Monitor] PsCreateSystemThread failed: 0x%08X\n", + status + ); + } + + return status; +} + +VOID +StopMonitorThread() +{ + KeSetEvent( + &g_stop_event, + IO_NO_INCREMENT, + FALSE + ); + + if (g_thread_handle != nullptr) + { + ZwWaitForSingleObject( + g_thread_handle, + FALSE, + nullptr + ); + + ZwClose(g_thread_handle); + g_thread_handle = nullptr; + } +} + EXTERN_C NTSTATUS DriverEntry(PDRIVER_OBJECT drv_obj, PUNICODE_STRING) @@ -15,12 +317,17 @@ EXTERN_C NTSTATUS DriverEntry(PDRIVER_OBJECT drv_obj, PUNICODE_STRING) { drv_obj->DriverUnload = [](PDRIVER_OBJECT) { - kernel_comm_create::remove(); - //ob_call_back::uninstall_ob_callback(); - process_notify_callback::remov_process_notify(); - thread_notify_routine::unload_thread_routine(); + //kernel_comm_create::remove(); + ////ob_call_back::uninstall_ob_callback(); + //process_notify_callback::remov_process_notify(); + //thread_notify_routine::unload_thread_routine(); + //process_function::stop_process_handle_monitor(); //load_image_notify_routine::remove_image_load_notify_routine(); + + //process_function::stop_process_handle_monitor(); + + //StopMonitorThread(); }; } @@ -29,15 +336,29 @@ EXTERN_C NTSTATUS DriverEntry(PDRIVER_OBJECT drv_obj, PUNICODE_STRING) kernel_comm_create::Init(); //把保护的进程插入到链表中 - //protect_filter::add_protect_list(12204, 0, FALSE); + //protect_filter::add_protect_list(3344, 0, FALSE); //mouse_keybord_hook::install_mouse_keybord_hook(); - DbgPrintEx(77, 0, "[+]ob_reg_callback status:%x\n", ob_call_back::register_ob_reg_callback()); - DbgPrintEx(77, 0, "[+]process_notify_callback status:%x\n", process_notify_callback::install_process_notify()); - DbgPrintEx(77, 0, "[+]create_thread_routine status:%x\n", thread_notify_routine::create_thread_routine()); + + //DbgPrintEx(77, 0, "[+]ob_reg_callback status:%x\n", ob_call_back::register_ob_reg_callback()); + //DbgPrintEx(77, 0, "[+]process_notify_callback status:%x\n", process_notify_callback::install_process_notify()); + //DbgPrintEx(77, 0, "[+]create_thread_routine status:%x\n", thread_notify_routine::create_thread_routine()); + //DbgPrintEx(77, 0, "[+]start_thread_monitor:%x\n", process_function::start_process_handle_monitor()); + + + + //StartMonitorThread(); + + + ob_call_back::register_ob_reg_callback(); + process_notify_callback::install_process_notify(); + thread_notify_routine::create_thread_routine(); + process_function::start_process_handle_monitor(); + + + //auto status = memory_protect::ProtectUserRegionByPid((HANDLE)2920, 0x00400000); + //DbgPrintEx(77, 0, "status:%x\n", status); - //我过滤了那个任务管理器 所以看不了结束进程效果 直接看能不能读搜索就行了 没有图标吧 - //也搜索不了 我这个就是内存加载 也不会蓝屏啊 return STATUS_SUCCESS; } \ No newline at end of file