Skip to content

fix: strengthen anti-debug detection (Safe mode blocking startup check) (#76) - #94

Merged
mcpolo99 merged 1 commit into
developfrom
76-anti-debug-protection-is-ineffective-in-safe-mode-dnspy-can-run-and-debug-protected-assemblies
Jul 4, 2026
Merged

fix: strengthen anti-debug detection (Safe mode blocking startup check) (#76)#94
mcpolo99 merged 1 commit into
developfrom
76-anti-debug-protection-is-ineffective-in-safe-mode-dnspy-can-run-and-debug-protected-assemblies

Conversation

@mcpolo99

@mcpolo99 mcpolo99 commented Jul 4, 2026

Copy link
Copy Markdown
Owner

Phase 1 hardening for #76 — the anti-debug protection let a debugger (dnSpy F5) attach and the app ran before the async worker ever checked.

Fixes #76

Changes (Safe + Win32 runtime)

  • Blocking startup check in Initialize() (injected at the top of the module cctor): if a debugger is attached when the module loads, the app fails immediately — before any user code — instead of only being caught later on the background thread. This is the fix for the dnSpy-F5 case.
  • Win32 startup check also calls native IsDebuggerPresent().
  • Watchdog poll interval 1000ms → 200ms; removed the 500ms initial sleep → smaller detection race window.

Deliberately deferred: default Safe → Win32 (issue proposal #1)

Win32 P/Invokes ntdll/kernel32 and would crash cross-platform .NET targets on non-Windows, conflicting with the project's all-frameworks goal. The strengthened Safe mode fixes the reported issue portably; OS-aware mode selection is a larger Phase 2 change.

Test

New AntiDebug.Test (safe + win32) — regression guard that an anti-debug-protected assembly still runs normally when not debugged (START / output / exit 42). Both cases green locally. Verified the injected runtime rebuilds (net20).

Not in this PR (Phase 2, per issue)

Hash-based process detection, anti-attach, OS-aware checks, Antinet .NET Core support.

Phase 1 hardening for the anti-debug protection. The core complaint — a debugger
(e.g. dnSpy F5) can attach and the app runs before the async worker ever checks —
is fixed portably:

- Safe and Win32 modes now run a BLOCKING debugger check inside Initialize()
  (injected at the top of the module cctor), so an assembly launched under a
  debugger fails immediately, before any user code runs, instead of only being
  caught later on the background thread.
- Win32's startup check also calls the native IsDebuggerPresent().
- Reduced the watchdog poll interval from 1000ms to 200ms and removed the 500ms
  initial sleep, shrinking the detection race window.

Deliberately NOT changing the default mode from Safe to Win32 (issue's proposal
1): Win32 P/Invokes ntdll/kernel32 and would crash cross-platform .NET targets on
non-Windows, conflicting with the project's all-frameworks goal. The strengthened
Safe mode addresses the reported issue portably; OS-aware mode selection is a
larger Phase 2 change.

Added AntiDebug.Test (safe + win32) as a regression guard that an anti-debug
protected assembly still runs normally when not debugged (START/output/exit 42).
@mcpolo99
mcpolo99 merged commit a8ad297 into develop Jul 4, 2026
3 checks passed
@mcpolo99
mcpolo99 deleted the 76-anti-debug-protection-is-ineffective-in-safe-mode-dnspy-can-run-and-debug-protected-assemblies branch July 4, 2026 12:57
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.

1 participant