Upgrade to Pro — share decks privately, control downloads, hide ads and more …

UCCU Monthly Talk: Windows Driver

NotSurprised
September 30, 2018

UCCU Monthly Talk: Windows Driver

Structure of Windows Driver. Building environment & Driver signing.

NotSurprised

September 30, 2018
Tweet

More Decks by NotSurprised

Other Decks in Programming

Transcript

  1. > • Background • Driver Compiling • IRP • Driver

    Dev. • Driver Debug • Driver Signing • Conclusion
  2. > • Windows Driver Model,簡稱WDM • Windows OS的I驅動程式的種類有: – 匯流排驅動程式(bus

    driver) – 功能驅動程式(function driver) – 篩選驅動程式(filter driver)
  3. > & • Windows 7 後 Filter 的編譯生成被整合進VS,並進行了 一次架構更新,更名為Minifilter。 •

    Minifilter比傳統Filter更容易構建編譯,並且其可以動態 安裝/卸載是傳統Filter所不能的。
  4. > • VXD (Virtual X Driver) – Windows 95、Windows 98

    • KDM (Kernel Driver Model) – Windows NT • WDM (Windows Driver Model) – Windows 2000 ~ Windows 8.1 – 使用 DDK (Driver Developer Kit) • WDF (Windows Driver Frameworks) – Windows 7 ~ Windows 10 – 使用 WDK (Windows Driver Kit)
  5. >

  6. >

  7. • 需要以下 header 檔案: fltKernel.h, mce.h, ntnls.h, crtdefs.h, ctype.h, excpt.h,

    stdio.h, string.h, swprintf.inl, vadefs.h, ntddk.h, ntifs.h, wdm.h, wsk.h, netioddk.h 這些在裝完 WDK 後會存於C:\Program Files (x86)\Windows Kits\10\Include 以管理員權限到 Visual Studio 開起專案, 設定「解決方案配置」(ex. win 8.1 debug)與「解決方案平台」(ex. win32),然後 debug。 • minispy.exe & minispy.sys 建置: > cd C:\WDK 10.0 Sample\filesys\miniFilter\minispy > msbuild /t:clean /t:build .\minispy.sln > msbuild /p:KernelBufferOverflowLib=“C:\Program Files (x86)\Windows Kits\10\Lib\win8\km\x64\BufferOverflowK.lib” /p:platform=x64 /p:Configuration=“Win8.1 Release” minispy.sln minispy.exe minispy.sys 會生成在 C:\WDK 10.0 Sample\filesys\miniFilter\minispy\ • 至實驗機右鍵 minispy.inf 安裝,確認 C:\Windows\System32\drivers 下有無 minispy.sys 有則代 表可以使用管理者權限開啟 cmd 並使用 net start minispy 的指令將 minispy load 進 fltmc • 使用同個具管理者權限的 cmd 視窗 cd 至 minispy.exe 所在位置,使用 minispy.exe 執行 minispy。 > 、 、 WDM編譯
  8. • 需要以下 header 檔案: fltKernel.h, mce.h, ntnls.h, crtdefs.h, ctype.h, excpt.h,

    stdio.h, string.h, swprintf.inl, vadefs.h, ntddk.h, ntifs.h, wdm.h, wsk.h, netioddk.h 這些在裝完 WDK 後會存於C:\Program Files (x86)\Windows Kits\10\Include 以管理員權限到 Visual Studio 開起專案, 設定「解決方案配置」(ex. win 8.1 debug)與「解決方案平台」(ex. win32),然後 debug。 • minispy.exe & minispy.sys 建置: > cd C:\WDK 10.0 Sample\filesys\miniFilter\minispy > msbuild /t:clean /t:build .\minispy.sln > msbuild /p:KernelBufferOverflowLib=“C:\Program Files (x86)\Windows Kits\10\Lib\win8\km\x64\BufferOverflowK.lib” /p:platform=x64 /p:Configuration=“Win8.1 Release” minispy.sln minispy.exe minispy.sys 會生成在 C:\WDK 10.0 Sample\filesys\miniFilter\minispy\ • 至實驗機右鍵 minispy.inf 安裝,確認 C:\Windows\System32\drivers 下有無 minispy.sys 有則代表可 以使用管理者權限開啟 cmd 並使用 net start minispy 的指令將 minispy load 進 fltmc • 使用同個具管理者權限的 cmd 視窗 cd 至 minispy.exe 所在位置,使用 minispy.exe 執行 minispy。 > 、 、
  9. >

  10. >

  11. >

  12. >

  13. >

  14. ( )

  15. > • IRP (I/O Request Package) 是在 Windows 內核中的一種資料結構, 它是儲存與輸入輸出相關重要資料的資料結構。

    • IRP 是一個很複雜的資料結構,首先需要了解 IRP 有兩個基本屬性, MajorFunction 和 MinorFunction,其分別記錄 IRP 的主類型和子 類型。 • 同樣的 MajorFunction 可能因為 MinorFunction 及參數的不同組合 出複雜多變的行為
  16. > • IRP_MJ_DIRECTORY_CONTROL • IRP_MJ_READ • IRP_MJ_WRITE • IRP_MJ_QUERY_INFORMATION •

    IRP_MJ_SET_INFORMATION • IRP_MJ_CREATE • IRP_MJ_CLEANUP • IRP_MJ_CLOSE • IRP_MJ_DEVICE_CONTROL • IRP_MJ_LOCK_CONTROL • IRP_MJ_SET_VOLUME_INFORMATION • IRP_MJ_QUERY_SECURITY • IRP_MJ_SET_EA • …….
  17. >

  18. >

  19. >

  20. >

  21. >

  22. >

  23. > • UTF8 encode : 魯a = 9B 6F 00

    61 • ASCII encode : 9B 6F 00 61 = o a String Type Description char *str = {“kd string”} ANSI string wchar_t *wstr = {L”kd string”} Unicode string size_t len = strlen(str) ANSI string len size_t wlen Unicode string len printf(“%s %ws %d %d”, str, wstr, len, wlen) print format OutputDebugString(“%s”, wstr) print format
  24. > C WDK unsigned long ULONG unsigned char UCHAR unsigned

    int UINT void VOID unsigned long * PULONG unsigned char * PUCHAR unsigned int* PUINT void * PVOID
  25. > HANDLE Event UNICODE_STRING ObjectName Length MaximumLength Buffer 4 byte

    2 byte 2 byte 4 byte HANDLE Event UNICODE_STRING ObjectName Length MaximumLength Padding Buffer 8 byte 2 byte 2 byte 4 byte 8 byte
  26. > • On Microsoft Windows 2000 and later versions of

    the operating system, \?? is equivalent to \DosDevices. • For example, the object name of the C:\WINDOWS\example.txt file is \DosDevices\C:\WINDOWS\example.txt. Path Type Description \\abc\xyz MSDN C:\abc\xyz MSDN \\.\C:\abc\xyz MSDN \\?\C:\abc\xyz MSDN \\?\UNC\abc\xyz MSDN \??\UNC\abc\xyz MSDN ?\UNC\abc\xyz Actually in WindowsServer 2012 2016
  27. > • Use Driver to change IRP content. • If

    need error message, deny it. NTSTATUS IORequestPacketDeny(_In_ PFLT_CALLBACK_DATA Data, _In_ PCFLT_RELATED_OBJECTS FltObjects) { Data->Iopb->MajorFunction = IRP_MJ_CLOSE; Data->IoStatus.Information = 0; Data->IoStatus.Status = STATUS_ACCESS_DENIED; Data->Iopb->TargetFileObject->FileName.Buffer = NULL; FLT_SET_CALLBACK_DATA_DIRTY(Data); return FLT_PREOP_COMPLETE; }
  28. >

  29. > • Just change Sample, attach to device we could

    obtain. • RtlStringCchPrintfW(name, 32, L“\\Device\\Serial%d”, id); ;
  30. > • csrss.exe -> NtCreateFile -> IRP_MJ_READ(PDO) • IRP Pending(KeyBoard)

    • typedef struct _KEYBOARD_INPUT_DATA { USHORT UnitId; USHORT MakeCode; //mark pop up/press down USHORT Flags; USHORT Reserved; ULONG ExtraInformation; } KEYBOARD_INPUT_DATA, *PKEYBOARD_INPUT_DATA;
  31. > • PatchGuard x64 一言不合就藍屏 Process/3rdAPI (wxWidgets.lib) Windows API System

    Service Dispatcher (KiSystemService) NtCreateFile SSDT User Mode Kernel Mode ntdll.dll NtClose
  32. > System Service Dispatcher (KiSystemService) NtCreateFile SSDT User Mode Kernel

    Mode ntdll.dll NewCreateFile 你的指標不是你的指標,請用 MmGetSystemRoutineAddress()
  33. > • TDI internet socket filter (Transport Driver Interface) •

    WFP internet socket filter (Windows Filtering Platform) • It is similar to minifilter but focus on internet rather than storage device. > • Speed up • Shutdown reset
  34. >

  35. >

  36. >

  37. >

  38. >

  39. >

  40. >

  41. >

  42. >

  43. >

  44. >

  45. > • I assume you build your own dirver already,

    if you try to use sign function within VS project, that’s another issue. • You should already have .cat, .sys & .inf. (with sign function in VS, you will get your own usable .cer if you set it right.)
  46. > • MakeCert -r -pe -ss TEST -n "CN=TEST.org" test.cer

    > • CertMgr /add minispy.cer /s /r localMachine root • CertMgr /add minispy.cer /s /r localMachine trustedpublisher
  47. >

  48. >

  49. >