检测并阻止来自驱动程序的Read / WriteProcessMemory调用

时间:2018-07-27 12:55:16

标签: driver protection readprocessmemory kmdf filter-driver

您好,我是内核编程的相对新手(尽管我有很多c ++开发经验),并且有一个我想要实现的目标:

检测并有条件地阻止来自userland程序对我自己的userland进程中的特定内存地址进行写入或读取的尝试。这必须由驾驶员来完成。

我已经设置了一个开发环境(运行最新Windows 10 + virtualkd + windbg的虚拟机),并且已经通过Visual Studio集成(通过LAN)成功部署了一个小的kmdf测试驱动程序。

所以我的问题是: 我如何检测/拦截对ring3应用程序的Read / WriteProcessMemory调用?仅在这里阻塞句柄是不够的。

如果有人可以通过链接(一个不过期的)示例或仅仅告诉我如何做到这一点来将我指向正确的方向,那将是很好的选择。

更新: 阅读了很多有关过滤器驱动程序和将Windows Apis从内核模式挂起的知识,但是我真的不想惹上Patchguard,也不真正知道如何从用户区过滤RPM调用。保护我的程序不受驱动程序的影响,仅保护ring3应用程序的保护并不重要。

谢谢:)

1 个答案:

答案 0 :(得分:1)

here中的这段代码应该可以解决问题。

OB_PREOP_CALLBACK_STATUS PreCallback(PVOID RegistrationContext, 
POB_PRE_OPERATION_INFORMATION OperationInformation)
    {
UNREFERENCED_PARAMETER(RegistrationContext);

PEPROCESS OpenedProcess = (PEPROCESS)OperationInformation->Object,
    CurrentProcess = PsGetCurrentProcess();

PsLookupProcessByProcessId(ProtectedProcess, &ProtectedProcessProcess); // Getting the PEPROCESS using the PID 
PsLookupProcessByProcessId(Lsass, &LsassProcess); // Getting the PEPROCESS using the PID 
PsLookupProcessByProcessId(Csrss1, &Csrss1Process); // Getting the PEPROCESS using the PID 
PsLookupProcessByProcessId(Csrss2, &Csrss2Process); // Getting the PEPROCESS using the PID 


if (OpenedProcess == Csrss1Process) // Making sure to not strip csrss's Handle, will cause BSOD
    return OB_PREOP_SUCCESS;

if (OpenedProcess == Csrss2Process) // Making sure to not strip csrss's Handle, will cause BSOD
    return OB_PREOP_SUCCESS;

if (OpenedProcess == CurrentProcess) // make sure the driver isnt getting stripped ( even though we have a second check )
    return OB_PREOP_SUCCESS;

if (OpenedProcess == ProtectedProcess) // Making sure that the game can open a process handle to itself
    return OB_PREOP_SUCCESS;

if (OperationInformation->KernelHandle) // allow drivers to get a handle
    return OB_PREOP_SUCCESS;


// PsGetProcessId((PEPROCESS)OperationInformation->Object) equals to the created handle's PID, so if the created Handle equals to the protected process's PID, strip
if (PsGetProcessId((PEPROCESS)OperationInformation->Object) == ProtectedProcess)
{

    if (OperationInformation->Operation == OB_OPERATION_HANDLE_CREATE) // striping handle 
    {
        OperationInformation->Parameters->CreateHandleInformation.DesiredAccess = (SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION);
    }
    else
    {
        OperationInformation->Parameters->DuplicateHandleInformation.DesiredAccess = (SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION);
    }

    return OB_PREOP_SUCCESS;
}
}

此代码一旦在ObRegisterCallback中注册,将检测何时为您的受保护进程创建了新的句柄,如果不是来自Lsass,Csrss或它本身,则将杀死该句柄。这是为了防止来自关键进程的蓝屏被拒绝处理 您的应用程序。