# Debugger evasion lab

This Windows x64 lab has two debugger checks. The program exits with a different
code for each check if it sees a debugger. Once both checks pass, it decodes six
benign machine-code bytes, changes their private memory page from
read/write to read/execute, and calls them. The code returns `42`; it does not
open a network connection, write a file, or touch another process.

## Build and run

Install Visual Studio 2022 Build Tools with the **Desktop development with C++**
workload. From PowerShell in this directory:

```powershell
.\Build-Lab.ps1
.\bin\GuardedStage.exe
$LASTEXITCODE
```

The build writes the executable and matching private PDB to `bin/`. The normal
run prints `stage_result=42` and exits with `0`. Under a debugger, the sample
initially exits silently with `10`. After the first check is bypassed, the
second exits silently with `20`.

## What the two checks mean

`IsDebuggerPresent()` returns a nonzero value when the current process is being
debugged. `CheckRemoteDebuggerPresent()` returns whether the API call succeeded
and writes a separate Boolean through its second argument. The latter detects a
debugger on the current process too; “remote” does not require another computer.
The API failure path exits with `21` instead of treating failure as “clear.”

The WinDbg walkthrough below inspects the returned value of the first call and
the output Boolean of the second call separately. After bypassing both gates,
the `VirtualProtect` call requests `PAGE_EXECUTE_READ` (`0x20`). `!address` on
that buffer should show private memory with execute permission. The payload is
only `mov eax, 42; ret` (`B8 2A 00 00 00 C3`) and returns through the usual x64
calling convention. Addresses and module forwarding vary between runs.

The matching PDB provides `FirstGate`, `SecondGate`, `RunStage`, and
`InvokeStage` as named debugger stops. The sample is a controlled explanation of
debugger checks and staged code execution, not a malware specimen or a claim
that these two checks defeat modern analysis tools.

## Walk through it in WinDbg

Launch `bin\GuardedStage.exe` in WinDbg. At its initial loader break, add the
folder containing `GuardedStage.pdb` to the symbol path. The commands below are
for WinDbg's command window, not PowerShell. `KERNELBASE` contained these APIs on
the Windows 11 build used for this lab; use `x KERNELBASE!IsDebuggerPresent` and
similar symbol lookups if your build routes a call differently.

```text
.sympath+ C:\Lab\DebuggerEvasionLab\bin
.reload /f GuardedStage.exe
bu GuardedStage!FirstGate
bu KERNELBASE!IsDebuggerPresent
bu KERNELBASE!CheckRemoteDebuggerPresent
g
g
gu
r eax
gu
r eax
r eax=1
g
r rcx,rdx
r $t0=@rdx
dd @$t0 L1
gu
r eax
dd @$t0 L1
ed @$t0 0
dd @$t0 L1
gu
r eax
bu KERNELBASE!VirtualProtect
bu GuardedStage!InvokeStage
g
r rcx,rdx,r8,r9
r $t1=@rcx
db @$t1 L6
!address @$t1
gu
r eax
!address @$t1
g
r rcx
u @rcx L2
ba e1 @rcx
g
r rip
t
r eax
bc *
g
.lastevent
```

The first `gu` returns from `IsDebuggerPresent` with `EAX=1` in the recorded
debugger run. The next returns from `FirstGate` with `EAX=0`; changing it to one
lets `main` call the second gate. At `CheckRemoteDebuggerPresent`, `RCX` is the
current-process pseudo-handle and `RDX` points to the output Boolean. The API
returns `EAX=1` for success and writes `1` through that pointer. `SecondGate`
would then return `0x14` (decimal 20). `ed @$t0 0` changes the output Boolean
while `SecondGate` is paused, without changing the successful API return. It
returns zero and lets the lab reach the stage.

At `VirtualProtect`, `RCX` points to the decoded bytes, `RDX` is their length
(`6`), and `R8` requests `PAGE_EXECUTE_READ` (`0x20`). `!address` shows the same
private page as `PAGE_READWRITE` before the call and `PAGE_EXECUTE_READ` after
it returns with `EAX=1`. `ba e1` stops when the CPU reaches that address; one
step over `mov eax, 2Ah` leaves `EAX=0x2a` (decimal 42). The program prints
`stage_result=42` and exits with zero. `!address` also shows an **Allocation
Protect** field, which records the protection at allocation; use the **Protect**
field for the page's current protection.
