Implement .NET Core (CoreCLR) JIT and active AMSI buffer monitoring (PR-1) - #173
Open
doomedraven wants to merge 3 commits into
Open
Implement .NET Core (CoreCLR) JIT and active AMSI buffer monitoring (PR-1)#173doomedraven wants to merge 3 commits into
doomedraven wants to merge 3 commits into
Conversation
…PR-1) Surgically implements the first PR of our .NET modernization roadmap: 1. Adds full .NET Core / .NET 5+ JIT compileMethod monitoring support by registering hook_special(coreclr, compileMethod). This completely activates JIT MSIL scans/dumps for self-contained, unmanaged modern .NET runtimes. 2. Implements active Windows Antimalware Scan Interface (AMSI) intercepting by creating a dedicated hook_amsi.c module and hooking amsi.dll!AmsiScanBuffer and amsi.dll!AmsiScanString. 3. If g_config.amsidump is enabled, actively dumps plain-text scripts and buffers before execution using DumpMemoryRaw and SetCapeMetaData.
Owner
|
Thanks, this looks very nice, seems like a good addition. I note that the PR does not address preventing duplicate payload dumping, although the description mentions using "a simple state flag". I will have a look at implementing a good duplicate prevention method then merge. |
Add a thread-local AMSI guard to prevent re-entrant dumping while an AMSI hook is actively processing a scan. The dumper now exits early when the same thread is in a live AMSI scan, avoiding recursive dump attempts and duplicate processing while still reporting a non-detected result.
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.
Part 1: .NET Core / .NET 5+ JIT Support (coreclr.dll)
Self-contained, unmanaged modern .NET malware (written in .NET 5, 6, 7, or 8) bypasses the traditional clrjit.dll completamente, rendering standard JIT hooks blind.
inside hook_clr.c now automatically intercepts and scans/dumps all modern .NET Core intermediate bytecodes on-the-fly without changing a single line of parser code!
Part 2: Active AMSI Buffer Interception & Dumping (amsi.dll)
Passive COM provider scanning (like AmsiDumper.cpp) can be unregistered or actively patched/disabled in memory by malware using API hooking.
(contentName).
this amsi vs cape's current one
Technical Analysis: Active AMSI Hooking (
hook_amsi.c) vs. Passive COM Provider (AmsiDumper.cpp)capemoncurrently implements native AMSI support viaCAPE/AmsiDumper.cpp. However, our new active API hooks inhook_amsi.cdo not conflict with it; rather, they serve as a crucial, highly complementary defense-in-depth fail-safe that resolves several real-world limitations of the COM-provider approach.Here is a detailed breakdown of how both subsystems operate and why having both is the industry gold standard for malware sandboxing:
1. How CAPE’s Native AMSI Engine Works (
AmsiDumper.cpp)When
g_config.amsidumpis enabled,capemoninitializesAmsiDumperInit().capemon.dlldirectly in the Windows Registry (HKLM\Software\Microsoft\AMSI\Providers) as a native COM-basedIAntimalwareProvider.Scan()method to be dumped.2. Limitations of the Passive COM-Provider Approach
While highly elegant, the native COM provider has two significant Achilles' heels when dealing with modern real-world malware:
For
AmsiDumperInitto write to the local machine registry hive (HKLM), the monitored process or the CAPE agent must run with elevated Administrator privileges. If malware is detonated in a standard, non-elevated user context, registry registration fails silently:The vast majority of modern .NET and script-based payloads (such as AsyncRAT, AgentTesla, or Cobalt Strike) execute in-memory patching on
amsi.dll!AmsiScanBuffer(e.g., writing a hot-patchRET/0xC3instruction to the prologue) before running their malicious scripts.By blinding
AmsiScanBufferdirectly in memory, the engine is forced to return early, meaning the OS never invokes the registered COM providers (AmsiDumper), letting the payload execute completely unseen.AmsiDumperpassively dumps raw memory buffers to disk but does not output structured behavioral traces (such as the calling PID, the executing DLL context, or thecontentNameof the script) to the unifiedcapemonJSON/BSON behavioral log.3. Why Active Hooking (
hook_amsi.c) is the Perfect Fail-SafeBy placing active API hooks directly on
AmsiScanBufferandAmsiScanString, we solve all three issues:AmsiScanBufferto blind AMSI, our active hook intercepts the patch attempt (or detects hook modification via the unhook thread), protecting the integrity of the analysis.LOQ_hresult(logging script lengths, thread IDs, andcontentNamestrings) for seamless user visualization in the CAPE interface.4. Preventing Duplicate Payload Dumping
To prevent duplicate files on disk when both the active hook and the native COM provider succeed (i.e., when running elevated without AMSI patches), we can easily implement a simple state flag, or let the active hook handle the dumping exclusively.
Conclusion
Adding
opt/dotnet-coreclr-amsiintroduces an active hooking layer that complements the passiveAmsiDumper.cppCOM registration. This dual-layer architecture provides an incredibly robust, fail-safe environment capable of capturing scripts and assemblies even under standard user privileges and active in-memory AMSI-patching evasions.