WinDBG Kernel-mode Debugging (KD)

The WinDBG KD interface support for r2 allows you to attach to VM running Windows and debug its kernel over a serial port or network.

It is also possible to use the remote GDB interface to connect and debug Windows kernels without depending on Windows capabilities.

Bear in mind that WinDBG KD support is still work-in-progress, and this is just an initial implementation which will get better in time.

Setting Up KD on Windows

For a complete walkthrough, refer to Microsoft’s documentation.

Serial Port

Enable KD over a serial port on Windows Vista and higher like this:

  1. bcdedit /debug on
  2. bcdedit /dbgsettings serial debugport:1 baudrate:115200

Or like this for Windows XP: Open boot.ini and add /debug /debugport=COM1 /baudrate=115200:

  1. [boot loader]
  2. timeout=30
  3. default=multi(0)disk(0)rdisk(0)partition(1)\WINDOWS
  4. [operating systems]
  5. multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Debugging with Cable" /fastdetect /debug /debugport=COM1 /baudrate=57600

In case of VMWare

  1. Virtual Machine Settings -> Add -> Serial Port
  2. Device Status:
  3. [v] Connect at power on
  4. Connection:
  5. [v] Use socket (named pipe)
  6. [_/tmp/winkd.pipe________]
  7. From: Server To: Virtual Machine

Configure the VirtualBox Machine like this:

  1. Preferences -> Serial Ports -> Port 1
  2. [v] Enable Serial Port
  3. Port Number: [_COM1_______[v]]
  4. Port Mode: [_Host_Pipe__[v]]
  5. [v] Create Pipe
  6. Port/File Path: [_/tmp/winkd.pipe____]

Or just spawn the VM with qemu like this:

  1. $ qemu-system-x86_64 -chardev socket,id=serial0,\
  2. path=/tmp/winkd.pipe,nowait,server \
  3. -serial chardev:serial0 -hda Windows7-VM.vdi

Network

Enable KD over network (KDNet) on Windows 7 or later likes this:

  1. bcdedit /debug on
  2. bcdedit /dbgsettings net hostip:w.x.y.z port:n

Starting from Windows 8 there is no way to enforce debugging for every boot, but it is possible to always show the advanced boot options, which allows to enable kernel debugging:

  1. bcedit /set {globalsettings} advancedoptions true

Connecting to KD interface on r2

Serial Port

Radare2 will use the winkd io plugin to connect to a socket file created by virtualbox or qemu. Also, the winkd debugger plugin and we should specify the x86-32 too. (32 and 64 bit debugging is supported)

  1. $ r2 -a x86 -b 32 -D winkd winkd:///tmp/winkd.pipe

On Windows you should run the following line:

  1. $ radare2 -D winkd winkd://\\.\pipe\com_1

Network

  1. $ r2 -a x86 -b 32 -d winkd://<hostip>:<port>:w.x.y.z

Using KD

When connecting to a KD interface, r2 will send a breakin packet to interrupt the target and we will get stuck here:

  1. [0x828997b8]> pd 20
  2. ;-- eip:
  3. 0x828997b8 cc int3
  4. 0x828997b9 c20400 ret 4
  5. 0x828997bc cc int3
  6. 0x828997bd 90 nop
  7. 0x828997be c3 ret
  8. 0x828997bf 90 nop

In order to skip that trap we will need to change eip and run ‘dc’ twice:

  1. dr eip=eip+1
  2. dc
  3. dr eip=eip+1
  4. dc

Now the Windows VM will be interactive again. We will need to kill r2 and attach again to get back to control the kernel.

In addition, the dp command can be used to list all processes, and dpa or dp= to attach to the process. This will display the base address of the process in the physical memory layout.

WinDBG Backend for Windows (DbgEng)

On Windows, radare2 can use DbgEng.dll as a debugging backend, allowing it to make use of WinDBG’s capabilities, supporting dump files, local and remote user and kernel mode debugging.

You can use the debugging DLLs included on Windows or get the latest version from Microsoft’s download page (recommended).

You cannot use DLLs from the Microsoft Store’s WinDbg Preview app folder directly as they are not marked as executable for normal users.

radare2 will try to load dbgeng.dll from the _NT_DEBUGGER_EXTENSION_PATH environment variable before using Windows’ default library search path.

Using the plugin

To use the windbg plugin, pass the same command-line options as you would for WinDBG or kd (see Microsoft’s documentation), quoting/escaping when necessary:

  1. > r2 -d "windbg://-remote tcp:server=Server,port=Socket"
  1. > r2 -d "windbg://MyProgram.exe \"my arg\""
  1. > r2 -d "windbg://-k net:port=<n>,key=<MyKey>"
  1. > r2 -d "windbg://-z MyDumpFile.dmp"

You can then debug normally (see d? command) or interact with the backend shell directly with the =! command:

  1. [0x7ffcac9fcea0]> dcu 0x0007ffc98f42190
  2. Continue until 0x7ffc98f42190 using 1 bpsize
  3. ModLoad: 00007ffc`ab6b0000 00007ffc`ab6e0000 C:\WINDOWS\System32\IMM32.DLL
  4. Breakpoint 1 hit
  5. hit breakpoint at: 0x7ffc98f42190
  6. [0x7fffcf232190]> =!k4
  7. Child-SP RetAddr Call Site
  8. 00000033`73b1f618 00007ff6`c67a861d r_main!r_main_radare2
  9. 00000033`73b1f620 00007ff6`c67d0019 radare2!main+0x8d
  10. 00000033`73b1f720 00007ff6`c67cfebe radare2!invoke_main+0x39
  11. 00000033`73b1f770 00007ff6`c67cfd7e radare2!__scrt_common_main_seh+0x12e