一:背景
1. 講故事
前幾天訓(xùn)練營(yíng)里的一位朋友在復(fù)習(xí)課件的時(shí)候,程序一跑就報(bào)錯(cuò),截圖如下:

從給出的錯(cuò)誤信息看大概是因?yàn)閖son格式無(wú)效導(dǎo)致的,在早期的訓(xùn)練營(yíng)里曾經(jīng)也有一例這樣的報(bào)錯(cuò),最后定位下來(lái)是公司的電腦安全軟件導(dǎo)致的,一旦有非托管調(diào)試器,安全軟件就會(huì)加密 runtimeconfig.json,最后導(dǎo)致程序無(wú)法正常被調(diào)試執(zhí)行。
此時(shí)相信有很多人想搞清楚,windbg 在那個(gè)時(shí)刻到底讀到了什么臟東西?到底經(jīng)歷了怎樣的驚魂時(shí)刻?
二:WinDbg 到底遇到了什么
1. 一個(gè)小案例
為了方便演示,寫一個(gè)簡(jiǎn)單的 hello world
程序,代碼如下:
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello world!");
Debugger.Break();
Console.ReadLine();
}
}
接下來(lái)將程序編譯之后觀察 runtimeconfig.json
內(nèi)容,截圖如下。

2. 如何用 windbg 觀察文件內(nèi)容
熟悉 win32 api 的朋友都知道,C# 的 ReadFile 底層會(huì)調(diào)用 win32 的 ReadFile 方法,方法簽名如下:
BOOL ReadFile(
[in] HANDLE hFile,
[out] LPVOID lpBuffer,
[in] DWORD nNumberOfBytesToRead,
[out, optional] LPDWORD lpNumberOfBytesRead,
[in, out, optional] LPOVERLAPPED lpOverlapped
);
方法的 lpBuffer 參數(shù)存放的就是讀取到的文件內(nèi)容。
這里還有一個(gè)問題就是 ReadFile 是在 dotnet 進(jìn)程被初始化的時(shí)候讀取到內(nèi)存的,在這個(gè)初始化過(guò)程中會(huì)有一系列的加載,那到底在哪個(gè)時(shí)刻埋點(diǎn)呢?這時(shí)候可以借助 procmon 工具,觀察 runtimeconfig.json
的加載時(shí)機(jī),截圖如下:

上面的卦中 runtimeconfig.json
的讀取是發(fā)生在 hostfxr.dll
加載之后,有了這些信息,思路就有了。
- sxe ld hostfxr 獲取插入點(diǎn)。
- bp KERNELBASE!ReadFile 觀察第二個(gè)參數(shù)。
0:000> sxe ld hostfxr
0:000> g
ModLoad: 00007ffc`29b60000 00007ffc`29b8f000 C:\windows\System32\IMM32.DLL
ModLoad: 00007ffc`03660000 00007ffc`036ba000 C:\Program Files\dotnet\host\fxr\9.0.2\hostfxr.dll
ntdll!NtMapViewOfSection+0x14:
00007ffc`2b1ad9f4 c3 ret
0:000> bp KERNELBASE!ReadFile
0:000> g
Breakpoint 1 hit
KERNELBASE!ReadFile:
00007ffc`28822670 48895c2410 mov qword ptr [rsp+10h],rbx ss:0000006c`c8f7de88=00000259c2eb0000
0:000> r
rax=0000000000000000 rbx=0000000000000000 rcx=000000000000012c
rdx=00000259c2ec8ee0 rsi=0000000000000003 rdi=0000000000000000
rip=00007ffc28822670 rsp=0000006cc8f7de78 rbp=0000000000001000
r8=0000000000001000 r9=0000006cc8f7df38 r10=00000259c2ec8ee0
r11=0000006cc8f7db38 r12=000000000000001b r13=0000000000001000
r14=0000000000000003 r15=00000259c2ec8ee0
iopl=0 nv up ei pl zr na po nc
cs=0033 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00000246
KERNELBASE!ReadFile:
00007ffc`28822670 48895c2410 mov qword ptr [rsp+10h],rbx ss:0000006c`c8f7de88=00000259c2eb0000
0:000> pt
KERNELBASE!ReadFile+0xad:
00007ffc`2882271d c3 ret
0:000> dp 0000006cc8f7df38 L1
0000006c`c8f7df38 00000000`0000010c
0:000> db 00000259c2ec8ee0 L?10c
00000259`c2ec8ee0 7b 0d 0a 20 20 22 72 75-6e 74 69 6d 65 4f 70 74 {.. "runtimeOpt
00000259`c2ec8ef0 69 6f 6e 73 22 3a 20 7b-0d 0a 20 20 20 20 22 74 ions": {.. "t
00000259`c2ec8f00 66 6d 22 3a 20 22 6e 65-74 38 2e 30 22 2c 0d 0a fm": "net8.0",..
00000259`c2ec8f10 20 20 20 20 22 66 72 61-6d 65 77 6f 72 6b 22 3a "framework":
00000259`c2ec8f20 20 7b 0d 0a 20 20 20 20-20 20 22 6e 61 6d 65 22 {.. "name"
00000259`c2ec8f30 3a 20 22 4d 69 63 72 6f-73 6f 66 74 2e 4e 45 54 : "Microsoft.NET
00000259`c2ec8f40 43 6f 72 65 2e 41 70 70-22 2c 0d 0a 20 20 20 20 Core.App",..
00000259`c2ec8f50 20 20 22 76 65 72 73 69-6f 6e 22 3a 20 22 38 2e "version": "8.
00000259`c2ec8f60 30 2e 30 22 0d 0a 20 20-20 20 7d 2c 0d 0a 20 20 0.0".. },..
00000259`c2ec8f70 20 20 22 63 6f 6e 66 69-67 50 72 6f 70 65 72 74 "configPropert
00000259`c2ec8f80 69 65 73 22 3a 20 7b 0d-0a 20 20 20 20 20 20 22 ies": {.. "
00000259`c2ec8f90 53 79 73 74 65 6d 2e 52-75 6e 74 69 6d 65 2e 53 System.Runtime.S
00000259`c2ec8fa0 65 72 69 61 6c 69 7a 61-74 69 6f 6e 2e 45 6e 61 erialization.Ena
00000259`c2ec8fb0 62 6c 65 55 6e 73 61 66-65 42 69 6e 61 72 79 46 bleUnsafeBinaryF
00000259`c2ec8fc0 6f 72 6d 61 74 74 65 72-53 65 72 69 61 6c 69 7a ormatterSerializ
00000259`c2ec8fd0 61 74 69 6f 6e 22 3a 20-66 61 6c 73 65 0d 0a 20 ation": false..
00000259`c2ec8fe0 20 20 20 7d 0d 0a 20 20-7d 0d 0a 7d }.. }..}
0:000> .writemem D:\testdump\1.txt 00000259c2ec8ee0 L?0x10c
Writing 10c bytes.
從上面的輸出中果然看到了 runtimeconfig.json
中的內(nèi)容,最后打開導(dǎo)出的文件,沒毛病。

3. 有沒有快捷的方式
剛才的操作確實(shí)能夠完成,但還是不爽,因?yàn)榻槿肓说谌焦ぞ?,所以能不能完全通過(guò) windbg 顯示打開的 文件路徑
和對(duì)應(yīng)的 文件內(nèi)容
呢?當(dāng)然是可以的,只需關(guān)注如下兩個(gè)方法 KERNELBASE!CreateFileW
和 KERNELBASE!ReadFile
即可,簽名如下:
HANDLE CreateFileW(
[in] LPCWSTR lpFileName,
[in] DWORD dwDesiredAccess,
[in] DWORD dwShareMode,
[in, optional] LPSECURITY_ATTRIBUTES lpSecurityAttributes,
[in] DWORD dwCreationDisposition,
[in] DWORD dwFlagsAndAttributes,
[in, optional] HANDLE hTemplateFile
);
BOOL ReadFile(
[in] HANDLE hFile,
[out] LPVOID lpBuffer,
[in] DWORD nNumberOfBytesToRead,
[out, optional] LPDWORD lpNumberOfBytesRead,
[in, out, optional] LPOVERLAPPED lpOverlapped
);
在 CreateFileW 中我們提取 lpFileName
和返回的 handle
句柄,在 ReadFile 中提取 hFile
句柄 和 lpBuffer
文件內(nèi)容,參考腳本如下:
sxe ld hostfxr;g
bp KERNELBASE!CreateFileW+0x6a " .echo WriteFile----------------------------------; du /c100 @rbx; r @rax; gc"
bp KERNELBASE!ReadFile+0x73 ".echo ReadFile----------------------------------; r @rsi; da poi(@rsp+0x28); gc " ;
稍微說(shuō)一下 KERNELBASE!CreateFileW+0x6a
是方法的ret處,可以通過(guò) uf KERNELBASE!CreateFileW
計(jì)算得到,如下輸出所示:
0:000> uf KERNELBASE!CreateFileW
KERNELBASE!CreateFileW:
00007ffc`33341410 4883ec58 sub rsp,58h
00007ffc`33341414 448b942488000000 mov r10d,dword ptr [rsp+88h]
...
00007ffc`3334147a c3 ret
0:000> ? 00007ffc`3334147a - 00007ffc`33341410
Evaluate expression: 106 = 00000000`0000006a
這里的 KERNELBASE!ReadFile+0x73
是內(nèi)部函數(shù) ntdll!NtReadFile
返回的RIP處。
最后用 windbg 執(zhí)行如下:
(3770.3164): Break instruction exception - code 80000003 (first chance)
ntdll!LdrpDoDebuggerBreak+0x30:
00007ffc`35a407a0 cc int 3
0:000> sxe ld hostfxr;g
ModLoad: 00007ffc`34250000 00007ffc`3427f000 C:\windows\System32\IMM32.DLL
ModLoad: 00007ffc`2deb0000 00007ffc`2df0a000 C:\Program Files\dotnet\host\fxr\9.0.2\hostfxr.dll
ntdll!NtMapViewOfSection+0x14:
00007ffc`35a0d9f4 c3 ret
0:000> bp KERNELBASE!ReadFile+0x73 ".echo ReadFile----------------------------------; r @rsi; da poi(@rsp+0x28); gc " ;
0:000> bp KERNELBASE!CreateFileW+0x6a " .echo WriteFile----------------------------------; du /c100 @rbx; r @rax; gc"
0:000> g
WriteFile----------------------------------
0000020d`1c77ace0 "D:\skyfly\20.20250116\src\Example\Example_20_1_1\bin\Debug\net8.0\Example_20_1_1.runtimeconfig.json"
rax=0000000000000128
ReadFile----------------------------------
rsi=0000000000000128
0000020d`1c778ee0 "{.. "runtimeOptions": {.. "t"
0000020d`1c778f00 "fm": "net8.0",.. "framework":"
0000020d`1c778f20 " {.. "name": "Microsoft.NET"
0000020d`1c778f40 "Core.App",.. "version": "8."
0000020d`1c778f60 "0.0".. },.. "configPropert"
0000020d`1c778f80 "ies": {.. "System.Runtime.S"
0000020d`1c778fa0 "erialization.EnableUnsafeBinaryF"
0000020d`1c778fc0 "ormatterSerialization": false.. "
0000020d`1c778fe0 " }.. }..}"
ReadFile----------------------------------
...
三:總結(jié)
有了windbg之后,很多東西都會(huì)豁然開朗,而不再像以前那樣人云亦云,高級(jí)調(diào)試這門救火技術(shù)
應(yīng)該是高級(jí)程序員必須的進(jìn)階之路。
?轉(zhuǎn)自https://www.cnblogs.com/huangxincheng/p/18875770
該文章在 2025/6/9 10:04:46 編輯過(guò)