Skip to content

InstrCallback: follow indirect/direct NtCreateUserProcess children and inherited targets - #256

Open
doomedraven wants to merge 1 commit into
kevoreilly:capemonfrom
doomedraven:feat/instr-callback-process-follow
Open

doomedraven wants to merge 1 commit into
kevoreilly:capemonfrom
doomedraven:feat/instr-callback-process-follow

Conversation

@doomedraven

Copy link
Copy Markdown
Collaborator

Summary

Extends x64 InstrumentationCallback (CAPE/InstrCallback.c) to track child processes spawned via direct or indirect NtCreateUserProcess syscalls and pivot across CIG-hardened relay child stages via PS_ATTRIBUTE_HANDLE_LIST (companion to #255 for loaders such as Maldev-Academy/WhisperLoader):

  1. Post-syscall x64 stack parameter inspection (HandleSyscallCreateUserProcess):

    • Although KiSystemCall64 clobbers volatile registers (RCX, RDX, R8, R9, R10, R11) before returning to InstrHook, the caller's user stack at Context->Rsp (TEB + 0x2e0) remains intact: [Context->Rsp + 0x48] holds PRTL_USER_PROCESS_PARAMETERS ProcessParameters (ImagePathName, CommandLine) and [Context->Rsp + 0x58] holds PPS_ATTRIBUTE_LIST AttributeList.
    • Pre-validates all stack, ProcessParameters, UNICODE_STRING, and PS_ATTRIBUTE_LIST pointers via IsReadableRangeNoFault (QueryMemoryRange / VirtualQuery) with zero __try / __except page-fault probing and zero loader-lock API calls (s_pNtQuerySystemInformation is pre-resolved in NirvanaInit() before InstrHook is registered).
  2. Direct/indirect NtCreateUserProcess child discovery & logging:

    • On NT_SUCCESS(ReturnValue), snapshots SystemProcessInformation into a VirtualAlloc buffer (avoiding heap locks), matches newly created children (InheritedFromUniqueProcessId == GetCurrentProcessId(), deduplicated in spinlock-guarded g_syscall_followed_children), emits a structured NtCreateUserProcess record (ImagePathName, CommandLine, ProcessId, ThreadId, BlockNonMicrosoftBinaries), re-arms hook_disable() after loq(), and calls ProcessMessage(child_pid, child_tid) whenever PROCESS_CREATION_MITIGATION_POLICY_BLOCK_NON_MICROSOFT_BINARIES_ALWAYS_ON is not set on the child.
  3. CIG relay bypass via PS_ATTRIBUTE_HANDLE_LIST (PsAttributeHandleList):

    • When a parent stage spawns CIG-hardened relay children (Writer / Flipper) and hands down open handles to the remote TP_POOL target via PS_ATTRIBUTE_HANDLE_LIST, HandleSyscallCreateUserProcess resolves foreign process and thread handles (pid_from_process_handle / pid_from_thread_handle), registers process handles with OpenProcessHandler, and invokes ProcessMessage(target_pid, 0) (deduplicated in g_syscall_followed_targets) so capemon attaches directly to the target host process before the relay child stages deliver and trigger the payload.

See #255 (## Hard User-Mode Limits) for the OS-enforced boundaries on CIG-protected relay children and CIG/ACG-protected target hosts.

Are these related?

Yes — unlike the coupled NDEBUG case where build flags alter macro semantics across translation units, child discovery (SystemProcessInformation) and PS_ATTRIBUTE_HANDLE_LIST target following share a single validated pass over the NtCreateUserProcess stack frame ([Rsp + 0x48] / [Rsp + 0x58]) inside CAPE/InstrCallback.c.

Verification

  • Note on MSVC compilation: No MSVC toolchain is available on this Linux host, so full Visual Studio solution builds (Win32 / x64) are unverified beyond source inspection.
  • POSIX C ASan/UBSan + TSan harness: Extracted the live C slices from CAPE/InstrCallback.c, ntapi.h, and lookup.c and compiled under gcc -O1 -g -Wall -Wextra -Werror -fshort-wchar -D_WIN64:
    • 64-bit ASan+UBSan (-fsanitize=address,undefined): passed all 4 suites covering WhisperLoader Grandparent CIG + PS_ATTRIBUTE_HANDLE_LIST frames, non-CIG child following and deduplication, STATUS_INFO_LENGTH_MISMATCH buffer growth, and malformed/unmapped/underflowing TotalLength inputs (0 warnings, 0 leaks/UB).
    • 64-bit TSan (-fsanitize=thread): passed 8-thread concurrent child/target deduplication stress (0 warnings).

Series

Builds cleanly on latest upstream/capemon (616de99). Companion to #255 (feat/tnc-and-tp-direct-injection, zero shared files) and merges cleanly on CAPE/InstrCallback.c with open PRs (#207, #209, #210, #213, #215, #243, #251, #253, #254).

…d inherited targets

When x64 NtCreateUserProcess succeeds via direct or indirect syscall,
inspect the preserved user-stack arguments ([Rsp+0x48] ProcessParameters
and [Rsp+0x58] AttributeList) using QueryMemoryRange pre-validation
without SEH faults or loader-lock APIs:

- Snapshot SystemProcessInformation via VirtualAlloc to discover newly
  created child PIDs/TIDs, emit a structured NtCreateUserProcess log
  record (ImagePathName, CommandLine, ProcessId, ThreadId, and CIG flag),
  and invoke ProcessMessage when Microsoft-only CIG is not set on the
  child.
- Inspect PS_ATTRIBUTE_HANDLE_LIST (PsAttributeHandleList) to register
  and follow foreign target process/thread handles inherited across
  CIG-hardened relay child stages.
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