Skip to content
fl0sec
Windows Internals

Where Hardware-Breakpoint Syscalls Resume

PivotGate, HWSyscalls and LayeredSyscall all hand the thread back through the same Windows path. A vectored continue handler sitting on that path catches all three, and stays quiet on ordinary programs.

8 minestimated reading time

PivotGate ended with a claim I had only half tested: once the private stub and the fake stack are gone, what is left is the control plane. A breakpoint gets armed, the #DB arrives at a native entry, and the thread leaves somewhere else. I had a detector for my own proof. I did not know whether the same idea would hold against other people's code.

So I built one sensor and pointed it at three public techniques that use hardware breakpoints to reach a syscall: PivotGate, HWSyscalls and LayeredSyscall. It caught all three, they all still completed their syscall under it, and seven ordinary programs produced nothing. The sensor is called Dr7Watch.

View the PoC on GitHub

The step none of them can skip

The three techniques disagree on almost everything. HWSyscalls arms its first breakpoint with SetThreadContext. LayeredSyscall forces an access violation and writes the debug registers from inside the handler. PivotGate raises a private software exception for the same purpose. They redirect to different places, build different stacks and trust different parts of ntdll.

They agree on one thing. Each of them changes a CONTEXT inside a vectored exception handler and returns EXCEPTION_CONTINUE_EXECUTION. That return value sends the thread down a short path in ntdll: KiUserExceptionDispatcher, RtlDispatchException, the vectored exception handlers, the vectored continue handlers, then NtContinue.

#DB at Nt entrydebug exceptionKiUserExceptionDispatcherVEHtechnique edits CONTEXTVCHDr7Watch reads itNtContinueCONTEXT appliedExceptionAddress + edited RIP, RSP, RAX, DR0-DR7What each technique changes before resumingPivotGateRIP: entry -> own syscall; retREDIRECTHWSyscallsRSP - 0x70, [RSP] = add rsp,68h; retRETURNLayeredSyscallRIP: entry -> user code -> syscallREDIRECT x2
Every technique in this article resumes through the same path. The vectored continue handler sees the exception address and the context the technique produced, before NtContinue applies it.

Vectored continue handlers are the less famous half of the VEH API. When a handler returns EXCEPTION_CONTINUE_EXECUTION, they run with the record of the exception that was raised and the context that will be restored. That pair holds both ends of the redirect: ExceptionAddress says where the thread stopped, Rip says where it will go, and Dr0-Dr7 say which breakpoints will be live once it does.

Registration order does not matter here. A technique can register its own handler first and stop the vectored exception chain. Continue handlers still run after it.

Four questions at every resume

The sensor builds a sorted table of Nt* exports from the loaded ntdll and treats each entry as a 0x20-byte stub. On every resumed exception it asks four things:

Dr7Watch findings
FindingCondition at a resumed exception
ARMAn enabled execute breakpoint in DR0-DR3 points into an Nt* stub
REDIRECTA single-step leaves an Nt* stub, or resumes onto a stub's syscall from elsewhere
SSNExecution resumes on a stub's syscall with a different service number in RAX than the stub's own mov eax, imm32
RETURNA single-step at a stub entry resumes with a return address at [RSP] that no call precedes

ARM is the weak one. Debuggers put breakpoints on system calls all the time, and I come back to that below. The other three describe a context that ordinary code does not hand to NtContinue.

The return check is crude on purpose. It reads the eight bytes before the return address and accepts the usual call encodings: E8 rel32, FF 15 [rip+disp32], register calls and the register-plus-displacement forms. It does not try to prove which instruction ran. It only asks whether one could have.

The first version crashed PivotGate

My first sensor logged each finding with WriteFile from inside the continue handler. Under it, cmd and PowerShell were clean, PivotGate logged its ARM, and then the process died with 0x80000004, an unhandled single-step.

The context the handler reads is the one that will be restored. The thread's real debug registers are still the old ones until NtContinue runs. PivotGate had armed DR0 on NtWriteFile, so my own WriteFile walked into that breakpoint from inside the dispatcher. PivotGate's handler had already consumed its request, returned EXCEPTION_CONTINUE_SEARCH, and nothing else claimed the #DB.

Three techniques, three different tells

Every run below used the same Windows 11 build (ntdll 10.0.26100.8246). The HWSyscalls and LayeredSyscall harnesses each make one call to NtQueryInformationProcess on their own process through the library. Both completed with STATUS_SUCCESS under the sensor.

PivotGate

dr7watch pivotgate_poc.exe

[dr7watch] ARM tid=11720 dr0=ntdll!NtWriteFile+0x0 code=0xe0425001 at=KERNELBASE.dll+0xc79da [dr7watch] REDIRECT tid=11720 from=ntdll!NtWriteFile+0x0 to=ntdll!NtWriteFile+0x12 rax=0x8 [dr7watch] summary: 1 arm, 1 redirect, 0 ssn, 0 return finding(s)

The ARM line shows the arming exception itself: code 0xE0425001, raised from KernelBase by RaiseException, resumed with DR0 on NtWriteFile. The REDIRECT is the move from the stub entry to its own syscall; ret at +0x12, with RAX already holding the service number. There is no RETURN finding because PivotGate does not touch the stack. The genuine KernelBase return address is still at [RSP].

HWSyscalls

dr7watch hwsyscalls_benign.exe

[dr7watch] ARM tid=21508 dr0=ntdll!NtQueryInformationProcess+0x0 code=0x80000004 at=hwsyscalls_benign.exe+0x15d0 [dr7watch] RETURN tid=21508 at=ntdll!NtQueryInformationProcess+0x0 ret=KERNEL32.DLL+0xe295 not-after-call [dr7watch] summary: 1 arm, 0 redirect, 0 ssn, 1 return finding(s)

HWSyscalls keeps a breakpoint on its own PrepareSyscall function. When that fires, the handler moves DR0 onto the requested Nt* export. That is the ARM line: a single-step in the harness, resumed with DR0 in ntdll.

The stub was not hooked on this machine, so HWSyscalls did not move RIP at all. It subtracted 0x70 from RSP and wrote a gadget address as the new return address. KERNEL32.DLL+0xe295 is add rsp, 68h; ret. The two bytes before it belong to a jz, not a call.

kernel32+0xe293:  74 11            jz   ...
kernel32+0xe295:  48 83 c4 68      add  rsp, 68h      ; the pushed return address
kernel32+0xe299:  c3               ret

The rule does not know what a gadget is. It sees a return address that no call could have produced, at the moment a breakpoint on a system call is resumed.

LayeredSyscall

dr7watch layered_benign.exe

[dr7watch] ARM tid=10232 dr0=ntdll!NtQueryInformationProcess+0x0 code=0xc0000005 at=layered_benign.exe+0x1f47 [dr7watch] ARM tid=10232 dr1=ntdll!NtQueryInformationProcess+0x14 code=0xc0000005 at=layered_benign.exe+0x1f47 [dr7watch] REDIRECT tid=10232 from=ntdll!NtQueryInformationProcess+0x0 to=layered_benign.exe+0x1740 rax=0x7dfeeff728 [dr7watch] REDIRECT tid=10232 from=ntdll+0x135fa8 to=ntdll!NtQueryInformationProcess+0x12 rax=0x19 [dr7watch] (975 more resumes with an already reported breakpoint live) [dr7watch] summary: 2 arm, 2 redirect, 0 ssn, 0 return finding(s)

LayeredSyscall arms two breakpoints from an access violation it causes on purpose: one on the stub entry and one on the ret after its syscall. When the entry fires, it saves the context and sends the thread to an ordinary function in the harness. Then it sets the trap flag and single-steps until execution is back inside ntdll, about 975 steps in this run. There it restores the saved registers and jumps onto the stub's syscall with the right service number.

Both jumps are reported. The first leaves the stub for user code. The second lands on a syscall from an unrelated place in ntdll. The SSN rule stays quiet because RAX is correct. Every trap-flag resume in between carries the same live DR1, so the launcher folds them into one line.

What the kernel saw

The PivotGate article stopped short of an ETW capture because the shared NT Kernel Logger was busy on my machine. This time I wrote a small controller, syscall_trace, that starts a private SystemTraceProvider session in system-logger mode, enables syscall events with user stacks on SysCallEnter, and runs one program. Each run below is the same harness as above, without the sensor. Frames are resolved against each module's export table, so a name means "nearest export", not "this function".

PivotGate's file write reached the kernel like this:

pivotgate_poc.exe, NtWriteFile
ntdll.dll!NtWriteFile+0x14
KernelBase.dll!WriteFile+0x8d
pivotgate_poc.exe+0x271e
pivotgate_poc.exe+0x2ddf
kernel32.dll!BaseThreadInitThunk+0x17
ntdll.dll!RtlUserThreadStart+0x2c

That is a normal WriteFile. The same process's ordinary CRT output, written later through printf, has the same top two frames. Nothing in the syscall event or its stack separates the redirected call from a real one. That was the whole point of PivotGate, and the kernel agrees.

HWSyscalls:

hwsyscalls_benign.exe, NtQueryInformationProcess
ntdll.dll!NtQueryInformationProcess+0x14
kernel32.dll!GetLogicalDrives+0x45
hwsyscalls_benign.exe+0x107c
hwsyscalls_benign.exe+0x23bf
kernel32.dll!BaseThreadInitThunk+0x17
ntdll.dll!RtlUserThreadStart+0x2c

GetLogicalDrives+0x45 is kernel32+0xe295, the same add rsp, 68h; ret gadget the sensor flagged. A symbolizer turns it into a plausible frame. Only reading the bytes before it shows that nothing called from there.

LayeredSyscall:

layered_benign.exe, NtQueryInformationProcess
ntdll.dll!NtQueryInformationProcess+0x14
ntdll.dll+0x135caa
ntdll.dll+0x41646
ntdll.dll!ApiSetQueryApiSetPresence+0x5a
gdi32.dll!gdiPlaySpoolStream+0x4a
gdi32.dll!CreateFontIndirectW+0x5a
user32.dll+0x32985
user32.dll!SoftModalMessageBox+0x119
user32.dll+0x5f3f2
user32.dll!MessageBoxTimeoutW+0x18f
user32.dll!MessageBoxW+0x45
layered_benign.exe+0x1760
...

This is the stack LayeredSyscall borrowed by tracing into MessageBoxW until it reached a deep enough frame in ntdll. Every return address is real. The story they tell, a message box querying process information through font creation, is not.

So the kernel view splits the three in a different place from the sensor. HWSyscalls leaves a frame that fails a "preceded by a call" test. LayeredSyscall leaves a chain that is real but makes no sense for the service. PivotGate leaves nothing at all. Only the resumed context catches all three.

Programs that should stay quiet

The same launcher ran seven programs that have no reason to touch debug registers:

ProgramFindings
cmd /c dir0
PowerShell, listing System32 and catching an exception0
dotnet --info0
Python, importing ssl, sqlite3 and hashlib0
Node, catching a TypeError and using crypto0
git --version0
A test app with 500 SEH exceptions and 500 VEH-recovered null calls0

The last one is there for the continue handler. Its VEH recovers from a call through a null pointer by pulling the return address off the stack, which is a real redirect. It resumes in the application's own code, not in ntdll, so none of the rules apply.

Seven programs on one machine prove nothing about a fleet. They show that the rules do not fire on normal exception traffic, which was the cheapest way for this to fail.

The debugger case

I attached WinDbg to the test app and set ba e1 ntdll!NtWriteFile. The breakpoint hit three times. Each hit went to the debugger first, the debugger handled it, and the app's handlers never saw it.

The app still raised its own access violations, though, and its VEH resumed them. The context of every one of those resumes contained WinDbg's DR0 on NtWriteFile, so the sensor reported an ARM. That is a true observation and a useless alert. The sensor now reads PEB.BeingDebugged (without a call) and tags those events debugger=attached instead of counting them.

What it does not see

  • A handler can call NtContinue itself instead of returning EXCEPTION_CONTINUE_EXECUTION. Continue handlers never run.
  • Anything in the process can unregister the handler or edit LdrpVectorHandlerList. NCC Group walks that list to find rogue handlers, and IBM X-Force describes attackers editing it. An in-process sensor is only as strong as the process.
  • TamperingSyscalls and MutationGate change registers without moving RIP off a stub. The SSN rule covers MutationGate's swap only when execution resumes on a syscall. I did not test either.
  • Native x64 only.

None of this makes the signal go away. It moves it. A technique that avoids continue handlers still needs a context with a breakpoint on a system call, and something still has to restore it.

Conclusion

I started this to check a sentence at the end of the PivotGate article. The sentence held up better than I expected. Three techniques built by different people, with different arming paths and different ideas about stacks, all resumed through the same ntdll function with the evidence still in the context.

The sensor is under 400 lines of C. The part that took longest was learning that it could not call WriteFile.

Dr7Watch, the harnesses and the test matrix are on GitHub.

Was this useful?