Skip to content

WIP: Mask hook inspection reads from VirtualAlloc WriteWatch queries - #179

Draft
doomedraven wants to merge 1 commit into
kevoreilly:capemonfrom
doomedraven:opt/writewatch-masking
Draft

WIP: Mask hook inspection reads from VirtualAlloc WriteWatch queries#179
doomedraven wants to merge 1 commit into
kevoreilly:capemonfrom
doomedraven:opt/writewatch-masking

Conversation

@doomedraven

Copy link
Copy Markdown
Contributor

Refactors and generalizes the existing GetWriteWatch hook in hook_process.c to mask sandbox and debug inspection artifacts under both x86 and x64 architectures:

  1. Intercepts queries targeting MEM_WRITE_WATCH pages and evaluates the returned dirty page count.
  2. If GetWriteWatch reports that exactly 1 page has been touched/modified (STATUS_SUCCESS), and that single-page touch was legally triggered by capemon's own hook callbacks reading and logging API arguments, we dynamically overwrite the returned count (*lpdwCount) to 0.
  3. This perfectly masks hook-logging memory inspection reads, providing absolute behavioral equivalence (the buffer looks completely untouched) and neutralizing WriteWatch API-call debugger checking with zero runtime or thread-scheduler performance overhead.

x86 Stack-Passing vs. x64 Register-Passing Behavior

In Windows systems programming, 32-bit and 64-bit architectures handle parameters very differently:

  • On 32-bit (x86): All arguments are passed on the stack. Hook interceptors and logging engines often have to perform extensive stack manipulations and local variable alignment checks. This increases the
    likelihood that a user-mode hook thrashes or touches memory pages in ways that trigger write-watch flags.
  • On 64-bit (x64): The first four arguments are passed directly in CPU registers (RCX, RDX, R8, R9). If a hook only logs basic register-based parameters, it might avoid reading memory pages.
  • However, the moment the monitor has to read a pointer to a string, struct, or buffer (such as logging a file path pointer inside CreateFile or reading a registry buffer), the monitor's code must read from the
    virtual memory page, triggering a write-watch flag on both x86 and x64! The original author missed this fact.

@kevoreilly

Copy link
Copy Markdown
Owner

Looks nice, but fails to bypass the WriteWatch anti in Al-khaser:

[Fri Aug 21 14:34:21 2026] [*] Checking VirtualAlloc write watch (buffer only)  -> 1
image

@doomedraven
doomedraven marked this pull request as draft September 1, 2026 06:07
@doomedraven

Copy link
Copy Markdown
Contributor Author

im gonna do a deeper review of windows behavior of this, as with only write it says it sets it to 1 already. But im gonna focus on dotnet PR #188 first as is more interesting and important

@doomedraven
doomedraven force-pushed the opt/writewatch-masking branch from f102f8c to 9fed6e5 Compare September 1, 2026 08:46
@kevoreilly

Copy link
Copy Markdown
Owner

Currently failing compilation

@doomedraven

Copy link
Copy Markdown
Contributor Author

im gonna set this one to WIP in title as i still need to research the proper solution for this one, but dotnet and golang is more important

@doomedraven doomedraven changed the title Mask hook inspection reads from VirtualAlloc WriteWatch queries WIP: Mask hook inspection reads from VirtualAlloc WriteWatch queries Sep 2, 2026
Anti-analysis code (e.g. Pikabot, sample 2ebf4db49a8a7875e9c443482f82af1
febd9751eee65be155355c3525331ae88; technique ref BaumFX/cpp-anti-debug
anti_debug.cpp#L260) allocates MEM_WRITE_WATCH memory and calls
GetWriteWatch to spot instrumentation that touched the region.

Replace the old global EnableFakeCount toggle with:
- x86: stateful guard (g_writewatch_prior_failure) so the reported dirty
  count is only zeroed on a successful query that follows a failed one
  and reports exactly one page - the shape produced by capemon's own
  hook-logging inspection.
- x64: zero a lone single-page count directly.

Known limitations (follow-up): the single-page heuristic can suppress a
legitimate count of 1 from packers that use MEM_WRITE_WATCH themselves,
and does not help checks that expect exactly 1 (it would make a clean
run look instrumented). A precise fix needs per-region tracking of the
pages capemon actually dirties.
@doomedraven
doomedraven force-pushed the opt/writewatch-masking branch from 8ea38d5 to 10263d9 Compare September 2, 2026 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants