# WinDbg Kernel

### Kernel exploration

#### CheatSheet

**Symbols and types**

| Command                                                                                                                   | Usage                                                                         | Examples                                                                                                        | Description                                                                |
| ------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- |
| [lm (List Loaded Modules)](https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/lm--list-loaded-modules-)   | <p><code>lm</code><br><br><code>lm \<PATTERN></code></p>                      | `lm nt*`                                                                                                        | Displays all or the specified loaded modules.                              |
| [x (Examine Symbols)](https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/x--examine-symbols-)             | <p><code>x \<MODULE>!\*</code><br><br><code>x \<MODULE>!\<PATTERN></code></p> | <p><code>x nt!\*</code><br><br><code>x nt!*process*</code></p>                                                  | Displays the symbols in the specified module.                              |
| [ln (List Nearest Symbols)](https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/ln--list-nearest-symbols-) | `ln <ADDRESS>`                                                                | `ln fffff80705d4c9d4`                                                                                           | Displays the symbol(s) at or near the specified address.                   |
| [dt (Display Type)](https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/dt--display-type-)                 | <p><code>dt \<STRUCT></code><br><br><code>dt nt!*\<PATTERN>*</code></p>       | `dt nt!_EPROCESS`                                                                                               | Displays information about a local variable, global variable or data type. |
| [.printf](https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/-printf)                                     | `.printf [<OPTIONS>] <FORMAT_STRING> <ARGUMENT \| ARGUMENT_LIST>`             | <p><code>.printf "%y", \<ADDRESS></code><br>displays the eventual symbol associated with the given address.</p> | C printf-like function.                                                    |

**Memory exploration**

| Command                                                                                                                                                   | Usage                                                                                                   | Examples                                                                                                                      | Description                                                                                                                                                                                                                                                                                                                                                    |
| --------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [dds, dps, dqs (Display Words and Symbols)](https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/dds--dps--dqs--display-words-and-symbols-) | `d*s <ADDRESS \| ADDRESS_RANGE>`                                                                        | <p><code>dqs fffff80705d1a410</code><br><br><code>dqs fffff80705d1a410 fffff80705d1a418</code></p>                            | <p>Displays the contents of memory in the given range.<br><br>The <code>dds</code> command displays <code>DWORD</code> (4 byte) values.<br><br>The <code>dqs</code> command displays <code>QWORD</code> (8 byte) values.<br><br>The <code>dps</code> command displays pointer-sized values (4 byte or 8 byte depending on the system architecture) values.</p> |
| [ds, dS (Display String)](https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/ds--ds--display-string-)                                     | <p><code>ds \<ADDRESS></code><br><br><code>dS \<ADDRESS></code></p>                                     |                                                                                                                               | Display a `STRING` / `ANSI_STRING` (`ds`) or `UNICODE_STRING` (`dS`) strings.                                                                                                                                                                                                                                                                                  |
| [u, ub, uu (Unassemble)](https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/u--unassemble-)                                               | `u <ADDRESS \| ADDRESS_RANGE>`                                                                          | <p><code>u 0xfffff8015be478d0</code><br><br><code>u nt!NtOpenProcessToken</code></p>                                          | Displays an assembly translation of the code at the specified memory address or range.                                                                                                                                                                                                                                                                         |
| [uf (Unassemble Function)](https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/uf--unassemble-function-)                                   | `u <ADDRESS>`                                                                                           | <p><code>uf fffff80705685060</code><br><br><code>uf nt!NtOpenProcessTokenEx</code></p>                                        | Displays an assembly translation of the function at the specified memory address.                                                                                                                                                                                                                                                                              |
| [!address](https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/-address)                                                                   | `!address <ADDRESS>`                                                                                    | <p><code>!address fffff80705d1a410</code><br><br><code>!address nt!NtOpenProcessTokenEx</code></p>                            | Displays information on the module to which the specified address belong (module name, path and base start / end ADDRESSs).                                                                                                                                                                                                                                    |
| <p><em>\[dx advised]</em><br><a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/-process">!process</a></p>                       | `!process [/s <SESSION>] [/m <MODULE>] <0 \| PROCESS_ADDRESS \| PROCESS_PID> <INFORMATION_LEVEL_FLAG>]` | <p><code>!process 0 0</code><br>display all the process of the system, with a minimum level of information.</p>               | Displays information about all or the specified processes, including the `EPROCESS` block.                                                                                                                                                                                                                                                                     |
| <p><em>\[dx advised]</em><br><a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/-thread">!thread</a></p>                         | `!thread [-p] [-t] <ADDRESS>`                                                                           | <p><code>!thread 0xffffcb088f0d6840</code><br>display all the process of the system, with a minimum level of information.</p> | Displays summary information about a thread, including the `ETHREAD` block.                                                                                                                                                                                                                                                                                    |
| [!acl](https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/-acl)                                                                           | `!acl <HEXA_ADDRESS>`                                                                                   |                                                                                                                               | Displays the contents of an `Access Control List (ACL)`.                                                                                                                                                                                                                                                                                                       |
| `poi(<ADDRESS>)`                                                                                                                                          |                                                                                                         |                                                                                                                               | Dereference pointer                                                                                                                                                                                                                                                                                                                                            |

**Display Debugger Object Model Expression (dx)**

```
dx <CPP_EXPRESSION>

// Displays information about the specified variable (using the variable address directly or the variable eventual symbol).
dx (<TYPE> *) <ADDRESS>
dx (<TYPE> *) &<VARIABLE>
// If the variable is a pointer to an object.
dx (<TYPE> **) &<VARIABLE>

// Examples:
dx -r1 (ntkrnlmp!_EPROCESS *) 0xffffa10d10c37080
dx (nt!_EPROCESS *) &nt!PsIdleProcess
dx *(nt!_OBJECT_TYPE **) &nt!AlpcPortObjectType

// Displays the first debugging session processes as a grid view.
dx -g Debugger.Sessions.First().Processes

// Displays the first debugging session processes as a grid view.
dx -g Debugger.Sessions.First().Processes

// Displays information about the process PID in the first debugging session.
dx Debugger.Sessions.First().Processes[<DECIMAL_PID>]

// Displays the threads of the first or specified process in the first debugging session processes as a grid view.
dx -g Debugger.Sessions.First().Processes.First().Threads
dx -g Debugger.Sessions.First().Processes[<DECIMAL_PID>].Threads

// Displays the loaded module of the first or specified process in the first debugging session processes as a grid view.
dx -g Debugger.Sessions.First().Processes.First().Modules
dx -g Debugger.Sessions.First().Processes[<DECIMAL_PID>].Modules

// Retrieve the first or specified thread of the specified process in the first debugging session.
dx -r1 Debugger.Sessions.First().Processes[<DECIMAL_PID>].Threads.First()
dx -r1 Debugger.Sessions.First().Processes[<DECIMAL_PID>].Threads[<DECIMAL_THREAD_ID>]

// Stores the specified process as a variable, allowing later reference to the process properties.
dx @$process = Debugger.Sessions.First().Processes[<DECIMAL_PID>]
dx @$process->Name
dx @$process->Threads
// Get process Ldr.
dx -r2 (_PEB_LDR_DATA *) @$process.Environment.EnvironmentBlock.Ldr

// Stores the specified process handles in a variable and display all or selected information about each handle.
dx @$processHandles = Debugger.Sessions.First().Processes[<DECIMAL_PID>].Io.Handles
// Display all available information.
dx -g @$processHandles
// Displays selected information.
dx -g @$processHandles->Select(o => new { Handle = o->Handle, Type = o->Type, ObjectName = o->ObjectName})
// Filters handles of type "Directory" and displays selected information.
dx -g @$processHandles->Where(o => (o.Type == "Directory"))->Select(o => new { Handle = o->Handle, Type = o->Type, ObjectName = o->ObjectName})

// Get process Ldr first entry (in memory order).
dx -r1 @$process.Environment.EnvironmentBlock.Ldr->InMemoryOrderModuleList.Flink
```

**Execution control flow**

| Command                                                                                                                       | Usage | Examples | Description |
| ----------------------------------------------------------------------------------------------------------------------------- | ----- | -------- | ----------- |
| [bp, bu, bm (Set Breakpoint)](https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/bp--bu--bm--set-breakpoint-) |       |          |             |
| [bl (Breakpoint List)](https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/bl--breakpoint-list-)               |       |          |             |
| [g (Go)](https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/g--go-)                                           |       |          |             |

### Userland process crashdump / dump analysis

| Command                                                                                                                                                                      | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `!analyze -v`                                                                                                                                                                | <p>Provides an overview of the dump: process name, error code, stack trace, etc.<br><br>More useful for crashdump, limited use for voluntarily taken process dump.</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| `!peb`                                                                                                                                                                       | Parses the `Process Environment Block (PEB)` of the process, notably retrieving: the process image file and command line, current directory, loaded `DLLs`, windows title, environment variables.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| `lm f`                                                                                                                                                                       | Lists the loaded `DLLs`, with the possibility to click on any `DLL` to retrieve more information on a specific `DLL`: size, file / product version, metadata, etc.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| `lmDv`                                                                                                                                                                       | Prints verbose information (as mentioned on `lm f`) for all loaded `DLLs`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| `!address`                                                                                                                                                                   | <p>Lists the address pages and associated information:<br>- Page type (<code>MEM\_IMAGE</code>, <code>MEM\_MAPPED</code>, <code>MEM\_PRIVATE</code>)<br>- Page state (<code>MEM\_COMMIT</code>, <code>MEM\_RESERVE</code>, <code>MEM\_FREE</code>)<br>- Page protection (<code>PAGE\_READWRITE</code>, <code>PAGE\_EXECUTE\_READ</code>, <code>PAGE\_EXECUTE\_READWRITE</code>, etc.)<br>- Eventual associated file on disk<br><br>Can be useful to detect a number of suspicious indicators / anomalies:<br>- In memory <code>PE</code> (<code>MZ</code> header) not backed by an on-disk file.<br>- Suspicious <code>PAGE\_EXECUTE\_READWRITE</code> protection.<br>- Modification of modules usually patched to bypass security mechanism (such as the patching of the <code>amsi.dll</code> to bypass <code>AMSI</code>). Indeed a modification of a module memory will result in the page to change from <code>MEM\_IMAGE</code> to <code>MEM\_PRIVATE</code>.</p> |
| <p><code>!address -f:\<FILTER></code><br><br>Examples:<br><br><code>!address -f:PAGE\_EXECUTE\_READWRITE</code><br><br><code>!address -f:MEM\_PRIVATE,MEM\_COMMIT</code></p> | <p>Filters memory pages based on the specified filter.<br><br>All filters in the list are AND-combined.</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |

***

### References

* Microsoft Windows Debugging Tools official documentation

  <https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/>
* "Modern Debugging with WinDbg Preview" DEFCON 27 workshop by hugsy and 0vercl0k"

  <https://github.com/hugsy/defcon\\_27\\_windbg\\_workshop>
* "WinDbg — the Fun Way: Part 1 / 2" by Yarden Shafir

  <https://medium.com/@yardenshafir2/windbg-the-fun-way-part-1-2e4978791f9b>

  <https://medium.com/@yardenshafir2/windbg-the-fun-way-part-2-7a904cba5435>
