驅動程式 - Windows NT Driver (Legacy) - BASIC - Hello, world!



參考資訊:
https://github.com/steward-fu/ddk

main.bas

#include "ntdef.bi"
#include "ntddk.bi"
#include "ntstatus.bi"
#include "ntoskrnl.bi"

Declare Function DriverEntry stdcall Alias "DriverEntry"(ByVal pDriverObject As PDRIVER_OBJECT, ByVal pRegistryPath As PUNICODE_STRING) As NTSTATUS

Sub Unload(ByVal DriverObject As PDRIVER_OBJECT)
End Sub

Function DriverEntry(ByVal pDriverObject As PDRIVER_OBJECT, ByVal pRegistryPath As PUNICODE_STRING) As NTSTATUS
    DbgPrint(@!"Hello, world!")
    with *pDriverObject
        .DriverUnload = @Unload
    end with
    return STATUS_SUCCESS
End Function

編譯

c:\> c:\freebasic\fbc32.exe -c -i c:\freebasic\inc\w2k main.bas
c:\> c:\masm32\bin\link.exe main.o /driver /base:0x10000 /subsystem:native,5.01 /entry:GsDriverEntry@8 "c:\masm32\lib\wxp\i386\bufferoverflowk.lib" "c:\masm32\lib\wxp\i386\ntoskrnl.lib" /OUT:main.sys

在開始安裝驅動程式之前,我們需要先下載除錯工具,讓驅動程式的Debug訊息可以顯示在除錯工具上面,目前在Kernel Mode以及User Mode上,最佳的Debug輸出訊息工具是DbgView,該公司目前已經被Microsoft併購,所以可以從Microsoft網站下載,下載完後執行DbgView並將Capture => Capture Kernel選項打勾,接著重啟DbgView


Legacy(Nt-Style)驅動程式的安裝很方便,它是使用Service的方式安裝,因此,複製main.sys到c:\windows\system32\drivers資料夾下並輸入如下命令進行安裝

c:\> sc create MyDriver binPath= "c:\windows\system32\drivers\main.sys" type= "kernel" start= "demand" error= "normal" Displayname= "MyDriver"
c:\> sc start MyDriver

P.S. 要記得在"="前面都需要一個空格

輸入完上列指令後,就可以看到輸出的Hello, world!字串


當然,使用者也可以使用Four-F撰寫的KmdManager進行安裝,開啟KmdManager後,載入main.sys後,選擇Register => Run即可