Windows内核驱动开发:HelloWorld

测试信息

Dev Machine:windows

Windows Version: 2004 (19041.264)
WDK Version: 10.0.19041.1
SDK Version: 10.0.19041.1
Visual Studio: Community 2019

Test Machine:函数

Windows 7 SP1 + KMD Manager + DbgView

开发环境搭建

参照:https://docs.microsoft.com/zh-cn/windows-hardware/drivers/download-the-wdk工具

除了在安装VS2019的时候,选择C++桌面开发环境,里面自带一个和当前系统版本一致的SDK,也能够本身修改,可是可能会和系统不兼容。测试

还须要安装的组件:ui

MSVC v142 - VS 2019 C++ ARM build tools (v14.25)
MSVC v142 - VS 2019 C++ ARM Spectre-mitigated libs (v14.25)
MSVC v142 - VS 2019 C++ ARM64 build tools (v14.25)
MSVC v142 - VS 2019 C++ ARM64 Spectre-mitigated libs (v14.25)
MSVC v142 - VS 2019 C++ x64/x86 build tools (v14.25)
MSVC v142 - VS 2019 C++ x64/x86 Spectre-mitigated libs (v14.25)

v14.25根据在全部组件里看到最新版本的为准,作一下适当调整。this

而后下载适用2004WDK安装文件,双击以后,须要联网下载安装WDK相关文件和VS2019驱动开发插件,根据提示点过去就能够。spa

测试环境

启动的时候,须要按f8关闭驱动签名验证,也能够参考这篇文章添加一个关闭驱动签名检查的启动项。插件

KMD Mananger工具用来管理内核驱动服务(注册、启动、中止、卸载),DbgView用来查看驱动打印信息,这两个工具都须要管理员权限运行debug

新建HelloWorld项目

新建一个以Kernel Mode Driver, Empty(KMDF)为模板的驱动项目,项目名称HelloDriver,在项目中新建一个hello.c文件做为驱动入口文件,写一点简单的代码作测试:调试

///
/// @file hello.c
/// @author REInject
/// @date 2020-05-31
///

#include <ntddk.h>

// 提供一个Unload 函数只是为了让这个程序可以动态卸载,方便调试
VOID DriverUnload(PDRIVER_OBJECT driver)
{
	// 可是实际上咱们什么都不作,只打印一句话
	DbgPrint("hello: Our driver is unloading...\r\n");
}

// DriverEntry,入口函数。至关于main。
NTSTATUS DriverEntry(PDRIVER_OBJECT driver, PUNICODE_STRING reg_path)
{
	// 这是内核模块入口,能够在这里写入咱们想写的东西
	DbgPrint("hello: my salary!");

	// 设置一个卸载函数,便于这个函数退出
	driver->DriverUnload = DriverUnload;
	return STATUS_SUCCESS;
}

这时候若是直接运行,默认生成的驱动文件是Win10平台的,并且基本上会编译失败,须要改一些配置信息(Debug-x64):

  • 调整属性 - C/C++ - 常规,警告等级 4,将警告视为错误
  • 连接器 - 常规,警告视为错误 否;
  • Driver Settings - GeneralTarget OS Version改为Windows 7Target Platform改成Desktop
  • Stampinf里全部 的地方改为
  • Inf2Cat里全部 的地方改为
  • Driver Signing - General中的Sign Mode改成 Off

改好后,使用Debug-x64配置,Ctrl-B生成驱动文件,若是报下面这个错误的话:

error 1297: Device driver does not install on any devices, use primitive driver if this is intended.

根据官网的描述,若是建立的驱动不是基于设备的,即通用型内核驱动,则须要删或者改一些东西,若是能够看懂怎么改就直接改就能够,例如:

原始inf中要改的部分:

[Manufacturer]
%ManufacturerName%=Standard,NT$ARCH$

[Standard.NT$ARCH$]
%HelloDriver.DeviceDesc%=HelloDriver_Device, Root\HelloDriver ; TODO: edit hw-id

[HelloDriver_Device.NT]
CopyFiles=Drivers_Dir

[Drivers_Dir]
HelloDriver.sys

;-------------- Service installation
[HelloDriver_Device.NT.Services]
AddService = HelloDriver,%SPSVCINST_ASSOCSERVICE%, HelloDriver_Service_Inst

改为:

[DefaultInstall.NT$ARCH$]
CopyFiles=Drivers_Dir

[Drivers_Dir]
HelloDirver.sys

;-------------- Service installation
[DefaultInstall.NT$ARCH$.Services]
AddService = HelloDriver,%SPSVCINST_ASSOCSERVICE%, HelloDriver_Service_Inst

或者看不懂的,直接删了这个Inf文件就能够了,这个文件暂时用不到。

从新编译以后,会在x64/debug目录下生成HelloDriver.sys驱动文件。

驱动安装测试

打开win7,使用KMD Manager工具进行注册启动中止卸载服务,发现有出现Error Number not found的错误,经过一些测试发现是驱动签名检查没有彻底禁用,根据官网给出的信息,nointegrity参数在win7上是无效的:

nointegritychecks [ on | off ] Disables integrity checks. Cannot be set when secure boot is enabled. This value is ignored by Windows 7 and Windows 8.

只能每次开机手动f8或者使用测试签名,这样就正常了: