bug引入

我们在驱动中引入一个错误 ,在0x0地址位置写入数据
在用户态这样做,进程会崩溃,但是在驱动里这样写,系统会挂掉
先把符号文件加载到windebug,用分号隔开,勾选reload

image.png

  1. #include <ntddk.h>
  2. VOID Unload(IN PDRIVER_OBJECT DriverObject) {
  3. DbgPrint("driver unload\r\n");
  4. }
  5. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegisteryPath) {
  6. PCHAR string;
  7. DriverObject->DriverUnload = Unload;
  8. //在 0x0的位置写入 'a'
  9. string = 0;
  10. *string = 'a';
  11. DbgPrint("hello driver\r\n");
  12. return STATUS_SUCCESS;
  13. }

加载驱动运行调试

加载运行驱动后,执行代码,调试器捕捉到,并中断
image.png

分析调试信息

使用 !analyze -v 命令自动分析,需要等一小会

  1. *******************************************************************************
  2. * *
  3. * Bugcheck Analysis *
  4. * *
  5. *******************************************************************************
  6. SYSTEM_THREAD_EXCEPTION_NOT_HANDLED (7e)
  7. This is a very common bugcheck. Usually the exception address pinpoints
  8. the driver/function that caused the problem. Always note this address
  9. as well as the link date of the driver/image that contains this address.
  10. Arguments:
  11. Arg1: c0000005, The exception code that was not handled
  12. Arg2: 94407018, The address that the exception occurred at
  13. Arg3: 807ed8fc, Exception Record Address
  14. Arg4: 807ed360, Context Record Address
  15. Debugging Details:
  16. ------------------
  17. DBGHELP: Timeout to store: e:\symbol*http://msdl.microsoft.com/download/symbols
  18. KEY_VALUES_STRING: 1
  19. Key : AV.Dereference
  20. Value: NullPtr
  21. Key : AV.Fault
  22. Value: Write
  23. Key : Analysis.CPU.mSec
  24. Value: 14609
  25. Key : Analysis.DebugAnalysisManager
  26. Value: Create
  27. Key : Analysis.Elapsed.mSec
  28. Value: 181920
  29. Key : Analysis.Init.CPU.mSec
  30. Value: 8046
  31. Key : Analysis.Init.Elapsed.mSec
  32. Value: 840119
  33. Key : Analysis.Memory.CommitPeak.Mb
  34. Value: 68
  35. Key : WER.OS.Branch
  36. Value: win7sp1_ldr_escrow
  37. Key : WER.OS.Timestamp
  38. Value: 2019-02-20T18:00:00Z
  39. Key : WER.OS.Version
  40. Value: 7.1.7601.24384
  41. BUGCHECK_CODE: 7e
  42. BUGCHECK_P1: ffffffffc0000005
  43. BUGCHECK_P2: ffffffff94407018
  44. BUGCHECK_P3: ffffffff807ed8fc
  45. BUGCHECK_P4: ffffffff807ed360
  46. EXCEPTION_RECORD: 807ed8fc -- (.exr 0xffffffff807ed8fc)
  47. ExceptionAddress: 94407018 (helloDriver!DriverEntry+0x00000018)
  48. ExceptionCode: c0000005 (Access violation)
  49. ExceptionFlags: 00000000
  50. NumberParameters: 2
  51. Parameter[0]: 00000001
  52. Parameter[1]: 00000000
  53. Attempt to write to address 00000000
  54. CONTEXT: 807ed360 -- (.cxr 0xffffffff807ed360)
  55. eax=85b0d0f8 ebx=00000000 ecx=00000000 edx=00002351 esi=85b0d0f8 edi=86cd7000
  56. eip=94407018 esp=807ed9c4 ebp=807ed9c8 iopl=0 nv up ei pl nz na pe nc
  57. cs=0008 ss=0010 ds=0023 es=0023 fs=0030 gs=0000 efl=00010206
  58. helloDriver!DriverEntry+0x18:
  59. 94407018 c60161 mov byte ptr [ecx],61h ds:0023:00000000=??
  60. Resetting default scope
  61. PROCESS_NAME: System
  62. WRITE_ADDRESS: 00000000
  63. ERROR_CODE: (NTSTATUS) 0xc0000005 - 0x%p 0x%p %s
  64. EXCEPTION_CODE_STR: c0000005
  65. EXCEPTION_PARAMETER1: 00000001
  66. EXCEPTION_PARAMETER2: 00000000
  67. EXCEPTION_STR: 0xc0000005
  68. STACK_TEXT:
  69. 807ed9c8 840227b2 85b0d0f8 86cd7000 00000000 helloDriver!DriverEntry+0x18 [E:\codemix\windowsDevelopment\helloDriver\helloDriver\Source.c @ 18]
  70. 807edbac 840262c1 00000001 00000000 807edbd4 nt!IopLoadDriver+0x7ed
  71. 807edbf4 83ebdb4b 8d427bd0 855c38c8 855d84c0 nt!IopLoadUnloadDriver+0x70
  72. 807edc44 84068b38 00000001 9cdb13d2 00000000 nt!ExpWorkerThread+0x10d
  73. 807edc90 83f06301 83ebda3e 00000001 00000000 nt!PspSystemThreadStartup+0x159
  74. 00000000 00000000 00000000 00000000 00000000 nt!KiThreadStartup+0x19
  75. CHKIMG_EXTENSION: !chkimg -lo 50 -d !nt
  76. 83eb76a7 - nt!SwapContext_XRstorBegin+2
  77. [ 89:a9 ]
  78. 83eb794d - nt!EnlightenedSwapContext_XRstorBegin+2 (+0x2a6)
  79. [ 89:a9 ]
  80. 2 errors : !nt (83eb76a7-83eb794d)
  81. MODULE_NAME: memory_corruption
  82. IMAGE_NAME: memory_corruption
  83. MEMORY_CORRUPTOR: ONE_BIT_LARGE
  84. STACK_COMMAND: .cxr 0xffffffff807ed360 ; kb
  85. FAILURE_BUCKET_ID: MEMORY_CORRUPTION_ONE_BIT_LARGE
  86. OS_VERSION: 7.1.7601.24384
  87. BUILDLAB_STR: win7sp1_ldr_escrow
  88. OSPLATFORM_TYPE: x86
  89. OSNAME: Windows 7
  90. FAILURE_ID_HASH: {31545515-196b-fab5-2300-9ce714226f43}
  91. Followup: memory_corruption
  92. ---------
  93. ************* Path validation summary **************
  94. Response Time (ms) Location
  95. OK E:\codemix\windowsDevelopment\helloDriver\helloDriver
  96. ************* Path validation summary **************
  97. Response Time (ms) Location
  98. OK E:\codemix\windowsDevelopment\helloDriver\helloDriver
  99. kd> g
  100. Shutdown occurred at (Wed Jul 13 21:01:42.441 2022 (UTC + 8:00))...unloading all symbol tables.
  101. ************* Path validation summary **************
  102. Response Time (ms) Location
  103. Deferred SRV*E:\symbol*http://msdl.microsoft.com/download/symbols
  104. Deferred srv*c:\symbols* http://msdl.microsoft.com/download/symbols
  105. OK E:\codemix\windowsDevelopment\helloDriver\Debug
  106. Deferred SRV*E:\symboll* http://msdl.microsoft.com/download/symbols
  107. Waiting to reconnect...
  108. BD: Boot Debugger Initialized
  109. Connected to Windows Boot Debugger 7601 x86 compatible target at (Wed Jul 13 21:01:54.082 2022 (UTC + 8:00)), ptr64 FALSE
  110. Kernel Debugger connection established.
  111. ************* Path validation summary **************
  112. Response Time (ms) Location
  113. Deferred SRV*E:\symbol*http://msdl.microsoft.com/download/symbols
  114. Deferred srv*c:\symbols* http://msdl.microsoft.com/download/symbols
  115. OK E:\codemix\windowsDevelopment\helloDriver\Debug
  116. Deferred SRV*E:\symboll* http://msdl.microsoft.com/download/symbols
  117. Symbol search path is: SRV*E:\symbol*http://msdl.microsoft.com/download/symbols;srv*c:\symbols* http://msdl.microsoft.com/download/symbols;E:\codemix\windowsDevelopment\helloDriver\Debug;SRV*E:\symboll* http://msdl.microsoft.com/download/symbols
  118. Executable search path is:
  119. ReadVirtual() failed in GetXStateConfiguration() first read attempt (error == 0.)
  120. Windows Boot Debugger Kernel Version 7601 UP Free x86 compatible
  121. Machine Name:
  122. Primary image base = 0x00539000 Loaded module list = 0x005dbd60
  123. System Uptime: not available
  124. ************* Path validation summary **************
  125. Response Time (ms) Location
  126. Deferred SRV*E:\symbol*http://msdl.microsoft.com/download/symbols
  127. Deferred srv*c:\symbols* http://msdl.microsoft.com/download/symbols
  128. OK E:\codemix\windowsDevelopment\helloDriver\Debug
  129. Deferred SRV*E:\symboll* http://msdl.microsoft.com/download/symbols
  130. ************* Path validation summary **************
  131. Response Time (ms) Location
  132. OK E:\codemix\windowsDevelopment\helloDriver\helloDriver
  133. winload!DbgLoadImageSymbols+0x44:
  134. 0056f93d cc int 3
  135. kd> g
  136. Shutdown occurred at (Wed Jul 13 21:02:18.377 2022 (UTC + 8:00))...unloading all symbol tables.
  137. ************* Path validation summary **************
  138. Response Time (ms) Location
  139. Deferred SRV*E:\symbol*http://msdl.microsoft.com/download/symbols
  140. Deferred srv*c:\symbols* http://msdl.microsoft.com/download/symbols
  141. OK E:\codemix\windowsDevelopment\helloDriver\Debug
  142. Deferred SRV*E:\symboll* http://msdl.microsoft.com/download/symbols
  143. Waiting to reconnect...
  144. Connected to Windows 7 7601 x86 compatible target at (Wed Jul 13 21:02:19.339 2022 (UTC + 8:00)), ptr64 FALSE
  145. Kernel Debugger connection established.
  146. ************* Path validation summary **************
  147. Response Time (ms) Location
  148. Deferred SRV*E:\symbol*http://msdl.microsoft.com/download/symbols
  149. Deferred srv*c:\symbols* http://msdl.microsoft.com/download/symbols
  150. OK E:\codemix\windowsDevelopment\helloDriver\Debug
  151. Deferred SRV*E:\symboll* http://msdl.microsoft.com/download/symbols
  152. Symbol search path is: SRV*E:\symbol*http://msdl.microsoft.com/download/symbols;srv*c:\symbols* http://msdl.microsoft.com/download/symbols;E:\codemix\windowsDevelopment\helloDriver\Debug;SRV*E:\symboll* http://msdl.microsoft.com/download/symbols
  153. Executable search path is:
  154. Windows 7 Kernel Version 7601 MP (1 procs) Free x86 compatible
  155. Edition build lab: 7601.24384.x86fre.win7sp1_ldr_escrow.190220-1800
  156. Machine Name:
  157. Kernel base = 0x83e4c000 PsLoadedModuleList = 0x83fa1730
  158. System Uptime: not available
  159. ************* Path validation summary **************
  160. Response Time (ms) Location
  161. Deferred SRV*E:\symbol*http://msdl.microsoft.com/download/symbols
  162. Deferred srv*c:\symbols* http://msdl.microsoft.com/download/symbols
  163. OK E:\codemix\windowsDevelopment\helloDriver\Debug
  164. Deferred SRV*E:\symboll* http://msdl.microsoft.com/download/symbols
  165. ************* Path validation summary **************
  166. Response Time (ms) Location
  167. OK E:\codemix\windowsDevelopment\helloDriver\helloDriver
  168. nt!DbgLoadImageSymbols+0x47:
  169. 83e64d0e cc int 3

报错参数

image.png
arg1 : 错误代码 c000005
arg2:94407018 发生错误的内存地址

image.png

报错位置

发生错误的代码在 helloDriver!DriverEntry+0x00000018 的位置
错误码:c0000005 (Access violation)
原因,尝试在0x0 地址写入数据 :Attempt to write to address 00000000

寄存器状态

寄存器状态:
image.png

发生错误代码
image.png

  1. 94407018 c60161 mov byte ptr [ecx],61h ds:0023:00000000=??

调用栈

image.png
在E:\codemix\windowsDevelopment\helloDriver\helloDriver\Source.c @ 18 第18行发生错误