參考資訊:
https://wasm.in/
http://four-f.narod.ru/
https://github.com/steward-fu/ddk
main.pas
unit main; interface uses DDDK; const DEV = '\Device\MyDriver'; function _DriverEntry(pMyDriver : PDRIVER_OBJECT; pMyRegistry : PUNICODE_STRING) : NTSTATUS; stdcall; implementation var pNextDevice : PDEVICE_OBJECT; procedure Unload(pMyDriver : PDRIVER_OBJECT); stdcall; begin end; function IrpPnp(pMyDevice : PDEVICE_OBJECT; pIrp : PIRP) : NTSTATUS; stdcall; var pStack : PIO_STACK_LOCATION; begin pStack := IoGetCurrentIrpStackLocation(pIrp); if pStack^.MinorFunction = IRP_MN_REMOVE_DEVICE then begin IoDetachDevice(pNextDevice); IoDeleteDevice(pMyDevice); IoCompleteRequest(pIrp, IO_NO_INCREMENT); end else begin IoSkipCurrentIrpStackLocation(pIrp); Result := IoCallDriver(pNextDevice, pIrp); end; end; function AddDevice(pMyDriver : PDRIVER_OBJECT; pPhyDevice : PDEVICE_OBJECT) : NTSTATUS; stdcall; var suDevName : UNICODE_STRING; pMyDevice : PDEVICE_OBJECT; begin DbgPrint('Hello, world!', []); RtlInitUnicodeString(@suDevName, DEV); IoCreateDevice(pMyDriver, 0, @suDevName, FILE_DEVICE_UNKNOWN, 0, FALSE, pMyDevice); pNextDevice := IoAttachDeviceToDeviceStack(pMyDevice, pPhyDevice); pMyDevice^.Flags := pMyDevice^.Flags or DO_BUFFERED_IO; pMyDevice^.Flags := pMyDevice^.Flags and not DO_DEVICE_INITIALIZING; Result := STATUS_SUCCESS; end; function _DriverEntry(pMyDriver : PDRIVER_OBJECT; pMyRegistry : PUNICODE_STRING) : NTSTATUS; stdcall; begin pMyDriver^.MajorFunction[IRP_MJ_PNP] := @IrpPnp; pMyDriver^.DriverExtension^.AddDevice := @AddDevice; pMyDriver^.DriverUnload := @Unload; Result := STATUS_SUCCESS; end; end.
L53~56:只有做Callback設定的動作,需要設定AddDevice、PNP、DriverUnload這三個Callback
L43~48:產生一個Device Object,用來管理新加入的裝置(純軟體虛擬裝置也算),接著初使化相關旗標
L21~32:主要處理移除Device Object的動作,其餘Irp不處理,直接往下傳遞
main.inf
[Version] Signature=$CHICAGO$ Class=Unknown Provider=%MFGNAME% DriverVer=8/21/2019,1.0.0.0 [Manufacturer] %MFGNAME%=DeviceList [DeviceList] %DESCRIPTION%=DriverInstall, *MyDriver [DestinationDirs] DefaultDestDir=10,System32\Drivers [SourceDisksFiles] main.sys=1,,, [SourceDisksNames] 1=%INSTDISK%,,, [DriverInstall.NT] CopyFiles=DriverCopyFiles [DriverCopyFiles] main.sys,,,2 [DriverInstall.NT.Services] AddService=FILEIO,2,DriverService [DriverService] ServiceType=1 StartType=3 ErrorControl=1 ServiceBinary=%10%\system32\drivers\main.sys [DriverInstall.NT.HW] AddReg=DriverHwAddReg [DriverHwAddReg] HKR,,SampleInfo,,"" [DriverInstall] AddReg=DriverAddReg CopyFiles=DriverCopyFiles [DriverAddReg] HKR,,DevLoader,,*ntkern HKR,,NTMPDriver,,main.sys [DriverInstall.HW] AddReg=DriverHwAddReg [Strings] MFGNAME="MyDriver" INSTDISK="MyDriver Disc" DESCRIPTION="MyDriver"
編譯
"c:\dddk\bin\dcc32.exe" -U"c:\dddk\inc" -B -CG -JP -$A-,C-,D-,G-,H-,I-,L-,P-,V-,W+,Y- main.pas "c:\dddk\bin\omf2d.exe" main.obj "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEZwClose=_ZwClose@4 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEDbgPrint=_DbgPrint "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEExFreePool=_ExFreePool@4 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEKeSetTimer=_KeSetTimer@16 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEIoStopTimer=_IoStopTimer@4 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEIoCallDriver=_IoCallDriver@8 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEIoStartTimer=_IoStartTimer@4 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEKeSetTimerEx=_KeSetTimerEx@20 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEProbeForRead=_ProbeForRead@12 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEKeCancelTimer=_KeCancelTimer@4 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEIoStartPacket=_IoStartPacket@16 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEZwOpenProcess=_ZwOpenProcess@16 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEIoDetachDevice=_IoDetachDevice@4 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEIoDeleteDevice=_IoDeleteDevice@4 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEExAllocatePool=_ExAllocatePool@8 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEKeReleaseMutex=_KeReleaseMutex@8 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEIoCsqInsertIrp=_IoCsqInsertIrp@12 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEIoCreateDevice=_IoCreateDevice@28 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEKeInitializeDpc=_KeInitializeDpc@12 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEIoCsqInitialize=_IoCsqInitialize@28 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEIoStartNextPacket=_IoStartNextPacket@8 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEIoCompleteRequest=_IoCompleteRequest@8 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEKeInitializeMutex=_KeInitializeMutex@8 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEKeInitializeTimer=_KeInitializeTimer@4 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEKeReleaseSpinLock=_KeReleaseSpinLock@8 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEIoInitializeTimer=_IoInitializeTimer@12 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEObDereferenceObject=_ObDereferenceObject@4 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEIoGetCurrentProcess=_IoGetCurrentProcess@0 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEInterlockedExchange=@InterlockedExchange@8 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEKeInitializeSpinLock=_KeInitializeSpinLock@4 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEIoCreateSymbolicLink=_IoCreateSymbolicLink@8 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEIoDeleteSymbolicLink=_IoDeleteSymbolicLink@4 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CERtlInitUnicodeString=_RtlInitUnicodeString@8 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEPsCreateSystemThread=_PsCreateSystemThread@28 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEKeWaitForSingleObject=_KeWaitForSingleObject@20 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEExAllocatePoolWithTag=_ExAllocatePoolWithTag@12 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEKeDelayExecutionThread=_KeDelayExecutionThread@12 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEKeServiceDescriptorTable=_KeServiceDescriptorTable "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEExAllocatePoolWithQuota=_ExAllocatePoolWithQuota@8 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEPsTerminateSystemThread=_PsTerminateSystemThread@4 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEObReferenceObjectByHandle=_ObReferenceObjectByHandle@24 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEIoAttachDeviceToDeviceStack=_IoAttachDeviceToDeviceStack@8 "c:\dddk\bin\omf2d.exe" "c:\dddk\inc\dddk.obj" /U- /CEMmMapLockedPagesSpecifyCache=_MmMapLockedPagesSpecifyCache@24 "c:\dddk\bin\link.exe" /nologo /align:32 /base:0x10000 /subsystem:native /driver /force:unresolved /force:multiple /entry:DriverEntry "c:\dddk\inc\dddk.obj" "c:\dddk\lib\ntoskrnl.lib" main.obj /out:main.sys
在開始安裝驅動程式之前,需要先下載除錯工具,讓驅動程式的Debug訊息可以顯示在除錯工具上面,目前最佳的Debug輸出訊息工具是DbgView,該公司目前已經被Microsoft併購,所以可以從Microsoft網站下載,下載完後執行DbgView並將Capture => Capture Kernel選項打勾,接著重啟DbgView
對於驅動程式的安裝工具,司徒目前使用NuMega公司製作的EzDriverInstaller,將main.sys和main.inf放在同一個目錄並執行EzDriverInstaller,選擇File => Open...(開啟main.inf檔案),接著按Add New Device就可以在DbgView上面看到輸出訊息
Device Manager
Device Object