Slide 1

Slide 1 text

NotSurprised @ UCCU [email protected]

Slide 2

Slide 2 text

> NotSurprised • ITRI engineer • UCCU hacker • 中山資管五年學碩士 Email : [email protected]

Slide 3

Slide 3 text

> • Background • Driver Compiling • IRP • Driver Dev. • Driver Debug • Driver Signing • Conclusion

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

> • Windows Driver Model,簡稱WDM • Windows OS的I驅動程式的種類有: – 匯流排驅動程式(bus driver) – 功能驅動程式(function driver) – 篩選驅動程式(filter driver)

Slide 6

Slide 6 text

> & • Windows 7 後 Filter 的編譯生成被整合進VS,並進行了 一次架構更新,更名為Minifilter。 • Minifilter比傳統Filter更容易構建編譯,並且其可以動態 安裝/卸載是傳統Filter所不能的。

Slide 7

Slide 7 text

> • 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)

Slide 8

Slide 8 text

>

Slide 9

Slide 9 text

> • 在 Windows作業系統的核心層(kernel-mode)中,是階層式的組態, 這種階層式驅動程式架構(Layered driver Architecture)又被稱為 驅動程式堆疊(Driver Stack)。 source : MSDN

Slide 10

Slide 10 text

> • Windows OS的IRP(I/O Request Packets)傳遞架構: source : MSDN

Slide 11

Slide 11 text

>

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

• 需要以下 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編譯

Slide 14

Slide 14 text

• 需要以下 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。 > 、 、

Slide 15

Slide 15 text

• 在Microsoft發布微過濾器(minifilter)框架後,先後歷經 Windows 7到Windows 8之間的開發轉變,Microsoft 將許多編譯方式整合進Visual Studio,因此在安裝Visual Studio以及相關WDK、SDK時必須注意版本問題。 • 其中WDK可從官網下載,而SDK則建議於安裝Visual Studio時安裝想匹配的版本,而Visual Studio安裝SDK 預設為不安裝,因此需開啟自訂安裝並加以勾選。 • ARM/ARM64 > 、 、

Slide 16

Slide 16 text

> 、 、

Slide 17

Slide 17 text

> 、 、

Slide 18

Slide 18 text

> 、 、

Slide 19

Slide 19 text

> 、 、

Slide 20

Slide 20 text

>

Slide 21

Slide 21 text

>

Slide 22

Slide 22 text

> • WDF builder

Slide 23

Slide 23 text

>

Slide 24

Slide 24 text

> 、 、

Slide 25

Slide 25 text

> 、 、 終於把檔案安裝進System32\drivers中

Slide 26

Slide 26 text

> 、 、 Sample code problem.

Slide 27

Slide 27 text

>

Slide 28

Slide 28 text

> 、 、

Slide 29

Slide 29 text

> 、 、 Do not test driver on your host.

Slide 30

Slide 30 text

>

Slide 31

Slide 31 text

> • 合作碼農需要統一開發環境。

Slide 32

Slide 32 text

> • [編譯不同平台]從 build setting 換到 project->driver setting

Slide 33

Slide 33 text

> • [編譯不同平台]從 build setting 換到 project->driver setting

Slide 34

Slide 34 text

> • driver.inf 安裝完成後的 driver 註冊表項

Slide 35

Slide 35 text

> • 亦可使用WinObj檢查安裝成果。

Slide 36

Slide 36 text

> • 亦可使用WinObj檢查安裝成果。

Slide 37

Slide 37 text

( )

Slide 38

Slide 38 text

> • IRP (I/O Request Package) 是在 Windows 內核中的一種資料結構, 它是儲存與輸入輸出相關重要資料的資料結構。 • IRP 是一個很複雜的資料結構,首先需要了解 IRP 有兩個基本屬性, MajorFunction 和 MinorFunction,其分別記錄 IRP 的主類型和子 類型。 • 同樣的 MajorFunction 可能因為 MinorFunction 及參數的不同組合 出複雜多變的行為

Slide 39

Slide 39 text

> • 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 • …….

Slide 40

Slide 40 text

> • IRP結構

Slide 41

Slide 41 text

>

Slide 42

Slide 42 text

>

Slide 43

Slide 43 text

>

Slide 44

Slide 44 text

>

Slide 45

Slide 45 text

>

Slide 46

Slide 46 text

> • User module

Slide 47

Slide 47 text

>

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

> • 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

Slide 50

Slide 50 text

> C WDK unsigned long ULONG unsigned char UCHAR unsigned int UINT void VOID unsigned long * PULONG unsigned char * PUCHAR unsigned int* PUINT void * PVOID

Slide 51

Slide 51 text

> 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

Slide 52

Slide 52 text

> • 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

Slide 53

Slide 53 text

> • %windir%\system32\drivers • %windir%\sysWOW64\drivers

Slide 54

Slide 54 text

> • 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; }

Slide 55

Slide 55 text

>

Slide 56

Slide 56 text

> • Just change Sample, attach to device we could obtain. • RtlStringCchPrintfW(name, 32, L“\\Device\\Serial%d”, id); ;

Slide 57

Slide 57 text

> • 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;

Slide 58

Slide 58 text

> • PatchGuard x64 一言不合就藍屏 Process/3rdAPI (wxWidgets.lib) Windows API System Service Dispatcher (KiSystemService) NtCreateFile SSDT User Mode Kernel Mode ntdll.dll NtClose

Slide 59

Slide 59 text

> System Service Dispatcher (KiSystemService) NtCreateFile SSDT User Mode Kernel Mode ntdll.dll NewCreateFile 你的指標不是你的指標,請用 MmGetSystemRoutineAddress()

Slide 60

Slide 60 text

> • 根據函數名稱找到SSDT位址 • 關閉處理器寫入保護(0fffefffh取代CR0保護) • cli 關閉中斷(interrupt) • 改寫函數指標(pointer) • 復原處理器寫入保護 • sti 開啟中斷 (I implement this in ASM.)

Slide 61

Slide 61 text

> • 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

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

>

Slide 64

Slide 64 text

>

Slide 65

Slide 65 text

>

Slide 66

Slide 66 text

>

Slide 67

Slide 67 text

>

Slide 68

Slide 68 text

>

Slide 69

Slide 69 text

>

Slide 70

Slide 70 text

>

Slide 71

Slide 71 text

> • Copy .pdb and add srv*c:\MyServerSymbols*https://msdl.microsoft.co m/download/symbols to WinDBG symbol path.

Slide 72

Slide 72 text

>

Slide 73

Slide 73 text

>

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

> • Hold the shift while click reboot button. SHIFT

Slide 76

Slide 76 text

>

Slide 77

Slide 77 text

> • cmd.exe (system administrator ) > bcdedit /set TESTSIGNING ON

Slide 78

Slide 78 text

> • 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.)

Slide 79

Slide 79 text

> • 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

Slide 80

Slide 80 text

>

Slide 81

Slide 81 text

>

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

>

Slide 84

Slide 84 text

> 難道為了安全,框架可以說改就改嗎? WDK

Slide 85

Slide 85 text

> 抱歉,我微軟還真的想改就改。

Slide 86

Slide 86 text

> Windows,回頭再上兩個Patch讓他編譯失敗。

Slide 87

Slide 87 text

> 我就是混不到飯吃,餓死,死外邊,淪落當SOD (Stack Overflow Developer),也不再寫你家的破框架。

Slide 88

Slide 88 text

> 真香。 Windows Driver Jobs

Slide 89

Slide 89 text

No content