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或者使用測試簽名,這樣就正常了:

相關文章
相關標籤/搜索