zoukankan      html  css  js  c++  java
  • Windows Minifilter驱动

    https://blog.csdn.net/zj510/article/details/39476171

    Windows Minifilter驱动 - 获取进程ID, 进程名字和线程ID 

    在minifilter里面可能有好几种获取调用进程id,名字和线程的办法。我这里有一种:

    使用

    PsSetCreateProcessNotifyRoutine 和

    PsSetLoadImageNotifyRoutine

    这是两个API,我们可以借助它们获取进程信息。具体看:http://msdn.microsoft.com/en-us/library/windows/hardware/ff559951(v=vs.85).aspx

    PsSetLoadImageNotifyRoutine

    可以使用这个函数设置一个通知回调,每当操作系统新加载一个影像文件的时候,回调函数会被调用。

    比如在DriverEntry里面调用:

    [cpp] view plain copy
     
    1. PsSetLoadImageNotifyRoutine(MyMiniFilterLoadImage);  

    回调函数:

    [cpp] view plain copy
     
    1. VOID   
    2. MyMiniFilterLoadImage( __in_opt PUNICODE_STRING FullImageName, __in HANDLE ProcessId, __in PIMAGE_INFO ImageInfo )  
    3. {  
    4.     UNREFERENCED_PARAMETER(ImageInfo);  
    5.   
    6.     if (FullImageName)  
    7.     {  
    8.         DbgPrint("MyMiniFilterLoadImage, image name: %wZ, pid: %d ", FullImageName, ProcessId);  
    9.     }  
    10.     else  
    11.         DbgPrint("MyMiniFilterLoadImage, image name: null, pid: %d ", ProcessId);  
    12. }  

    这样,当用户新启动一个进程的时候,这个函数就会被调用,里面可以获取进程名和进程id,还有个ImageInfo。

    PsSetCreateProcessNotifyRoutine

    我们可以使用这个函数来设置一个回调函数。每当操作系统创建一个进程或者关闭一个进程的时候,回调都会被调用。

    DriverEntry调用:

    [cpp] view plain copy
     
    1. PsSetCreateProcessNotifyRoutine(MyMiniFilterProcessNotify, FALSE);  


    具体回调:

    [cpp] view plain copy
     
    1. VOID  
    2. MyMiniFilterProcessNotify(  
    3. IN HANDLE  ParentId,  
    4. IN HANDLE  ProcessId,  
    5. IN BOOLEAN  Create  
    6. )  
    7. {  
    8.     DbgPrint("MyMiniFilterProcessNotify, pid: %d, tid: %d, create: %d ", ParentId, ProcessId, Create);  
    9. }  

    IRP Event中获取进程ID和线程ID

    比如在IRP_MJ_WRITE中获取:

    [cpp] view plain copy
     
    1. ULONG ProcessId = FltGetRequestorProcessId(Data);  
    2. ULONG ThreadId = (ULONG)PsGetThreadId(Data->Thread);  
    3.   
    4. DbgPrint("IRP_MJ_WRITE, pid: %d, tid: %d ", ProcessId, ThreadId);  

    注意:PsGetThreadId是个未公开API。调用需要小心一些。

    如果我们想在IRP回调中获取进程名字。有个办法就是在PsSetLoadImageNotifyRoutine的回调函数里面把进程名和进程ID保存下来,比如保存到一个list。这样,在IRP回调里面可以用进程ID来获取进程名。

    取消回调

    在驱动被卸载之前,需要取消回调。不然会蓝屏。比如可以在FilterUnload那里取消。

    [cpp] view plain copy
     
      1. PsSetCreateProcessNotifyRoutine(MyMiniFilterProcessNotify, TRUE);  
      2.     PsRemoveLoadImageNotifyRoutine(MyMiniFilterLoadImage);  
  • 相关阅读:
    程序员学习方法差在哪里
    解析域名
    tomcat下的公共jar包配置
    Ubuntu 16.04 修改状态栏位置
    sqlite3 C语言 API 函数
    vim配置文件
    关于 ioctl 函数
    字符设备基础了解
    Ubuntu14.04搭建Boa服务
    gcc 交叉工具链中工具使用(arm-linux-xxx)
  • 原文地址:https://www.cnblogs.com/endenvor/p/9067933.html
Copyright © 2011-2022 走看看