天天看點

windows上調試win32子系統的方法

  最近做的一個東西需要調試win32子系統(例如Lsass.exe程序),但是發現要對win32子系統進行調試好像比較麻煩。

首先想到的是它是運作在user mode, 是以一般來講應該用user mode的調試工具來調試。可是,如果用debuuger直接

跟它相連,調試器退出時計算機就會自動重新開機,使得調試無法進行下去。其次,如果我們想在機器啟動的時候調試

lsass也不可能,因為user mode debugger那個時候還不能工作。

經過一番google及認真研究,發現有三種方法可以用來調試lsass,并且各有優缺點。下面是收集到的資料。

1. ntsd piped through KD

    a. 修改系統資料庫

        HKLM/Software/Microsoft/Windows NT/CurrenVersion/Image File Execution Options/lsass.exe

            debugger = REG_SZ c:/debuggers/ntsd.exe -d -g -G

    b. 用另一台機器通過kernal debugger和測試機相連

    c.重起測試機

    這樣的話,在啟動階段,當系統調起lsass的時候,測試機的ntsd就開始工作,并且将輸入,輸出傳送到kernal debugger上。這個屬于

在kernal debugger裡進行user mode的調試。

優點:可以在啟動的時候調試lsass。

缺點:symbols and source files 必須要copy在測試機上。(不太友善)

2.Debugging LSA via dbgsrv.exe

    a.Find the PID for LSA via tlist.exe

    b. C:/Program Files/Debugging Tools for Windows>dbgsrv.exe -t tcp:port=1234,password=spat

    c.Run this command to attach to LSA on the remote machine.

        I:/debugger>windbg.exe -premote tcp:server=192.168.1.102,port=1234,password=spat -p 596 -- where 596 = PID of LSASS

優點:symbols and source files 可以在調試機上

缺點:不能在啟動的時候進行調試

3.Debugging LSA from Kernel

    a. Get the process address for LSASS

        0: kd> !process 0 0 lsass.exe

            PROCESS 815196c0 SessionId: 0 Cid: 010c Peb: 7ffdf000 ParentCid: 00e4

            DirBase: 042d2000 ObjectTable: 81519aa8 TableSize: 859.

            Image: LSASS.EXE

    b. Switch to the process context:

        Either .process /p /r 815196c0

        Or .process –i 815196c0 ;g;.reload /user

優點:symbols and source files 可以在調試機上,可以進行log out/ log on 過程的調試

缺點:不能在啟動的時候進行調試

是以,如果想在啟動的時候調試,就必然要選方法1。

如果想在log on的時候調試,選擇方法3。其他情況,可以選擇方法2,或者方法3。

進過分析及一些實踐侯決定采用dbgsrv.exe的方式對lsass.exe進行調試,下面是我的一些調試總結:

1、在虛拟機和本機上都裝個windbg, 在本機安裝windows symbols檔案。在虛拟機安裝windbg的目的主要

在于使DbgSrv (dbgsrv.exe)能夠在遠端調試機器上運作起來。(DbgSrv (dbgsrv.exe). 用于遠端調試的程序伺服器。)

然後在虛拟機上運作:dbgsrv.exe -t tcp:port=1234,password=lisl

在調試機(本機)運作:

    windbg.exe -premote tcp:server=172.21.119.135,port=1234,password=lisl03 -p 596

    其中lisl03是調試密碼,172.21.119.135是虛拟機的ip,1234是遠端調試端口,596是虛拟機的lsass.exe進

程的pid。

    由于使用Debugging LSA via dbgsrv.exe的方式的優點在于:symbols and source files 可以在調試機上,

但是缺點在于不能在啟動的時候進行調試。是以為了在啟動的時候進行調試,有網友利用windows的任務計劃

讓dbgsrv開機就運作起來。

    這裡作為試驗,使用指令runas重制密碼驗證過程。

2、在桌面建立快捷方式:

    "C:/Program Files/Debugging Tools for Windows (x86)/windbg.exe" -premote tcp:server=172.21.119

.135,port=1234,password=lisl03 -p 716

    輕按兩下打開後連接配接遠端伺服器,然後執行:.logopen建立日志檔案,安裝symbols符号,執行:

    !peb

3、在核心中執行對lsass的調試時,需要注意以下幾點:

    a、連接配接時需要選擇reconnected

    b、執行程序的切換操作:!process 0 0; .process /r /p EPROCESS(lsass)或者執行 .process /i EPROCESS(lsass)

        g、.reload

4、

可以使用計劃任務的方法在登陸之前就運作dbgsrv,這樣連接配接上dbgsrv就可以調試了。否則因為登陸之後運作dbgsrv後,

登出的時候dbgsrv就被關了。

後記:進過實踐後發現windbug是一個很不錯的調試器,特别是在核心調試方面,在經曆了softice不時導緻的一些錯誤之後

覺得今後的調試應該轉向windbg。而且windbg微軟還在不停的維護中,其他常用的調試器在這方面好像沒有了優勢。另外,

windbg還有很多的強大的指令,如wt,使用qd可以在退出調試器時不結束被調試程序等。