当前位置:编程学习 > 网站相关 >>

驱动编程之终极隐藏文件驱动技术

关于驱动的编程资料总是很少,文件驱动也是这样。主要资料是MSDN上Windows Driver Kit: Installable File System Drivers的介绍,
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/IFSK_d/hh/IFSK_d/fsfiltdrvhdr_74bd027c-19ed-4137-81e5-2fca622fda09.xml.asp
。主要资料就是这个了,说得还算可以,虽然细节不太详细,但做文件驱动的话,这个是必须看的。其他的资料的话还有《Windows NT File System Internals, A Developers Guide》
听说这是关于文件驱动编程的唯一图书,不过年代很久了。电子书在网上可以找的到,不过我看了一下,讲的并不比MSDN上面的清楚,所以我也没怎么看这个。网站的话有一个
驱动程序开发网技术社区 -> 文件系统(过滤)驱动程序开发 http://bbs.zndev.com/thread.php?fid-39.html 上面有认讨论这方面的知识,也有人发了例子。也有参考资料
不过也都是从MSDN上面翻译下来的。
 
编程工具:
     Windows Driver Kit----Windows Server 2003 with Service Pack 1 (SP1)
     这个可以到微软的主页上下载。WDK有很多个版本,并不是每个都可以用来编写File System Minifilter Drivers(文件驱动有两种,这个是比较新的,
MSDN上说这个比较简单,所以我就用这个了^_^)。我是在http://connect.microsoft.com/上面注册参加他的Beta Testing the Windows Driver Kit。从那里
下载的最新的Windows Driver Kit,下载下来有2G多。有的版本不能写这种新的文件驱动的,那就只能写老的那种了,不过好像那个更复杂哟。
本来File System Minifilter Drivers就是微软为了简化文件驱动编程而新增加的。我水平不够就用这个了!!
 
 
学习过程:
Windows Driver Kit 安装完之后,SRC目录下有很多例子,关于filesys的有好多个,minifilter的几个例子如
filespy,scanner,swapbuffer等几个例子都很不错,都要看一下。自己看一下这些驱动编程的文件结构,*.inf文件
是安装定义的。安装好Windows Driver Kit后,启动在开始菜单里的几个创建驱动的快捷方式(我用 free xp emvironment 版本的)
使用CD命令切换 到这些 例子的目录下,输入build -cZg就可以编译生成 驱动文件*.sys文件了,右击相应的*.inf文件,选择Install,就可以
把这些驱动安装系统中去了。
 
inf文件中StartType可以根据需要来设置。
 
StartType    可以为如下值。                
;SERVICE_AUTO_START (0x00000002)   设置为 SERVICE_AUTO_START时,安全模式下不会自动加载
; SERVICE_BOOT_START (0x00000000) 在系统安全模式下启动时 驱动也会自动加载
; 使用 SERVICE_DEMAND_START (0x00000003) 则驱动不会自动加载
 
如果是采用SERVICE_DEMAND_START的话,还要在命令提示行中 使用 “fltmc load 你的驱动名字” 才能真正载入minifile驱动。其他两个值的话,系统启动时会自动加载,
当然也可以使用 fltmc load 来加载了。相应的卸载命令是 “fltmc unload 你的驱动名字”。
 
如果你在程序中使用了 Dbgprint 函数了的话,可以使用DbgView 工具来查看信息,可以看到你写的驱动已经在工作了,或者直接在命令提示行下输入fltmc 命令,也可以看到哪些命令加载了。使用Dbgprint 函数和DbgView 工具也是一个非常重要的调试驱动方法。我没有装其他的调试工具,只能用这个来做调试程序了,驱动的调试很难啊!
 
编程简单介绍:
 
驱动入口函数DriverEntry: 和C中的Winmain 函数作用差不多。在这个函数中要注册filter 和启动fltter。
 
 
NTSTATUS
DriverEntry (
    __in PDRIVER_OBJECT DriverObject,
    __in PUNICODE_STRING RegistryPath
    )
/*++
 
Routine Description:
 
    This is the initialization routine for this miniFilter driver. This
    registers the miniFilter with FltMgr and initializes all
    its global data structures.
 
Arguments:
 
    DriverObject - Pointer to driver object created by the system to
        represent this driver.
    RegistryPath - Unicode string identifying where the parameters for this
        driver are located in the registry.
 
Return Value:
 
    Returns STATUS_SUCCESS.
 
--*/
{
    NTSTATUS status;
status = FltRegisterFilter(               //注册Filter
           DriverObject,                  //Driver
           &FilterRegistration,           //Registration
           &MiniSpyData.FilterHandle);    //RetFilter
 
status = FltStartFiltering( MiniSpyData.FilterHandle ); //启动,初始化Filter
if( !NT_SUCCESS( status )) {
FltUnregisterFilter( MiniSpyData.FilterHandle );
 
 
}
 
returns STATUS_SUCCESS; //表示注册成功。
 
}
 
 
FilterUnloadCallback 函数看名字就知道是卸载函数了,驱动卸载时调用,作一些清理工作。
 
NTSTATUS
HideFileFilterUnload (
    __in FLT_FILTER_UNLOAD_FLAGS Flags
    )
/*++
 
Routine Description:
 
    This is the unload routine for this miniFilter driver. This is called
    when the minifilter is about to be unloaded. We can fail this unload
    request if this is not a mandatory unloaded indicated by the Flags
    parameter.
 
Arguments:
 
    Flags - Indicating if this is a mandatory unload.
 
Return Value:
 
    Returns the final status of this operation.
 
--*/
{
    UNREFERENCED_PARAMETER( Flags );
 
    PAGED_CODE();
 
 
     FltUnregisterFilter( HideFileFilterData.FilterHandle );
     return STATUS_SUCCESS;             //可以被 fltmc unload hidefilefilter 命令卸载
   
}
 
 
还有一个 简单的QueryTeardown 函数。
NTSTATUS
HideFileFilterQueryTeardown (
    __in PCFLT_RELATED_OBJECTS FltObjects,
    __in FLT_INSTANCE_QUERY_TEARDOWN_FLAGS Flags
    )
/*++
 
Routine Description:
 
    This is the instance detach routine for this miniFilter driver.
    This is called when an instance is being manually deleted by a
    call to FltDetachVolume or FilterDetach thereby giving us a
    chance to fail that detach request.
 
Arguments:
 
    FltObjects - Pointer to the FLT_RELATED_OBJECTS data structure containing
        opaque handles to this filter, instance and its associated volume.
 
    Flags - Indicating where this detach request came from.
 
Return Value:
 
    Returns the status of this operation.
 
--*/
{
    UNREFERENCED_PARAMETER( FltObjects );
    UNREFERENCED_PARAMETER( Flags );
 
    PAGED_CODE();
 
    return STATUS_SUCCESS;
}
 
 
其他的函数就是那些IRP callback函数了,有pre callback(系统处理之前) 和post callback(系统处理之后)。看你要做哪些功能了,做隐藏文件的话处理IRP_MJ_DIRECTORY_CONTROL
就行了。这些函数都是在DriverEntry中创建时的那几个结构中定义的,然后系统就
补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,