364 lines
8.0 KiB
C++
364 lines
8.0 KiB
C++
#include "ob_reg_callback.h"
|
|
#include "create_thread_callback.h"
|
|
#include "process_notify_callback.h"
|
|
#include "load_Image_callback.h"
|
|
#include "protect_filter.h"
|
|
#include "comm.h"
|
|
#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<PKAPC_STATE>(
|
|
reinterpret_cast<PUCHAR>(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)
|
|
{
|
|
if (drv_obj)
|
|
{
|
|
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();
|
|
//process_function::stop_process_handle_monitor();
|
|
|
|
//load_image_notify_routine::remove_image_load_notify_routine();
|
|
|
|
//process_function::stop_process_handle_monitor();
|
|
|
|
//StopMonitorThread();
|
|
};
|
|
}
|
|
|
|
protect_filter::Init();
|
|
kernel_api::kernel_api_init();
|
|
kernel_comm_create::Init();
|
|
|
|
//把保护的进程插入到链表中
|
|
//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, "[+]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;
|
|
} |