如何使用windbg在驅動加載時下斷

首先說說應用層的調試吧.當我們在調試windows可執行程序的時候,通過將PE文件頭中的ImageBase和AddressOfEntryPoint相加,從而得出第一條指令的地址.針對這個地址下斷之後目標程序就中斷在了了入口處.但是這個方法在驅動調試的時候卻有心無力.這是因爲可執行程序都是首先被加載到各自的私有地址空間,他們不會有地址衝突.然而驅動程序運行在內核裏面,所有的驅動程序共享一個地址空間.所以需要重新設定基地址.

1.利用bu命令下延遲斷點.

之前提到過,bu可以針對符號下斷點.這裏是用bu下延遲斷點的意義在於即使目標驅動沒有被加載,windbg也允許我們針對符號設置斷點.當新加載驅動程序後,windbg就會檢查驅動程序中是否包含了設置了延遲斷點的函數.如果找到了,就把斷點替換爲地址形式,然後再設置斷點.筆者的測試驅動程序爲TestDriver.sys.

  1. 0: kd> .sympath  
  2. Symbol search path is: E:\Symbol\windbg Symbols;E:\Code\Vc_code\Ring3Ring0\Driver\objchk_wxp_x86\i386  
  3. Expanded Symbol search path is: e:\symbol\windbg symbols;e:\code\vc_code\ring3ring0\driver\objchk_wxp_x86\i386  
  4. 0: kd> .srcpath  
  5. Source search path is: E:\Code\Vc_code\Ring3Ring0\Driver  

在VMware客戶機裏,安裝驅動後,敲下 net start TestDevice之後,系統理所應當的被斷下來了.

  1. Breakpoint 0 hit  
  2. TestDriver!DriverEntry:  
  3. f8c4ee10 8bff            mov     edi,edi  



假如我們調試的驅動並不是以DriverEntry作爲入口函數,bu針對符號下斷也就沒有了意義.但是我們可以使用模塊加偏移的方式下斷.假設TestDriver的入口函數的偏移爲0xFB4.直接bu TestDriver+0xFB4即可.

2.在系統調用DriverEntry之前下斷.

現在來看看我們的程序,中斷在了DriverEntry之中.我們能不能在進入DriverEntry之前下斷.首先我們dv和esp看一下當前的棧

  1. 1: kd> dv  
  2.   pDriverObject = 0x81f346e8  
  3.   pRegistryPath = 0x81865000 "\REGISTRY\MACHINE\SYSTEM\ControlSet001\Services\TestDevice"  
  4.          status = 0n8  
  5. 1: kd> r esp  
  6. esp=f8af9c88  
  7. 1: kd> dd f8af9c88 L8  
  8. f8af9c88  80582377 81f346e8 81865000 00000000  
  9. f8af9c98  b2926cf4 00000000 00000018 00000000  

dv命令只顯示了參數,還是用esp靠譜一點,至少給出了我們返回地址0x80582377.接下來該ln命令登場了

  1. 1: kd> ln 80582377  
  2. (80581d0a)   nt!IopLoadDriver+0x66d   |  (80582442)   nt!IopLoadUnloadDriver  

返回地址位於nt!IopLoadDriver+0x66d處,直接反彙編一下吧,看看再哪裏調用了DriverEntry

  1. 0: kd> bu TestDriver!DriverEntry  
  2. 0: kd> bl  
  3.  0 eu             0001 (0001) (TestDriver!DriverEntry)  
  4. <pre name="code" class="plain">1: kd> u nt!IopLoadDriver+0x660  
  5. nt!IopLoadDriver+0x660:  
  6. 8058236a 8b7d80          mov     edi,dword ptr [ebp-80h]  
  7. 8058236d ffb570ffffff    push    dword ptr [ebp-90h]  
  8. 80582373 57              push    edi  
  9. 80582374 ff572c          call    dword ptr [edi+2Ch]  
  10. 80582377 3bc3            cmp     eax,ebx  
  11. 80582379 8b8d68ffffff    mov     ecx,dword ptr [ebp-98h]  
  12. 8058237f 8945ac          mov     dword ptr [ebp-54h],eax  
  13. 80582382 8901            mov     dword ptr [ecx],eax</pre><br>  
  14. <pre></pre>  
  15. <pre></pre>  
  16. <pre></pre>  
  17. <pre></pre>  
0x80582377處是nt!IopLoadDriver+0x66d.前面的call指令佔了3個字節.所以我們下斷在nt!IopLoadDriver+0x66a就能在進入DriverEntry之前中斷下來.

3.使用事件異常

先來看看系統中提供了哪些事件異常吧.直接sx下

  1. 1: kd> sx  
  2.   ct - Create thread - ignore  
  3.   et - Exit thread - ignore  
  4.  cpr - Create process - ignore  
  5.  epr - Exit process - ignore  
  6.  <strong>ld - Load module - output</strong>  
  7.   ud - Unload module - ignore  
  8.  ser - System error - ignore  
  9.  ibp - Initial breakpoint - break  
  10.  iml - Initial module load - ignore  
  11.  out - Debuggee output - output  

這裏我們要設置一下Load module事件爲break, sxe -set enable        sxd - set disable               sxi -set ignore           sxn -set output

  1. 1: kd> sxe ld  
  2. 1: kd> sx  
  3.   ct - Create thread - ignore  
  4.   et - Exit thread - ignore  
  5.  cpr - Create process - ignore  
  6.  epr - Exit process - ignore  
  7.   ld - Load module - break  
  8.   ud - Unload module - ignore  
  9.  ser - System error - ignore  
  10.  ibp - Initial breakpoint - break  
  11.  iml - Initial module load - ignore  
  12.  out - Debuggee output - output  

加載我們的驅動吧,在load module的時候系統中斷

然後我們找到模塊基址,並在入口處下斷即可.

  1. 1: kd> lm n  
  2. start    end        module name  
  3. [...]  
  4. f8b9c000 f8b9d100   WMILIB   WMILIB.SYS    
  5. f8b9e000 f8b9f580   intelide intelide.sys  
  6. f8ba0000 f8ba1700   dmload   dmload.sys    
  7. f8ba4000 f8ba5280   vmmouse  vmmouse.sys   
  8. f8bb0000 f8bb1100   swenum   swenum.sys    
  9. f8bb6000 f8bb7280   USBD     USBD.SYS      
  10. f8bba000 f8bbbf00   Fs_Rec   Fs_Rec.SYS    
  11. f8bbe000 f8bbf080   Beep     Beep.SYS      
  12. f8bc2000 f8bc3080   mnmdd    mnmdd.SYS     
  13. f8bc6000 f8bc7080   RDPCDD   RDPCDD.sys    
  14. f8bf8000 f8bf9a80   ParVdm   ParVdm.SYS    
  15. f8bfc000 f8bfde00   vmmemctl vmmemctl.sys  
  16. <strong>f8c4e000 f8c4f300   TestDriver TestDriver.sys</strong>  
  17. [...]  

解析一下pe文件

  1. 1: kd> !dh -a f8c4e000  
  2.   
  3. File Type: EXECUTABLE IMAGE  
  4. FILE HEADER VALUES  
  5.      14C machine (i386)  
  6.        6 number of sections  
  7. 5077C38E time date stamp Fri Oct 12 15:15:26 2012  
  8.   
  9.        0 file pointer to symbol table  
  10.        0 number of symbols  
  11.       E0 size of optional header  
  12.      102 characteristics  
  13.             Executable  
  14.             32 bit word machine  
  15.   
  16. OPTIONAL HEADER VALUES  
  17.      10B magic #  
  18.     9.00 linker version  
  19.      C00 size of code  
  20.      280 size of initialized data  
  21.        0 size of uninitialized data  
  22. <strong>    FB4 address of entry point</strong>  
  23.      480 base of code  
  24.          ----- new -----  
  25. [...]  

然後bp TestDriver+0xFB4即可讓系統中斷在我們驅動程序的入口處.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章