一个原生 C++ 工具,通过恢复原始的 nLoadImage 函数实现,绕过 .NET 公共语言运行时中的 EDR/AV 挂钩。
此工具从 CLR 的 nLoadImage 函数中移除安全产品挂钩——该函数是处理所有内存中 .NET 程序集加载的关键原生入口点。通过从磁盘读取干净的 clr.dll 并覆盖内存中被挂钩的函数字节,它恢复了原始的 CLR 行为,使得 Assembly.Load(byte[]) 可以在没有 EDR 检查或扫描的情况下执行。
现代安全产品(如 BitDefender、CrowdStrike、SentinelOne 等)挂钩 clr.dll 中的 nLoadImage 函数,以拦截和扫描内存中的 .NET 程序集加载。此工具通过以下方式解除该函数的挂钩:
clr.dllnLoadImage 字节解除挂钩后,Assembly.Load(byte[]) 无需 EDR 检查即可执行。
nLoadImage 是 .NET 运行时中处理所有内存中程序集加载的关键原生函数。它在托管代码中被声明为 InternalCall,意味着它没有 C# 实现——而是直接桥接到原生 CLR 代码。
调用链:
托管代码 (C#)
↓
Assembly.Load(byte[])
↓
RuntimeAssembly.nLoadImage(...) [InternalCall - 无托管体]
↓
clr.dll!AssemblyNative::LoadImage (原生 C++ 实现)
↓
程序集加载到 AppDomain
为什么它很关键:
几乎所有内存中的程序集加载都通过 nLoadImage。Assembly.Load(byte[]) 方法及其重载(包括加载符号字节)都在底层调用 nLoadImage。当您调用 Assembly.Load(byte[]) 时,mscorlib.dll 中的托管代码通过 RuntimeAssembly.nLoadImage() 传递您的字节数组,该方法标记为 [MethodImpl(MethodImplOptions.InternalCall)]——这意味着其 C# 体为空,执行立即跳转到原生 CLR 代码。
即使是动态代码生成场景——运行时发出程序集的序列化框架、XML 序列化生成器以及像 Cobalt Strike 的 execute-assembly 这样的红队工具——都汇聚到这一个函数。
原生实现:
mscorlib.dll 中的 nLoadImage InternalCall 存根指向 clr.dll 内的原生 C++ 函数 AssemblyNative::LoadImage。此函数:
在 .NET Framework 4.8+ 中,每次 nLoadImage 调用都会自动将程序集字节传递给 Windows Defender 的 AMSI(AmsiScanBuffer)进行扫描,这使其成为安全产品的关键阻塞点。
函数签名 (.NET Framework 4.7+):
[MethodImpl(MethodImplOptions.InternalCall)]
static internal extern Assembly nLoadImage(
byte[] rawAssembly, // PE 字节
byte[] rawSymbolStore, // 可选的 PDB 字节
Evidence evidence, // CAS 证据(已废弃)
ref StackCrawlMark stackMark, // 安全堆栈标记
bool fIntrospection, // 仅反射标志
bool fSkipIntegrityCheck, // 跳过完整性验证
SecurityContextSource securityContextSource // 安全上下文
);
当您调用 Assembly.Load(byte[]) 时,它会使用这些典型参数调用 nLoadImage:
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return RuntimeAssembly.nLoadImage(
rawAssembly, // 您的字节数组
null, // rawSymbolStore
null, // evidence
ref stackMark, // LookForMyCaller
false, // fIntrospection
SecurityContextSource.CurrentAssembly // securityContextSource
);
fIntrospection 参数控制程序集是加载用于执行(false)还是仅反射检查(true)。Assembly.ReflectionOnlyLoad(byte[]) 方法以 fIntrospection=true 调用 nLoadImage,允许在无需代码执行的情况下检查元数据。
为什么 EDR 要挂钩它:
由于 nLoadImage 是所有内存中程序集加载的单一入口点,EDR 产品在 clr.dll 的原生层面挂钩它。这使得它们能够:
传统的绕过方法(AMSI 修补、ETW 禁用)不会影响 CLR 级别的挂钩,因为它们运行在堆栈中更高的层次。挂钩发生在 CLR 内部,甚至在 AMSI 被调用之前。
CLRUnhook.exe
解除当前进程中 CLR 的挂钩。注意: 只有在 CLR 已加载的情况下才有效(即从 .NET 应用程序运行,或手动加载 CLR 后)。
CLRUnhook.exe powershell.exe
CLRUnhook.exe 1234
解除远程进程中 CLR 的挂钩。
=== CLR Unhooking Tool ===
[*] Mode -> Remote Process Unhooking
[*] Target -> PID 21436
[+] Found PID -> 21436
[*] Unhooking CLR->nLoadImage in remote process...
[DEBUG] Remote mode enabled
[DEBUG] Found clr.dll at 0x00007FFD38CB0000
[DEBUG] CLR path -> C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
[DEBUG] CLR module size -> 10108928 bytes
[DEBUG] Read 10108928 bytes from remote process
[DEBUG] Searching for 'nLoadImage' in module (size: 10108928)
[DEBUG] Remote base address: 0x00007FFD38CB0000
[DEBUG] Scanning for string 'nLoadImage' (11 bytes)...
[DEBUG] Found string at RVA 0x7c12b8
[DEBUG] Searching for remote pointer: 0x7ffd394712b8
[DEBUG] Found pointer at offset 0x7a4340
[DEBUG] Valid function pointer found at RVA 0x5e4f30
[DEBUG] Found nLoadImage at RVA 0x00000000005E4F30
[DEBUG] Hooked function address -> 0x00007FFD39294F30
[DEBUG] Clean function at offset 0x00000000005E4F30 in disk file
[DEBUG] Reading hooked bytes before patch...
[DEBUG] First 16 bytes BEFORE unhook:
4C 8B DC 49 89 5B 08 49 89 73 10 4D 89 4B 20 57
[DEBUG] Clean bytes from disk:
8B 4B 78 E8 88 A9 EA FF C6 44 24 28 00 80 3D A4
[DEBUG] Wrote 30 bytes successfully
[DEBUG] First 16 bytes AFTER unhook:
8B 4B 78 E8 88 A9 EA FF C6 44 24 28 00 80 3D A4
[DEBUG] VERIFICATION SUCCESS: Patched bytes match clean bytes!
[+] SUCCESS -> CLR nLoadImage unhooked in remote process!
[+] EDR/AV hooks bypassed
[*] Press Enter to exit...
托管代码 (C#)
↓
Assembly.Load(byte[])
↓
RuntimeAssembly.nLoadImage(...) [InternalCall]
↓
clr.dll!AssemblyNative::LoadImage
↓
[EDR 挂钩] ← 我们绕过这里
↓
原始 CLR 代码
clr.dll 中的 nLoadImage(当前已被挂钩)C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ 读取原始 clr.dll使用模式扫描定位 nLoadImage:
技术研究:
实现:
仅供教育和授权的安全研究使用。
未经授权使用此工具绕过安全控制可能违反计算机欺诈法律(如 CFAA 等类似法规)。仅可在您拥有所有权或获得明确书面许可的系统上使用。