Friday, June 26, 2009

WDM Driver

WDM程式編譯出來的並不是我們常見的.exe,而是.sys檔案,在未經設定編譯環境之前,是不能直接用VC來編譯的.其實驅動程式也是一種PE檔案,它同樣由DOS MZ header開頭,也有完整的DOS stub和PE header,同樣擁有Import table和Export table.

我們可以使用tdump.exe來分析.sys檔案.首先使用Delphi裡附帶的tdump.exe程式,然後鍵入︰C:\WINNT\System32\Drivers>tdump ccport.sys -em -ee
參數-em是列出Import table,-ee是列出Export table。返回之後,螢幕列出一大堆東西︰

C:\WINNT\SYSTEM32\DRIVERS>tdump ccport.sys -em -ee
Turbo Dump Version 5.0.16.12 Copyright ? 1988, 2000 Inprise Corporation
Display of File CCPORT.SYS

IMPORT: NTOSKRNL.EXE={hint:011Fh}.’memcpy’
IMPORT: NTOSKRNL.EXE={hint:003Dh}.’IoDeleteDevice’
IMPORT: NTOSKRNL.EXE={hint:0030h}.’IoAttachDeviceToDeviceStack’
IMPORT: NTOSKRNL.EXE={hint:008Eh}.’KeSetEvent’
IMPORT: NTOSKRNL.EXE={hint:0068h}.’IofCallDriver’
IMPORT: NTOSKRNL.EXE={hint:0095h}.’KeWaitForSingleObject’
IMPORT: NTOSKRNL.EXE={hint:0074h}.’KeInitializeEvent’
IMPORT: NTOSKRNL.EXE={hint:003Fh}.’IoDetachDevice’
IMPORT: NTOSKRNL.EXE={hint:00D3h}.’RtlFreeUnicodeString’
IMPORT: NTOSKRNL.EXE={hint:0077h}.’KeInitializeSpinLock’
IMPORT: NTOSKRNL.EXE={hint:0129h}.’strcpy’
IMPORT: NTOSKRNL.EXE={hint:0121h}.’memset’
IMPORT: NTOSKRNL.EXE={hint:003Ch}.’IoCreateUnprotectedSymbolicLink’
IMPORT: NTOSKRNL.EXE={hint:0038h}.’IoCreateDevice’
IMPORT: NTOSKRNL.EXE={hint:00C2h}.’RtlAnsiStringToUnicodeString’
IMPORT: NTOSKRNL.EXE={hint:0069h}.’IofCompleteRequest’
IMPORT: NTOSKRNL.EXE={hint:0124h}.’sprintf’
IMPORT: NTOSKRNL.EXE={hint:003Eh}.’IoDeleteSymbolicLink’
IMPORT: NTOSKRNL.EXE={hint:0042h}.’IoFreeIrp’
IMPORT: NTOSKRNL.EXE={hint:004Dh}.’IoInitializeIrp’
IMPORT: NTOSKRNL.EXE={hint:002Dh}.’IoAllocateIrp’
IMPORT: NTOSKRNL.EXE={hint:0027h}.’InterlockedExchange’
IMPORT: NTOSKRNL.EXE={hint:0025h}.’InterlockedCompareExchange’
IMPORT: NTOSKRNL.EXE={hint:0035h}.’IoCancelIrp’
IMPORT: NTOSKRNL.EXE={hint:012Ah}.’strlen’
IMPORT: NTOSKRNL.EXE={hint:0126h}.’strcat’
IMPORT: NTOSKRNL.EXE={hint:0114h}.’atoi’
IMPORT: NTOSKRNL.EXE={hint:0128h}.’strcmp’
IMPORT: NTOSKRNL.EXE={hint:0034h}.’IoBuildSynchronousFsdRequest’
IMPORT: NTOSKRNL.EXE={hint:00D5h}.’RtlInitAnsiString’
IMPORT: HAL.DLL={hint:0006h}.’KfAcquireSpinLock’
IMPORT: HAL.DLL={hint:0009h}.’KfReleaseSpinLock’

EXPORT ord:0001=’Vcomm_DriverControl’

我們可以很清楚地看到,它主要是用了NTOSKRNL.EXE和HAL.DLL檔案(幾乎所有的WDM驅動程式都會使用NTOSKRNL.EXE檔案),並且輸出了一個函數“Vcomm_DriverControl”。

這說明.sys跟.exe檔案都是一種PE檔案,不同的是.sys檔案Import的通常是NTOSKRNL.EXE,而.exe檔案Import的通常是KERNEL32.DLL和USER32.DLL。

因為.sys通常不使用KERNEL32.DLL和USER32.DLL,所以是不能在設備驅動程式裡面使用任何C、C++和Win32函數,而且也不能使用C++關鍵字new和delete等(不過可以用malloc和free來代替),而必須使用大量的內核函數。

下面是列出一些常見的驅動程式可用的內核函數︰
Ex… 執行支援
Hal… 硬體抽象層(僅NT/Windows 2000)
Io… I/O管理器(包括即插即用函數)
Ke… 內核
Ks… 內核流IRP管理函數
Mm… 內存管理器
Ob… 對象管理器
Po… 電源管理
Ps… 進程架構
Rtl… 營運時庫
Se… 安全引用監視
Zw… 其他函數

No comments: