# WinDbg

## Intermediate WinDbg

| **ctrl + e** – *open file executable*                                |
| -------------------------------------------------------------------- |
| **.reload /f** – *will restart program execution from the beginning* |
| **q, qq** – *quit*                                                   |
| **k, kb, kc, kd, kp, kP, kv** – *Display Stack Backtrace*            |
| **pa \<address or symbol name>** *– for ste**p** to **a**ddress*     |
| **ta \<address or symbol name>** – *for **t**race to **a**ddress*    |

Dưới đây là note của tôi vì thời gian có hạn nên tôi up lên đây:

{% embed url="<https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcB4LtHTD0vCwLOFVEnvi%2Fuploads%2Fxg7J1tOaaihzRomoANsl%2FWinDbg.pdf?alt=media&token=2756fa90-a595-4f83-b1b5-9e28293e37a0>" %}

{% file src="/files/t7gMzUaYWQJLGPCWX5F2" %}

Phần tiếp theo...

## Advanced WinDbg

### 1. Chuẩn bị 2 máy ảo

* 1 máy target và 1 máy Debugger
* Mỗi máy sẽ đều được cài đặt python và cài đặt các công cụ được tự động thông qua file setup\_vm.py (6)

#### Enable kernel debugge

```
bcdedit /debug on
bcdedit /dbgsettings net hostip:<Debugger_VM host-only adapter IP> port:50000 key:a.b.c.d
```

* Debugger\_VM host-only adapter IP - là địa chỉ IP của máy ảo Debugger\_VM
* port và key là tùy

#### Tạo file để chạy windbg với việc thiết lập tự động (.bat)

```batch

REM Debug only
"C:\Users\IEUser\AppData\Local\Microsoft\WindowsApps\WinDbgX.exe" /k net:port=50000,key=a.b.c.d

REM Debug only and specifying the IP address for maximizing success
REM "C:\Users\IEUser\AppData\Local\Microsoft\WindowsApps\WinDbgX.exe" /k net:port=50000,key=a.b.c.d,target=192.168.119.172

REM Debug and load a script
REM http://yeilho.blogspot.com/2012/10/windbg-init-script.html
REM "C:\Users\IEUser\AppData\Local\Microsoft\WindowsApps\WinDbgX.exe" /k net:port=50000,key=a.b.c.d -c "$$>< dbg-prep.cmd"

REM Debug and load a script / specifying the IP address for maximizing success
REM "C:\Users\IEUser\AppData\Local\Microsoft\WindowsApps\WinDbgX.exe" /k net:port=50000,key=a.b.c.d,target=192.168.119.172 -c "$$>< dbg-prep.cmd"
```

### 2. Configuring Ghidra/IDA Pro

* Phần này chỉ tiết lập một số bước để GUI được thuận tiện cho việc debug của mình, tối ưu hóa việc đọc và hiểu
* Tải về phiên bản phù hợp (1)
* Edit -> Tool Options và thiết lập như sau:

  <figure><img src="/files/Egd3hbb51qmgayOsMjrn" alt=""><figcaption><p>bật hiển thị số dưới dạng hex</p></figcaption></figure>

  <figure><img src="/files/kdNgpePbqBJp5GTPGzWN" alt=""><figcaption><p>Chuyển Mouse Button to Activate -> LEFT</p></figcaption></figure>

  <figure><img src="/files/mBraPapdj7KRdpSnEhuV" alt=""><figcaption><p>Bỏ chọn phần Markup Register Variable References</p></figcaption></figure>
* Để hiển thị hoặc không hiển thị một số trường ở Listing ta thao tác như sau: ta nhấn vào Edit the Listing fields -> sau disable những trường mà mình không muốn hiển thị hoặc tăng giảm độ rộng của trường.<br>

  <figure><img src="/files/bKRhDyGqphfNAgRbvehv" alt=""><figcaption><p>chọn edit the listing fields</p></figcaption></figure>

  <figure><img src="/files/LSJ2uUzY6R1Ayra3hApA" alt=""><figcaption><p>chuột phải vào trường mà mình muốn không hiển thị -> chọn disable field</p></figcaption></figure>
* Hiển thị phần Bookmarks: có thể sử dụng shortcut: Ctrl + B hoặc vào Window -> Bookmarks<br>

  <figure><img src="/files/DYSAnP54Sm4ew4oJ7Q7X" alt=""><figcaption><p>bookmarks</p></figcaption></figure>

### 3. Configuring ret-sync

* một bộ của các plugin giúp đồng bộ hóa phiên bản gỡ lỗi với bộ tháo rời

Đầu tiên ta sẽ tạo file `.sync` có nội dung sau:

```
[INTERFACE]
host=127.0.0.1
port=9100
```

Sau đó copy file vừa tạo dán vào thư mục `C:\Users\IEUser\` in our Debugger\_VM

Tiếp theo là cấu hình file `.sync` đối với ghidra:

```
[INTERFACE]
host=127.0.0.1
port=9100

[ALIASES]
ntoskrnl.exe=ntkrnlmp.exe
```

Lưu file này vào thư mục `name_project.rep/`

Tiếp theo .... Thêm extension vào ghidra:

<figure><img src="/files/Xvq4A1oasFKVCpcYgJoH" alt=""><figcaption><p>Vào file -> install extensions. </p></figcaption></figure>

<figure><img src="/files/dn7B7GsDOJ0Sz8GZTdVH" alt=""><figcaption><p>Sau đó ta nhận được cửa sổ -> click on the Add Extension</p></figcaption></figure>

<figure><img src="/files/h2mXptjlbVuQKoEP0SHG" alt=""><figcaption><p>lựa chọn phiên bản phù hợp với phiển bản mình cài đặt nhé (2)</p></figcaption></figure>

<figure><img src="/files/6P6VC4ORr0izrBzObar3" alt=""><figcaption><p>Lưu ý tích vào mục retsync  -> sau đó nhấn ok -> restart lại ghidra</p></figcaption></figure>

<figure><img src="/files/E8zPdDPhtDJdF083yFvz" alt=""><figcaption><p>Cửa sổ khi add thành công</p></figcaption></figure>

Tiếp theo để lắng nghe từ winDbg ta click chọn RetSyncPlugin rồi nhấn shortcut: Alt + S để lắng nghe:

<figure><img src="/files/ycW5irx8lOtefV3cCZur" alt=""><figcaption><p>Alt + S</p></figcaption></figure>

Tiếp theo ta qua WinDbg: sử dụng 1 số câu lệnh sau:

```
.load sync
!sync
ba e 1 nt!NtReadFile
g
```

Hoặc ta có thể tạo file dbg-preg.cmd:

```
$$ automatically start ret-sync
.load sync
!sync

$$ reload symbols so bps are actually set
.reload /f nt

$$ba e 1 nt!NtReadFile

$$ show them to make sure everything is sane
$$bl

$$ let it go :)
g
```

Sau đó có thể thay đổi file để mở auto của windbg như sau:

```
"C:\Users\IEUser\AppData\Local\Microsoft\WindowsApps\WinDbgX.exe" /k net:port=50000,key=a.b.c.d -c "$$>< dbg-prep.cmd"
```

* Lưu ý các file này phải cùng một thư mục nhé...

<figure><img src="/files/wB5zSEQT7S6iXasvdr5r" alt=""><figcaption><p>kết quả kết nối thành công</p></figcaption></figure>

### 4. Configuring Visual Studio & SSH

#### Đối với Target VM - linux subsystem

* run ubuntu app (run ubuntu or bash in cmd)
* run ssh service `sudo service ssh start`

<figure><img src="/files/7zKFaF19Rz0F6mEZ0tww" alt=""><figcaption></figcaption></figure>

#### Visual Studio build test

Build a project on the Debugger VM: `build.bat hello` (file `build.bat` lấy từ nguồn của khóa học (6)) - Built binary will be auto-copied to the Target VM desktop. Run from the console on Target VM:

```shell
C:\Users\IEUser\Desktop\HelloWorld_lab.exe
```

#### env.bat

```batch
@echo off

echo Setting persistent environment variables...
setx SCP_OVERRIDE 0
REM SCP_REMOTE_IP: ip của Target_VM
setx SCP_REMOTE_IP 192.168.92.131 
REM SCP_REMOTE_USER: wsl username
setx SCP_REMOTE_USER user
REM SCP_REMOTE_PATH: path to the windows desktop in the wsl context
setx SCP_REMOTE_PATH /mnt/c/Users/IEUser/Desktop
REM SCP_PRE_DELETE: nếu ta cần xóa một tập tin trước khi thực hiện sao chép: 
REM nó sẽ hữu ích khi làm việc với shared folders. Ex:"yes"
setx SCP_PRE_DELETE ""

REM Second set lets us call from build.bat if user forgot to reboot
set SCP_OVERRIDE=0
set SCP_REMOTE_IP=192.168.92.131
set SCP_REMOTE_USER=user
set SCP_REMOTE_PATH=/mnt/c/Users/IEUser/Desktop
set SCP_PRE_DELETE=""

echo Done. Now please reboot your machine so the environment variables are taken into account.
```

Cài đặt phần mềm PuTTY (bằng đường link mục 3)

Tiếp theo thiết lập PuTTY:

* Đầu tiên là đặt Host Name (or IP address)<br>

  <figure><img src="/files/jYyTCsMYRx8wScDN7bf6" alt=""><figcaption><p>Host  name</p></figcaption></figure>
* Tiếp theo thiết lập user:<br>

  <figure><img src="/files/gFBNZierwcSTAHCnyLnV" alt=""><figcaption></figcaption></figure>
* Tiếp đến chèn file private key for authentication:<br>

  <figure><img src="/files/lAjXhCGA62wHlp7a0waF" alt=""><figcaption><p>chèn file đuôi .ppk</p></figcaption></figure>
* Nội dung file private keys

```
PuTTY-User-Key-File-2: ssh-rsa
Encryption: none
Comment: rsa-key-20191118
Public-Lines: 6
AAAAB3NzaC1yc2EAAAABJQAAAQEAqmWoIsmfC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30O5NruoKszcG0DkGu0GeCEtWNXqQhPNPgpTdEa5S/uHqNqVhOoN5CcUiK
xvFtS9UYlBbXzi2RdqNW/8BBvoMhDtPJJJnXDOVHK0HXCcU0qJVN4olXKIAfisyX
5zgWozSNh6Pyxwb8FAqkKY0i4RoMLc/jbNP5p6WRwIQPtw==
Private-MAC: 24121ffaf55b25c6785fa3f2996a741fd7717239
```

* Sau đó nhấn Open<br>

  <figure><img src="/files/AtOljckxmcIz2nh9kHpc" alt=""><figcaption><p>nhấn accept</p></figcaption></figure>
* Tiếp theo nếu kết nối được là xong. Còn nếu gặp lỗi qua lại máy target\_VM ta thiết lập lại như sau như câu lệnh thao tác trong ubuntu của wsl nhé:

  ```
  sudo ssh-keygen -A
  mkdir ~/.ssh/
  vim ~/.ssh/authorized_keys
  chmod 700 ~/.ssh
  chmod 600 ~/.ssh/authorized_keys
  sudo service ssh start
  ```
* và nội dung của file `authorized_keys` như sau:&#x20;

  ```
  ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAqmWoIsmfCMLLDN78r+1j2Cy2j2C/cPk5iCEcKGv/vWzxi2YfzKh7Ain234DqIK8otp/INoRglN79ENPHhnM8A9elQJrI7LPYOUswVqz+3/baZVZ8y1k/g+V5hTemhm6iSC2H248a3kU0zgKnh3nN31Yv6ui7DwcaKbVInZnAsLStLD74/Tu1WRVb1Kg84fobtMgnIrXINdTuKKm5Bme8RtZvOfpSLbqjd3ZcvpgqTYJJw9AyZDITPLE0swMdt/zs/BQnlz6P5/8PmEqDo/hBRmKOd4jFwUIy7z/CdE3ZwlsZ6sR+nq5YF+hnVp+qGIFHAHZTOl6QC23AfEJqUCu3VQ== rsa-key-20191118
  ```
* Rồi sau đó ta resart lại và vào lại ubuntu wsl rồi thực hiện lại câu lệnh sau: \
  `sudo service ssh start`

Kết quả nhận được thế này là thành công:

<figure><img src="/files/cE2tRpVTGKhU9RtKquVQ" alt=""><figcaption></figcaption></figure>

Tiếp theo khởi động lại `build.bat hello` -> sau đó ta vào Visual Studio -> rồi build ta nhận được:

<figure><img src="/files/Cv1WXLg6NeFEoiNNVBP3" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/zsRxd0OIWYYTDxJFgra2" alt=""><figcaption></figcaption></figure>

Kết quả nhận được ở máy ảo Target\_VM:<br>

<figure><img src="/files/lApiLKrpDDOObaUdFj30" alt=""><figcaption></figcaption></figure>

Lưu ý về bài lap: khi thực hiện debug nhớ sử dụng các câu lệnh đã được biết để có thể thêm thông tin như `!process`, `.process`, ...

***

#### Một số link cần và nguồn tải các tool và tài liệu:

1. <https://github.com/NationalSecurityAgency/ghidra/releases?page=2> (Ghidra, lưu ý về phiên bản để tải sao cho phù hợp với cái minh mong muốn)
2. <https://github.com/bootleg/ret-sync> (ret-sync là một bộ của các plugin giúp đồng bộ hóa phiên bản gỡ lỗi với bộ tháo rời. Nó giúp tận dụng tốt nhất từ cả tĩnh và động)
3. <https://www.puttygen.com/download-putty#PuTTY_for_windows> (PuTTY - là một giả lập thiết bị đầu cuối phổ biến, nó giúp kết nối với máy tính từ xa trong khi cung cấp các phương tiện truyền tệp, thao tác dữ liệu, v.v.
4. <https://drive.google.com/file/d/1qdjmbC7nAymdpQfwQrcQHqTHcD1By80l/view> (Phiên bản win10 MSEdge VMware, nó là gói cài sẵn hệ điều hành win10 với VMware.
5. <https://www.python.org/downloads/release/python-380/> (Python 3.8)
6. <https://gitlab.com/opensecuritytraining/dbg3011_advanced_windbg/-/tree/main> (trong này gồm tài liệu khóa học Advanced WinDbg kèm theo các tool và hướng dẫn cài đặt để sử dụng phục vụ phần Advanced)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://viettaliii.gitbook.io/home/education/tools/windbg.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
