syscall-detect
repository·master·Indexed 42 days ago
https://github.com/jackullrich/syscall-detectA Proof of Concept tool designed to detect manual system call execution from usermode to identify evasion techniques. It utilizes the Windows ProcessInstrumentationCallback mechanism via NtSetInformationProcess to verify that syscall return addresses reside within legitimate modules like ntdll.dll or win32u.dll.
What's inside syscall-detect
- syscall-detect is a Proof of Concept (PoC) designed to detect manual system calls (syscalls) originating from usermode. It is intended for security research and detection engineering to identify attempts to bypass standard API monitoring by executing syscalls directly.
How syscall-detect works via Instrumentation Callbacks
masterThe tool uses the Windows
ProcessInstrumentationCallbackmechanism to intercept transitions from kernel mode to user mode.When a syscall completes, the kernel executes a callback function.
syscall-detectregisters a thunk (InstrumentationCallbackThunk) that points toInstrumentationCallback. This callback inspects the CPU context (PCONTEXT) to verify that the return address (Rip) resides within a known, legitimate module (specificallyntdll.dllorwin32u.dll).If the return address is outside these verified modules, the tool identifies it as an unverified module return (potentially indicating direct syscalls or shellcode execution) and triggers a
DebugBreak()to halt execution.Registering the instrumentation callback via NtSetInformationProcess
masterThe tool enables detection by calling
NtSetInformationProcesswith theProcessInstrumentationCallbackinformation class (integer value40). This must be done duringDLL_PROCESS_ATTACH.It requires a
PROCESS_INSTRUMENTATION_CALLBACK_INFORMATIONstructure containing a pointer to the callback thunk.PROCESS_INSTRUMENTATION_CALLBACK_INFORMATION nirvana; nirvana.Callback = (PVOID)(ULONG_PTR)InstrumentationCallbackThunk; nirvana.Reserved = 0; nirvana.Version = 0; NtSetInformationProcess( GetCurrentProcess(), (PROCESS_INFORMATION_CLASS)40, &nirvana, sizeof(nirvana));Reference: PROCESS_INSTRUMENTATION_CALLBACK_INFORMATION
masterThe structure used to pass the callback information to
NtSetInformationProcess.typedef struct _PROCESS_INSTRUMENTATION_CALLBACK_INFORMATION { ULONG Version; ULONG Reserved; PVOID Callback; } PROCESS_INSTRUMENTATION_CALLBACK_INFORMATION, * PPROCESS_INSTRUMENTATION_CALLBACK_INFORMATION;InstrumentationCallback
masterThe core logic function that processes the CPU context during a syscall return. It performs the following steps:
- Context Restoration: Retrieves the previous instruction pointer (
Rip) and stack pointer (Rsp) from the Thread Environment Block (TEB) to ensure the execution flow can be correctly analyzed. - Recursion Prevention: Temporarily disables the instrumentation callback via the TEB (
TEB->InstrumentationCallbackDisabled) to prevent an infinite loop during the detection process. - Module Verification: Uses
RIP_SANITY_CHECKto verify if the currentRipis within the memory bounds ofntdll.dllorwin32u.dll. - Symbol Resolution (Debug Mode): If compiled in
_DEBUG, it usesSymFromAddrto resolve the function name and attempts a reverse lookup viaGetProcAddressto confirm the function's legitimacy. - Security Enforcement: If the
Ripis not within the verified modules, it prints a warning and callsDebugBreak(). - Cleanup: Re-enables the instrumentation callback and restores the context.
VOID InstrumentationCallback(PCONTEXT ctx)- Context Restoration: Retrieves the previous instruction pointer (