Skip to content

Instantly share code, notes, and snippets.

@hLunaaa
Forked from esoterix/callback.c
Created April 30, 2025 02:43
Show Gist options
  • Save hLunaaa/6dc6a3f00269b7e012dd7f9282657b9f to your computer and use it in GitHub Desktop.
Save hLunaaa/6dc6a3f00269b7e012dd7f9282657b9f to your computer and use it in GitHub Desktop.
void InstrumentationCallback(CONTEXT *context)
{
TEB *teb = NtCurrentTeb();
context->Rip = teb->InstrumentationCallbackPreviousPc;
context->Rsp = teb->InstrumentationCallbackPreviousSp;
context->Rcx = context->R10;
// Prevent recursion
if (!teb->InstrumentationCallbackDisabled) {
teb->InstrumentationCallbackDisabled = TRUE;
// Do whatever you want
teb->InstrumentationCallbackDisabled = FALSE;
}
RtlRestoreContext(context, NULL);
}
include ksamd64.inc
extern InstrumentationCallback:proc
EXTERNDEF __imp_RtlCaptureContext:QWORD
.code
InstrumentationCallbackThunk proc
mov gs:[2e0h], rsp ; Win10 TEB InstrumentationCallbackPreviousSp
mov gs:[2d8h], r10 ; Win10 TEB InstrumentationCallbackPreviousPc
mov r10, rcx ; Save original RCX
sub rsp, 4d0h ; Alloc stack space for CONTEXT structure
and rsp, -10h ; RSP must be 16 byte aligned before calls
mov rcx, rsp
call __imp_RtlCaptureContext ; Save the current register state. RtlCaptureContext does not require shadow space
sub rsp, 20h ; Shadow space
call InstrumentationCallback
int 3
InstrumentationCallbackThunk endp
end
@hLunaaa
Copy link
Author

hLunaaa commented Apr 30, 2025

guj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment