Repository navigation
hooks: add Thread Name-Calling and TP_DIRECT.Callback hijack coverage - #255
Open
doomedraven wants to merge 1 commit into
Open
doomedraven wants to merge 1 commit into
doomedraven wants to merge 1 commit into
Conversation
- ntapi.h: complete THREADINFOCLASS up to ThreadNameInformation (0x26) pinned by C_ASSERT. - hook_thread.c: inspect ThreadNameInformation in NtSetInformationThread and NtQueryInformationThread, following cross-process target PIDs via ProcessMessage and dumping non-UTF16 / embedded-NUL / oversized binary thread-name buffers (deduplicated and capped). Append ApcArgument1..3 to NtQueueApcThread / NtQueueApcThreadEx logs and hook NtAlertThread. - hook_misc.c: track cross-process handles duplicated into the current process in NtDuplicateObject (cleared on NtClose) and register remote processes with OpenProcessHandler. - hook_sync.c, CAPE/Injection.c: resolve same-process and cross-process TP_DIRECT.Callback (+0x38 x64 / +0x28 x86) in NtSetIoCompletion and NtSetIoCompletionEx, dumping non-MEM_IMAGE remote executable regions and hooking TpAllocAlpcCompletion and TpAllocJobNotification. - hook_process.c, loader/loader/Loader.c: follow and dump remote regions flipped to executable in NtProtectVirtualMemory, and accept ThreadId=0 in standalone CreateMonitorPipe PROCESS: notifications.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds API hook, handle-tracking, and payload-extraction coverage for cross-process Thread Name-Calling (TNC) delivery and
TP_DIRECT.Callbackthread-pool I/O completion hijacking (as used by loaders such asMaldev-Academy/WhisperLoaderand PoolParty Variant 8):Thread Name-Calling (
ThreadNameInformation=0x26) (ntapi.h,hook_thread.c):THREADINFOCLASSup toThreadNameInformation(0x26), pinned byC_ASSERT(ThreadNameInformation == 0x26).New_NtSetInformationThread(injector side): snapshotsUNICODE_STRINGthread-name buffers (IsReadableLocalBufferviaVirtualQuery, preservinglasterror_tanddisable_count), invokesProcessMessage(pid, 0)on cross-process targets, logs a singleLOQ_ntstatusrecord (ThreadNameraw'b'buffer preserving embedded0x00bytes and odd lengths) when cross-process, suspicious (IsSuspiciousThreadNameBuffer: odd byte length,>= 256bytes, embedded0x0000WCHAR, invalid UTF-16 surrogates,0xFFFE/0xFFFF, or C0 control units), orfull_logs, and dumps raw payloads (INJECTION_SHELLCODE/UNPACKED_SHELLCODE, FNV-1a deduplicated and capped at 16 dumps).New_NtQueryInformationThread(target side): inspects returnedThreadNameInformationUNICODE_STRINGbuffers onNT_SUCCESS(ret)and dumps binary/suspicious buffers viaDumpThreadNameBuffer. When an unmonitored injector queueskernelbase!GetThreadDescription(NtCurrentThread(), ...)as a remote APC inside a monitored target,GetThreadDescriptioninvokesntdll!NtQueryInformationThread(ThreadNameInformation)through the target's hookedntdll.dll, capturing the raw stashed TNC payload on the target side.Cross-process
TP_DIRECT.Callback&IoCompletionhijack (hook_misc.c,hook_sync.c,CAPE/Injection.{h,c},hooks.{h,c}):New_NtDuplicateObject/New_NtClose: tracks handles duplicated from foreign processes into the current process (remote_handle_set/remote_handle_clear/remote_handle_get_pidin a spinlock-guardedlookup_t, overwritten/cleared onNtCloseto handle kernel handle reuse safely) and registers foreign processes viaOpenProcessHandler.New_NtSetIoCompletion/New_NtSetIoCompletionEx:IoCompletionHandlemaps to a foreignremote_pid, notifiesProcessMessage(remote_pid, 0), reads remote[KeyContext + TP_DIRECT_CALLBACK_OFFSET](+0x38on x64,+0x28on x86) viaReadProcessMemory, and dumps non-MEM_IMAGEcommitted executable regions (DumpRemoteExecutableRegion, skipping legitimatentdllMEM_IMAGEcallbacks).[KeyContext + TP_DIRECT_CALLBACK_OFFSET]viaVirtualQueryin addition toKeyContext/ApcContextand forwardsLocalCallbacktoNewThreadHandler(&TP_DIRECTsits on the heap, soNewThreadHandler(KeyContext)alone previously missed in-processTP_DIRECT.Callbackshellcode).TpAllocAlpcCompletionandTpAllocJobNotificationalongsideTpAllocIoCompletion.APC argument visibility,
NtAlertThread, remoteNtProtectVirtualMemory, and standalone pipeThreadId=0(hook_thread.c,hook_process.c,loader/loader/Loader.c):ApcArgument1(ApcRoutineContext),ApcArgument2(ApcStatusBlock), andApcArgument3(ApcReserved) toNew_NtQueueApcThreadandNew_NtQueueApcThreadExwhile preserving existing field order, and guardsProcessMessage(pid, 0)onpid != 0 && !g_config.single_process.NtAlertThreadacrossfull_hooks,native_hooks, andoffice_hooks.New_NtProtectVirtualMemoryto invokeProcessMessage(pid, 0)andDumpRemoteExecutableRegionwhen a remote process's memory is flipped toEXECUTABLE_FLAGS.CreateMonitorPipe(Loader.c) to acceptPROCESS:<pid>,0(ThreadId == 0) notifications, matching CAPEv2's guest pipe server.Hard User-Mode Limits
Pure user-mode
capemonremains subject to two OS-enforced boundaries when loaders combine these primitives with indirect syscalls and process mitigations:BLOCK_NON_MICROSOFT_BINARIES_ALWAYS_ON):WhisperLoader'sGrandparent -> Writer -> Flippersplit) invokesNtCreateUserProcessvia an indirect syscall withPROCESS_CREATION_MITIGATION_POLICY_BLOCK_NON_MICROSOFT_BINARIES_ALWAYS_ON, inlineNew_NtCreateUserProcessmitigation stripping is bypassed, andProcessInstrumentationCallback(Nirvana) executes after the kernel latchesEPROCESS.MitigationFlags2on the child. Injecting unsignedcapemonDLLs intoWriterorFlipperviaLoadLibraryA/ IAT (SEC_IMAGE) is rejected by the kernel (STATUS_INVALID_IMAGE_HASH).feat/instr-callback-process-follow) mitigates CIG relay stages from user mode by inspectingPS_ATTRIBUTE_HANDLE_LISTonNtCreateUserProcesssyscall return and following the inherited foreign target handle (target_pid) directly; oncecapemonis attached totarget_pid,New_NtQueryInformationThread(ThreadNameInformation)in this PR captures the TNC-stashed payload insidetarget_pideven thoughWriterandFlipperthemselves run unmonitored.TP_POOLhost process selected by the injector itself enforcesBLOCK_NON_MICROSOFT_BINARIES_ALWAYS_ON(orPROHIBIT_DYNAMIC_CODE/ ACG), user-modeLoadLibraryA/SEC_IMAGEinjection cannot load an unsigned monitor DLL intotarget_pideither without a kernel driver, Microsoft-signed monitor binary, or aSEC_IMAGE-free manual PE mapper.Are these related?
Yes — unlike the coupled
NDEBUGcase where build flags change macro semantics across unrelated modules, all changes here form the user-mode API hook and payload-capture path for Thread Name-Calling +TP_DIRECT.Callbackcompletion hijacking (both injector-side and target-side).Verification
Win32/x64) are unverified beyond source inspection.ntapi.h,lookup.c,hook_misc.c,CAPE/Injection.c,hook_thread.c,hook_sync.c,hook_process.c, andloader/loader/Loader.cand compiled undergcc -O1 -g -Wall -Wextra -Werror -fshort-wchar:-D_WIN64 -fsanitize=address,undefined): passed all 5 suites (0warnings,0leaks/UB).-m32 -U_WIN64 -fsanitize=address,undefined): passed all 5 suites (0warnings,0leaks/UB).-D_WIN64 -fsanitize=thread): passed 8-thread concurrent handle duplication/close/reuse stress (0warnings on new locks/tables).Series
Builds cleanly on latest
upstream/capemon(616de99) with zero file overlap against companion PR #256 (feat/instr-callback-process-follow) and zero merge conflicts on touched files against open PRs (#207, #209, #210, #213, #215, #243, #251, #253, #254).