如何使用windbg在驱动加载时下断

首先说说应用层的调试吧.当我们在调试windows可执行程序的时候,通过将PE文件头中的ImageBase和AddressOfEntryPoint相加,从而得出第一条指令的地址.针对这个地址下断之后目标程序就中断在了了入口处.但是这个方法在驱动调试的时候却有心无力.这是因为可执行程序都是首先被加载到各自的私有地址空间,他们不会有地址冲突.然而驱动程序运行在内核里面,所有的驱动程序共享一个地址空间.所以需要重新设定基地址.

1.利用bu命令下延迟断点.

之前提到过,bu可以针对符号下断点.这里是用bu下延迟断点的意义在于即使目标驱动没有被加载,windbg也允许我们针对符号设置断点.当新加载驱动程序后,windbg就会检查驱动程序中是否包含了设置了延迟断点的函数.如果找到了,就把断点替换为地址形式,然后再设置断点.笔者的测试驱动程序为TestDriver.sys.

[plain]  view plain copy print ?
  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之后,系统理所应当的被断下来了.

[plain]  view plain copy print ?
  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看一下当前的栈

[plain]  view plain copy print ?
  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命令登场了

[plain]  view plain copy print ?
  1. 1: kd> ln 80582377  
  2. (80581d0a)   nt!IopLoadDriver+0x66d   |  (80582442)   nt!IopLoadUnloadDriver  

返回地址位于nt!IopLoadDriver+0x66d处,直接反汇编一下吧,看看再哪里调用了DriverEntry

[plain]  view plain copy print ?
  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下

[plain]  view plain copy print ?
  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

[plain]  view plain copy print ?
  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的时候系统中断

然后我们找到模块基址,并在入口处下断即可.

[plain]  view plain copy print ?
  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文件

[plain]  view plain copy print ?
  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即可让系统中断在我们驱动程序的入口处.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值